pgterra 0.2.1 → 0.2.2
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 +70 -58
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1844,7 +1844,7 @@ var require_types2 = __commonJS((exports) => {
|
|
|
1844
1844
|
|
|
1845
1845
|
// node_modules/libpg-query/wasm/libpg-query.js
|
|
1846
1846
|
var require_libpg_query = __commonJS((exports, module) => {
|
|
1847
|
-
var __dirname = "/home/runner/work/
|
|
1847
|
+
var __dirname = "/home/runner/work/pgterra/pgterra/node_modules/libpg-query/wasm", __filename = "/home/runner/work/pgterra/pgterra/node_modules/libpg-query/wasm/libpg-query.js";
|
|
1848
1848
|
var PgQueryModule = (() => {
|
|
1849
1849
|
var _scriptName = typeof document != "undefined" ? document.currentScript?.src : undefined;
|
|
1850
1850
|
return async function(moduleArg = {}) {
|
|
@@ -26472,17 +26472,17 @@ function columnsAreDifferent(desired, current) {
|
|
|
26472
26472
|
}
|
|
26473
26473
|
function generateCreateTableStatement(table) {
|
|
26474
26474
|
const columnDefs = table.columns.map((col) => {
|
|
26475
|
-
const
|
|
26476
|
-
|
|
26475
|
+
const builder2 = new SQLBuilder;
|
|
26476
|
+
builder2.ident(col.name).p(col.type);
|
|
26477
26477
|
if (col.generated) {
|
|
26478
|
-
|
|
26478
|
+
builder2.p(`GENERATED ${col.generated.always ? "ALWAYS" : "BY DEFAULT"} AS (${col.generated.expression}) ${col.generated.stored ? "STORED" : "VIRTUAL"}`);
|
|
26479
26479
|
} else {
|
|
26480
26480
|
if (!col.nullable)
|
|
26481
|
-
|
|
26481
|
+
builder2.p("NOT NULL");
|
|
26482
26482
|
if (col.default)
|
|
26483
|
-
|
|
26483
|
+
builder2.p(`DEFAULT ${col.default}`);
|
|
26484
26484
|
}
|
|
26485
|
-
return
|
|
26485
|
+
return builder2.build();
|
|
26486
26486
|
});
|
|
26487
26487
|
if (table.primaryKey) {
|
|
26488
26488
|
const primaryKeyClause = generatePrimaryKeyClause(table.primaryKey);
|
|
@@ -26500,11 +26500,11 @@ function generateCreateTableStatement(table) {
|
|
|
26500
26500
|
columnDefs.push(uniqueClause);
|
|
26501
26501
|
}
|
|
26502
26502
|
}
|
|
26503
|
-
const
|
|
26504
|
-
|
|
26505
|
-
|
|
26506
|
-
|
|
26507
|
-
)
|
|
26503
|
+
const builder = new SQLBuilder().p("CREATE TABLE").table(table.name, table.schema).p(`(
|
|
26504
|
+
` + columnDefs.join(`,
|
|
26505
|
+
`) + `
|
|
26506
|
+
)`);
|
|
26507
|
+
return builder.build() + ";";
|
|
26508
26508
|
}
|
|
26509
26509
|
function generatePrimaryKeyClause(primaryKey) {
|
|
26510
26510
|
const columns = primaryKey.columns.map((col) => `"${col.replace(/"/g, '""')}"`).join(", ");
|
|
@@ -27126,10 +27126,9 @@ class SchemaDiffer {
|
|
|
27126
27126
|
statements.push(generateCreateTableStatement(filteredTable));
|
|
27127
27127
|
} else {
|
|
27128
27128
|
const currentTable = currentTables.get(table.name);
|
|
27129
|
-
const qualifiedName = getQualifiedTableName(table);
|
|
27130
27129
|
const alterations = this.collectTableAlterations(table, currentTable);
|
|
27131
27130
|
if (alterations.length > 0) {
|
|
27132
|
-
const batchedStatement = this.batchAlterTableChanges(
|
|
27131
|
+
const batchedStatement = this.batchAlterTableChanges(table, alterations);
|
|
27133
27132
|
if (batchedStatement) {
|
|
27134
27133
|
statements.push(batchedStatement);
|
|
27135
27134
|
}
|
|
@@ -27310,18 +27309,19 @@ class SchemaDiffer {
|
|
|
27310
27309
|
generateUsingExpression(columnName, currentType, desiredType) {
|
|
27311
27310
|
const currentNormalized = normalizeType(currentType).toLowerCase();
|
|
27312
27311
|
const desiredNormalized = normalizeType(desiredType).toLowerCase();
|
|
27312
|
+
const quotedCol = `"${columnName.replace(/"/g, '""')}"`;
|
|
27313
27313
|
if (currentNormalized.includes("varchar") || currentNormalized.includes("text")) {
|
|
27314
27314
|
if (desiredNormalized.includes("decimal") || desiredNormalized.includes("numeric")) {
|
|
27315
|
-
return `${
|
|
27315
|
+
return `${quotedCol}::${desiredType}`;
|
|
27316
27316
|
}
|
|
27317
27317
|
if (desiredNormalized.includes("integer") || desiredNormalized.includes("int")) {
|
|
27318
|
-
return `TRUNC(${
|
|
27318
|
+
return `TRUNC(${quotedCol}::DECIMAL)::integer`;
|
|
27319
27319
|
}
|
|
27320
27320
|
if (desiredNormalized.includes("bool")) {
|
|
27321
|
-
return `TRIM(${
|
|
27321
|
+
return `TRIM(${quotedCol})::boolean`;
|
|
27322
27322
|
}
|
|
27323
27323
|
}
|
|
27324
|
-
return `${
|
|
27324
|
+
return `${quotedCol}::${desiredType}`;
|
|
27325
27325
|
}
|
|
27326
27326
|
generatePrimaryKeyStatements(desiredTable, currentTable) {
|
|
27327
27327
|
const statements = [];
|
|
@@ -27461,35 +27461,37 @@ class SchemaDiffer {
|
|
|
27461
27461
|
});
|
|
27462
27462
|
}
|
|
27463
27463
|
generateCreateIndexSQL(index, useConcurrent = true) {
|
|
27464
|
-
|
|
27464
|
+
const builder = new SQLBuilder;
|
|
27465
|
+
builder.p("CREATE");
|
|
27465
27466
|
if (index.unique) {
|
|
27466
|
-
|
|
27467
|
+
builder.p("UNIQUE");
|
|
27467
27468
|
}
|
|
27468
|
-
|
|
27469
|
+
builder.p("INDEX");
|
|
27469
27470
|
const shouldUseConcurrent = index.concurrent !== undefined ? index.concurrent : useConcurrent;
|
|
27470
27471
|
if (shouldUseConcurrent) {
|
|
27471
|
-
|
|
27472
|
+
builder.p("CONCURRENTLY");
|
|
27472
27473
|
}
|
|
27473
|
-
|
|
27474
|
+
builder.ident(index.name).p("ON").table(index.tableName, index.schema);
|
|
27474
27475
|
if (index.type && index.type !== "btree") {
|
|
27475
|
-
|
|
27476
|
+
builder.p(`USING ${index.type.toUpperCase()}`);
|
|
27476
27477
|
}
|
|
27477
27478
|
if (index.expression) {
|
|
27478
|
-
|
|
27479
|
+
builder.p(`(${index.expression})`);
|
|
27479
27480
|
} else {
|
|
27480
|
-
|
|
27481
|
+
const quotedColumns = index.columns.map((col) => `"${col.replace(/"/g, '""')}"`).join(", ");
|
|
27482
|
+
builder.p(`(${quotedColumns})`);
|
|
27481
27483
|
}
|
|
27482
27484
|
if (index.where) {
|
|
27483
|
-
|
|
27485
|
+
builder.p(`WHERE ${index.where}`);
|
|
27484
27486
|
}
|
|
27485
27487
|
if (index.storageParameters && Object.keys(index.storageParameters).length > 0) {
|
|
27486
27488
|
const params = Object.entries(index.storageParameters).map(([key, value]) => `${key}=${value}`).join(", ");
|
|
27487
|
-
|
|
27489
|
+
builder.p(`WITH (${params})`);
|
|
27488
27490
|
}
|
|
27489
27491
|
if (index.tablespace) {
|
|
27490
|
-
|
|
27492
|
+
builder.p(`TABLESPACE ${index.tablespace}`);
|
|
27491
27493
|
}
|
|
27492
|
-
return
|
|
27494
|
+
return builder.build() + ";";
|
|
27493
27495
|
}
|
|
27494
27496
|
generateConstraintStatementsWithColumnContext(desiredTable, currentTable, qualifiedName) {
|
|
27495
27497
|
const statements = [];
|
|
@@ -27802,7 +27804,7 @@ class SchemaDiffer {
|
|
|
27802
27804
|
}
|
|
27803
27805
|
}
|
|
27804
27806
|
}
|
|
27805
|
-
batchAlterTableChanges(
|
|
27807
|
+
batchAlterTableChanges(table, alterations) {
|
|
27806
27808
|
if (alterations.length === 0) {
|
|
27807
27809
|
return "";
|
|
27808
27810
|
}
|
|
@@ -27815,7 +27817,7 @@ class SchemaDiffer {
|
|
|
27815
27817
|
return 1;
|
|
27816
27818
|
return 0;
|
|
27817
27819
|
});
|
|
27818
|
-
const builder = new SQLBuilder().p("ALTER TABLE").
|
|
27820
|
+
const builder = new SQLBuilder().p("ALTER TABLE").table(table.name, table.schema);
|
|
27819
27821
|
builder.indentIn();
|
|
27820
27822
|
builder.mapComma(sorted, (alt, b) => {
|
|
27821
27823
|
b.nl();
|
|
@@ -27853,7 +27855,7 @@ class SchemaDiffer {
|
|
|
27853
27855
|
b.p("ALTER COLUMN").ident(alt.columnName).p("DROP NOT NULL");
|
|
27854
27856
|
break;
|
|
27855
27857
|
case "add_primary_key": {
|
|
27856
|
-
const constraintName = alt.constraint.name || `pk_${
|
|
27858
|
+
const constraintName = alt.constraint.name || `pk_${table.name}`;
|
|
27857
27859
|
const columns = alt.constraint.columns.map((col) => `"${col.replace(/"/g, '""')}"`).join(", ");
|
|
27858
27860
|
b.p("ADD CONSTRAINT").ident(constraintName).p(`PRIMARY KEY (${columns})`);
|
|
27859
27861
|
break;
|
|
@@ -27862,7 +27864,7 @@ class SchemaDiffer {
|
|
|
27862
27864
|
b.p("DROP CONSTRAINT").ident(alt.constraintName);
|
|
27863
27865
|
break;
|
|
27864
27866
|
case "add_check": {
|
|
27865
|
-
const constraintName = alt.constraint.name || `check_${
|
|
27867
|
+
const constraintName = alt.constraint.name || `check_${table.name}_${Date.now()}`;
|
|
27866
27868
|
b.p("ADD CONSTRAINT").ident(constraintName).p(`CHECK (${alt.constraint.expression})`);
|
|
27867
27869
|
break;
|
|
27868
27870
|
}
|
|
@@ -27870,7 +27872,7 @@ class SchemaDiffer {
|
|
|
27870
27872
|
b.p("DROP CONSTRAINT").ident(alt.constraintName);
|
|
27871
27873
|
break;
|
|
27872
27874
|
case "add_foreign_key": {
|
|
27873
|
-
const constraintName = alt.constraint.name || `fk_${
|
|
27875
|
+
const constraintName = alt.constraint.name || `fk_${table.name}_${alt.constraint.referencedTable}`;
|
|
27874
27876
|
const columns = alt.constraint.columns.map((col) => `"${col.replace(/"/g, '""')}"`).join(", ");
|
|
27875
27877
|
const referencedColumns = alt.constraint.referencedColumns.map((col) => `"${col.replace(/"/g, '""')}"`).join(", ");
|
|
27876
27878
|
b.p("ADD CONSTRAINT").ident(constraintName).p(`FOREIGN KEY (${columns}) REFERENCES`).table(alt.constraint.referencedTable).p(`(${referencedColumns})`);
|
|
@@ -27886,7 +27888,7 @@ class SchemaDiffer {
|
|
|
27886
27888
|
b.p("DROP CONSTRAINT").ident(alt.constraintName);
|
|
27887
27889
|
break;
|
|
27888
27890
|
case "add_unique": {
|
|
27889
|
-
const constraintName = alt.constraint.name || `unique_${
|
|
27891
|
+
const constraintName = alt.constraint.name || `unique_${table.name}_${alt.constraint.columns.join("_")}`;
|
|
27890
27892
|
const columns = alt.constraint.columns.map((col) => `"${col.replace(/"/g, '""')}"`).join(", ");
|
|
27891
27893
|
b.p("ADD CONSTRAINT").ident(constraintName).p(`UNIQUE (${columns})`);
|
|
27892
27894
|
break;
|
|
@@ -28294,9 +28296,14 @@ To fix this, either:
|
|
|
28294
28296
|
if (valuesIdentical) {
|
|
28295
28297
|
Logger.info(`ENUM type '${desiredEnum.name}' values already match, no changes needed`);
|
|
28296
28298
|
} else if (isOnlyAppending) {
|
|
28297
|
-
const fullName = desiredEnum.schema ? `${desiredEnum.schema}.${desiredEnum.name}` : desiredEnum.name;
|
|
28298
28299
|
for (const value of valuesToAdd) {
|
|
28299
|
-
|
|
28300
|
+
const builder = new SQLBuilder().p("ALTER TYPE");
|
|
28301
|
+
if (desiredEnum.schema) {
|
|
28302
|
+
builder.ident(desiredEnum.schema).rewriteLastChar(".");
|
|
28303
|
+
}
|
|
28304
|
+
builder.ident(desiredEnum.name);
|
|
28305
|
+
builder.p(`ADD VALUE '${value}';`);
|
|
28306
|
+
statements.push(builder.build());
|
|
28300
28307
|
Logger.info(`Adding value '${value}' to ENUM type '${desiredEnum.name}'`);
|
|
28301
28308
|
}
|
|
28302
28309
|
} else {
|
|
@@ -28365,7 +28372,8 @@ To fix this, either:
|
|
|
28365
28372
|
const desiredExtensionNames = new Set(desiredExtensions.map((e) => e.name));
|
|
28366
28373
|
for (const currentExt of currentExtensions) {
|
|
28367
28374
|
if (!desiredExtensionNames.has(currentExt.name)) {
|
|
28368
|
-
|
|
28375
|
+
const dropBuilder = new SQLBuilder().p("DROP EXTENSION IF EXISTS").ident(currentExt.name).p("CASCADE");
|
|
28376
|
+
dropStatements.push(dropBuilder.build() + ";");
|
|
28369
28377
|
Logger.info(`Dropping extension '${currentExt.name}' (CASCADE will drop dependent objects)`);
|
|
28370
28378
|
}
|
|
28371
28379
|
}
|
|
@@ -28385,18 +28393,17 @@ To fix this, either:
|
|
|
28385
28393
|
return { create: createStatements, drop: dropStatements };
|
|
28386
28394
|
}
|
|
28387
28395
|
generateCreateExtensionSQL(extension) {
|
|
28388
|
-
|
|
28396
|
+
const builder = new SQLBuilder().p("CREATE EXTENSION IF NOT EXISTS").ident(extension.name);
|
|
28389
28397
|
if (extension.schema) {
|
|
28390
|
-
|
|
28398
|
+
builder.p("SCHEMA").ident(extension.schema);
|
|
28391
28399
|
}
|
|
28392
28400
|
if (extension.version) {
|
|
28393
|
-
|
|
28401
|
+
builder.p(`VERSION '${extension.version}'`);
|
|
28394
28402
|
}
|
|
28395
28403
|
if (extension.cascade) {
|
|
28396
|
-
|
|
28404
|
+
builder.p("CASCADE");
|
|
28397
28405
|
}
|
|
28398
|
-
|
|
28399
|
-
return sql;
|
|
28406
|
+
return builder.build() + ";";
|
|
28400
28407
|
}
|
|
28401
28408
|
generateSequenceStatements(desiredSequences, currentSequences) {
|
|
28402
28409
|
const statements = [];
|
|
@@ -28529,13 +28536,15 @@ To fix this, either:
|
|
|
28529
28536
|
const currentSchemaNames = new Set(currentSchemas.map((s) => s.name));
|
|
28530
28537
|
for (const desiredSchema of desiredSchemas) {
|
|
28531
28538
|
if (!currentSchemaNames.has(desiredSchema.name)) {
|
|
28532
|
-
const
|
|
28533
|
-
|
|
28539
|
+
const builder = new SQLBuilder().p("CREATE SCHEMA");
|
|
28540
|
+
if (desiredSchema.ifNotExists) {
|
|
28541
|
+
builder.p("IF NOT EXISTS");
|
|
28542
|
+
}
|
|
28543
|
+
builder.ident(desiredSchema.name);
|
|
28534
28544
|
if (desiredSchema.owner) {
|
|
28535
|
-
|
|
28545
|
+
builder.p("AUTHORIZATION").ident(desiredSchema.owner);
|
|
28536
28546
|
}
|
|
28537
|
-
|
|
28538
|
-
statements.push(sql);
|
|
28547
|
+
statements.push(builder.build() + ";");
|
|
28539
28548
|
Logger.info(`Creating schema '${desiredSchema.name}'`);
|
|
28540
28549
|
} else {
|
|
28541
28550
|
Logger.info(`Schema '${desiredSchema.name}' already exists, skipping`);
|
|
@@ -28568,15 +28577,18 @@ To fix this, either:
|
|
|
28568
28577
|
}
|
|
28569
28578
|
generateCommentSQL(comment) {
|
|
28570
28579
|
const escapedComment = comment.comment.replace(/'/g, "''");
|
|
28580
|
+
const builder = new SQLBuilder().p("COMMENT ON");
|
|
28571
28581
|
if (comment.objectType === "SCHEMA") {
|
|
28572
|
-
|
|
28573
|
-
}
|
|
28574
|
-
|
|
28575
|
-
|
|
28576
|
-
|
|
28582
|
+
builder.p("SCHEMA").ident(comment.objectName);
|
|
28583
|
+
} else if (comment.objectType === "COLUMN") {
|
|
28584
|
+
builder.p("COLUMN").table(comment.objectName, comment.schemaName);
|
|
28585
|
+
builder.rewriteLastChar(".");
|
|
28586
|
+
builder.ident(comment.columnName);
|
|
28587
|
+
} else {
|
|
28588
|
+
builder.p(comment.objectType).table(comment.objectName, comment.schemaName);
|
|
28577
28589
|
}
|
|
28578
|
-
|
|
28579
|
-
return
|
|
28590
|
+
builder.p(`IS '${escapedComment}'`);
|
|
28591
|
+
return builder.build() + ";";
|
|
28580
28592
|
}
|
|
28581
28593
|
}
|
|
28582
28594
|
|
|
@@ -28691,7 +28703,7 @@ async function applyCommand(options, config) {
|
|
|
28691
28703
|
// package.json
|
|
28692
28704
|
var package_default = {
|
|
28693
28705
|
name: "pgterra",
|
|
28694
|
-
version: "0.2.
|
|
28706
|
+
version: "0.2.2",
|
|
28695
28707
|
description: "Declarative schema management for Postgres",
|
|
28696
28708
|
keywords: [
|
|
28697
28709
|
"postgres",
|