From ad23820a2eea777672cd13f36dd8965db17e90a7 Mon Sep 17 00:00:00 2001 From: Will Wilson Date: Wed, 29 Apr 2026 14:30:04 +0100 Subject: [PATCH] fix: change postal_code column from integer to string (#1585) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- ...28120000_change_postal_code_to_string_in_addresses.rb | 9 +++++++++ db/schema.rb | 4 ++-- test/fixtures/addresses.yml | 2 +- test/models/address_test.rb | 6 ++++++ 4 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20260428120000_change_postal_code_to_string_in_addresses.rb diff --git a/db/migrate/20260428120000_change_postal_code_to_string_in_addresses.rb b/db/migrate/20260428120000_change_postal_code_to_string_in_addresses.rb new file mode 100644 index 000000000..a67374b95 --- /dev/null +++ b/db/migrate/20260428120000_change_postal_code_to_string_in_addresses.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 1c7ecd693..079f83bd6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/test/fixtures/addresses.yml b/test/fixtures/addresses.yml index 0124887ec..4d01359a6 100644 --- a/test/fixtures/addresses.yml +++ b/test/fixtures/addresses.yml @@ -4,6 +4,6 @@ one: locality: Los Angeles region: CA country: US - postal_code: 90001 + postal_code: "90001" addressable: one addressable_type: Property diff --git a/test/models/address_test.rb b/test/models/address_test.rb index 47890d040..6e23eadc7 100644 --- a/test/models/address_test.rb +++ b/test/models/address_test.rb @@ -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 ,",