firebase-contract 0.1.0-rc.8 → 0.1.0-rc.9
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.
package/README.md
CHANGED
|
@@ -590,7 +590,7 @@ project:
|
|
|
590
590
|
| `api-validation` | api | `api-validation.ts` | endpoint request-validation Zod |
|
|
591
591
|
| `api-dto` | api | `<operation>.dto.ts` per api | class-validator DTO classes (NestJS `dto/` convention) |
|
|
592
592
|
| `task-payloads` | api | `task-payloads.ts` | envelopes + `*TaskData`/`*TaskPayload` + constants |
|
|
593
|
-
| `sql-migrations` | document | `migrations/constraints.sql` | composite FK / CHECK / index SQL
|
|
593
|
+
| `sql-migrations` | document | `migrations/constraints.sql` | composite FK / CHECK / index SQL — idempotent (`IF NOT EXISTS` / `duplicate_object` guard), safe to re-apply whole |
|
|
594
594
|
| `id-codecs` | document | `id-codecs.ts` (+ `id-core.ts`) | typed per-entity id encode/decode wrappers; emits the Sqids primitives when `project.idCodec` is set |
|
|
595
595
|
| `unions` | document | `unions.ts` | Zod discriminated unions + TS union types |
|
|
596
596
|
| `config` | document | `dataconnect.yaml`, `connector.yaml`, `constants.ts` | DC configs + sync constants (preview — see [Project config](#project-config)) |
|
|
@@ -3,7 +3,8 @@ import { Generator } from '../generator.js';
|
|
|
3
3
|
* Generates raw SQL migrations for constraints Data Connect cannot express:
|
|
4
4
|
* composite foreign keys, CHECK constraints, and extra indexes — constraints
|
|
5
5
|
* that otherwise live only as hand-written schema comments with no executable
|
|
6
|
-
* artifact.
|
|
6
|
+
* artifact. Every statement is idempotent (`IF NOT EXISTS` / duplicate_object
|
|
7
|
+
* guard), so the whole file can be (re)applied to any environment safely.
|
|
7
8
|
*/
|
|
8
9
|
export declare const createSqlMigrationGenerator: () => Generator;
|
|
9
10
|
//# sourceMappingURL=sql-migration-generator.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sql-migration-generator.d.ts","sourceRoot":"","sources":["../../../../../src/lib/_internal/generators/sql/sql-migration-generator.ts"],"names":[],"mappings":"AAEA,OAAO,EAAiB,SAAS,EAAoB,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"sql-migration-generator.d.ts","sourceRoot":"","sources":["../../../../../src/lib/_internal/generators/sql/sql-migration-generator.ts"],"names":[],"mappings":"AAEA,OAAO,EAAiB,SAAS,EAAoB,MAAM,iBAAiB,CAAA;AA0C5E;;;;;;GAMG;AACH,eAAO,MAAM,2BAA2B,QAAO,SAY7C,CAAA"}
|
|
@@ -2,6 +2,12 @@ import { pluralize, snakeCase } from '../support/naming.js';
|
|
|
2
2
|
import { headerBlocks } from '../support/header.js';
|
|
3
3
|
import { outputFile } from '../support/templates.js';
|
|
4
4
|
const tableName = (model) => model.table ?? pluralize(snakeCase(model.name));
|
|
5
|
+
/**
|
|
6
|
+
* Postgres has no `ADD CONSTRAINT IF NOT EXISTS`, so constraint statements are
|
|
7
|
+
* wrapped in a DO block that swallows `duplicate_object` — rerunning the whole
|
|
8
|
+
* file against an already-migrated database is a no-op.
|
|
9
|
+
*/
|
|
10
|
+
const guarded = (statement) => ['DO $$ BEGIN', ` ${statement}`, 'EXCEPTION WHEN duplicate_object THEN NULL;', 'END $$;'].join('\n');
|
|
5
11
|
const renderModelSql = (model) => {
|
|
6
12
|
const sql = model.sql;
|
|
7
13
|
if (!sql)
|
|
@@ -10,17 +16,17 @@ const renderModelSql = (model) => {
|
|
|
10
16
|
const statements = [];
|
|
11
17
|
sql.checks.forEach((expr, i) => {
|
|
12
18
|
const name = `${table}_chk_${i + 1}`;
|
|
13
|
-
statements.push(`ALTER TABLE "${table}" ADD CONSTRAINT "${name}" CHECK (${expr});`);
|
|
19
|
+
statements.push(guarded(`ALTER TABLE "${table}" ADD CONSTRAINT "${name}" CHECK (${expr});`));
|
|
14
20
|
});
|
|
15
21
|
for (const fk of sql.foreignKeys) {
|
|
16
22
|
const name = fk.name ?? `${table}_${fk.columns.join('_')}_fkey`;
|
|
17
23
|
const cols = fk.columns.map(c => `"${c}"`).join(', ');
|
|
18
|
-
statements.push(`ALTER TABLE "${table}" ADD CONSTRAINT "${name}" FOREIGN KEY (${cols}) REFERENCES ${fk.references};`);
|
|
24
|
+
statements.push(guarded(`ALTER TABLE "${table}" ADD CONSTRAINT "${name}" FOREIGN KEY (${cols}) REFERENCES ${fk.references};`));
|
|
19
25
|
}
|
|
20
26
|
for (const idx of sql.indexes) {
|
|
21
27
|
const name = idx.name ?? `${table}_${idx.columns.join('_')}_${idx.unique ? 'uidx' : 'idx'}`;
|
|
22
28
|
const cols = idx.columns.map(c => `"${c}"`).join(', ');
|
|
23
|
-
statements.push(`CREATE ${idx.unique ? 'UNIQUE ' : ''}INDEX "${name}" ON "${table}" (${cols});`);
|
|
29
|
+
statements.push(`CREATE ${idx.unique ? 'UNIQUE ' : ''}INDEX IF NOT EXISTS "${name}" ON "${table}" (${cols});`);
|
|
24
30
|
}
|
|
25
31
|
return statements.length > 0 ? [`-- ${model.name} (${table})`, ...statements] : [];
|
|
26
32
|
};
|
|
@@ -28,7 +34,8 @@ const renderModelSql = (model) => {
|
|
|
28
34
|
* Generates raw SQL migrations for constraints Data Connect cannot express:
|
|
29
35
|
* composite foreign keys, CHECK constraints, and extra indexes — constraints
|
|
30
36
|
* that otherwise live only as hand-written schema comments with no executable
|
|
31
|
-
* artifact.
|
|
37
|
+
* artifact. Every statement is idempotent (`IF NOT EXISTS` / duplicate_object
|
|
38
|
+
* guard), so the whole file can be (re)applied to any environment safely.
|
|
32
39
|
*/
|
|
33
40
|
export const createSqlMigrationGenerator = () => ({
|
|
34
41
|
name: 'sql-migrations',
|