Skip to content

Pattern 6 — Renaming Concatenated Table Names#

← Assimilation hub · Live test fixture: cases/07_concatenated_table_names/

Some frameworks derive a table name by lowercasing the model class and dropping the word boundaries. Peewee is the best-known case: class OrderTag becomes the table ordertag, and ShippingAddress becomes shippingaddress. pgdorm derives the table name from the class name instead (OrderTag ↔ order_tag), so a scaffold taken as-is gives you classes called Ordertag and Shippingaddress.

pgdorm cannot guess where the words were: ordertag could just as well be orderta_g. The scaffold therefore keeps the name it finds, and you name the target once, in the AdoptionMap. Everything else follows from that one edit.

What this pattern demonstrates#

  • One edit per table: set TableMapping(target="order_tag", ...) for each concatenated source table. For a renamed lookup table, rename its primary-key column along with it (columns={"id": "order_status_type_id"}), because a table's own key is always <table_name>_id.
  • Scaffold again with your map: pgm.scaffold_from_db(pool, adoption=my_map, ...) emits the matching classes (OrderTag, ShippingAddress, OrderStatusType) and keeps your targets in its ADOPTION_MAP.
  • Renames are plain ALTER TABLE ... RENAME TO: they run in the transactional phase with the rest of the assimilation, rewrite no data, and keep every foreign key, index, and sequence attached. Existing index names such as ordertag_order_id are left as they are.
  • Re-plan is empty afterwards: the live fixture checks for drift and re-plans after the cutover, so the renamed schema and the models agree exactly.

Tip

Applications that still talk to the old table names (a Peewee app running next to pgdorm during the cutover) break at the moment of the rename. Rename in the same deployment that switches those code paths over, or keep the old names until then — leaving target equal to the source name is always valid; only the class name looks unusual.

Before & After — Table-by-Table Comparison#

orderstatus → order_status_type
TypeTable + IntEnumid → order_status_type_id
CREATE TABLE orderstatus (
    id SERIAL NOT NULL PRIMARY KEY,
    code VARCHAR(32) NOT NULL UNIQUE
);
class OrderStatusKind(IntEnum):
    PENDING = 1
    PAID = 2
    CANCELLED = 3


class OrderStatusType(pg.TypeTable, enum=OrderStatusKind):
    order_status_type_id: pg.Col[OrderStatusKind] = pg.pk()
account
SERIAL → IDENTITYid → account_id
CREATE TABLE account (
    id SERIAL NOT NULL PRIMARY KEY,
    email VARCHAR(120) NOT NULL UNIQUE
);
class Account(pg.Table):
    account_id: pg.Col[pg.Int4] = pg.pk(default=pg.identity())
    email: pg.Col[str] = pg.col(max_len=120, unique=True)
order
SERIAL → IDENTITYid → order_id
CREATE TABLE "order" (
    id SERIAL NOT NULL PRIMARY KEY,
    account_id INTEGER NOT NULL REFERENCES account(id) ON DELETE CASCADE,
    status_id INTEGER NOT NULL REFERENCES orderstatus(id)
);

CREATE INDEX order_account_id ON "order" (account_id);

CREATE INDEX order_status_id ON "order" (status_id);
class Order(pg.Table):
    order_id: pg.Col[pg.Int4] = pg.pk(default=pg.identity())
    account_id: pg.Col[pg.Int4] = pg.fk(Account, on_delete=pg.enums.OnDelete.CASCADE)
    status_id: pg.Col[pg.Int4] = pg.fk(OrderStatusType)
shippingaddress → shipping_address
SERIAL → IDENTITYid → shipping_address_id
CREATE TABLE shippingaddress (
    id SERIAL NOT NULL PRIMARY KEY,
    account_id INTEGER NOT NULL REFERENCES account(id) ON DELETE CASCADE,
    city VARCHAR(80) NOT NULL
);

CREATE INDEX shippingaddress_account_id ON shippingaddress (account_id);
class ShippingAddress(pg.Table):
    shipping_address_id: pg.Col[pg.Int4] = pg.pk(default=pg.identity())
    account_id: pg.Col[pg.Int4] = pg.fk(Account, on_delete=pg.enums.OnDelete.CASCADE)
    city: pg.Col[str] = pg.col(max_len=80)
