fix(customers): re-issue the country backfill skipping migration-reset source companies (#2249)
* fix(customers): re-issue the country backfill skipping migration-reset source companies Migration 20260903170000 (PR #2241) failed on prod at its first UPDATE: "Archived migration reset source records are immutable" (SQLSTATE P0001). Rows of a company listed in company_migration_resets are frozen by trigger, and the backfill touched them, so the whole file rolled back and prod has neither normalize_country_code() nor country_raw. The same SQL ships again as 20260903173000 with every UPDATE excluding those companies (their legacy text keeps being read through normalizeCountryCode() at runtime). The old file is removed rather than edited: prod never recorded it, staging was re-tracked under the new version by hand. References in code, tests and DECISIONS.md follow. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01D5EmmndLyDCmY5NHYAvYkE * docs(decisions): cite the shipped backfill version 20260903173000 The country-ISO entry still named 20260903170000, the version that never landed on prod; only the follow-up entry keeps that number, as history. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01D5EmmndLyDCmY5NHYAvYkE * docs(decisions): drop the duplicate country-ISO entry the merge carried in Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01D5EmmndLyDCmY5NHYAvYkE * docs(decisions): keep a single country-ISO entry Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01D5EmmndLyDCmY5NHYAvYkE * docs(decisions): rebuild the tail from main so the merge leaves no duplicated entries Co-Authored-By: Claude Fable 5.1 <[email protected]> --------- Co-authored-by: Claude Fable 5.1 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.1
parent
a48508e5b0
commit
3159920d7c
+2
-1
File diff suppressed because one or more lines are too long
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* The backfill in migration 20260903170000 maps country names with a SQL
|
||||
* The backfill in migration 20260903173000 maps country names with a SQL
|
||||
* function that carries its own copy of the name table. This test holds the
|
||||
* two copies to each other: every name the SQL knows must map to the same
|
||||
* code in TypeScript, and every name TypeScript knows must be in the SQL.
|
||||
@@ -11,7 +11,7 @@ import { listKnownCountryNames, normalizeCountryCode } from '../country-codes'
|
||||
|
||||
const MIGRATION = join(
|
||||
process.cwd(),
|
||||
'supabase/migrations/20260903170000_customer_supplier_country_iso.sql',
|
||||
'supabase/migrations/20260903173000_customer_supplier_country_iso.sql',
|
||||
)
|
||||
|
||||
function sqlNameTable(): Map<string, string> {
|
||||
|
||||
@@ -132,7 +132,7 @@ const ALPHA2_RE = /^[A-Z]{2}$/
|
||||
|
||||
/**
|
||||
* Every folded name the TypeScript table knows, with its code. Exists so a
|
||||
* test can hold the SQL twin in migration 20260903170000 to the same table.
|
||||
* test can hold the SQL twin in migration 20260903173000 to the same table.
|
||||
*/
|
||||
export function listKnownCountryNames(): Array<[name: string, code: string]> {
|
||||
return [...NAME_TO_CODE.entries()]
|
||||
@@ -148,7 +148,7 @@ export function listKnownCountryNames(): Array<[name: string, code: string]> {
|
||||
* "unknown, keep the raw text" (reports, backfill) or a 400 (API writes).
|
||||
*
|
||||
* The SQL twin `public.normalize_country_code(text)` (migration
|
||||
* 20260903170000) carries the same table for the one-off backfill; keep them
|
||||
* 20260903173000) carries the same table for the one-off backfill; keep them
|
||||
* in step when adding names here.
|
||||
*/
|
||||
export function normalizeCountryCode(input: string | null | undefined): string | null {
|
||||
@@ -362,7 +362,7 @@ export function checkCountryConsistency(input: {
|
||||
* either: refusing reverse charge on a genuine German customer whose row
|
||||
* still says "Deutschland" would put Swedish VAT on a correct invoice, which
|
||||
* is the worse error. The consistency check above stops new contradictory
|
||||
* rows from being saved, and migration 20260903170000 repairs the old ones
|
||||
* rows from being saved, and migration 20260903173000 repairs the old ones
|
||||
* whose country was only ever the writer default.
|
||||
*/
|
||||
export function countryPermitsReverseCharge(country: string | null | undefined): boolean {
|
||||
|
||||
+13
-5
@@ -14,6 +14,11 @@
|
||||
-- were (and listed in country_raw); the periodisk report already warns
|
||||
-- on those and the customer form asks for a pick before it saves.
|
||||
--
|
||||
-- Rows of a company that is a migration-reset source (company_migration_resets)
|
||||
-- are immutable by trigger and are skipped by every UPDATE below; the first
|
||||
-- attempt (as 20260903170000) failed on prod because it touched them. Their
|
||||
-- legacy text is still read through normalizeCountryCode() at runtime.
|
||||
--
|
||||
-- 4. for eu_business rows whose country is missing or only the old writer
|
||||
-- default (SE) while the VAT number names another EU member, takes the
|
||||
-- country from the VAT prefix. The pre-2026-09 VAT rules granted reverse
|
||||
@@ -197,17 +202,19 @@ comment on column public.suppliers.country_raw is
|
||||
-- Backfill: every row that is not already an uppercase alpha-2 code. An
|
||||
-- empty string is "no country" and becomes null (the periodisk report
|
||||
-- already treats both the same); a null stays null, nothing is guessed.
|
||||
update public.customers
|
||||
update public.customers c
|
||||
set country_raw = country,
|
||||
country = case when btrim(country) = '' then null else coalesce(public.normalize_country_code(country), country) end
|
||||
where country is not null
|
||||
and country !~ '^[A-Z]{2}$';
|
||||
and country !~ '^[A-Z]{2}$'
|
||||
and not exists (select 1 from public.company_migration_resets r where r.source_company_id = c.company_id);
|
||||
|
||||
update public.suppliers
|
||||
update public.suppliers s
|
||||
set country_raw = country,
|
||||
country = case when btrim(country) = '' then null else coalesce(public.normalize_country_code(country), country) end
|
||||
where country is not null
|
||||
and country !~ '^[A-Z]{2}$';
|
||||
and country !~ '^[A-Z]{2}$'
|
||||
and not exists (select 1 from public.company_migration_resets r where r.source_company_id = s.company_id);
|
||||
|
||||
-- Step 4: EU-business rows with a missing or defaulted (SE) country and a
|
||||
-- VAT number registered in another EU member. Only a prefix that names a
|
||||
@@ -228,6 +235,7 @@ update public.customers c
|
||||
where d.id = c.id
|
||||
and d.code is not null
|
||||
and d.code <> 'SE'
|
||||
and d.code in ('AT','BE','BG','HR','CY','CZ','DK','EE','FI','FR','DE','GR','HU','IE','IT','LV','LT','LU','MT','NL','PL','PT','RO','SK','SI','ES');
|
||||
and d.code in ('AT','BE','BG','HR','CY','CZ','DK','EE','FI','FR','DE','GR','HU','IE','IT','LV','LT','LU','MT','NL','PL','PT','RO','SK','SI','ES')
|
||||
and not exists (select 1 from public.company_migration_resets r where r.source_company_id = c.company_id);
|
||||
|
||||
NOTIFY pgrst, 'reload schema';
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* pg-real test for migration 20260903170000: customers.country and
|
||||
* pg-real test for migration 20260903173000: customers.country and
|
||||
* suppliers.country are ISO 3166-1 alpha-2 (#2025, #2028).
|
||||
*
|
||||
* The backfill ran once when the migration was applied, so what can be
|
||||
@@ -51,7 +51,7 @@ describe('normalize_country_code()', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('country columns after migration 20260903170000', () => {
|
||||
describe('country columns after migration 20260903173000', () => {
|
||||
it('keeps the pre-backfill text in country_raw on both tables', async () => {
|
||||
const { rows } = await getPool().query<{ table_name: string; column_name: string }>(
|
||||
`select table_name, column_name
|
||||
|
||||
Reference in New Issue
Block a user