Skip to content

Commit 084cb22

Browse files
committed
feat: fold-atomic-statements and flatten-history examples
1 parent 5f54f86 commit 084cb22

28 files changed

Lines changed: 284 additions & 3 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ produced it.
4040
| [granularity-consolidated](examples/granularity-consolidated) | [the module](examples/import-dump/output/shop) | [same schema, compact history](examples/granularity-consolidated/output/shop-consolidated) |
4141
| [partition-security](examples/partition-security) | [the module](examples/import-dump/output/shop) + [partition.json](examples/partition-security/input/partition.json) | [shop-app](examples/partition-security/output/shop-app) + [shop-security](examples/partition-security/output/shop-security) with cross-package requires |
4242
| [diff-migration](examples/diff-migration) | [the module (v1)](examples/import-dump/output/shop) vs [shop.v2.sql](examples/diff-migration/input/shop.v2.sql) | [migration module](examples/diff-migration/output/shop-v1-to-v2) + [delta as one SQL file](examples/diff-migration/output/shop.v1-to-v2.sql) |
43+
| [fold-atomic-statements](examples/fold-atomic-statements) | [atomic statement soup](examples/fold-atomic-statements/input/blog.atomic.sql) (empty `CREATE TABLE ()` + one `ADD COLUMN` per statement) | [grouped module](examples/fold-atomic-statements/output/blog) with fully folded CREATE TABLEs |
44+
| [flatten-history](examples/flatten-history) | [evolution log with churn](examples/flatten-history/input/inventory.churn.sql) (ADD then DROP column, late NOT NULL) | [flattened module](examples/flatten-history/output/inventory) |
4345

44-
More examples are planned (atomic statement soup → grouped module, history
45-
flattening, `--naming flat`, bundles + `deploy --fast`, `--append-module`,
46-
live-db diff sides) — see the full combination matrix in
46+
More examples are planned (`--naming flat`, bundles + `deploy --fast`,
47+
`--append-module`, live-db diff sides) — see the full combination matrix in
4748
[constructive-planning#1344](https://github.com/constructive-io/constructive-planning/issues/1344).
4849

4950
## Layout

examples/flatten-history/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# flatten-history — ADD / DROP / ALTER churn → flattened module
2+
3+
**Input:** [`input/inventory.churn.sql`](input/inventory.churn.sql) — an evolution log with real churn: a column is added and later dropped (`legacy_code`), NOT NULL is bolted on after the fact, constraints and an FK arrive one statement at a time.
4+
5+
**Output:** [`output/inventory/`](output/inventory) — the imported module. Late `ALTER COLUMN ... SET NOT NULL` folds into the column definition; the add→drop pair is preserved semantically (a `DROP COLUMN` rider after the folded `CREATE TABLE`), with an explicit warning that its revert isn't derivable (the prior state is unknown).
6+
7+
```sh
8+
pgpm import examples/flatten-history/input/inventory.churn.sql --pkg inventory \
9+
--out examples/flatten-history/output
10+
```
11+
12+
14 statements → 3 changes, 1 warning. This example pins down exactly how much history import flattens vs preserves — a natural place to watch the engine improve (e.g. eliding add→drop pairs entirely). CI deploys the module and loads the raw input fresh, asserting identical catalogs.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CREATE SCHEMA inventory;
2+
3+
CREATE TABLE inventory.warehouses (
4+
);
5+
6+
ALTER TABLE inventory.warehouses ADD COLUMN id serial PRIMARY KEY;
7+
ALTER TABLE inventory.warehouses ADD COLUMN name text;
8+
ALTER TABLE inventory.warehouses ALTER COLUMN name SET NOT NULL;
9+
10+
CREATE TABLE inventory.items (
11+
);
12+
13+
ALTER TABLE inventory.items ADD COLUMN id serial;
14+
ALTER TABLE inventory.items ADD COLUMN sku text;
15+
ALTER TABLE inventory.items ADD COLUMN legacy_code text;
16+
ALTER TABLE inventory.items DROP COLUMN legacy_code;
17+
ALTER TABLE inventory.items ALTER COLUMN sku SET NOT NULL;
18+
ALTER TABLE inventory.items ADD PRIMARY KEY (id);
19+
ALTER TABLE inventory.items ADD CONSTRAINT items_sku_key UNIQUE (sku);
20+
ALTER TABLE inventory.items ADD COLUMN warehouse_id int REFERENCES inventory.warehouses(id);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Deploy: schemas/inventory/schema
2+
-- made with <3 @ constructive.io
3+
4+
5+
6+
7+
CREATE SCHEMA inventory;
8+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- Deploy: schemas/inventory/tables/items/table
2+
-- made with <3 @ constructive.io
3+
4+
-- requires: schemas/inventory/schema
5+
-- requires: schemas/inventory/tables/warehouses/table
6+
7+
8+
CREATE TABLE inventory.items (
9+
id serial,
10+
sku text NOT NULL,
11+
legacy_code text,
12+
PRIMARY KEY (id),
13+
CONSTRAINT items_sku_key
14+
UNIQUE (sku),
15+
warehouse_id int REFERENCES inventory.warehouses (id)
16+
);
17+
18+
ALTER TABLE inventory.items
19+
DROP COLUMN legacy_code RESTRICT;
20+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Deploy: schemas/inventory/tables/warehouses/table
2+
-- made with <3 @ constructive.io
3+
4+
-- requires: schemas/inventory/schema
5+
6+
7+
CREATE TABLE inventory.warehouses (
8+
id serial PRIMARY KEY,
9+
name text NOT NULL
10+
);
11+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# inventory extension
2+
comment = 'inventory extension'
3+
default_version = '0.0.1'
4+
relocatable = false
5+
superuser = false
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
%syntax-version=1.0.0
2+
%project=inventory
3+
%uri=inventory
4+
5+
schemas/inventory/schema 2017-08-11T08:11:51Z constructive <constructive@5b0c196eeb62> # add schemas/inventory/schema
6+
schemas/inventory/tables/warehouses/table [schemas/inventory/schema] 2017-08-11T08:11:51Z constructive <constructive@5b0c196eeb62> # add schemas/inventory/tables/warehouses/table
7+
schemas/inventory/tables/items/table [schemas/inventory/schema schemas/inventory/tables/warehouses/table] 2017-08-11T08:11:51Z constructive <constructive@5b0c196eeb62> # add schemas/inventory/tables/items/table
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Revert: schemas/inventory/schema
2+
3+
4+
DROP SCHEMA inventory;
5+
6+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Revert: schemas/inventory/tables/items/table
2+
3+
4+
-- revert not derivable: ALTER TABLE inventory.items AT_DropColumn (prior state unknown)
5+
6+
DROP TABLE inventory.items;
7+
8+

0 commit comments

Comments
 (0)