tag
SERIAL → IDENTITYid → tag_id
CREATE TABLE tag (
    id SERIAL NOT NULL PRIMARY KEY,
    name VARCHAR(64) NOT NULL UNIQUE
);
class Tag(pg.Table):
    tag_id: pg.Col[pg.Int4] = pg.pk(default=pg.identity())
    name: pg.Col[str] = pg.col(max_len=64, unique=True)
ordertag → order_tag
+ order_tag_id IDENTITY
CREATE TABLE ordertag (
    order_id INTEGER NOT NULL REFERENCES "order"(id) ON DELETE CASCADE,
    tag_id INTEGER NOT NULL REFERENCES tag(id) ON DELETE CASCADE,
    PRIMARY KEY (order_id, tag_id)
);

CREATE INDEX ordertag_order_id ON ordertag (order_id);

CREATE INDEX ordertag_tag_id ON ordertag (tag_id);
class OrderTag(pg.Table):
    order_tag_id: pg.Col[int] = pg.pk(default=pg.identity())
    order_id: pg.Col[pg.Int4] = pg.fk(Order, on_delete=pg.enums.OnDelete.CASCADE)
    tag_id: pg.Col[pg.Int4] = pg.fk(Tag, on_delete=pg.enums.OnDelete.CASCADE)

    @classmethod
    def __constraints__(cls) -> tuple[pg.TableConstraint, ...]:
        return (
            pg.Unique(cls.order_id, cls.tag_id),
        )
Generated by pgdorm scaffold · tests/integration/migrations/onboarding/cases/07_concatenated_table_names/__captured__/scaffold.py.txt
from enum import IntEnum

import pgdorm as pg
import pgdorm.migrate as pgm


class OrderStatusKind(IntEnum):
    PENDING = 1
    PAID = 2
    CANCELLED = 3


class OrderStatusType(pg.TypeTable, enum=OrderStatusKind):
    order_status_type_id: pg.Col[OrderStatusKind] = pg.pk()


class Account(pg.Table):
    account_id: pg.Col[pg.Int4] = pg.pk(default=pg.identity())
    email: pg.Col[str] = pg.col(max_len=120, unique=True)


class Order(pg.Table):
    order_id: pg.Col[pg.Int4] = pg.pk(default=pg.identity())
    account_id: pg.Col[pg.Int4] = pg.fk(Account, on_delete=pg.enums.OnDelete.CASCADE)
    status_id: pg.Col[pg.Int4] = pg.fk(OrderStatusType)


class ShippingAddress(pg.Table):
    shipping_address_id: pg.Col[pg.Int4] = pg.pk(default=pg.identity())
    account_id: pg.Col[pg.Int4] = pg.fk(Account, on_delete=pg.enums.OnDelete.CASCADE)
    city: pg.Col[str] = pg.col(max_len=80)


class Tag(pg.Table):
    tag_id: pg.Col[pg.Int4] = pg.pk(default=pg.identity())
    name: pg.Col[str] = pg.col(max_len=64, unique=True)


class OrderTag(pg.Table):
    order_tag_id: pg.Col[int] = pg.pk(default=pg.identity())
    order_id: pg.Col[pg.Int4] = pg.fk(Order, on_delete=pg.enums.OnDelete.CASCADE)
    tag_id: pg.Col[pg.Int4] = pg.fk(Tag, on_delete=pg.enums.OnDelete.CASCADE)

    @classmethod
    def __constraints__(cls) -> tuple[pg.TableConstraint, ...]:
        return (
            pg.Unique(cls.order_id, cls.tag_id),
        )


ADOPTION_MAP = pgm.AdoptionMap(
    tables={
        'account': pgm.TableMapping(target='account', columns={'id': 'account_id'}, pk_strategy=pgm.enums.PkStrategy.RENAME, migrate_serial_to_identity=True),
        'order': pgm.TableMapping(target='order', columns={'id': 'order_id'}, pk_strategy=pgm.enums.PkStrategy.RENAME, migrate_serial_to_identity=True),
        'orderstatus': pgm.TableMapping(target='order_status_type', columns={'id': 'order_status_type_id'}, as_type_table=True, pk_strategy=pgm.enums.PkStrategy.RENAME),
        'ordertag': pgm.TableMapping(target='order_tag', pk_strategy=pgm.enums.PkStrategy.ADD_SURROGATE),
        'shippingaddress': pgm.TableMapping(target='shipping_address', columns={'id': 'shipping_address_id'}, pk_strategy=pgm.enums.PkStrategy.RENAME, migrate_serial_to_identity=True),
        'tag': pgm.TableMapping(target='tag', columns={'id': 'tag_id'}, pk_strategy=pgm.enums.PkStrategy.RENAME, migrate_serial_to_identity=True),
    },
)


