Files
visa/migrations/0002_companies_and_stays.sql
T
grabowski 83bc481952
CI / secret scan (push) Successful in 4s
CI / fmt, clippy, tests (push) Successful in 22s
Phase 2: companies, stay records, deadlines
Companies become records of their own, and the stay in Thailand is
recorded in full; from these records the app works out each person's
deadlines (docs/PLAN.md, phase 2).

- Companies: list, page, form, archive; Thai name, the 13-digit
  registration number with its check digit verified, address and the
  director who signs. The employer is chosen from them on the person form
  and on work permits; a company's page adds people to it; the people
  list and the Deadlines page filter by employer. Migration 0002 turns
  the free-text employers into companies.
- Stay records, each one `Kind` in src/records.rs with one form, one set
  of routes and the same audit entries: arrivals and departures with the
  TDAC, extensions of stay (approved, under consideration, refused),
  90-day reports with the receipt's next date, visas, re-entry permits and
  work permits. Passports, visas and companies are chosen from the
  person's own records; the server checks the choice.
- The deadline engine (src/deadlines.rs, a pure function with a unit
  test per rule): the 90-day report from day 1 of each arrival, reset by
  a report or an extension, with the in-person and online windows and the
  in-person rule after a new passport or an extension; the extension
  window from 30 days before to the last working day; the decision to
  collect; overstay with its fine; TM.30 after each arrival; passport and
  work permit expiry; re-entry permits; visas still to be used.
- The Deadlines page at /, the person's status and deadlines on the
  Overview and the new "Visa and stay" tab, the person's Activity tab,
  and the next deadline in the people list.
