Adding a tenant ID column to about 200 tables in a live multi-tenant service
Counterworks describes how it added the tenant identifier column enterprise_id to about 200 tables that hold tenant data in its live service, Shop Counter Enterprise (SCE), a cloud management system for digital transformation of leasing. The tech lead, shim, says this groundwork enables double defense: filtering in the app layer plus PostgreSQL Row Level Security[1]. Rails internal tables, the prefectures master table and the enterprises table itself were excluded.
The article compares keeping the column only in parent tables with keeping it in every table that holds tenant data. The team chose every table, because a policy that reads other tables can leak other tenants' data through race conditions, and new tables must also get the column, with a CI check that every table has a policy.
The column was added in three steps: NULLABLE first, then filling existing rows with values (backfill[2]), then NOT NULL. One migration that updated all tables at once hit the deploy wait limit of about 10 minutes, so the team released the column and the backfill earlier from the develop branch. The NOT NULL constraint and the composite unique constraint[3] came later, in the release where the app began setting enterprise_id.
Sources
-
JPZenn JA
稼働中サービスでテナントのデータを持つ約200テーブルにテナント識別カラムを追加した話(マルチテナント化連載 第4回・200テーブル改修編)



