feat: flatten tenant_configs table with type column#304
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
WalkthroughThe PR restructures TenantConfig storage from a single legacy JSON blob to typed flat rows keyed by (Key, Type), introducing ChangesTenantConfig flat-row storage and dependent wiring
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@coderabbitai summary |
✅ Action performedSummary regeneration triggered. |
28c09e5 to
bb8f912
Compare
bb8f912 to
cf29c9d
Compare
| UPDATE tenant_configs | ||
| SET value = jsonb_build_object( | ||
| 'roleManagementConfig', jsonb_build_object( -- NOSONAR | ||
| 'localityId', value::jsonb -> 'localityId', -- NOSONAR | ||
| 'commonName', value::jsonb -> 'commonName', -- NOSONAR | ||
| 'accessData', value::jsonb -> 'managementAccessData' | ||
| ), | ||
| 'supportedRegions', value::jsonb -> 'supportedRegions' -- NOSONAR | ||
| )::jsonb | ||
| WHERE "key" = 'DEFAULT_KEYSTORE' | ||
| AND value::jsonb ? 'localityId' | ||
| AND NOT (value::jsonb ? 'roleManagementConfig'); |
There was a problem hiding this comment.
These are data migrations, not schema ones. This is also a long running one
There was a problem hiding this comment.
Nice catch moved
| -- This migration flattens tenant_configs from a single JSON blob per key into | ||
| -- (key, value, type) flat rows so individual config entries can be filtered by | ||
| -- type/key. Data migration 00002_flatten_tenant_configs.go backfills flat rows | ||
| -- from the legacy blobs; the legacy blobs are dropped in a future release. | ||
| -- | ||
| -- Down drops flat rows. Since post-flatten writes target flat rows only, Down | ||
| -- discards any changes made after the migration. Use only before traffic; | ||
| -- restore from backup for operational rollback after production writes. | ||
| -- +goose Up | ||
|
|
||
| ALTER TABLE tenant_configs ADD COLUMN IF NOT EXISTS "type" varchar(255) NOT NULL DEFAULT ''; | ||
|
|
||
| ALTER TABLE tenant_configs ALTER COLUMN value TYPE text USING value::text; | ||
|
|
||
| ALTER TABLE tenant_configs DROP CONSTRAINT IF EXISTS tenant_configs_pkey; | ||
| ALTER TABLE tenant_configs ADD CONSTRAINT tenant_configs_pkey PRIMARY KEY ("key", "type"); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS idx_tenant_configs_type ON tenant_configs ("type"); | ||
|
|
||
| -- +goose Down | ||
|
|
||
| DROP INDEX IF EXISTS idx_tenant_configs_type; | ||
|
|
||
| -- Flat rows hold non-JSON text and would fail the jsonb cast below. | ||
| DELETE FROM tenant_configs WHERE length("type") > 0; | ||
|
|
||
| ALTER TABLE tenant_configs DROP CONSTRAINT IF EXISTS tenant_configs_pkey; | ||
| ALTER TABLE tenant_configs DROP COLUMN IF EXISTS "type"; | ||
| ALTER TABLE tenant_configs ALTER COLUMN value TYPE jsonb USING value::jsonb; | ||
| ALTER TABLE tenant_configs ADD CONSTRAINT tenant_configs_pkey PRIMARY KEY ("key"); |
There was a problem hiding this comment.
None of this is safe. We need to use Expand and Contract pattern. If we run it like this we break running instances
There was a problem hiding this comment.
Reworked to ensure use of the Expand & Contract pattern
There was a problem hiding this comment.
These data migrations are also not safe. THere needs to be version/schema checks before running them as they must only be executed if the Database is in a certain state, otherwise they need to be skipped
There was a problem hiding this comment.
Added guards to both migrations
78a6ea9 to
7b1bed4
Compare
|
@coderabbitai summary |
✅ Action performedSummary regeneration triggered. |
Signed-off-by: Niaz Khan <niazkhansap@gmail.com>
7b1bed4 to
3808e84
Compare
Related cleanup PR needs to be rolled out after this.