diff --git a/tinyurl/src/main/resources/db/migration/V2__strip_short_code_leading_zeros.sql b/tinyurl/src/main/resources/db/migration/V2__strip_short_code_leading_zeros.sql index 7e0d8d0..ce76e56 100644 --- a/tinyurl/src/main/resources/db/migration/V2__strip_short_code_leading_zeros.sql +++ b/tinyurl/src/main/resources/db/migration/V2__strip_short_code_leading_zeros.sql @@ -1,6 +1,10 @@ -- Remove leading zeros from existing short codes, relax the minimum length constraint, -- and add creator tracking columns. +-- Drop the old constraint first (required min 4 chars) so the UPDATE below can produce shorter codes +ALTER TABLE url_mappings + DROP CONSTRAINT chk_short_code_format; + -- Strip leading zeros from all existing short codes UPDATE url_mappings SET short_code = LTRIM(short_code, '0') @@ -8,7 +12,6 @@ WHERE short_code ~ '^0+.+$'; -- Update the format constraint to allow codes as short as 1 character ALTER TABLE url_mappings - DROP CONSTRAINT chk_short_code_format, ADD CONSTRAINT chk_short_code_format CHECK (short_code ~ '^[0-9a-zA-Z_-]{1,32}$'); -- Add creator tracking columns