- The online 90-day window ends 7 days before the due date (the plan
  said 8; the Immigration Bureau's TM.47 manual says 7).
- 69 tests: 45 unit tests and 24 integration tests.
2026-11-17 18:20:14 +07:00

169 lines
6.7 KiB
SQL

-- visa.buildfor.life, phase 2: companies (the employers, later also the
-- agency's corporate clients) and the records a stay in Thailand is made of:
-- visas, arrivals with the TDAC, extensions of stay, re-entry permits,
-- 90-day reports and work permits (docs/PLAN.md "Data model").
--
-- As in 0001: optional text is NOT NULL DEFAULT '', unknown dates are NULL.
CREATE TABLE companies (
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL,
name_th TEXT NOT NULL DEFAULT '',
-- the 13-digit juristic person number, which is also the tax ID
registration_no TEXT NOT NULL DEFAULT '',
business TEXT NOT NULL DEFAULT '',
-- the registered address, in the order Thai forms use
house_no TEXT NOT NULL DEFAULT '',
moo TEXT NOT NULL DEFAULT '',
building TEXT NOT NULL DEFAULT '',
soi TEXT NOT NULL DEFAULT '',
road TEXT NOT NULL DEFAULT '',
subdistrict TEXT NOT NULL DEFAULT '',
district TEXT NOT NULL DEFAULT '',
province TEXT NOT NULL DEFAULT '',
postal_code TEXT NOT NULL DEFAULT '',
phone TEXT NOT NULL DEFAULT '',
email TEXT NOT NULL DEFAULT '',
-- the director who signs for the company (STM.1, the employer's part of TM.7)
signatory TEXT NOT NULL DEFAULT '',
signatory_position TEXT NOT NULL DEFAULT '',
notes TEXT NOT NULL DEFAULT '',
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
archived_at TIMESTAMPTZ
);
CREATE UNIQUE INDEX companies_registration_no ON companies (registration_no)
WHERE registration_no <> '';
-- The employer was free text; it becomes a company, chosen from a list.
ALTER TABLE people ADD COLUMN employer_id BIGINT REFERENCES companies (id);
INSERT INTO companies (name)
SELECT DISTINCT employer FROM people WHERE employer <> '';
UPDATE people p SET employer_id = c.id
FROM companies c WHERE p.employer <> '' AND c.name = p.employer;
ALTER TABLE people DROP COLUMN employer;
CREATE INDEX people_employer ON people (employer_id);
CREATE TABLE visas (
id BIGSERIAL PRIMARY KEY,
person_id BIGINT NOT NULL REFERENCES people (id),
category TEXT NOT NULL,
entries TEXT NOT NULL DEFAULT '',
number TEXT NOT NULL DEFAULT '',
passport_id BIGINT REFERENCES passports (id) ON DELETE SET NULL,
issued_at TEXT NOT NULL DEFAULT '',
issued_on DATE,
-- the last day to enter Thailand with the visa
valid_until DATE NOT NULL,
notes TEXT NOT NULL DEFAULT '',
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CHECK (issued_on IS NULL OR issued_on <= valid_until)
);
CREATE INDEX visas_person ON visas (person_id);
CREATE TABLE arrivals (
id BIGSERIAL PRIMARY KEY,
person_id BIGINT NOT NULL REFERENCES people (id),
arrived_on DATE NOT NULL,
port TEXT NOT NULL DEFAULT '',
-- visa, exempt, voa (visa on arrival) or reentry (with a re-entry permit)
admission TEXT NOT NULL,
visa_id BIGINT REFERENCES visas (id) ON DELETE SET NULL,
-- the date in the arrival stamp
permitted_until DATE NOT NULL,
passport_id BIGINT REFERENCES passports (id) ON DELETE SET NULL,
flight TEXT NOT NULL DEFAULT '',
-- the Thailand Digital Arrival Card, which replaced the TM.6 card in 2025
tdac_no TEXT NOT NULL DEFAULT '',
tdac_submitted_on DATE,
departed_on DATE,
departure_port TEXT NOT NULL DEFAULT '',
notes TEXT NOT NULL DEFAULT '',
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CHECK (permitted_until >= arrived_on),
CHECK (departed_on IS NULL OR departed_on >= arrived_on),
CHECK (tdac_submitted_on IS NULL OR tdac_submitted_on <= arrived_on)
);
CREATE INDEX arrivals_person ON arrivals (person_id, arrived_on);
CREATE TABLE extensions (
id BIGSERIAL PRIMARY KEY,
person_id BIGINT NOT NULL REFERENCES people (id),
applied_on DATE NOT NULL,
-- work, marriage, family, retirement, education, tourism or other
basis TEXT NOT NULL,
-- approved, consideration (the 30-day stamp while it is decided) or refused
decision TEXT NOT NULL,
-- the date in the stamp: the new end of the stay, the day to come back
-- for the decision, or the day to leave after a refusal
permitted_until DATE,
office TEXT NOT NULL DEFAULT '',
receipt_no TEXT NOT NULL DEFAULT '',
fee_thb INTEGER,
notes TEXT NOT NULL DEFAULT '',
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CHECK (permitted_until IS NULL OR permitted_until >= applied_on)
);
CREATE INDEX extensions_person ON extensions (person_id, applied_on);
CREATE TABLE reentry_permits (
id BIGSERIAL PRIMARY KEY,
person_id BIGINT NOT NULL REFERENCES people (id),
-- single or multiple
kind TEXT NOT NULL,
number TEXT NOT NULL DEFAULT '',
issued_on DATE NOT NULL,
valid_until DATE NOT NULL,
office TEXT NOT NULL DEFAULT '',
notes TEXT NOT NULL DEFAULT '',
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CHECK (issued_on <= valid_until)
);
CREATE INDEX reentry_permits_person ON reentry_permits (person_id);
CREATE TABLE ninety_day_reports (
id BIGSERIAL PRIMARY KEY,
person_id BIGINT NOT NULL REFERENCES people (id),
reported_on DATE NOT NULL,
-- in-person, agent, online or mail
method TEXT NOT NULL,
receipt_no TEXT NOT NULL DEFAULT '',
-- the date printed on the receipt
next_due_on DATE,
notes TEXT NOT NULL DEFAULT '',
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CHECK (next_due_on IS NULL OR next_due_on >= reported_on)
);
CREATE INDEX ninety_day_reports_person ON ninety_day_reports (person_id, reported_on);
CREATE TABLE work_permits (
id BIGSERIAL PRIMARY KEY,
person_id BIGINT NOT NULL REFERENCES people (id),
company_id BIGINT REFERENCES companies (id),
number TEXT NOT NULL DEFAULT '',
position TEXT NOT NULL DEFAULT '',
issued_on DATE,
valid_until DATE NOT NULL,
notes TEXT NOT NULL DEFAULT '',
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CHECK (issued_on IS NULL OR issued_on <= valid_until)
);
CREATE INDEX work_permits_person ON work_permits (person_id);
CREATE INDEX work_permits_company ON work_permits (company_id);
-- The audit log names the new records too. Changing a constraint is no
-- update of a row, so the append-only trigger lets it through.
ALTER TABLE audit_log DROP CONSTRAINT audit_log_entity_check;
ALTER TABLE audit_log ADD CONSTRAINT audit_log_entity_check CHECK (
entity IN (
'person', 'passport', 'address', 'session', 'company', 'visa', 'arrival',
'extension', 'reentry-permit', 'report', 'work-permit'
)
);