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