fix: change postal_code column from integer to string (#1585)

* fix: change postal_code column from integer to string

Allows non-numeric postal codes such as UK format (e.g. SW1A 2AA).
The integer column was silently dropping any alphanumeric input.

The migration is marked irreversible — once alphanumeric postal codes
exist, they cannot be safely cast back to integer.

* fix: update schema.rb, quote fixture postal_code, and add alphanumeric test

* fix: use conventional migration timestamp
This commit is contained in:
Will Wilson
2026-04-29 14:30:04 +01:00
committed by GitHub
parent c9f9e04071
commit ad23820a2e
4 changed files with 18 additions and 3 deletions

View File

@@ -0,0 +1,9 @@
class ChangePostalCodeToStringInAddresses < ActiveRecord::Migration[7.2]
def up
change_column :addresses, :postal_code, :string, using: "postal_code::text"
end
def down
raise ActiveRecord::IrreversibleMigration, "postal_code was changed from integer to string; alphanumeric values cannot be cast back to integer"
end
end

4
db/schema.rb generated
View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.2].define(version: 2026_04_12_120000) do
ActiveRecord::Schema[7.2].define(version: 2026_04_28_120000) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
@@ -116,7 +116,7 @@ ActiveRecord::Schema[7.2].define(version: 2026_04_12_120000) do
t.string "locality"
t.string "region"
t.string "country"
t.integer "postal_code"
t.string "postal_code"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["addressable_type", "addressable_id"], name: "index_addresses_on_addressable"

View File

@@ -4,6 +4,6 @@ one:
locality: Los Angeles
region: CA
country: US
postal_code: 90001
postal_code: "90001"
addressable: one
addressable_type: Property

View File

@@ -39,6 +39,12 @@ class AddressTest < ActiveSupport::TestCase
assert_equal "", address.to_s
end
test "accepts alphanumeric postal codes" do
address = addresses(:one)
address.update!(postal_code: "SW1A 2AA")
assert_equal "SW1A 2AA", address.reload.postal_code
end
test "can strip extras commas and spaces" do
address = Address.new(
line1: "123 Main St ,",