APP = pgm.MigrationApp(tables=(OrderStatusType, Account, Order, ShippingAddress, Tag, OrderTag))
tests/integration/migrations/onboarding/cases/07_concatenated_table_names/schema.sql
CREATE TABLE orderstatus (
    id SERIAL NOT NULL PRIMARY KEY,
    code VARCHAR(32) NOT NULL UNIQUE
);

CREATE TABLE account (
    id SERIAL NOT NULL PRIMARY KEY,
    email VARCHAR(120) NOT NULL UNIQUE
);

CREATE TABLE shippingaddress (
    id SERIAL NOT NULL PRIMARY KEY,
    account_id INTEGER NOT NULL REFERENCES account(id) ON DELETE CASCADE,
    city VARCHAR(80) NOT NULL
);
CREATE INDEX shippingaddress_account_id ON shippingaddress (account_id);

CREATE TABLE tag (
    id SERIAL NOT NULL PRIMARY KEY,
    name VARCHAR(64) NOT NULL UNIQUE
);

CREATE TABLE "order" (
    id SERIAL NOT NULL PRIMARY KEY,
    account_id INTEGER NOT NULL REFERENCES account(id) ON DELETE CASCADE,
    status_id INTEGER NOT NULL REFERENCES orderstatus(id)
);
CREATE INDEX order_account_id ON "order" (account_id);
CREATE INDEX order_status_id ON "order" (status_id);

CREATE TABLE ordertag (
    order_id INTEGER NOT NULL REFERENCES "order"(id) ON DELETE CASCADE,
    tag_id INTEGER NOT NULL REFERENCES tag(id) ON DELETE CASCADE,
    PRIMARY KEY (order_id, tag_id)
);
CREATE INDEX ordertag_order_id ON ordertag (order_id);
CREATE INDEX ordertag_tag_id ON ordertag (tag_id);
tests/integration/migrations/onboarding/cases/07_concatenated_table_names/seed.sql
INSERT INTO orderstatus (id, code) VALUES (1, 'pending'), (2, 'paid'), (3, 'cancelled');
INSERT INTO account (email) VALUES ('gus@example.com');
INSERT INTO shippingaddress (account_id, city) VALUES (1, 'Leipzig');
INSERT INTO tag (name) VALUES ('sync');
INSERT INTO "order" (account_id, status_id) VALUES (1, 2);
INSERT INTO ordertag (order_id, tag_id) VALUES (1, 1);

Live Audit Report#

migratable · exit 0
6 migration8 info
  • MIGRATIONPK_NAME_MISMATCH
    account · id
    Table 'account' PK is 'id' instead of 'account_id'.
  • MIGRATIONPK_NAME_MISMATCH
    order · id
    Table 'order' PK is 'id' instead of 'order_id'.
  • MIGRATIONPK_NAME_MISMATCH
    orderstatus · id
    Table 'orderstatus' PK is 'id' instead of 'orderstatus_id'.
  • MIGRATIONPK_COMPOSITE
    ordertag · order_id,tag_id
    Table 'ordertag' uses composite PK (order_id, tag_id).
  • MIGRATIONPK_NAME_MISMATCH
    shippingaddress · id
    Table 'shippingaddress' PK is 'id' instead of 'shippingaddress_id'.
  • MIGRATIONPK_NAME_MISMATCH
    tag · id
    Table 'tag' PK is 'id' instead of 'tag_id'.
  • INFOSERIAL_COLUMN
    account · id
    Column account.id uses owned SERIAL sequence default (nextval('account_id_seq'::regclass)).
  • INFOSERIAL_COLUMN
    order · id
    Column order.id uses owned SERIAL sequence default (nextval('order_id_seq'::regclass)).
  • INFOSERIAL_COLUMN
    orderstatus · id
    Column orderstatus.id uses owned SERIAL sequence default (nextval('orderstatus_id_seq'::regclass)).
  • INFOSERIAL_COLUMN
    shippingaddress · id
    Column shippingaddress.id uses owned SERIAL sequence default (nextval('shippingaddress_id_seq'::regclass)).
  • INFOSERIAL_COLUMN
    tag · id
    Column tag.id uses owned SERIAL sequence default (nextval('tag_id_seq'::regclass)).
  • INFOTYPE_TABLE_CANDIDATE
    account
    Table 'account' looks like a lookup table (Single integer PK + unique character varying(120) email, referenced by order, shippingaddress.)
  • INFOTYPE_TABLE_CANDIDATE
    orderstatus
    Table 'orderstatus' looks like a lookup table (Single integer PK + unique character varying(32) code, referenced by order.)
  • INFOTYPE_TABLE_CANDIDATE
    tag
    Table 'tag' looks like a lookup table (Single integer PK + unique character varying(64) name, referenced by ordertag.)
