@prisma-next/target-postgres 0.13.0-dev.34 → 0.13.0-dev.36
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/dist/contract-free.d.mts +147 -14
- package/dist/contract-free.d.mts.map +1 -1
- package/dist/contract-free.mjs +3 -16
- package/dist/contract-free.mjs.map +1 -1
- package/dist/control.mjs +2 -2
- package/dist/ddl-QDyOSeLc.mjs +251 -0
- package/dist/ddl-QDyOSeLc.mjs.map +1 -0
- package/dist/{issue-planner-DsjB7xDj.mjs → issue-planner-CoI_0uM1.mjs} +9 -119
- package/dist/issue-planner-CoI_0uM1.mjs.map +1 -0
- package/dist/issue-planner.d.mts +2 -2
- package/dist/issue-planner.d.mts.map +1 -1
- package/dist/issue-planner.mjs +1 -1
- package/dist/migration.d.mts +4 -80
- package/dist/migration.d.mts.map +1 -1
- package/dist/migration.mjs +3 -3
- package/dist/{op-factory-call-CjR846f7.mjs → op-factory-call-B1bXWtfa.mjs} +551 -241
- package/dist/op-factory-call-B1bXWtfa.mjs.map +1 -0
- package/dist/{op-factory-call-CdtMyrlU.d.mts → op-factory-call-DmQEc3XV.d.mts} +111 -20
- package/dist/op-factory-call-DmQEc3XV.d.mts.map +1 -0
- package/dist/op-factory-call.d.mts +1 -1
- package/dist/op-factory-call.mjs +1 -1
- package/dist/{planner-_FOL4I21.mjs → planner-DS5XBhmi.mjs} +4 -4
- package/dist/{planner-_FOL4I21.mjs.map → planner-DS5XBhmi.mjs.map} +1 -1
- package/dist/{planner-produced-postgres-migration-BmCpyWLJ.mjs → planner-produced-postgres-migration-DTwCCek_.mjs} +2 -2
- package/dist/{planner-produced-postgres-migration-BmCpyWLJ.mjs.map → planner-produced-postgres-migration-DTwCCek_.mjs.map} +1 -1
- package/dist/{planner-produced-postgres-migration-wLhnJMMA.d.mts → planner-produced-postgres-migration-QqHa2C2l.d.mts} +2 -2
- package/dist/{planner-produced-postgres-migration-wLhnJMMA.d.mts.map → planner-produced-postgres-migration-QqHa2C2l.d.mts.map} +1 -1
- package/dist/planner-produced-postgres-migration.d.mts +1 -1
- package/dist/planner-produced-postgres-migration.mjs +1 -1
- package/dist/planner-sql-checks-jqUUGyQR.mjs +152 -0
- package/dist/planner-sql-checks-jqUUGyQR.mjs.map +1 -0
- package/dist/planner-sql-checks.d.mts +1 -47
- package/dist/planner-sql-checks.d.mts.map +1 -1
- package/dist/planner-sql-checks.mjs +2 -2
- package/dist/planner.d.mts +1 -1
- package/dist/planner.mjs +1 -1
- package/dist/{postgres-contract-serializer-CyAe8ZFv.mjs → postgres-contract-serializer-E92REOFk.mjs} +3 -3
- package/dist/postgres-contract-serializer-E92REOFk.mjs.map +1 -0
- package/dist/postgres-migration-Y4YBJqkS.d.mts +181 -0
- package/dist/postgres-migration-Y4YBJqkS.d.mts.map +1 -0
- package/dist/postgres-migration-otiaw3Ru.mjs +145 -0
- package/dist/postgres-migration-otiaw3Ru.mjs.map +1 -0
- package/dist/{postgres-schema-CTKYiTHu.mjs → postgres-schema-COGZ1ark.mjs} +71 -29
- package/dist/postgres-schema-COGZ1ark.mjs.map +1 -0
- package/dist/runtime.mjs +1 -1
- package/dist/table-source-BvFo7gVs.d.mts +15 -0
- package/dist/table-source-BvFo7gVs.d.mts.map +1 -0
- package/dist/types.d.mts +28 -8
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs +1 -1
- package/package.json +17 -17
- package/src/contract-free/checks.ts +363 -0
- package/src/core/migrations/op-factory-call.ts +417 -94
- package/src/core/migrations/operations/columns.ts +175 -140
- package/src/core/migrations/operations/constraints.ts +79 -108
- package/src/core/migrations/operations/dependencies.ts +16 -14
- package/src/core/migrations/operations/indexes.ts +31 -28
- package/src/core/migrations/operations/shared.ts +2 -2
- package/src/core/migrations/operations/tables.ts +13 -14
- package/src/core/migrations/planner-recipes.ts +42 -33
- package/src/core/migrations/planner-sql-checks.ts +1 -172
- package/src/core/migrations/planner-strategies.ts +25 -73
- package/src/core/migrations/postgres-migration.ts +272 -7
- package/src/core/postgres-contract-serializer.ts +1 -1
- package/src/core/postgres-schema.ts +70 -52
- package/src/exports/contract-free.ts +21 -0
- package/src/exports/migration.ts +1 -22
- package/src/exports/planner-sql-checks.ts +0 -7
- package/dist/ddl-DY2R_Yqz.mjs +0 -45
- package/dist/ddl-DY2R_Yqz.mjs.map +0 -1
- package/dist/issue-planner-DsjB7xDj.mjs.map +0 -1
- package/dist/op-factory-call-CdtMyrlU.d.mts.map +0 -1
- package/dist/op-factory-call-CjR846f7.mjs.map +0 -1
- package/dist/planner-sql-checks-CJJtPfDH.mjs +0 -272
- package/dist/planner-sql-checks-CJJtPfDH.mjs.map +0 -1
- package/dist/postgres-contract-serializer-CyAe8ZFv.mjs.map +0 -1
- package/dist/postgres-migration-DLXL0GBf.d.mts +0 -77
- package/dist/postgres-migration-DLXL0GBf.d.mts.map +0 -1
- package/dist/postgres-migration-dG-J0aI8.mjs +0 -75
- package/dist/postgres-migration-dG-J0aI8.mjs.map +0 -1
- package/dist/postgres-schema-CTKYiTHu.mjs.map +0 -1
- package/dist/shared-jcsbXxiW.d.mts +0 -25
- package/dist/shared-jcsbXxiW.d.mts.map +0 -1
package/dist/types.d.mts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { t as PostgresTableSource } from "./table-source-BvFo7gVs.mjs";
|
|
1
2
|
import { t as PostgresColumnDefault } from "./types-BDKkx8MA.mjs";
|
|
2
3
|
import { NamespaceBase } from "@prisma-next/framework-components/ir";
|
|
3
|
-
import {
|
|
4
|
+
import { CfExpr } from "@prisma-next/sql-relational-core/contract-free";
|
|
5
|
+
import { SqlNamespaceEntries, SqlNamespaceTablesInput, SqlStorage, StorageTable, StorageValueSet } from "@prisma-next/sql-contract/types";
|
|
4
6
|
|
|
5
7
|
//#region src/core/postgres-schema.d.ts
|
|
6
8
|
interface PostgresSchemaInput {
|
|
@@ -32,7 +34,7 @@ declare class PostgresSchema extends NamespaceBase {
|
|
|
32
34
|
static unbound: PostgresUnboundSchema;
|
|
33
35
|
readonly kind: 'schema';
|
|
34
36
|
readonly id: string;
|
|
35
|
-
readonly entries:
|
|
37
|
+
readonly entries: SqlNamespaceEntries;
|
|
36
38
|
constructor(input: PostgresSchemaInput);
|
|
37
39
|
get table(): Readonly<Record<string, StorageTable>>;
|
|
38
40
|
get valueSet(): Readonly<Record<string, StorageValueSet>> | undefined;
|
|
@@ -65,6 +67,22 @@ declare class PostgresSchema extends NamespaceBase {
|
|
|
65
67
|
* connection's `search_path` resolved at runtime.
|
|
66
68
|
*/
|
|
67
69
|
schemaSqlExpression(): string;
|
|
70
|
+
/**
|
|
71
|
+
* Typed-AST counterpart of {@link schemaSqlExpression}: the expression a
|
|
72
|
+
* builder-built catalog check compares `n.nspname` / `table_schema`
|
|
73
|
+
* against. Named schemas bind the schema name as a text parameter; the
|
|
74
|
+
* unbound singleton overrides this to the opaque `current_schema()`
|
|
75
|
+
* expression so the live connection's `search_path` decides at runtime.
|
|
76
|
+
*/
|
|
77
|
+
schemaFilterExpression(): CfExpr;
|
|
78
|
+
/**
|
|
79
|
+
* Typed-AST counterpart of {@link qualifyTable}: the FROM source a
|
|
80
|
+
* builder-built check uses to address a user table in this namespace.
|
|
81
|
+
* Named schemas qualify (`"schema"."table"`); the unbound singleton
|
|
82
|
+
* overrides this to leave the table unqualified so `search_path`
|
|
83
|
+
* resolves it at runtime.
|
|
84
|
+
*/
|
|
85
|
+
tableSource(tableName: string, alias?: string): PostgresTableSource;
|
|
68
86
|
/**
|
|
69
87
|
* The bare schema name a DDL planner should target when emitting
|
|
70
88
|
* statements that need to identify this namespace in the live
|
|
@@ -72,9 +90,9 @@ declare class PostgresSchema extends NamespaceBase {
|
|
|
72
90
|
* catalog filters, planner conflict lookups). Named schemas resolve
|
|
73
91
|
* to their own id. The `PostgresUnboundSchema` singleton inherits
|
|
74
92
|
* this and returns `UNBOUND_NAMESPACE_ID` — callers that dispatch
|
|
75
|
-
* through `qualifyTableName`
|
|
76
|
-
*
|
|
77
|
-
*
|
|
93
|
+
* through `qualifyTableName` route through the polymorphic
|
|
94
|
+
* `PostgresUnboundSchema` overrides and produce unqualified
|
|
95
|
+
* (search-path-resolved) output automatically.
|
|
78
96
|
*/
|
|
79
97
|
ddlSchemaName(_storage: SqlStorage): string;
|
|
80
98
|
}
|
|
@@ -93,9 +111,9 @@ declare class PostgresSchema extends NamespaceBase {
|
|
|
93
111
|
* `search_path`).
|
|
94
112
|
*
|
|
95
113
|
* `ddlSchemaName` is inherited from `PostgresSchema` and returns
|
|
96
|
-
* `UNBOUND_NAMESPACE_ID`. Downstream helpers
|
|
97
|
-
*
|
|
98
|
-
*
|
|
114
|
+
* `UNBOUND_NAMESPACE_ID`. Downstream helpers such as `qualifyTableName`
|
|
115
|
+
* route through the polymorphic factory and produce unqualified output
|
|
116
|
+
* automatically.
|
|
99
117
|
*/
|
|
100
118
|
declare class PostgresUnboundSchema extends PostgresSchema {
|
|
101
119
|
static readonly instance: PostgresUnboundSchema;
|
|
@@ -103,6 +121,8 @@ declare class PostgresUnboundSchema extends PostgresSchema {
|
|
|
103
121
|
qualifier(): string;
|
|
104
122
|
qualifyTable(tableName: string): string;
|
|
105
123
|
schemaSqlExpression(): string;
|
|
124
|
+
schemaFilterExpression(): CfExpr;
|
|
125
|
+
tableSource(tableName: string, alias?: string): PostgresTableSource;
|
|
106
126
|
}
|
|
107
127
|
/**
|
|
108
128
|
* Target-supplied `Namespace` factory the Postgres target plumbs
|
package/dist/types.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.mts","names":[],"sources":["../src/core/postgres-schema.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.d.mts","names":[],"sources":["../src/core/postgres-schema.ts"],"mappings":";;;;;;;UAqBiB,mBAAA;EAAA,SACN,EAAA;EAAA,SACA,OAAA,EAAS,QAAA,CAAS,MAAA,SAAe,QAAA,CAAS,MAAA;AAAA;;;;;;;;;;;;;;;cAiBxC,cAAA,SAAuB,aAAA;EAjBuB;AAiB3D;;;;;;EAjB2D,OAyBlD,OAAA,EAAS,qBAAA;EAAA,SAEC,IAAA;EAAA,SACR,EAAA;EAAA,SACA,OAAA,EAAS,mBAAA;cAEN,KAAA,EAAO,mBAAA;EAAA,IA4Bf,KAAA,IAAS,QAAA,CAAS,MAAA,SAAe,YAAA;EAAA,IAIjC,QAAA,IAAY,QAAA,CAAS,MAAA,SAAe,eAAA;EA+DQ;;;;;EAtDhD,SAAA;EA/CO;;;;;;EAyDP,YAAA,CAAa,SAAA;EAnDM;;;;;;EA6DnB,eAAA,CAAgB,IAAA;EA7BA;;;;;;;;EAyChB,mBAAA;EAWA;;;;;;;EAAA,sBAAA,IAA0B,MAAA;EA8BZ;;AAAoB;AAwBpC;;;;EA3CE,WAAA,CAAY,SAAA,UAAmB,KAAA,YAAiB,mBAAA;EA8Db;;;;;;;;;;;EA3CnC,aAAA,CAAc,QAAA,EAAU,UAAA;AAAA;;;;;;;;;;AA+CoD;AAkC9E;;;;;;;;AAAuF;cAzD1E,qBAAA,SAA8B,cAAA;EAAA,gBACzB,QAAA,EAAU,qBAAA;cAEd,KAAA,GAAQ,mBAAA;EAIX,SAAA;EAIA,YAAA,CAAa,SAAA;EAIb,mBAAA;EAIA,sBAAA,IAA0B,MAAA;EAI1B,WAAA,CAAY,SAAA,UAAmB,KAAA,YAAiB,mBAAA;AAAA;;;;;;;;;;;;iBAkC3C,uBAAA,CAAwB,KAAA,EAAO,uBAAA,GAA0B,cAAc"}
|
package/dist/types.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { i as postgresCreateNamespace, n as PostgresUnboundSchema, t as PostgresSchema } from "./postgres-schema-
|
|
1
|
+
import { i as postgresCreateNamespace, n as PostgresUnboundSchema, t as PostgresSchema } from "./postgres-schema-COGZ1ark.mjs";
|
|
2
2
|
export { PostgresSchema, PostgresUnboundSchema, postgresCreateNamespace };
|
package/package.json
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/target-postgres",
|
|
3
|
-
"version": "0.13.0-dev.
|
|
3
|
+
"version": "0.13.0-dev.36",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"description": "Postgres target pack for Prisma Next",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@prisma-next/cli": "0.13.0-dev.
|
|
10
|
-
"@prisma-next/contract": "0.13.0-dev.
|
|
11
|
-
"@prisma-next/errors": "0.13.0-dev.
|
|
12
|
-
"@prisma-next/family-sql": "0.13.0-dev.
|
|
13
|
-
"@prisma-next/framework-components": "0.13.0-dev.
|
|
14
|
-
"@prisma-next/migration-tools": "0.13.0-dev.
|
|
15
|
-
"@prisma-next/ts-render": "0.13.0-dev.
|
|
16
|
-
"@prisma-next/sql-contract": "0.13.0-dev.
|
|
17
|
-
"@prisma-next/sql-errors": "0.13.0-dev.
|
|
18
|
-
"@prisma-next/sql-operations": "0.13.0-dev.
|
|
19
|
-
"@prisma-next/sql-relational-core": "0.13.0-dev.
|
|
20
|
-
"@prisma-next/sql-schema-ir": "0.13.0-dev.
|
|
21
|
-
"@prisma-next/utils": "0.13.0-dev.
|
|
9
|
+
"@prisma-next/cli": "0.13.0-dev.36",
|
|
10
|
+
"@prisma-next/contract": "0.13.0-dev.36",
|
|
11
|
+
"@prisma-next/errors": "0.13.0-dev.36",
|
|
12
|
+
"@prisma-next/family-sql": "0.13.0-dev.36",
|
|
13
|
+
"@prisma-next/framework-components": "0.13.0-dev.36",
|
|
14
|
+
"@prisma-next/migration-tools": "0.13.0-dev.36",
|
|
15
|
+
"@prisma-next/ts-render": "0.13.0-dev.36",
|
|
16
|
+
"@prisma-next/sql-contract": "0.13.0-dev.36",
|
|
17
|
+
"@prisma-next/sql-errors": "0.13.0-dev.36",
|
|
18
|
+
"@prisma-next/sql-operations": "0.13.0-dev.36",
|
|
19
|
+
"@prisma-next/sql-relational-core": "0.13.0-dev.36",
|
|
20
|
+
"@prisma-next/sql-schema-ir": "0.13.0-dev.36",
|
|
21
|
+
"@prisma-next/utils": "0.13.0-dev.36",
|
|
22
22
|
"@standard-schema/spec": "^1.1.0",
|
|
23
23
|
"arktype": "^2.2.0",
|
|
24
24
|
"pathe": "^2.0.3"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@prisma-next/test-utils": "0.13.0-dev.
|
|
28
|
-
"@prisma-next/tsconfig": "0.13.0-dev.
|
|
29
|
-
"@prisma-next/tsdown": "0.13.0-dev.
|
|
27
|
+
"@prisma-next/test-utils": "0.13.0-dev.36",
|
|
28
|
+
"@prisma-next/tsconfig": "0.13.0-dev.36",
|
|
29
|
+
"@prisma-next/tsdown": "0.13.0-dev.36",
|
|
30
30
|
"tsdown": "0.22.1",
|
|
31
31
|
"typescript": "5.9.3",
|
|
32
32
|
"vitest": "4.1.8"
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import type { SelectAst } from '@prisma-next/sql-relational-core/ast';
|
|
2
|
+
import {
|
|
3
|
+
type CfExpr,
|
|
4
|
+
type CfExprSelectQuery,
|
|
5
|
+
cfExpr,
|
|
6
|
+
cfTable,
|
|
7
|
+
exprSelect,
|
|
8
|
+
} from '@prisma-next/sql-relational-core/contract-free';
|
|
9
|
+
import { PostgresTableSource } from '../core/ast/table-source';
|
|
10
|
+
import { PG_TEXT_CODEC_ID } from '../core/codec-ids';
|
|
11
|
+
import { postgresCreateNamespace } from '../core/postgres-schema';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* `to_regclass($1)` with the qualified table name bound as a text parameter.
|
|
15
|
+
* Thin vocabulary wrapper over the core `cfExpr.fn` helper — the target
|
|
16
|
+
* supplies only the template and the codec'd operand.
|
|
17
|
+
*/
|
|
18
|
+
export function toRegclass(qualifiedName: string): CfExpr {
|
|
19
|
+
return cfExpr.fn({
|
|
20
|
+
method: 'to_regclass',
|
|
21
|
+
template: 'to_regclass({{self}})',
|
|
22
|
+
self: cfExpr.param(qualifiedName, PG_TEXT_CODEC_ID),
|
|
23
|
+
returns: { codecId: PG_TEXT_CODEC_ID, nullable: true },
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface TableExistsCheckBuilder {
|
|
28
|
+
tableAbsent(): SelectAst;
|
|
29
|
+
tablePresent(): SelectAst;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Typed builder for the migration planner's table-existence checks. Produces
|
|
34
|
+
* FROM-less `SELECT to_regclass($1) IS [NOT] NULL AS "result"` ASTs with the
|
|
35
|
+
* qualified table name bound as a text parameter — never inlined into the SQL.
|
|
36
|
+
*
|
|
37
|
+
* `schema` is a namespace coordinate: the framework `__unbound__` sentinel
|
|
38
|
+
* elides the qualifier (search_path decides at runtime); any other id
|
|
39
|
+
* qualifies as `"schema"."table"`.
|
|
40
|
+
*/
|
|
41
|
+
export function tableExistsAst(schema: string, table: string): TableExistsCheckBuilder {
|
|
42
|
+
const qualified = postgresCreateNamespace({ id: schema, entries: { table: {} } }).qualifyTable(
|
|
43
|
+
table,
|
|
44
|
+
);
|
|
45
|
+
const regclass = toRegclass(qualified);
|
|
46
|
+
return {
|
|
47
|
+
tableAbsent: () => exprSelect().project('result', regclass.isNull()).build(),
|
|
48
|
+
tablePresent: () => exprSelect().project('result', regclass.isNotNull()).build(),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface ConstraintExistsCheckBuilder {
|
|
53
|
+
constraintPresent(): SelectAst;
|
|
54
|
+
constraintAbsent(): SelectAst;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Typed builder for the migration planner's constraint-existence checks.
|
|
59
|
+
* Produces `SELECT [NOT ]EXISTS (SELECT 1 FROM pg_constraint c JOIN
|
|
60
|
+
* pg_namespace n ON n.oid = c.connamespace WHERE c.conname = $1 AND
|
|
61
|
+
* n.nspname = $2 [AND c.conrelid = to_regclass($3)]) AS "result"` with the
|
|
62
|
+
* constraint name, schema name, and qualified table name bound as text
|
|
63
|
+
* parameters.
|
|
64
|
+
*
|
|
65
|
+
* When `table` is omitted the check matches by name + schema across all
|
|
66
|
+
* tables. Pass `table` to scope the check to a single table (prevents false
|
|
67
|
+
* matches on identically-named constraints in different tables). `schema`
|
|
68
|
+
* is a namespace coordinate: the `__unbound__` sentinel compares `nspname`
|
|
69
|
+
* against `current_schema()` instead of a bound parameter.
|
|
70
|
+
*/
|
|
71
|
+
export function constraintExistsAst(options: {
|
|
72
|
+
readonly constraintName: string;
|
|
73
|
+
readonly schema: string;
|
|
74
|
+
readonly table?: string;
|
|
75
|
+
}): ConstraintExistsCheckBuilder {
|
|
76
|
+
const namespace = postgresCreateNamespace({ id: options.schema, entries: { table: {} } });
|
|
77
|
+
const conditions = [
|
|
78
|
+
cfExpr.columnRef('c', 'conname').eqParam(options.constraintName, PG_TEXT_CODEC_ID),
|
|
79
|
+
cfExpr.columnRef('n', 'nspname').eqExpr(namespace.schemaFilterExpression()),
|
|
80
|
+
];
|
|
81
|
+
if (options.table !== undefined) {
|
|
82
|
+
conditions.push(
|
|
83
|
+
cfExpr.columnRef('c', 'conrelid').eqExpr(toRegclass(namespace.qualifyTable(options.table))),
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
const inner = (): CfExprSelectQuery =>
|
|
87
|
+
exprSelect()
|
|
88
|
+
.from(cfTable('pg_constraint', 'c'))
|
|
89
|
+
.join(
|
|
90
|
+
cfTable('pg_namespace', 'n'),
|
|
91
|
+
cfExpr.columnRef('n', 'oid').eqExpr(cfExpr.columnRef('c', 'connamespace')),
|
|
92
|
+
)
|
|
93
|
+
.project('one', cfExpr.lit(1))
|
|
94
|
+
.where(cfExpr.allOf(conditions));
|
|
95
|
+
return {
|
|
96
|
+
constraintPresent: () => exprSelect().project('result', cfExpr.exists(inner())).build(),
|
|
97
|
+
constraintAbsent: () => exprSelect().project('result', cfExpr.notExists(inner())).build(),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function checkNamespace(schema: string) {
|
|
102
|
+
return postgresCreateNamespace({ id: schema, entries: { table: {} } });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function informationSchemaColumns(): PostgresTableSource {
|
|
106
|
+
return new PostgresTableSource({ schema: 'information_schema', name: 'columns' });
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function infoSchemaColumnConditions(options: {
|
|
110
|
+
readonly schema: string;
|
|
111
|
+
readonly table: string;
|
|
112
|
+
readonly column: string;
|
|
113
|
+
}): CfExpr[] {
|
|
114
|
+
return [
|
|
115
|
+
cfExpr
|
|
116
|
+
.identifierRef('table_schema')
|
|
117
|
+
.eqExpr(checkNamespace(options.schema).schemaFilterExpression()),
|
|
118
|
+
cfExpr.identifierRef('table_name').eqParam(options.table, PG_TEXT_CODEC_ID),
|
|
119
|
+
cfExpr.identifierRef('column_name').eqParam(options.column, PG_TEXT_CODEC_ID),
|
|
120
|
+
];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function infoSchemaColumnQuery(conditions: ReadonlyArray<CfExpr>): CfExprSelectQuery {
|
|
124
|
+
return exprSelect()
|
|
125
|
+
.from(informationSchemaColumns())
|
|
126
|
+
.project('one', cfExpr.lit(1))
|
|
127
|
+
.where(cfExpr.allOf(conditions));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface ColumnExistsCheckBuilder {
|
|
131
|
+
columnPresent(): SelectAst;
|
|
132
|
+
columnAbsent(): SelectAst;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Typed builder for column-existence checks over
|
|
137
|
+
* `information_schema.columns`, with schema, table, and column names bound
|
|
138
|
+
* as text parameters.
|
|
139
|
+
*/
|
|
140
|
+
export function columnExistsAst(options: {
|
|
141
|
+
readonly schema: string;
|
|
142
|
+
readonly table: string;
|
|
143
|
+
readonly column: string;
|
|
144
|
+
}): ColumnExistsCheckBuilder {
|
|
145
|
+
const inner = () => infoSchemaColumnQuery(infoSchemaColumnConditions(options));
|
|
146
|
+
return {
|
|
147
|
+
columnPresent: () => exprSelect().project('result', cfExpr.exists(inner())).build(),
|
|
148
|
+
columnAbsent: () => exprSelect().project('result', cfExpr.notExists(inner())).build(),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Typed nullability check: EXISTS over `information_schema.columns` with
|
|
154
|
+
* `is_nullable` compared against the bound `'YES'` / `'NO'` marker.
|
|
155
|
+
*/
|
|
156
|
+
export function columnNullabilityAst(options: {
|
|
157
|
+
readonly schema: string;
|
|
158
|
+
readonly table: string;
|
|
159
|
+
readonly column: string;
|
|
160
|
+
readonly nullable: boolean;
|
|
161
|
+
}): SelectAst {
|
|
162
|
+
const conditions = [
|
|
163
|
+
...infoSchemaColumnConditions(options),
|
|
164
|
+
cfExpr.identifierRef('is_nullable').eqParam(options.nullable ? 'YES' : 'NO', PG_TEXT_CODEC_ID),
|
|
165
|
+
];
|
|
166
|
+
return exprSelect()
|
|
167
|
+
.project('result', cfExpr.exists(infoSchemaColumnQuery(conditions)))
|
|
168
|
+
.build();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export interface ColumnDefaultCheckBuilder {
|
|
172
|
+
defaultPresent(): SelectAst;
|
|
173
|
+
defaultAbsent(): SelectAst;
|
|
174
|
+
noDefault(): SelectAst;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Typed default-presence checks over `information_schema.columns`.
|
|
179
|
+
* `defaultPresent` / `defaultAbsent` assert the column row exists with a
|
|
180
|
+
* non-null / null `column_default`; `noDefault` is the NOT EXISTS variant
|
|
181
|
+
* (also true when the column row is missing entirely).
|
|
182
|
+
*/
|
|
183
|
+
export function columnDefaultAst(options: {
|
|
184
|
+
readonly schema: string;
|
|
185
|
+
readonly table: string;
|
|
186
|
+
readonly column: string;
|
|
187
|
+
}): ColumnDefaultCheckBuilder {
|
|
188
|
+
const withDefault = () =>
|
|
189
|
+
infoSchemaColumnQuery([
|
|
190
|
+
...infoSchemaColumnConditions(options),
|
|
191
|
+
cfExpr.identifierRef('column_default').isNotNull(),
|
|
192
|
+
]);
|
|
193
|
+
const withoutDefault = () =>
|
|
194
|
+
infoSchemaColumnQuery([
|
|
195
|
+
...infoSchemaColumnConditions(options),
|
|
196
|
+
cfExpr.identifierRef('column_default').isNull(),
|
|
197
|
+
]);
|
|
198
|
+
return {
|
|
199
|
+
defaultPresent: () => exprSelect().project('result', cfExpr.exists(withDefault())).build(),
|
|
200
|
+
defaultAbsent: () => exprSelect().project('result', cfExpr.exists(withoutDefault())).build(),
|
|
201
|
+
noDefault: () => exprSelect().project('result', cfExpr.notExists(withDefault())).build(),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Typed column-type check: EXISTS over `pg_attribute` joined to `pg_class`
|
|
207
|
+
* and `pg_namespace`, comparing `format_type(a.atttypid, a.atttypmod)`
|
|
208
|
+
* against the bound expected display type and excluding dropped columns.
|
|
209
|
+
*/
|
|
210
|
+
export function columnTypeAst(options: {
|
|
211
|
+
readonly schema: string;
|
|
212
|
+
readonly table: string;
|
|
213
|
+
readonly column: string;
|
|
214
|
+
readonly expectedType: string;
|
|
215
|
+
}): SelectAst {
|
|
216
|
+
const formatType = cfExpr.fn({
|
|
217
|
+
method: 'format_type',
|
|
218
|
+
template: 'format_type({{self}}, {{arg0}})',
|
|
219
|
+
self: cfExpr.columnRef('a', 'atttypid'),
|
|
220
|
+
args: [cfExpr.columnRef('a', 'atttypmod')],
|
|
221
|
+
returns: { codecId: PG_TEXT_CODEC_ID, nullable: false },
|
|
222
|
+
});
|
|
223
|
+
const inner = exprSelect()
|
|
224
|
+
.from(cfTable('pg_attribute', 'a'))
|
|
225
|
+
.join(
|
|
226
|
+
cfTable('pg_class', 'c'),
|
|
227
|
+
cfExpr.columnRef('c', 'oid').eqExpr(cfExpr.columnRef('a', 'attrelid')),
|
|
228
|
+
)
|
|
229
|
+
.join(
|
|
230
|
+
cfTable('pg_namespace', 'n'),
|
|
231
|
+
cfExpr.columnRef('n', 'oid').eqExpr(cfExpr.columnRef('c', 'relnamespace')),
|
|
232
|
+
)
|
|
233
|
+
.project('one', cfExpr.lit(1))
|
|
234
|
+
.where(
|
|
235
|
+
cfExpr.allOf([
|
|
236
|
+
cfExpr
|
|
237
|
+
.columnRef('n', 'nspname')
|
|
238
|
+
.eqExpr(checkNamespace(options.schema).schemaFilterExpression()),
|
|
239
|
+
cfExpr.columnRef('c', 'relname').eqParam(options.table, PG_TEXT_CODEC_ID),
|
|
240
|
+
cfExpr.columnRef('a', 'attname').eqParam(options.column, PG_TEXT_CODEC_ID),
|
|
241
|
+
formatType.eqParam(options.expectedType, PG_TEXT_CODEC_ID),
|
|
242
|
+
cfExpr.columnRef('a', 'attisdropped').not(),
|
|
243
|
+
]),
|
|
244
|
+
);
|
|
245
|
+
return exprSelect().project('result', cfExpr.exists(inner)).build();
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export interface TablePrimaryKeyCheckBuilder {
|
|
249
|
+
pkPresent(): SelectAst;
|
|
250
|
+
pkAbsent(): SelectAst;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Typed primary-key existence check over `pg_index` joined to `pg_class`
|
|
255
|
+
* and `pg_namespace`, with a LEFT JOIN on the index relation so an
|
|
256
|
+
* optional `constraintName` can scope the match to a named constraint.
|
|
257
|
+
*/
|
|
258
|
+
export function tablePrimaryKeyAst(options: {
|
|
259
|
+
readonly schema: string;
|
|
260
|
+
readonly table: string;
|
|
261
|
+
readonly constraintName?: string;
|
|
262
|
+
}): TablePrimaryKeyCheckBuilder {
|
|
263
|
+
const conditions = [
|
|
264
|
+
cfExpr
|
|
265
|
+
.columnRef('n', 'nspname')
|
|
266
|
+
.eqExpr(checkNamespace(options.schema).schemaFilterExpression()),
|
|
267
|
+
cfExpr.columnRef('c', 'relname').eqParam(options.table, PG_TEXT_CODEC_ID),
|
|
268
|
+
cfExpr.columnRef('i', 'indisprimary'),
|
|
269
|
+
];
|
|
270
|
+
if (options.constraintName !== undefined) {
|
|
271
|
+
conditions.push(
|
|
272
|
+
cfExpr.columnRef('c2', 'relname').eqParam(options.constraintName, PG_TEXT_CODEC_ID),
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
const inner = () =>
|
|
276
|
+
exprSelect()
|
|
277
|
+
.from(cfTable('pg_index', 'i'))
|
|
278
|
+
.join(
|
|
279
|
+
cfTable('pg_class', 'c'),
|
|
280
|
+
cfExpr.columnRef('c', 'oid').eqExpr(cfExpr.columnRef('i', 'indrelid')),
|
|
281
|
+
)
|
|
282
|
+
.join(
|
|
283
|
+
cfTable('pg_namespace', 'n'),
|
|
284
|
+
cfExpr.columnRef('n', 'oid').eqExpr(cfExpr.columnRef('c', 'relnamespace')),
|
|
285
|
+
)
|
|
286
|
+
.leftJoin(
|
|
287
|
+
cfTable('pg_class', 'c2'),
|
|
288
|
+
cfExpr.columnRef('c2', 'oid').eqExpr(cfExpr.columnRef('i', 'indexrelid')),
|
|
289
|
+
)
|
|
290
|
+
.project('one', cfExpr.lit(1))
|
|
291
|
+
.where(cfExpr.allOf(conditions));
|
|
292
|
+
return {
|
|
293
|
+
pkPresent: () => exprSelect().project('result', cfExpr.exists(inner())).build(),
|
|
294
|
+
pkAbsent: () => exprSelect().project('result', cfExpr.notExists(inner())).build(),
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Typed emptiness check: NOT EXISTS over the user table itself with
|
|
300
|
+
* `LIMIT 1`. The table is addressed through the namespace's polymorphic
|
|
301
|
+
* `tableSource` (qualified for named schemas, bare for the unbound slot).
|
|
302
|
+
*/
|
|
303
|
+
export function tableIsEmptyAst(schema: string, table: string): SelectAst {
|
|
304
|
+
const inner = exprSelect()
|
|
305
|
+
.from(checkNamespace(schema).tableSource(table))
|
|
306
|
+
.project('one', cfExpr.lit(1))
|
|
307
|
+
.limit(1);
|
|
308
|
+
return exprSelect().project('result', cfExpr.notExists(inner)).build();
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Typed no-NULL-values data check used by `SET NOT NULL` prechecks:
|
|
313
|
+
* NOT EXISTS over the user table where the column IS NULL.
|
|
314
|
+
*/
|
|
315
|
+
export function noNullValuesAst(options: {
|
|
316
|
+
readonly schema: string;
|
|
317
|
+
readonly table: string;
|
|
318
|
+
readonly column: string;
|
|
319
|
+
}): SelectAst {
|
|
320
|
+
const inner = exprSelect()
|
|
321
|
+
.from(checkNamespace(options.schema).tableSource(options.table))
|
|
322
|
+
.project('one', cfExpr.lit(1))
|
|
323
|
+
.where(cfExpr.identifierRef(options.column).isNull());
|
|
324
|
+
return exprSelect().project('result', cfExpr.notExists(inner)).build();
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
export interface ExtensionExistsCheckBuilder {
|
|
328
|
+
extensionPresent(): SelectAst;
|
|
329
|
+
extensionAbsent(): SelectAst;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Typed extension existence check over `pg_extension`, with the extension
|
|
334
|
+
* name bound as a text parameter.
|
|
335
|
+
*/
|
|
336
|
+
export function extensionExistsAst(extensionName: string): ExtensionExistsCheckBuilder {
|
|
337
|
+
const inner = () =>
|
|
338
|
+
exprSelect()
|
|
339
|
+
.from(cfTable('pg_extension'))
|
|
340
|
+
.project('one', cfExpr.lit(1))
|
|
341
|
+
.where(cfExpr.identifierRef('extname').eqParam(extensionName, PG_TEXT_CODEC_ID));
|
|
342
|
+
return {
|
|
343
|
+
extensionPresent: () => exprSelect().project('result', cfExpr.exists(inner())).build(),
|
|
344
|
+
extensionAbsent: () => exprSelect().project('result', cfExpr.notExists(inner())).build(),
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export interface IndexExistsCheckBuilder {
|
|
349
|
+
indexPresent(): SelectAst;
|
|
350
|
+
indexAbsent(): SelectAst;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Typed index existence check riding the same `to_regclass` vocabulary as
|
|
355
|
+
* `tableExistsAst`, with the qualified index name bound as a text parameter.
|
|
356
|
+
*/
|
|
357
|
+
export function indexExistsAst(schema: string, indexName: string): IndexExistsCheckBuilder {
|
|
358
|
+
const regclass = toRegclass(checkNamespace(schema).qualifyTable(indexName));
|
|
359
|
+
return {
|
|
360
|
+
indexPresent: () => exprSelect().project('result', regclass.isNotNull()).build(),
|
|
361
|
+
indexAbsent: () => exprSelect().project('result', regclass.isNull()).build(),
|
|
362
|
+
};
|
|
363
|
+
}
|