Skip to content

Commit 16a2bca

Browse files
authored
Merge pull request #70 from sqlcj-dev/chore/document-pg-dump-schema-snapshot-loading
docs: describe how pg_dump schema snapshots load
2 parents 815fd0f + ea0b202 commit 16a2bca

2 files changed

Lines changed: 51 additions & 3797 deletions

File tree

‎docs/postgresql.md‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,39 @@ order the files are read in.
120120
unqualified name always belongs to the one namespace sqlcj models, whatever a
121121
`SET search_path` statement in the snapshot says.
122122

123+
A `pg_dump --schema-only` file is such a snapshot and loads unchanged: it is
124+
configured as one schema file, and nothing in it has to be edited, removed, or
125+
reordered first. This is the schema input of a service that does not keep its
126+
schema as SQL migrations, for example one with a Liquibase changelog or a
127+
JPA-generated schema.
128+
129+
A dump states much more than the tables it creates, and sqlcj ignores all of it
130+
rather than rejecting it:
131+
132+
- the `\restrict` and `\unrestrict` meta-command lines recent releases write,
133+
each with a fresh key, and every other psql meta-command line,
134+
- the `SET` preamble and the `SELECT pg_catalog.set_config(...)` call, including
135+
`SET search_path`,
136+
- the identity form it writes for every identity column,
137+
`ALTER TABLE ... ALTER COLUMN ... ADD GENERATED ... AS IDENTITY (SEQUENCE NAME ...)`,
138+
which sqlcj's parser cannot read but which cannot change a column either, and
139+
its sequences and `SET DEFAULT nextval(...)` statements,
140+
- its constraint and index statements, its `OWNER TO` statements, and its
141+
grants, policies, comments, extensions, domains, composite types, functions,
142+
triggers, views, and materialized views, and
143+
- the tables, types, and statements of every schema other than `public`.
144+
145+
A dump writes a partition as a plain `CREATE TABLE` followed by
146+
`ALTER TABLE ONLY ... ATTACH PARTITION`, so the partition is modeled with its
147+
own columns and then linked to its partitioned table, and it qualifies every
148+
table, type, and column type with `public.`, which is the one namespace sqlcj
149+
models. A dump of a database built by a migration history that changes the
150+
schema only through statements sqlcj reads therefore generates the same Java as
151+
that history. A schema change the history makes indirectly, such as DDL inside
152+
a `DO` block or a function body, or a column dropped by
153+
`DROP TYPE ... CASCADE`, stands in the database and in its dump but not in the
154+
history sqlcj reads; see [Ignored Statements](#ignored-statements).
155+
123156
## Supported Column Types
124157

125158
Type spellings are matched case-insensitively, after parenthesized type
@@ -797,6 +830,24 @@ sqlcj: Invalid query 'ListTags' in /home/dev/project/queries.sql at line 5: Colu
797830

798831
## Verified by
799832

833+
Schema snapshot input:
834+
835+
- `ReferenceServiceIntegrationTest.shouldGenerateIdenticalRepositoriesFromHistorySnapshotAndDump`
836+
applies the reference service's ten migrations to PostgreSQL 16 with `psql`,
837+
runs `pg_dump --schema-only` against the migrated database, and generates one
838+
byte-identical, compiling output tree, manifest included, from the migration
839+
history, the hand-written snapshot, and that dump.
840+
- `ReferenceServiceIntegrationTest.shouldExecuteTheGeneratedReferenceServiceQueries`
841+
executes the generated repository against the migrated database, including the
842+
replaced enum, the renamed enum label, an optimistic update, the partition
843+
that follows the column its partitioned table gained, and the `LIKE` copy that
844+
does not.
845+
- `ReferenceServiceIntegrationTest.shouldRejectTheUnsupportedSetDataTypeMigration`
846+
and
847+
`ReferenceServiceIntegrationTest.shouldRejectAQueryOnASecondSchemaTable`
848+
cover the located `SET DATA TYPE` diagnostic and the unknown-table diagnostic
849+
of a query that names a table of the reporting schema, each writing no file.
850+
800851
Type table:
801852

802853
- `DefaultSchemaParserTest.shouldKeepParsingDeliveredColumnTypes`,

0 commit comments

Comments
 (0)