@coffre/db 0.1.11 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/dist/connect.d.ts +1 -1
  2. package/dist/{database-BhLco7OA.d.ts → database-CNMfHZwF.d.ts} +1 -1
  3. package/dist/dialect.d.ts +1 -1
  4. package/dist/hyperdrive.d.ts +1 -1
  5. package/dist/index.d.ts +2 -2
  6. package/dist/log.d.ts +1 -1
  7. package/dist/migrations/postgres/0001_remove_syncs.sql +13 -0
  8. package/dist/migrations/postgres/meta/0001_snapshot.json +1707 -0
  9. package/dist/migrations/postgres/meta/_journal.json +7 -0
  10. package/dist/migrations/sqlite/0001_remove_syncs.sql +13 -0
  11. package/dist/migrations/sqlite/meta/0001_snapshot.json +1575 -0
  12. package/dist/migrations/sqlite/meta/_journal.json +7 -0
  13. package/dist/portable.d.ts +2 -2
  14. package/dist/{relations-DqgxtlKu.js → relations-BfR3JjT1.js} +2 -22
  15. package/dist/{schema-Cd4WOnB5.d.ts → schema-AmvM6w0D.d.ts} +2 -362
  16. package/dist/schema-sqlite.d.ts +1 -369
  17. package/dist/schema-sqlite.js +5 -59
  18. package/dist/schema-version.d.ts +5 -3
  19. package/dist/schema-version.js +4 -2
  20. package/dist/schema.d.ts +2 -2
  21. package/dist/schema.js +5 -79
  22. package/package.json +2 -2
  23. package/src/baseline.ts +11 -19
  24. package/src/migrations/postgres/0001_remove_syncs.sql +13 -0
  25. package/src/migrations/postgres/meta/0001_snapshot.json +1707 -0
  26. package/src/migrations/postgres/meta/_journal.json +7 -0
  27. package/src/migrations/sqlite/0001_remove_syncs.sql +13 -0
  28. package/src/migrations/sqlite/meta/0001_snapshot.json +1575 -0
  29. package/src/migrations/sqlite/meta/_journal.json +7 -0
  30. package/src/relations.ts +1 -14
  31. package/src/schema-version.ts +4 -2
  32. package/src/schema.sqlite.ts +1 -65
  33. package/src/schema.ts +1 -81
package/src/baseline.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { execFileSync } from 'node:child_process';
2
- import { readFile, rm, writeFile } from 'node:fs/promises';
2
+ import { readFile } from 'node:fs/promises';
3
3
  import { fileURLToPath } from 'node:url';
4
4
 
5
5
  import {
@@ -15,17 +15,15 @@ import * as postgres from './schema.ts';
15
15
  import * as sqlite from './schema.sqlite.ts';
16
16
 
17
17
  /**
18
- * coffre has never been deployed, so there is no database to upgrade: each
19
- * engine has one migration, its baseline, and a schema change regenerates it
20
- * rather than adding a second. After the first deployment, changes become new
21
- * migrations and this goes.
18
+ * Applied migrations are immutable. Schema changes append a migration to
19
+ * each engine's journal; custom safety checks belong in that new migration.
22
20
  *
23
21
  * A baseline is baseline/<engine>.sql with its `-- @schema` line replaced by
24
22
  * the tables drizzle-kit generates from that engine's schema. The template
25
23
  * holds what a schema cannot say: the audit chain's first rows, and the
26
24
  * Postgres runtime role and its grants.
27
25
  *
28
- * `pnpm db:generate` runs this file and rewrites the baselines that are stale.
26
+ * `pnpm db:generate` runs this file and appends pending schema changes.
29
27
  */
30
28
 
31
29
  export const ENGINES: readonly Engine[] = ['postgres', 'sqlite'];
@@ -63,13 +61,13 @@ async function pending(engine: Engine): Promise<string[]> {
63
61
  }
64
62
  }
65
63
 
66
- /** Why the engine's baseline is out of date: empty when it is not. */
64
+ /** Why the migration tree is out of date: empty when it matches the schema. */
67
65
  export async function staleness(engine: Engine): Promise<string[]> {
68
66
  const tags = await journal(engine).then(
69
67
  (entries) => entries.map((entry) => entry.tag),
70
68
  () => [],
71
69
  );
72
- if (tags.length !== 1 || tags[0] !== TAG) return [`expected only ${TAG}, found [${tags.join(', ')}]`];
70
+ if (tags[0] !== TAG) return [`expected the immutable ${TAG} first`];
73
71
  const reasons = await pending(engine);
74
72
  const { head, tail } = await template(engine);
75
73
  const baseline = await readFile(`${migrationsFolder(engine)}/${TAG}.sql`, 'utf8');
@@ -77,28 +75,22 @@ export async function staleness(engine: Engine): Promise<string[]> {
77
75
  return reasons;
78
76
  }
79
77
 
80
- async function regenerate(engine: Engine): Promise<void> {
81
- const folder = migrationsFolder(engine);
82
- await rm(folder, { recursive: true, force: true });
78
+ function generate(engine: Engine): void {
83
79
  execFileSync(
84
80
  `${packageRoot}node_modules/.bin/drizzle-kit`,
85
- ['generate', '--config', `drizzle.${engine}.config.ts`, '--name', 'baseline'],
81
+ ['generate', '--config', `drizzle.${engine}.config.ts`, '--name', process.argv[2] ?? 'schema'],
86
82
  { cwd: here, stdio: 'inherit' },
87
83
  );
88
- const tables = (await readFile(`${folder}/${TAG}.sql`, 'utf8')).trimEnd();
89
- const breakpoint = tables.endsWith('--> statement-breakpoint') ? '' : '--> statement-breakpoint';
90
- const { head, tail } = await template(engine);
91
- await writeFile(`${folder}/${TAG}.sql`, `${head}${tables}${breakpoint}\n${tail}`);
92
84
  }
93
85
 
94
86
  if (process.argv[1] === fileURLToPath(import.meta.url)) {
95
87
  for (const engine of ENGINES) {
96
88
  const reasons = await staleness(engine);
97
89
  if (reasons.length === 0) {
98
- console.log(`${engine}: the baseline is up to date`);
90
+ console.log(`${engine}: the migration tree is up to date`);
99
91
  continue;
100
92
  }
101
- console.log(`${engine}: regenerating the baseline (${reasons.length} change${reasons.length === 1 ? '' : 's'})`);
102
- await regenerate(engine);
93
+ console.log(`${engine}: appending a migration (${reasons.length} change${reasons.length === 1 ? '' : 's'})`);
94
+ generate(engine);
103
95
  }
104
96
  }
@@ -0,0 +1,13 @@
1
+ -- Stop old app instances before upgrading. Never silently discard a configured
2
+ -- destination, including archived ones. Keep the audit and sealed legacy rows.
3
+ LOCK TABLE syncs, sync_keys IN ACCESS EXCLUSIVE MODE;
4
+ --> statement-breakpoint
5
+ DO $$ BEGIN
6
+ IF EXISTS (SELECT 1 FROM syncs) OR EXISTS (SELECT 1 FROM sync_keys) THEN
7
+ RAISE EXCEPTION 'syncs are removed: migrate destinations to service tokens, back up and clear syncs and sync_keys before upgrading';
8
+ END IF;
9
+ END $$;
10
+ --> statement-breakpoint
11
+ DROP TABLE sync_keys;
12
+ --> statement-breakpoint
13
+ DROP TABLE syncs;