Inspect raw machine-readable audit JSON (tests/integration/migrations/onboarding/cases/07_concatenated_table_names/captured/audit.json)
{
  "counts": {
    "blocker": 0,
    "info": 8,
    "migration_required": 6,
    "unmanaged": 0
  },
  "findings": [
    {
      "auto_fixable": true,
      "category": "primary_key",
      "code": "PK_NAME_MISMATCH",
      "doc_anchor": "primary-keys-and-serials",
      "message": "Table 'account' PK is 'id' instead of 'account_id'.",
      "object_kind": "primary_key",
      "object_name": "id",
      "severity": "migration_required",
      "suggestion": "Rename single int/UUID PK to `<table_name>_id`, or add surrogate PK.",
      "table": "account"
    },
    {
      "auto_fixable": true,
      "category": "primary_key",
      "code": "PK_NAME_MISMATCH",
      "doc_anchor": "primary-keys-and-serials",
      "message": "Table 'order' PK is 'id' instead of 'order_id'.",
      "object_kind": "primary_key",
      "object_name": "id",
      "severity": "migration_required",
      "suggestion": "Rename single int/UUID PK to `<table_name>_id`, or add surrogate PK.",
      "table": "order"
    },
    {
      "auto_fixable": true,
      "category": "primary_key",
      "code": "PK_NAME_MISMATCH",
      "doc_anchor": "primary-keys-and-serials",
      "message": "Table 'orderstatus' PK is 'id' instead of 'orderstatus_id'.",
      "object_kind": "primary_key",
      "object_name": "id",
      "severity": "migration_required",
      "suggestion": "Rename single int/UUID PK to `<table_name>_id`, or add surrogate PK.",
      "table": "orderstatus"
    },
    {
      "auto_fixable": true,
      "category": "primary_key",
      "code": "PK_COMPOSITE",
      "doc_anchor": "primary-keys-and-serials",
      "message": "Table 'ordertag' uses composite PK (order_id, tag_id).",
      "object_kind": "primary_key",
      "object_name": "order_id,tag_id",
      "severity": "migration_required",
      "suggestion": "Add surrogate `<table_name>_id` PK and demote composite PK to `pg.Unique(...)`.",
      "table": "ordertag"
    },
    {
      "auto_fixable": true,
      "category": "primary_key",
      "code": "PK_NAME_MISMATCH",
      "doc_anchor": "primary-keys-and-serials",
      "message": "Table 'shippingaddress' PK is 'id' instead of 'shippingaddress_id'.",
      "object_kind": "primary_key",
      "object_name": "id",
      "severity": "migration_required",
      "suggestion": "Rename single int/UUID PK to `<table_name>_id`, or add surrogate PK.",
      "table": "shippingaddress"
    },
    {
      "auto_fixable": true,
      "category": "primary_key",
      "code": "PK_NAME_MISMATCH",
      "doc_anchor": "primary-keys-and-serials",
      "message": "Table 'tag' PK is 'id' instead of 'tag_id'.",
      "object_kind": "primary_key",
      "object_name": "id",
      "severity": "migration_required",
      "suggestion": "Rename single int/UUID PK to `<table_name>_id`, or add surrogate PK.",
      "table": "tag"
    },
    {
      "auto_fixable": true,
      "category": "primary_key",
      "code": "SERIAL_COLUMN",
      "doc_anchor": "primary-keys-and-serials",
      "message": "Column account.id uses owned SERIAL sequence default (nextval('account_id_seq'::regclass)).",
      "object_kind": "column",
      "object_name": "id",
      "severity": "info",
      "suggestion": "Assimilate upgrades SERIAL PKs to IDENTITY; bind uses `server_managed=True`.",
      "table": "account"
    },
    {
      "auto_fixable": true,
      "category": "primary_key",
      "code": "SERIAL_COLUMN",
      "doc_anchor": "primary-keys-and-serials",
      "message": "Column order.id uses owned SERIAL sequence default (nextval('order_id_seq'::regclass)).",
      "object_kind": "column",
      "object_name": "id",
      "severity": "info",
      "suggestion": "Assimilate upgrades SERIAL PKs to IDENTITY; bind uses `server_managed=True`.",
      "table": "order"
    },
    {
      "auto_fixable": true,
      "category": "primary_key",
      "code": "SERIAL_COLUMN",
      "doc_anchor": "primary-keys-and-serials",
      "message": "Column orderstatus.id uses owned SERIAL sequence default (nextval('orderstatus_id_seq'::regclass)).",
      "object_kind": "column",
      "object_name": "id",
      "severity": "info",
      "suggestion": "Assimilate upgrades SERIAL PKs to IDENTITY; bind uses `server_managed=True`.",
      "table": "orderstatus"
    },
    {
      "auto_fixable": true,
      "category": "primary_key",
      "code": "SERIAL_COLUMN",
      "doc_anchor": "primary-keys-and-serials",
      "message": "Column shippingaddress.id uses owned SERIAL sequence default (nextval('shippingaddress_id_seq'::regclass)).",
      "object_kind": "column",
      "object_name": "id",
      "severity": "info",
      "suggestion": "Assimilate upgrades SERIAL PKs to IDENTITY; bind uses `server_managed=True`.",
      "table": "shippingaddress"
    },
    {
      "auto_fixable": true,
      "category": "primary_key",
      "code": "SERIAL_COLUMN",
      "doc_anchor": "primary-keys-and-serials",
      "message": "Column tag.id uses owned SERIAL sequence default (nextval('tag_id_seq'::regclass)).",
      "object_kind": "column",
      "object_name": "id",
      "severity": "info",
      "suggestion": "Assimilate upgrades SERIAL PKs to IDENTITY; bind uses `server_managed=True`.",
      "table": "tag"
    },
    {
      "auto_fixable": true,
      "category": "type_table",
      "code": "TYPE_TABLE_CANDIDATE",
      "doc_anchor": "type-table-namespace",
      "message": "Table 'account' looks like a lookup table (Single integer PK + unique character varying(120) `email`, referenced by order, shippingaddress.)",
      "object_kind": "table",
      "object_name": "account",
      "severity": "info",
      "suggestion": "Review candidate; pass `--type-table <table>` or `as_type_table=True` to promote.",
      "table": "account"
    },
    {
      "auto_fixable": true,
      "category": "type_table",
      "code": "TYPE_TABLE_CANDIDATE",
      "doc_anchor": "type-table-namespace",
      "message": "Table 'orderstatus' looks like a lookup table (Single integer PK + unique character varying(32) `code`, referenced by order.)",
      "object_kind": "table",
      "object_name": "orderstatus",
      "severity": "info",
      "suggestion": "Review candidate; pass `--type-table <table>` or `as_type_table=True` to promote.",
      "table": "orderstatus"
    },
    {
      "auto_fixable": true,
      "category": "type_table",
      "code": "TYPE_TABLE_CANDIDATE",
      "doc_anchor": "type-table-namespace",
      "message": "Table 'tag' looks like a lookup table (Single integer PK + unique character varying(64) `name`, referenced by ordertag.)",
      "object_kind": "table",
      "object_name": "tag",
      "severity": "info",
      "suggestion": "Review candidate; pass `--type-table <table>` or `as_type_table=True` to promote.",
      "table": "tag"
    }
  ],
  "ignored_tables": [],
  "migratable": true,
  "schema": "public",
  "table_count": 6,
  "type_table_candidates": [
    {
      "code_column": "email",
      "code_pg_type": "character varying(120)",
      "confidence": "medium",
      "pk_column": "id",
      "pk_pg_type": "integer",
      "reason": "Single integer PK + unique character varying(120) `email`, referenced by order, shippingaddress.",
      "referencing_tables": [
        "order",
        "shippingaddress"
      ],
      "table": "account"
    },
    {
      "code_column": "code",
      "code_pg_type": "character varying(32)",
      "confidence": "high",
      "pk_column": "id",
      "pk_pg_type": "integer",
      "reason": "Single integer PK + unique character varying(32) `code`, referenced by order.",
      "referencing_tables": [
        "order"
      ],
      "table": "orderstatus"
    },
    {
      "code_column": "name",
      "code_pg_type": "character varying(64)",
      "confidence": "high",
      "pk_column": "id",
      "pk_pg_type": "integer",
      "reason": "Single integer PK + unique character varying(64) `name`, referenced by ordertag.",
      "referencing_tables": [
        "ordertag"
      ],
      "table": "tag"
    }
  ],
  "unmanaged_objects": {
    "extensions": [],
    "indexes": [],
    "rls_policies": [],
    "routines": [],
    "triggers": [],
    "views": []
  },
  "workflow_steps": [
    {
      "detail": "Run `pgdorm scaffold` and `pgdorm adopt-plan` (use `execute_plan_tx` inside a psycopg3 transaction when recreating views/functions atomically).",
      "order": 1,
      "phase": "ddl",
      "summary": "Scaffold models, review AdoptionMap, and execute transactional DDL"
    },
    {
      "detail": "Run `finalize_external()` or `pgdorm upgrade`, then verify `pgdorm drift`.",
      "order": 2,
      "phase": "post_commit",
      "summary": "Run CONCURRENTLY indexes, VALIDATE constraints, and record ledger"
    }
  ]
}

