pgterra 0.2.7 → 0.2.8
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/index.js +54 -60
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -22926,6 +22926,7 @@ class DatabaseInspector {
|
|
|
22926
22926
|
SELECT
|
|
22927
22927
|
tc.constraint_name,
|
|
22928
22928
|
kcu.column_name,
|
|
22929
|
+
ccu.table_schema AS referenced_schema,
|
|
22929
22930
|
ccu.table_name AS referenced_table,
|
|
22930
22931
|
ccu.column_name AS referenced_column,
|
|
22931
22932
|
rc.delete_rule,
|
|
@@ -22968,7 +22969,7 @@ class DatabaseInspector {
|
|
|
22968
22969
|
foreignKeys.push({
|
|
22969
22970
|
name: constraintName,
|
|
22970
22971
|
columns,
|
|
22971
|
-
referencedTable: firstRow.referenced_table
|
|
22972
|
+
referencedTable: firstRow.referenced_schema === "public" ? firstRow.referenced_table : `${firstRow.referenced_schema}.${firstRow.referenced_table}`,
|
|
22972
22973
|
referencedColumns,
|
|
22973
22974
|
onDelete,
|
|
22974
22975
|
onUpdate
|
|
@@ -23622,6 +23623,17 @@ class SQLBuilder {
|
|
|
23622
23623
|
}
|
|
23623
23624
|
|
|
23624
23625
|
// src/utils/sql.ts
|
|
23626
|
+
function splitSchemaTable(qualifiedName) {
|
|
23627
|
+
const parts = qualifiedName.split(".");
|
|
23628
|
+
if (parts.length === 2) {
|
|
23629
|
+
return [parts[1], parts[0]];
|
|
23630
|
+
}
|
|
23631
|
+
return [qualifiedName, undefined];
|
|
23632
|
+
}
|
|
23633
|
+
function getBareTableName(tableName) {
|
|
23634
|
+
const parts = tableName.split(".");
|
|
23635
|
+
return parts[parts.length - 1];
|
|
23636
|
+
}
|
|
23625
23637
|
function getQualifiedTableName(table, schema) {
|
|
23626
23638
|
if (typeof table === "string") {
|
|
23627
23639
|
return schema ? `${schema}.${table}` : table;
|
|
@@ -23726,18 +23738,18 @@ function generateCreateTableStatement(table) {
|
|
|
23726
23738
|
return builder2.build();
|
|
23727
23739
|
});
|
|
23728
23740
|
if (table.primaryKey) {
|
|
23729
|
-
const primaryKeyClause = generatePrimaryKeyClause(table.primaryKey);
|
|
23741
|
+
const primaryKeyClause = generatePrimaryKeyClause(table.primaryKey, table.name);
|
|
23730
23742
|
columnDefs.push(primaryKeyClause);
|
|
23731
23743
|
}
|
|
23732
23744
|
if (table.checkConstraints) {
|
|
23733
23745
|
for (const checkConstraint of table.checkConstraints) {
|
|
23734
|
-
const checkClause = generateCheckConstraintClause(checkConstraint);
|
|
23746
|
+
const checkClause = generateCheckConstraintClause(checkConstraint, table.name);
|
|
23735
23747
|
columnDefs.push(checkClause);
|
|
23736
23748
|
}
|
|
23737
23749
|
}
|
|
23738
23750
|
if (table.uniqueConstraints) {
|
|
23739
23751
|
for (const uniqueConstraint of table.uniqueConstraints) {
|
|
23740
|
-
const uniqueClause = generateUniqueConstraintClause(uniqueConstraint);
|
|
23752
|
+
const uniqueClause = generateUniqueConstraintClause(uniqueConstraint, table.name);
|
|
23741
23753
|
columnDefs.push(uniqueClause);
|
|
23742
23754
|
}
|
|
23743
23755
|
}
|
|
@@ -23747,17 +23759,20 @@ function generateCreateTableStatement(table) {
|
|
|
23747
23759
|
)`);
|
|
23748
23760
|
return builder.build() + ";";
|
|
23749
23761
|
}
|
|
23750
|
-
function generatePrimaryKeyClause(primaryKey) {
|
|
23762
|
+
function generatePrimaryKeyClause(primaryKey, tableName) {
|
|
23751
23763
|
const columns = primaryKey.columns.map((col) => `"${col.replace(/"/g, '""')}"`).join(", ");
|
|
23752
|
-
|
|
23753
|
-
|
|
23764
|
+
const bareTable = tableName ? getBareTableName(tableName) : undefined;
|
|
23765
|
+
const constraintName = primaryKey.name ? primaryKey.name : bareTable ? `${bareTable}_pkey` : undefined;
|
|
23766
|
+
if (constraintName) {
|
|
23767
|
+
const builder = new SQLBuilder().p("CONSTRAINT").ident(constraintName).p(`PRIMARY KEY (${columns})`);
|
|
23754
23768
|
return builder.build();
|
|
23755
23769
|
} else {
|
|
23756
23770
|
return `PRIMARY KEY (${columns})`;
|
|
23757
23771
|
}
|
|
23758
23772
|
}
|
|
23759
23773
|
function generateAddPrimaryKeySQL(tableName, primaryKey) {
|
|
23760
|
-
const
|
|
23774
|
+
const bareTable = getBareTableName(tableName);
|
|
23775
|
+
const constraintName = primaryKey.name || `${bareTable}_pkey`;
|
|
23761
23776
|
const columns = primaryKey.columns.map((col) => `"${col.replace(/"/g, '""')}"`).join(", ");
|
|
23762
23777
|
return new SQLBuilder().p("ALTER TABLE").table(tableName).p("ADD CONSTRAINT").ident(constraintName).p(`PRIMARY KEY (${columns});`).build();
|
|
23763
23778
|
}
|
|
@@ -23768,7 +23783,7 @@ function generateAddForeignKeySQL(tableName, foreignKey) {
|
|
|
23768
23783
|
const constraintName = foreignKey.name || `fk_${tableName}_${foreignKey.referencedTable}`;
|
|
23769
23784
|
const columns = foreignKey.columns.map((col) => `"${col.replace(/"/g, '""')}"`).join(", ");
|
|
23770
23785
|
const referencedColumns = foreignKey.referencedColumns.map((col) => `"${col.replace(/"/g, '""')}"`).join(", ");
|
|
23771
|
-
const builder = new SQLBuilder().p("ALTER TABLE").table(tableName).p("ADD CONSTRAINT").ident(constraintName).p(`FOREIGN KEY (${columns}) REFERENCES`).table(foreignKey.referencedTable).p(`(${referencedColumns})`);
|
|
23786
|
+
const builder = new SQLBuilder().p("ALTER TABLE").table(tableName).p("ADD CONSTRAINT").ident(constraintName).p(`FOREIGN KEY (${columns}) REFERENCES`).table(...splitSchemaTable(foreignKey.referencedTable)).p(`(${referencedColumns})`);
|
|
23772
23787
|
if (foreignKey.onDelete) {
|
|
23773
23788
|
builder.p(`ON DELETE ${foreignKey.onDelete}`);
|
|
23774
23789
|
}
|
|
@@ -23781,13 +23796,14 @@ function generateDropForeignKeySQL(tableName, constraintName) {
|
|
|
23781
23796
|
return new SQLBuilder().p("ALTER TABLE").table(tableName).p("DROP CONSTRAINT").ident(constraintName).p(";").build();
|
|
23782
23797
|
}
|
|
23783
23798
|
function generateAddCheckConstraintSQL(tableName, checkConstraint) {
|
|
23784
|
-
const
|
|
23799
|
+
const bareTable = getBareTableName(tableName);
|
|
23800
|
+
const constraintName = checkConstraint.name || `${bareTable}_check`;
|
|
23785
23801
|
return new SQLBuilder().p("ALTER TABLE").table(tableName).p("ADD CONSTRAINT").ident(constraintName).p(`CHECK (${checkConstraint.expression});`).build();
|
|
23786
23802
|
}
|
|
23787
23803
|
function generateDropCheckConstraintSQL(tableName, constraintName) {
|
|
23788
23804
|
return new SQLBuilder().p("ALTER TABLE").table(tableName).p("DROP CONSTRAINT").ident(constraintName).p(";").build();
|
|
23789
23805
|
}
|
|
23790
|
-
function generateCheckConstraintClause(checkConstraint) {
|
|
23806
|
+
function generateCheckConstraintClause(checkConstraint, tableName) {
|
|
23791
23807
|
const builder = new SQLBuilder;
|
|
23792
23808
|
if (checkConstraint.name) {
|
|
23793
23809
|
builder.p("CONSTRAINT").ident(checkConstraint.name);
|
|
@@ -23796,18 +23812,21 @@ function generateCheckConstraintClause(checkConstraint) {
|
|
|
23796
23812
|
return builder.build();
|
|
23797
23813
|
}
|
|
23798
23814
|
function generateAddUniqueConstraintSQL(tableName, uniqueConstraint) {
|
|
23799
|
-
const
|
|
23815
|
+
const bareTable = getBareTableName(tableName);
|
|
23816
|
+
const constraintName = uniqueConstraint.name || `${bareTable}_${uniqueConstraint.columns.join("_")}_unique`;
|
|
23800
23817
|
const columns = uniqueConstraint.columns.map((col) => `"${col.replace(/"/g, '""')}"`).join(", ");
|
|
23801
23818
|
return new SQLBuilder().p("ALTER TABLE").table(tableName).p("ADD CONSTRAINT").ident(constraintName).p(`UNIQUE (${columns});`).build();
|
|
23802
23819
|
}
|
|
23803
23820
|
function generateDropUniqueConstraintSQL(tableName, constraintName) {
|
|
23804
23821
|
return new SQLBuilder().p("ALTER TABLE").table(tableName).p("DROP CONSTRAINT").ident(constraintName).p(";").build();
|
|
23805
23822
|
}
|
|
23806
|
-
function generateUniqueConstraintClause(uniqueConstraint) {
|
|
23823
|
+
function generateUniqueConstraintClause(uniqueConstraint, tableName) {
|
|
23807
23824
|
const columns = uniqueConstraint.columns.map((col) => `"${col.replace(/"/g, '""')}"`).join(", ");
|
|
23825
|
+
const bareTable = tableName ? getBareTableName(tableName) : undefined;
|
|
23826
|
+
const constraintName = uniqueConstraint.name ? uniqueConstraint.name : bareTable ? `${bareTable}_${uniqueConstraint.columns.join("_")}_unique` : undefined;
|
|
23808
23827
|
const builder = new SQLBuilder;
|
|
23809
|
-
if (
|
|
23810
|
-
builder.p("CONSTRAINT").ident(
|
|
23828
|
+
if (constraintName) {
|
|
23829
|
+
builder.p("CONSTRAINT").ident(constraintName);
|
|
23811
23830
|
}
|
|
23812
23831
|
builder.p(`UNIQUE (${columns})`);
|
|
23813
23832
|
return builder.build();
|
|
@@ -24749,8 +24768,9 @@ class SchemaDiffer {
|
|
|
24749
24768
|
}
|
|
24750
24769
|
generateCheckConstraintStatements(tableName, desiredConstraints, currentConstraints) {
|
|
24751
24770
|
const statements = [];
|
|
24752
|
-
const
|
|
24753
|
-
const
|
|
24771
|
+
const normalizeExpression = (expr) => expr.replace(/\s+/g, " ").trim();
|
|
24772
|
+
const currentMap = new Map(currentConstraints.map((c) => [normalizeExpression(c.expression), c]));
|
|
24773
|
+
const desiredMap = new Map(desiredConstraints.map((c) => [normalizeExpression(c.expression), c]));
|
|
24754
24774
|
for (const [key, constraint] of currentMap) {
|
|
24755
24775
|
if (!desiredMap.has(key)) {
|
|
24756
24776
|
if (constraint.name) {
|
|
@@ -24761,14 +24781,6 @@ class SchemaDiffer {
|
|
|
24761
24781
|
for (const [key, constraint] of desiredMap) {
|
|
24762
24782
|
if (!currentMap.has(key)) {
|
|
24763
24783
|
statements.push(generateAddCheckConstraintSQL(tableName, constraint));
|
|
24764
|
-
} else {
|
|
24765
|
-
const currentConstraint = currentMap.get(key);
|
|
24766
|
-
if (constraint.expression !== currentConstraint.expression) {
|
|
24767
|
-
if (currentConstraint.name) {
|
|
24768
|
-
statements.push(generateDropCheckConstraintSQL(tableName, currentConstraint.name));
|
|
24769
|
-
}
|
|
24770
|
-
statements.push(generateAddCheckConstraintSQL(tableName, constraint));
|
|
24771
|
-
}
|
|
24772
24784
|
}
|
|
24773
24785
|
}
|
|
24774
24786
|
return statements;
|
|
@@ -24821,14 +24833,9 @@ class SchemaDiffer {
|
|
|
24821
24833
|
}
|
|
24822
24834
|
generateUniqueConstraintStatements(tableName, desiredConstraints, currentConstraints) {
|
|
24823
24835
|
const statements = [];
|
|
24824
|
-
const
|
|
24825
|
-
|
|
24826
|
-
|
|
24827
|
-
]));
|
|
24828
|
-
const desiredMap = new Map(desiredConstraints.map((c) => [
|
|
24829
|
-
c.name || `unique_${c.columns.join("_")}`,
|
|
24830
|
-
c
|
|
24831
|
-
]));
|
|
24836
|
+
const getStructuralKey = (c) => [...c.columns].sort().join(",");
|
|
24837
|
+
const currentMap = new Map(currentConstraints.map((c) => [getStructuralKey(c), c]));
|
|
24838
|
+
const desiredMap = new Map(desiredConstraints.map((c) => [getStructuralKey(c), c]));
|
|
24832
24839
|
for (const [key, constraint] of currentMap) {
|
|
24833
24840
|
if (!desiredMap.has(key)) {
|
|
24834
24841
|
if (constraint.name) {
|
|
@@ -24942,8 +24949,9 @@ class SchemaDiffer {
|
|
|
24942
24949
|
}
|
|
24943
24950
|
}
|
|
24944
24951
|
collectCheckConstraintAlterations(desiredConstraints, currentConstraints, alterations) {
|
|
24945
|
-
const
|
|
24946
|
-
const
|
|
24952
|
+
const normalizeExpression = (expr) => expr.replace(/\s+/g, " ").trim();
|
|
24953
|
+
const currentMap = new Map(currentConstraints.map((c) => [normalizeExpression(c.expression), c]));
|
|
24954
|
+
const desiredMap = new Map(desiredConstraints.map((c) => [normalizeExpression(c.expression), c]));
|
|
24947
24955
|
for (const [key, constraint] of currentMap) {
|
|
24948
24956
|
if (!desiredMap.has(key) && constraint.name) {
|
|
24949
24957
|
alterations.push({
|
|
@@ -24958,18 +24966,6 @@ class SchemaDiffer {
|
|
|
24958
24966
|
type: "add_check",
|
|
24959
24967
|
constraint
|
|
24960
24968
|
});
|
|
24961
|
-
} else {
|
|
24962
|
-
const currentConstraint = currentMap.get(key);
|
|
24963
|
-
if (constraint.expression !== currentConstraint.expression && currentConstraint.name) {
|
|
24964
|
-
alterations.push({
|
|
24965
|
-
type: "drop_check",
|
|
24966
|
-
constraintName: currentConstraint.name
|
|
24967
|
-
});
|
|
24968
|
-
alterations.push({
|
|
24969
|
-
type: "add_check",
|
|
24970
|
-
constraint
|
|
24971
|
-
});
|
|
24972
|
-
}
|
|
24973
24969
|
}
|
|
24974
24970
|
}
|
|
24975
24971
|
}
|
|
@@ -25010,14 +25006,9 @@ class SchemaDiffer {
|
|
|
25010
25006
|
}
|
|
25011
25007
|
}
|
|
25012
25008
|
collectUniqueConstraintAlterations(desiredConstraints, currentConstraints, alterations) {
|
|
25013
|
-
const
|
|
25014
|
-
|
|
25015
|
-
|
|
25016
|
-
]));
|
|
25017
|
-
const desiredMap = new Map(desiredConstraints.map((c) => [
|
|
25018
|
-
c.name || `unique_${c.columns.join("_")}`,
|
|
25019
|
-
c
|
|
25020
|
-
]));
|
|
25009
|
+
const getStructuralKey = (c) => [...c.columns].sort().join(",");
|
|
25010
|
+
const currentMap = new Map(currentConstraints.map((c) => [getStructuralKey(c), c]));
|
|
25011
|
+
const desiredMap = new Map(desiredConstraints.map((c) => [getStructuralKey(c), c]));
|
|
25021
25012
|
for (const [key, constraint] of currentMap) {
|
|
25022
25013
|
if (!desiredMap.has(key) && constraint.name) {
|
|
25023
25014
|
alterations.push({
|
|
@@ -25086,7 +25077,8 @@ class SchemaDiffer {
|
|
|
25086
25077
|
b.p("ALTER COLUMN").ident(alt.columnName).p("DROP NOT NULL");
|
|
25087
25078
|
break;
|
|
25088
25079
|
case "add_primary_key": {
|
|
25089
|
-
const
|
|
25080
|
+
const bareTable = getBareTableName(table.name);
|
|
25081
|
+
const constraintName = alt.constraint.name || `${bareTable}_pkey`;
|
|
25090
25082
|
const columns = alt.constraint.columns.map((col) => `"${col.replace(/"/g, '""')}"`).join(", ");
|
|
25091
25083
|
b.p("ADD CONSTRAINT").ident(constraintName).p(`PRIMARY KEY (${columns})`);
|
|
25092
25084
|
break;
|
|
@@ -25095,7 +25087,8 @@ class SchemaDiffer {
|
|
|
25095
25087
|
b.p("DROP CONSTRAINT").ident(alt.constraintName);
|
|
25096
25088
|
break;
|
|
25097
25089
|
case "add_check": {
|
|
25098
|
-
const
|
|
25090
|
+
const bareTable = getBareTableName(table.name);
|
|
25091
|
+
const constraintName = alt.constraint.name || `${bareTable}_check`;
|
|
25099
25092
|
b.p("ADD CONSTRAINT").ident(constraintName).p(`CHECK (${alt.constraint.expression})`);
|
|
25100
25093
|
break;
|
|
25101
25094
|
}
|
|
@@ -25106,7 +25099,7 @@ class SchemaDiffer {
|
|
|
25106
25099
|
const constraintName = alt.constraint.name || `fk_${table.name}_${alt.constraint.referencedTable}`;
|
|
25107
25100
|
const columns = alt.constraint.columns.map((col) => `"${col.replace(/"/g, '""')}"`).join(", ");
|
|
25108
25101
|
const referencedColumns = alt.constraint.referencedColumns.map((col) => `"${col.replace(/"/g, '""')}"`).join(", ");
|
|
25109
|
-
b.p("ADD CONSTRAINT").ident(constraintName).p(`FOREIGN KEY (${columns}) REFERENCES`).table(alt.constraint.referencedTable).p(`(${referencedColumns})`);
|
|
25102
|
+
b.p("ADD CONSTRAINT").ident(constraintName).p(`FOREIGN KEY (${columns}) REFERENCES`).table(...splitSchemaTable(alt.constraint.referencedTable)).p(`(${referencedColumns})`);
|
|
25110
25103
|
if (alt.constraint.onDelete) {
|
|
25111
25104
|
b.p(`ON DELETE ${alt.constraint.onDelete}`);
|
|
25112
25105
|
}
|
|
@@ -25119,7 +25112,8 @@ class SchemaDiffer {
|
|
|
25119
25112
|
b.p("DROP CONSTRAINT").ident(alt.constraintName);
|
|
25120
25113
|
break;
|
|
25121
25114
|
case "add_unique": {
|
|
25122
|
-
const
|
|
25115
|
+
const bareTable = getBareTableName(table.name);
|
|
25116
|
+
const constraintName = alt.constraint.name || `${bareTable}_${alt.constraint.columns.join("_")}_unique`;
|
|
25123
25117
|
const columns = alt.constraint.columns.map((col) => `"${col.replace(/"/g, '""')}"`).join(", ");
|
|
25124
25118
|
b.p("ADD CONSTRAINT").ident(constraintName).p(`UNIQUE (${columns})`);
|
|
25125
25119
|
break;
|
|
@@ -25934,7 +25928,7 @@ async function applyCommand(options, config) {
|
|
|
25934
25928
|
// package.json
|
|
25935
25929
|
var package_default = {
|
|
25936
25930
|
name: "pgterra",
|
|
25937
|
-
version: "0.2.
|
|
25931
|
+
version: "0.2.8",
|
|
25938
25932
|
description: "Declarative schema management for Postgres",
|
|
25939
25933
|
keywords: [
|
|
25940
25934
|
"postgres",
|