feat(postgres,cocroach): added replica identity#5868
Open
letstri wants to merge 2 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a chained
.replicaIdentity(...)table method topgTableandcockroachTable, mirroring the existing.enableRLS()/.withRLS()ergonomics. It maps to PostgresALTER TABLE ... REPLICA IDENTITY { DEFAULT | FULL | NOTHING | USING INDEX <name> }and is wired end-to-end throughdrizzle-kit(schema → DDL, snapshot, diff/migrate, push, introspect/pull, and codegen) for both thepostgresandcockroachdialects.Type:
'default' | 'full' | 'nothing' | { usingIndex: string }. Omitting the call (or'default') keeps the database default. The value is exposed ongetTableConfig(table).replicaIdentity.What changed
drizzle-orm (
pg-core+cockroach-core)ReplicaIdentitysymbol andPgReplicaIdentity/CockroachReplicaIdentitytypes..replicaIdentity()builder on the table instance (alongsideenableRLS), plus the corresponding field onPgTableWithColumns/CockroachTableWithColumns.getTableConfig()now returnsreplicaIdentity.drizzle-kit (
postgres+cockroachdialects)tablesentity gainsreplicaIdentitymodeled as a nullable{ type: 'full' | 'nothing' | 'index'; index: string | null }, wherenull≡DEFAULT(so existing snapshots/entities deserialize without churn).drizzle.ts), snapshot serialization, and legacy snapshot upgrade (versions.ts).alter_replica_identityJSON statement + convertor emittingALTER TABLE ... REPLICA IDENTITY ..., registered in the convertor list and commutativity rules.USING INDEXform is intentionally ordered after index creation so the referenced index exists.pg_class.relreplidentplus thepg_index.indisreplidentindex name (introspect.ts,aws-introspect.ts,duckdb-introspect.ts)..replicaIdentity(...)(typescript.ts).Why the nullable single-field model
REPLICA IDENTITYhas four forms but onlyUSING INDEXcarries extra data. Storing it as one nullable object (null = DEFAULT) keepsDEFAULTand'default'from being two distinct states, lets the diff engine detect changes field-by-field, and avoids touching every existing table-entity construction site beyond adding the field whereisRlsEnabledalready lives.Test plan
New tests assert SQL generation and a real round-trip:
drizzle-kit/tests/postgres/pg-tables.test.ts— 5 tests run against in-process PGlite (real Postgres): create-with-full,default → full,full → nothing,full → default(reset), andUSING INDEX(verifies theALTERis emitted after theCREATE UNIQUE INDEX). Each pairsdiff()(statement gen) withpush()(executes against PGlite).drizzle-kit/tests/cockroach/replica-identity.test.ts— 4 DB-freediff()tests for the Cockroach dialect.integration-tests/tests/pg/utils.test.ts—getTableConfig().replicaIdentityfor all four forms.pull.test.tsand the table-entity literals incommutativity.test.ts/hints-probe-skip.test.ts/studio-postgres.tsto carry the new field (same pattern asisRlsEnabled).Empirical results on the affected suites:
drizzle-kit/tests/postgres/pg-tables.test.ts(replica identity, PGlite)drizzle-kit/tests/postgres/pull.test.ts(introspection round-trip)drizzle-kit/tests/cockroach/replica-identity.test.ts(DB-free)integration-tests/tests/pg/utils.test.ts -t replicaIdentitydrizzle-kitpostgres suitePOSTGIS_URL)Compliance
drizzle-kittsc -p tsconfig.typetest.json: clean (0 errors).drizzle-ormtest:types(type-tests): clean (0 errors).oxlinton all changeddrizzle-ormanddrizzle-kitsources: 0 warnings, 0 errors.dprint fmtapplied to all changed files.Note on CockroachDB
REPLICA IDENTITYis a Postgres logical-replication feature. The Cockroach dialect mirrors the Postgres implementation per request (the ORM API and kit codegen are symmetric), but the generatedALTER TABLE ... REPLICA IDENTITYmay be rejected at runtime depending on CockroachDB version — the Cockroach tests cover only the SQL-generation path, not execution against a live CockroachDB.