Phased Assimilation Plan#

Generated by pgdorm adopt-plan --format sql · tests/integration/migrations/onboarding/cases/07_concatenated_table_names/__captured__/plan.sql
-- pgdorm Assimilation Plan: 0001_assimilate (07_concatenated_table_names)
-- Checksum: 11a7c14acaa01f96ede674cde62406e81fade11b9e31995caffcd29355410817
-- Ignored tables: (none)
-- Preserved unmanaged objects: 0

-- ===========================================================================
-- PHASE 2 (TRANSACTIONAL DDL): Atomic pgdorm compiled schema assimilation
-- ===========================================================================
BEGIN;

-- (Optional PRE-DDL hook: DROP blocking views/matviews here)

-- [1] (ACCESS EXCLUSIVE) Rename table orderstatus -> order_status_type
ALTER TABLE "public"."orderstatus" RENAME TO "order_status_type";
-- [2] (ACCESS EXCLUSIVE) Rename table ordertag -> order_tag
ALTER TABLE "public"."ordertag" RENAME TO "order_tag";
-- [3] (ACCESS EXCLUSIVE) Rename table shippingaddress -> shipping_address
ALTER TABLE "public"."shippingaddress" RENAME TO "shipping_address";
-- [4] (ACCESS EXCLUSIVE) Rename column account.id -> account_id
ALTER TABLE "public"."account" RENAME COLUMN "id" TO "account_id";
-- [5] (ACCESS EXCLUSIVE) Rename column order.id -> order_id
ALTER TABLE "public"."order" RENAME COLUMN "id" TO "order_id";
-- [6] (ACCESS EXCLUSIVE) Rename column order_status_type.id -> order_status_type_id
ALTER TABLE "public"."order_status_type" RENAME COLUMN "id" TO "order_status_type_id";
-- [7] (ACCESS EXCLUSIVE) Rename column shipping_address.id -> shipping_address_id
ALTER TABLE "public"."shipping_address" RENAME COLUMN "id" TO "shipping_address_id";
-- [8] (ACCESS EXCLUSIVE) Rename column tag.id -> tag_id
ALTER TABLE "public"."tag" RENAME COLUMN "id" TO "tag_id";
-- [9] (ACCESS EXCLUSIVE) Drop default on account.account_id
ALTER TABLE "public"."account" ALTER COLUMN "account_id" DROP DEFAULT;
-- [10] (ACCESS EXCLUSIVE) Add identity on account.account_id
ALTER TABLE "public"."account" ALTER COLUMN "account_id" ADD GENERATED BY DEFAULT AS IDENTITY;
-- [11] (ACCESS EXCLUSIVE) Drop default on order.order_id
ALTER TABLE "public"."order" ALTER COLUMN "order_id" DROP DEFAULT;
-- [12] (ACCESS EXCLUSIVE) Add identity on order.order_id
ALTER TABLE "public"."order" ALTER COLUMN "order_id" ADD GENERATED BY DEFAULT AS IDENTITY;
-- [13] (ACCESS EXCLUSIVE) Drop default on order_status_type.order_status_type_id
ALTER TABLE "public"."order_status_type" ALTER COLUMN "order_status_type_id" DROP DEFAULT;
-- [14] (ACCESS EXCLUSIVE) Alter column type order_status_type.code -> text
ALTER TABLE "public"."order_status_type" ALTER COLUMN "code" TYPE TEXT;
-- [15] (ACCESS EXCLUSIVE) Add column order_tag.order_tag_id
ALTER TABLE "public"."order_tag" ADD COLUMN "order_tag_id" BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL;
-- [16] (ACCESS EXCLUSIVE) Drop constraint ordertag_pkey on order_tag
ALTER TABLE "public"."order_tag" DROP CONSTRAINT IF EXISTS "ordertag_pkey";
-- [17] (ACCESS EXCLUSIVE) Add PRIMARY KEY pk_czstclug on order_tag
ALTER TABLE "public"."order_tag" ADD CONSTRAINT "pk_czstclug" PRIMARY KEY ("order_tag_id");
-- [18] (ACCESS EXCLUSIVE) Drop default on shipping_address.shipping_address_id
ALTER TABLE "public"."shipping_address" ALTER COLUMN "shipping_address_id" DROP DEFAULT;
-- [19] (ACCESS EXCLUSIVE) Add identity on shipping_address.shipping_address_id
ALTER TABLE "public"."shipping_address" ALTER COLUMN "shipping_address_id" ADD GENERATED BY DEFAULT AS IDENTITY;
-- [20] (ACCESS EXCLUSIVE) Drop default on tag.tag_id
ALTER TABLE "public"."tag" ALTER COLUMN "tag_id" DROP DEFAULT;
-- [21] (ACCESS EXCLUSIVE) Add identity on tag.tag_id
ALTER TABLE "public"."tag" ALTER COLUMN "tag_id" ADD GENERATED BY DEFAULT AS IDENTITY;

