-- 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' ) );