ALTER TABLE permit_applications
    ADD COLUMN IF NOT EXISTS tenant_id INTEGER,
    ADD COLUMN IF NOT EXISTS branch_id INTEGER;
CREATE INDEX IF NOT EXISTS permit_applications_tenant_idx ON permit_applications(tenant_id);
CREATE INDEX IF NOT EXISTS permit_applications_branch_idx ON permit_applications(branch_id);

ALTER TABLE permit_documents
    ADD COLUMN IF NOT EXISTS tenant_id INTEGER,
    ADD COLUMN IF NOT EXISTS branch_id INTEGER;
CREATE INDEX IF NOT EXISTS permit_documents_tenant_idx ON permit_documents(tenant_id);
CREATE INDEX IF NOT EXISTS permit_documents_branch_idx ON permit_documents(branch_id);

ALTER TABLE permit_inspections
    ADD COLUMN IF NOT EXISTS tenant_id INTEGER,
    ADD COLUMN IF NOT EXISTS branch_id INTEGER;
CREATE INDEX IF NOT EXISTS permit_inspections_tenant_idx ON permit_inspections(tenant_id);
CREATE INDEX IF NOT EXISTS permit_inspections_branch_idx ON permit_inspections(branch_id);

UPDATE permit_applications SET tenant_id = 1, branch_id = 1 WHERE tenant_id IS NULL;
UPDATE permit_documents SET tenant_id = 1, branch_id = 1 WHERE tenant_id IS NULL;
UPDATE permit_inspections SET tenant_id = 1, branch_id = 1 WHERE tenant_id IS NULL;
