Changing a polymorphic_type in Rails
Rails stores the parent's class name in a polymorphic association's type column, so moving models into a new namespace (e.g. Car to Garage::Car) leaves mixed values like "Car" and "Garage::Car" and breaks lookups in both directions. Shopify's Payment Flexibility team decoupled the database from class names by overriding polymorphic_name to write arbitrary strings such as "car", then used an unscoped association scope to read both old and new values during the transition. After a backfill of existing rows, the temporary scope is removed and future renames only require updating a class-mapping hash. The approach adds complexity and is mainly worth it when module and class names are expected to change.
Related posts
- Caching Without Marshal Part 1: Marshal from the Inside Out
Shopify Engineering mediumAdeptRuby's Marshal serializes an object's full class graph into cache entries, so a refactor that renamed classes caused old and new code to choke on each other's cached beta-flag objects mid-deploy and triggered an incident at Shopify. Walking through marshal.c and real payloads, the post explains how Marshal encodes objects (TYPE_OBJECT), instance variables on core types (TYPE_IVAR), circular references (TYPE_LINK) and subclasses of String/Array/Hash/Regex (TYPE_UCLASS). These are exactly the features that break when replacing Marshal, setting up Shopify's migration of Rails cache serialization to MessagePack in part two.
- How GitHub converts previously encrypted and unencrypted columns to ActiveRecord encrypted columns
GitHub Engineering mediumAdeptGitHub migrated plaintext columns and columns encrypted with a deprecated internal library to ActiveRecord::Encryption. It used per-model previous encryptors instead of the global support_unencrypted_data option, and a custom ActiveModel type gated by an inverted, long-lived feature flag that acts as an on/off switch for each column. A changed_in_place? bug compared decrypted plaintext to stored ciphertext, which raised false 2FA recovery-code alerts in customer security logs; delegating the check to EncryptedAttributeType fixed it. A simplified recipe using a previous encryptor plus the MaintenanceTasks gem backfills existing records, and the same flow handles key rotation by adding a new key and re-running the task.
- Caching Without Marshal Part 2: The Path to MessagePack
Shopify Engineering mediumAdeptShopify replaced Ruby's Marshal with MessagePack as the Rails cache format for its core monolith so that code changes would stop breaking cached payloads on deploy. MessagePack only encodes types it knows about. Ruby types are added through registered extension types such as Date, Time, BigDecimal and a custom ActiveRecordCoder, which serializes records with their loaded associations and uses an instance tracker to handle circular references. The migration ran both formats side by side for about six months: payloads were prefixed with a version byte, the client fell back to Marshal when MessagePack raised NoMethodError, and each failure was logged to find missing extension types. Record payloads shrank up to 13x, total memcached usage fell by more than 25%, and decode errors from renamed classes are now treated as cache misses.
- Declarative Schemas for Simpler Database Management
Supabase mediumAdeptSupabase CLI now supports declarative schemas: the desired final state of Postgres tables, views, functions, RLS policies, grants and types lives in versioned .sql files, and a schema diff tool such as migra generates the migration files. Supabase used this internally for two years on its complex projects table, which sits in a private schema behind a public view with RLS-based ABAC and INSTEAD OF triggers. Previously, adding one column meant hand-copying view and trigger definitions into a migration of several hundred lines that conflicted often. Now it is a one-line diff, and generated migrations still get manual review. The team says this cut schema changes from hours to minutes and made rebasing much easier.
- Focus On Behavior, Not State, for a More Maintainable Codebase
Shopify Engineering mediumAdeptWhen adding pre-orders and try-before-you-buy to Shopify's Purchase Options APIs, code that equated "has a selling plan" with "is a subscription" forced repeated refactors. The fix was to model behavior rather than state: wrap selling plans in an object that answers questions like "has recurring deliveries?" or "is billing deferred?" (upfront/deferred/recurring × billing/delivery), so payments, delivery and inventory code ask only what they need and conditionals disappear. To shield the data team from schema churn, a separate reporting table records these traits and labels per order, acting as a translation layer between platform code and merchant-facing domain concepts.