COMMIT;

-- ===========================================================================
-- PHASE 4 (POST-COMMIT ONLINE / AUTOCOMMIT): CONCURRENTLY & VALIDATE steps
-- Run outside any BEGIN ... COMMIT block (or call pgm.finalize_external()).
-- ===========================================================================
-- [post-1] (NONE) Sync TypeTable seeds for order_status_type
-- (Parameterized query executed automatically by pgm.finalize_external())
INSERT INTO "public"."order_status_type" ("order_status_type_id", "code")
VALUES ($1, $2), ($3, $4), ($5, $6)
ON CONFLICT ("order_status_type_id") DO UPDATE SET "code" = EXCLUDED."code";
-- [post-2] (SHARE UPDATE EXCLUSIVE) Build unique index CONCURRENTLY uq_ebwjdgfd
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS "uq_ebwjdgfd" ON "public"."order_tag" ("order_id", "tag_id");
-- [post-3] (ACCESS EXCLUSIVE) Attach UNIQUE constraint uq_ebwjdgfd via index
ALTER TABLE "public"."order_tag" ADD CONSTRAINT "uq_ebwjdgfd" UNIQUE USING INDEX "uq_ebwjdgfd";
-- [post-4] (SHARE UPDATE EXCLUSIVE) Drop index ordertag_order_id
DROP INDEX CONCURRENTLY IF EXISTS "public"."ordertag_order_id";