bun-query-builder 0.1.50 → 0.1.52
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/bin/cli.js +114 -65
- package/dist/schema.d.ts +2 -0
- package/dist/src/index.js +113 -64
- package/package.json +1 -1
package/dist/bin/cli.js
CHANGED
|
@@ -18078,6 +18078,14 @@ class MySQLDriver {
|
|
|
18078
18078
|
if (defaultValue) {
|
|
18079
18079
|
parts.push(defaultValue);
|
|
18080
18080
|
}
|
|
18081
|
+
if (column.references) {
|
|
18082
|
+
let reference = `REFERENCES ${this.quoteIdentifier(column.references.table)}(${this.quoteIdentifier(column.references.column)})`;
|
|
18083
|
+
if (column.references.onDelete)
|
|
18084
|
+
reference += ` ON DELETE ${column.references.onDelete.toUpperCase()}`;
|
|
18085
|
+
if (column.references.onUpdate)
|
|
18086
|
+
reference += ` ON UPDATE ${column.references.onUpdate.toUpperCase()}`;
|
|
18087
|
+
parts.push(reference);
|
|
18088
|
+
}
|
|
18081
18089
|
return parts.join(" ");
|
|
18082
18090
|
}
|
|
18083
18091
|
}
|
|
@@ -18245,6 +18253,14 @@ class PostgresDriver {
|
|
|
18245
18253
|
if (defaultValue) {
|
|
18246
18254
|
parts.push(defaultValue);
|
|
18247
18255
|
}
|
|
18256
|
+
if (column.references) {
|
|
18257
|
+
let reference = `REFERENCES ${this.quoteIdentifier(column.references.table)}(${this.quoteIdentifier(column.references.column)})`;
|
|
18258
|
+
if (column.references.onDelete)
|
|
18259
|
+
reference += ` ON DELETE ${column.references.onDelete.toUpperCase()}`;
|
|
18260
|
+
if (column.references.onUpdate)
|
|
18261
|
+
reference += ` ON UPDATE ${column.references.onUpdate.toUpperCase()}`;
|
|
18262
|
+
parts.push(reference);
|
|
18263
|
+
}
|
|
18248
18264
|
return parts.join(" ");
|
|
18249
18265
|
}
|
|
18250
18266
|
}
|
|
@@ -28014,7 +28030,8 @@ function buildMigrationPlan2(models, options) {
|
|
|
28014
28030
|
for (const attrName of Object.keys(attrs)) {
|
|
28015
28031
|
const attr = attrs[attrName];
|
|
28016
28032
|
const columnName = snakeCase(attrName);
|
|
28017
|
-
const
|
|
28033
|
+
const validatorRequired = Boolean(attr.validation?.rule?.isRequired);
|
|
28034
|
+
const isNullable = attrName === primaryKey ? false : attr.nullable ?? !(attr.required ?? validatorRequired);
|
|
28018
28035
|
let inferred;
|
|
28019
28036
|
let enumValues;
|
|
28020
28037
|
const isPk = attrName === primaryKey;
|
|
@@ -28309,7 +28326,12 @@ function generateSql(plan, opts = {}) {
|
|
|
28309
28326
|
const emit = opts.dryRun ? () => false : createMigrationFile;
|
|
28310
28327
|
const statements = [];
|
|
28311
28328
|
const driver = getDialectDriver(plan.dialect);
|
|
28312
|
-
|
|
28329
|
+
const { tables, deferredForeignKeys } = orderTablesForCreate(plan.tables, plan.dialect);
|
|
28330
|
+
for (const [tableIndex, originalTable] of tables.entries()) {
|
|
28331
|
+
const t2 = {
|
|
28332
|
+
...originalTable,
|
|
28333
|
+
columns: originalTable.columns.map((column) => deferredForeignKeys.has(foreignKeyKey(originalTable.table, column.name)) ? { ...column, references: undefined } : column)
|
|
28334
|
+
};
|
|
28313
28335
|
const tableStatements = [];
|
|
28314
28336
|
const enumTypes = new Set;
|
|
28315
28337
|
for (const c2 of t2.columns) {
|
|
@@ -28327,30 +28349,25 @@ function generateSql(plan, opts = {}) {
|
|
|
28327
28349
|
}
|
|
28328
28350
|
const createTableStatement = driver.createTable(t2);
|
|
28329
28351
|
tableStatements.push(createTableStatement);
|
|
28352
|
+
for (const idx of t2.indexes)
|
|
28353
|
+
tableStatements.push(driver.createIndex(t2.table, idx));
|
|
28354
|
+
if (tableIndex === tables.length - 1) {
|
|
28355
|
+
for (const deferredTable of tables) {
|
|
28356
|
+
for (const column of deferredTable.columns) {
|
|
28357
|
+
if (!column.references || !deferredForeignKeys.has(foreignKeyKey(deferredTable.table, column.name)))
|
|
28358
|
+
continue;
|
|
28359
|
+
const addForeignKey = driver.addForeignKey(deferredTable.table, column.name, column.references.table, column.references.column, column.references.onDelete, column.references.onUpdate);
|
|
28360
|
+
if (addForeignKey)
|
|
28361
|
+
tableStatements.push(addForeignKey);
|
|
28362
|
+
}
|
|
28363
|
+
}
|
|
28364
|
+
}
|
|
28330
28365
|
statements.push(...tableStatements);
|
|
28331
28366
|
const combinedStatement = tableStatements.join(`
|
|
28332
28367
|
|
|
28333
28368
|
`);
|
|
28334
28369
|
emit(combinedStatement, `create-${t2.table}-table`);
|
|
28335
28370
|
}
|
|
28336
|
-
for (const t2 of plan.tables) {
|
|
28337
|
-
for (const c2 of t2.columns) {
|
|
28338
|
-
if (c2.references) {
|
|
28339
|
-
const alterTableStatement = driver.addForeignKey(t2.table, c2.name, c2.references.table, c2.references.column, c2.references.onDelete, c2.references.onUpdate);
|
|
28340
|
-
if (!alterTableStatement)
|
|
28341
|
-
continue;
|
|
28342
|
-
statements.push(alterTableStatement);
|
|
28343
|
-
emit(alterTableStatement, `alter-${t2.table}-${c2.name}`);
|
|
28344
|
-
}
|
|
28345
|
-
}
|
|
28346
|
-
}
|
|
28347
|
-
for (const t2 of plan.tables) {
|
|
28348
|
-
for (const idx of t2.indexes) {
|
|
28349
|
-
const createIndexStatement = driver.createIndex(t2.table, idx);
|
|
28350
|
-
statements.push(createIndexStatement);
|
|
28351
|
-
emit(createIndexStatement, `create-${idx.name}-index-in-${t2.table}`);
|
|
28352
|
-
}
|
|
28353
|
-
}
|
|
28354
28371
|
const totalChanges = migrationsCreatedCount + migrationsUpdatedCount;
|
|
28355
28372
|
if (totalChanges === 0) {
|
|
28356
28373
|
info2("-- Nothing to migrate");
|
|
@@ -28364,6 +28381,39 @@ function generateSql(plan, opts = {}) {
|
|
|
28364
28381
|
}
|
|
28365
28382
|
return statements;
|
|
28366
28383
|
}
|
|
28384
|
+
function foreignKeyKey(table, column) {
|
|
28385
|
+
return `${table}\x00${column}`;
|
|
28386
|
+
}
|
|
28387
|
+
function orderTablesForCreate(input, dialect) {
|
|
28388
|
+
if (dialect === "sqlite" || input.length < 2)
|
|
28389
|
+
return { tables: input, deferredForeignKeys: new Set };
|
|
28390
|
+
const byName = new Map(input.map((table) => [table.table, table]));
|
|
28391
|
+
const dependencies = new Map;
|
|
28392
|
+
for (const table of input) {
|
|
28393
|
+
dependencies.set(table.table, new Set(table.columns.map((column) => column.references?.table).filter((target) => Boolean(target && target !== table.table && byName.has(target)))));
|
|
28394
|
+
}
|
|
28395
|
+
const ordered = [];
|
|
28396
|
+
const emitted = new Set;
|
|
28397
|
+
while (ordered.length < input.length) {
|
|
28398
|
+
const ready = input.find((table) => !emitted.has(table.table) && [...dependencies.get(table.table) ?? []].every((target) => emitted.has(target)));
|
|
28399
|
+
if (!ready)
|
|
28400
|
+
break;
|
|
28401
|
+
ordered.push(ready);
|
|
28402
|
+
emitted.add(ready.table);
|
|
28403
|
+
}
|
|
28404
|
+
const cycleTables = new Set(input.filter((table) => !emitted.has(table.table)).map((table) => table.table));
|
|
28405
|
+
const deferredForeignKeys = new Set;
|
|
28406
|
+
for (const table of input) {
|
|
28407
|
+
if (!cycleTables.has(table.table))
|
|
28408
|
+
continue;
|
|
28409
|
+
for (const column of table.columns) {
|
|
28410
|
+
if (column.references && cycleTables.has(column.references.table) && column.references.table !== table.table)
|
|
28411
|
+
deferredForeignKeys.add(foreignKeyKey(table.table, column.name));
|
|
28412
|
+
}
|
|
28413
|
+
}
|
|
28414
|
+
ordered.push(...input.filter((table) => cycleTables.has(table.table)));
|
|
28415
|
+
return { tables: ordered, deferredForeignKeys };
|
|
28416
|
+
}
|
|
28367
28417
|
function generateSqlString(plan) {
|
|
28368
28418
|
return generateSql(plan).join(`
|
|
28369
28419
|
`);
|
|
@@ -28572,59 +28622,58 @@ function generateDiffOperations(previous, next, opts = {}) {
|
|
|
28572
28622
|
info2(`-- Detected dropped table: ${tableName}`);
|
|
28573
28623
|
}
|
|
28574
28624
|
}
|
|
28575
|
-
|
|
28576
|
-
|
|
28577
|
-
|
|
28578
|
-
|
|
28579
|
-
|
|
28580
|
-
|
|
28581
|
-
|
|
28582
|
-
|
|
28583
|
-
|
|
28584
|
-
|
|
28585
|
-
|
|
28586
|
-
|
|
28587
|
-
|
|
28588
|
-
|
|
28589
|
-
|
|
28625
|
+
const newTablePlans = Object.keys(nextTables).filter((tableName) => !prevTables[tableName]).map((tableName) => nextTables[tableName]);
|
|
28626
|
+
const { tables: orderedNewTables, deferredForeignKeys } = orderTablesForCreate(newTablePlans, dialect);
|
|
28627
|
+
for (const [tableIndex, originalTable] of orderedNewTables.entries()) {
|
|
28628
|
+
const t2 = {
|
|
28629
|
+
...originalTable,
|
|
28630
|
+
columns: originalTable.columns.map((column) => deferredForeignKeys.has(foreignKeyKey(originalTable.table, column.name)) ? { ...column, references: undefined } : column)
|
|
28631
|
+
};
|
|
28632
|
+
const tableStatements = [];
|
|
28633
|
+
const enumTypes2 = new Set;
|
|
28634
|
+
for (const c2 of t2.columns) {
|
|
28635
|
+
if (c2.type === "enum" && c2.enumValues && c2.enumValues.length > 0) {
|
|
28636
|
+
const enumTypeName = `${t2.table}_${c2.name}_type`;
|
|
28637
|
+
c2.enumTypeName = enumTypeName;
|
|
28638
|
+
if (!enumTypes2.has(enumTypeName)) {
|
|
28639
|
+
const createEnumStatement = driver.createEnumType(enumTypeName, c2.enumValues);
|
|
28640
|
+
if (createEnumStatement) {
|
|
28641
|
+
tableStatements.push(createEnumStatement);
|
|
28590
28642
|
}
|
|
28643
|
+
enumTypes2.add(enumTypeName);
|
|
28591
28644
|
}
|
|
28592
28645
|
}
|
|
28593
|
-
const createTableStatement = driver.createTable(t2);
|
|
28594
|
-
tableStatements.push(createTableStatement);
|
|
28595
|
-
chunks.push(...tableStatements);
|
|
28596
|
-
operations.push({ kind: "create_table", table: t2.table, destructive: false, sql: createTableStatement });
|
|
28597
|
-
const combinedStatement = tableStatements.join(`
|
|
28598
|
-
|
|
28599
|
-
`);
|
|
28600
|
-
emit(combinedStatement, `create-${t2.table}-table`);
|
|
28601
28646
|
}
|
|
28602
|
-
|
|
28603
|
-
|
|
28604
|
-
|
|
28605
|
-
const
|
|
28606
|
-
|
|
28607
|
-
|
|
28608
|
-
chunks.push(createIndexStatement);
|
|
28609
|
-
operations.push({ kind: "create_index", table: t2.table, column: idx.name, destructive: false, sql: createIndexStatement });
|
|
28610
|
-
emit(createIndexStatement, `create-${idx.name}-index-in-${t2.table}`);
|
|
28611
|
-
}
|
|
28647
|
+
const createTableStatement = driver.createTable(t2);
|
|
28648
|
+
tableStatements.push(createTableStatement);
|
|
28649
|
+
for (const idx of t2.indexes) {
|
|
28650
|
+
const createIndexStatement = driver.createIndex(t2.table, idx);
|
|
28651
|
+
tableStatements.push(createIndexStatement);
|
|
28652
|
+
operations.push({ kind: "create_index", table: t2.table, column: idx.name, destructive: false, sql: createIndexStatement });
|
|
28612
28653
|
}
|
|
28613
|
-
|
|
28614
|
-
|
|
28615
|
-
|
|
28616
|
-
|
|
28617
|
-
|
|
28618
|
-
|
|
28619
|
-
|
|
28620
|
-
if (!
|
|
28654
|
+
for (const column of originalTable.columns) {
|
|
28655
|
+
if (column.references && !deferredForeignKeys.has(foreignKeyKey(originalTable.table, column.name)))
|
|
28656
|
+
operations.push({ kind: "add_foreign_key", table: originalTable.table, column: column.name, destructive: false, sql: createTableStatement });
|
|
28657
|
+
}
|
|
28658
|
+
if (tableIndex === orderedNewTables.length - 1) {
|
|
28659
|
+
for (const deferredTable of orderedNewTables) {
|
|
28660
|
+
for (const column of deferredTable.columns) {
|
|
28661
|
+
if (!column.references || !deferredForeignKeys.has(foreignKeyKey(deferredTable.table, column.name)))
|
|
28621
28662
|
continue;
|
|
28622
|
-
|
|
28623
|
-
|
|
28624
|
-
|
|
28663
|
+
const addForeignKey = driver.addForeignKey(deferredTable.table, column.name, column.references.table, column.references.column, column.references.onDelete, column.references.onUpdate);
|
|
28664
|
+
if (!addForeignKey)
|
|
28665
|
+
continue;
|
|
28666
|
+
tableStatements.push(addForeignKey);
|
|
28667
|
+
operations.push({ kind: "add_foreign_key", table: deferredTable.table, column: column.name, destructive: false, sql: addForeignKey });
|
|
28625
28668
|
}
|
|
28626
28669
|
}
|
|
28627
28670
|
}
|
|
28671
|
+
chunks.push(...tableStatements);
|
|
28672
|
+
operations.push({ kind: "create_table", table: t2.table, destructive: false, sql: createTableStatement });
|
|
28673
|
+
const combinedStatement = tableStatements.join(`
|
|
28674
|
+
|
|
28675
|
+
`);
|
|
28676
|
+
emit(combinedStatement, `create-${t2.table}-table`);
|
|
28628
28677
|
}
|
|
28629
28678
|
const enumTypes = new Set;
|
|
28630
28679
|
for (const tableName of Object.keys(nextTables)) {
|
|
@@ -30902,7 +30951,7 @@ function getPrefix() {
|
|
|
30902
30951
|
}
|
|
30903
30952
|
var prefix = getPrefix();
|
|
30904
30953
|
// package.json
|
|
30905
|
-
var version2 = "0.1.
|
|
30954
|
+
var version2 = "0.1.52";
|
|
30906
30955
|
|
|
30907
30956
|
// bin/cli.ts
|
|
30908
30957
|
init_actions();
|
package/dist/schema.d.ts
CHANGED
package/dist/src/index.js
CHANGED
|
@@ -18077,6 +18077,14 @@ class MySQLDriver {
|
|
|
18077
18077
|
if (defaultValue) {
|
|
18078
18078
|
parts.push(defaultValue);
|
|
18079
18079
|
}
|
|
18080
|
+
if (column.references) {
|
|
18081
|
+
let reference = `REFERENCES ${this.quoteIdentifier(column.references.table)}(${this.quoteIdentifier(column.references.column)})`;
|
|
18082
|
+
if (column.references.onDelete)
|
|
18083
|
+
reference += ` ON DELETE ${column.references.onDelete.toUpperCase()}`;
|
|
18084
|
+
if (column.references.onUpdate)
|
|
18085
|
+
reference += ` ON UPDATE ${column.references.onUpdate.toUpperCase()}`;
|
|
18086
|
+
parts.push(reference);
|
|
18087
|
+
}
|
|
18080
18088
|
return parts.join(" ");
|
|
18081
18089
|
}
|
|
18082
18090
|
}
|
|
@@ -18244,6 +18252,14 @@ class PostgresDriver {
|
|
|
18244
18252
|
if (defaultValue) {
|
|
18245
18253
|
parts.push(defaultValue);
|
|
18246
18254
|
}
|
|
18255
|
+
if (column.references) {
|
|
18256
|
+
let reference = `REFERENCES ${this.quoteIdentifier(column.references.table)}(${this.quoteIdentifier(column.references.column)})`;
|
|
18257
|
+
if (column.references.onDelete)
|
|
18258
|
+
reference += ` ON DELETE ${column.references.onDelete.toUpperCase()}`;
|
|
18259
|
+
if (column.references.onUpdate)
|
|
18260
|
+
reference += ` ON UPDATE ${column.references.onUpdate.toUpperCase()}`;
|
|
18261
|
+
parts.push(reference);
|
|
18262
|
+
}
|
|
18247
18263
|
return parts.join(" ");
|
|
18248
18264
|
}
|
|
18249
18265
|
}
|
|
@@ -28013,7 +28029,8 @@ function buildMigrationPlan2(models, options) {
|
|
|
28013
28029
|
for (const attrName of Object.keys(attrs)) {
|
|
28014
28030
|
const attr = attrs[attrName];
|
|
28015
28031
|
const columnName = snakeCase(attrName);
|
|
28016
|
-
const
|
|
28032
|
+
const validatorRequired = Boolean(attr.validation?.rule?.isRequired);
|
|
28033
|
+
const isNullable = attrName === primaryKey ? false : attr.nullable ?? !(attr.required ?? validatorRequired);
|
|
28017
28034
|
let inferred;
|
|
28018
28035
|
let enumValues;
|
|
28019
28036
|
const isPk = attrName === primaryKey;
|
|
@@ -28308,7 +28325,12 @@ function generateSql(plan, opts = {}) {
|
|
|
28308
28325
|
const emit = opts.dryRun ? () => false : createMigrationFile;
|
|
28309
28326
|
const statements = [];
|
|
28310
28327
|
const driver = getDialectDriver(plan.dialect);
|
|
28311
|
-
|
|
28328
|
+
const { tables, deferredForeignKeys } = orderTablesForCreate(plan.tables, plan.dialect);
|
|
28329
|
+
for (const [tableIndex, originalTable] of tables.entries()) {
|
|
28330
|
+
const t2 = {
|
|
28331
|
+
...originalTable,
|
|
28332
|
+
columns: originalTable.columns.map((column) => deferredForeignKeys.has(foreignKeyKey(originalTable.table, column.name)) ? { ...column, references: undefined } : column)
|
|
28333
|
+
};
|
|
28312
28334
|
const tableStatements = [];
|
|
28313
28335
|
const enumTypes = new Set;
|
|
28314
28336
|
for (const c2 of t2.columns) {
|
|
@@ -28326,30 +28348,25 @@ function generateSql(plan, opts = {}) {
|
|
|
28326
28348
|
}
|
|
28327
28349
|
const createTableStatement = driver.createTable(t2);
|
|
28328
28350
|
tableStatements.push(createTableStatement);
|
|
28351
|
+
for (const idx of t2.indexes)
|
|
28352
|
+
tableStatements.push(driver.createIndex(t2.table, idx));
|
|
28353
|
+
if (tableIndex === tables.length - 1) {
|
|
28354
|
+
for (const deferredTable of tables) {
|
|
28355
|
+
for (const column of deferredTable.columns) {
|
|
28356
|
+
if (!column.references || !deferredForeignKeys.has(foreignKeyKey(deferredTable.table, column.name)))
|
|
28357
|
+
continue;
|
|
28358
|
+
const addForeignKey = driver.addForeignKey(deferredTable.table, column.name, column.references.table, column.references.column, column.references.onDelete, column.references.onUpdate);
|
|
28359
|
+
if (addForeignKey)
|
|
28360
|
+
tableStatements.push(addForeignKey);
|
|
28361
|
+
}
|
|
28362
|
+
}
|
|
28363
|
+
}
|
|
28329
28364
|
statements.push(...tableStatements);
|
|
28330
28365
|
const combinedStatement = tableStatements.join(`
|
|
28331
28366
|
|
|
28332
28367
|
`);
|
|
28333
28368
|
emit(combinedStatement, `create-${t2.table}-table`);
|
|
28334
28369
|
}
|
|
28335
|
-
for (const t2 of plan.tables) {
|
|
28336
|
-
for (const c2 of t2.columns) {
|
|
28337
|
-
if (c2.references) {
|
|
28338
|
-
const alterTableStatement = driver.addForeignKey(t2.table, c2.name, c2.references.table, c2.references.column, c2.references.onDelete, c2.references.onUpdate);
|
|
28339
|
-
if (!alterTableStatement)
|
|
28340
|
-
continue;
|
|
28341
|
-
statements.push(alterTableStatement);
|
|
28342
|
-
emit(alterTableStatement, `alter-${t2.table}-${c2.name}`);
|
|
28343
|
-
}
|
|
28344
|
-
}
|
|
28345
|
-
}
|
|
28346
|
-
for (const t2 of plan.tables) {
|
|
28347
|
-
for (const idx of t2.indexes) {
|
|
28348
|
-
const createIndexStatement = driver.createIndex(t2.table, idx);
|
|
28349
|
-
statements.push(createIndexStatement);
|
|
28350
|
-
emit(createIndexStatement, `create-${idx.name}-index-in-${t2.table}`);
|
|
28351
|
-
}
|
|
28352
|
-
}
|
|
28353
28370
|
const totalChanges = migrationsCreatedCount + migrationsUpdatedCount;
|
|
28354
28371
|
if (totalChanges === 0) {
|
|
28355
28372
|
info2("-- Nothing to migrate");
|
|
@@ -28363,6 +28380,39 @@ function generateSql(plan, opts = {}) {
|
|
|
28363
28380
|
}
|
|
28364
28381
|
return statements;
|
|
28365
28382
|
}
|
|
28383
|
+
function foreignKeyKey(table, column) {
|
|
28384
|
+
return `${table}\x00${column}`;
|
|
28385
|
+
}
|
|
28386
|
+
function orderTablesForCreate(input, dialect) {
|
|
28387
|
+
if (dialect === "sqlite" || input.length < 2)
|
|
28388
|
+
return { tables: input, deferredForeignKeys: new Set };
|
|
28389
|
+
const byName = new Map(input.map((table) => [table.table, table]));
|
|
28390
|
+
const dependencies = new Map;
|
|
28391
|
+
for (const table of input) {
|
|
28392
|
+
dependencies.set(table.table, new Set(table.columns.map((column) => column.references?.table).filter((target) => Boolean(target && target !== table.table && byName.has(target)))));
|
|
28393
|
+
}
|
|
28394
|
+
const ordered = [];
|
|
28395
|
+
const emitted = new Set;
|
|
28396
|
+
while (ordered.length < input.length) {
|
|
28397
|
+
const ready = input.find((table) => !emitted.has(table.table) && [...dependencies.get(table.table) ?? []].every((target) => emitted.has(target)));
|
|
28398
|
+
if (!ready)
|
|
28399
|
+
break;
|
|
28400
|
+
ordered.push(ready);
|
|
28401
|
+
emitted.add(ready.table);
|
|
28402
|
+
}
|
|
28403
|
+
const cycleTables = new Set(input.filter((table) => !emitted.has(table.table)).map((table) => table.table));
|
|
28404
|
+
const deferredForeignKeys = new Set;
|
|
28405
|
+
for (const table of input) {
|
|
28406
|
+
if (!cycleTables.has(table.table))
|
|
28407
|
+
continue;
|
|
28408
|
+
for (const column of table.columns) {
|
|
28409
|
+
if (column.references && cycleTables.has(column.references.table) && column.references.table !== table.table)
|
|
28410
|
+
deferredForeignKeys.add(foreignKeyKey(table.table, column.name));
|
|
28411
|
+
}
|
|
28412
|
+
}
|
|
28413
|
+
ordered.push(...input.filter((table) => cycleTables.has(table.table)));
|
|
28414
|
+
return { tables: ordered, deferredForeignKeys };
|
|
28415
|
+
}
|
|
28366
28416
|
function generateSqlString(plan) {
|
|
28367
28417
|
return generateSql(plan).join(`
|
|
28368
28418
|
`);
|
|
@@ -28571,59 +28621,58 @@ function generateDiffOperations(previous, next, opts = {}) {
|
|
|
28571
28621
|
info2(`-- Detected dropped table: ${tableName}`);
|
|
28572
28622
|
}
|
|
28573
28623
|
}
|
|
28574
|
-
|
|
28575
|
-
|
|
28576
|
-
|
|
28577
|
-
|
|
28578
|
-
|
|
28579
|
-
|
|
28580
|
-
|
|
28581
|
-
|
|
28582
|
-
|
|
28583
|
-
|
|
28584
|
-
|
|
28585
|
-
|
|
28586
|
-
|
|
28587
|
-
|
|
28588
|
-
|
|
28624
|
+
const newTablePlans = Object.keys(nextTables).filter((tableName) => !prevTables[tableName]).map((tableName) => nextTables[tableName]);
|
|
28625
|
+
const { tables: orderedNewTables, deferredForeignKeys } = orderTablesForCreate(newTablePlans, dialect);
|
|
28626
|
+
for (const [tableIndex, originalTable] of orderedNewTables.entries()) {
|
|
28627
|
+
const t2 = {
|
|
28628
|
+
...originalTable,
|
|
28629
|
+
columns: originalTable.columns.map((column) => deferredForeignKeys.has(foreignKeyKey(originalTable.table, column.name)) ? { ...column, references: undefined } : column)
|
|
28630
|
+
};
|
|
28631
|
+
const tableStatements = [];
|
|
28632
|
+
const enumTypes2 = new Set;
|
|
28633
|
+
for (const c2 of t2.columns) {
|
|
28634
|
+
if (c2.type === "enum" && c2.enumValues && c2.enumValues.length > 0) {
|
|
28635
|
+
const enumTypeName = `${t2.table}_${c2.name}_type`;
|
|
28636
|
+
c2.enumTypeName = enumTypeName;
|
|
28637
|
+
if (!enumTypes2.has(enumTypeName)) {
|
|
28638
|
+
const createEnumStatement = driver.createEnumType(enumTypeName, c2.enumValues);
|
|
28639
|
+
if (createEnumStatement) {
|
|
28640
|
+
tableStatements.push(createEnumStatement);
|
|
28589
28641
|
}
|
|
28642
|
+
enumTypes2.add(enumTypeName);
|
|
28590
28643
|
}
|
|
28591
28644
|
}
|
|
28592
|
-
const createTableStatement = driver.createTable(t2);
|
|
28593
|
-
tableStatements.push(createTableStatement);
|
|
28594
|
-
chunks.push(...tableStatements);
|
|
28595
|
-
operations.push({ kind: "create_table", table: t2.table, destructive: false, sql: createTableStatement });
|
|
28596
|
-
const combinedStatement = tableStatements.join(`
|
|
28597
|
-
|
|
28598
|
-
`);
|
|
28599
|
-
emit(combinedStatement, `create-${t2.table}-table`);
|
|
28600
28645
|
}
|
|
28601
|
-
|
|
28602
|
-
|
|
28603
|
-
|
|
28604
|
-
const
|
|
28605
|
-
|
|
28606
|
-
|
|
28607
|
-
chunks.push(createIndexStatement);
|
|
28608
|
-
operations.push({ kind: "create_index", table: t2.table, column: idx.name, destructive: false, sql: createIndexStatement });
|
|
28609
|
-
emit(createIndexStatement, `create-${idx.name}-index-in-${t2.table}`);
|
|
28610
|
-
}
|
|
28646
|
+
const createTableStatement = driver.createTable(t2);
|
|
28647
|
+
tableStatements.push(createTableStatement);
|
|
28648
|
+
for (const idx of t2.indexes) {
|
|
28649
|
+
const createIndexStatement = driver.createIndex(t2.table, idx);
|
|
28650
|
+
tableStatements.push(createIndexStatement);
|
|
28651
|
+
operations.push({ kind: "create_index", table: t2.table, column: idx.name, destructive: false, sql: createIndexStatement });
|
|
28611
28652
|
}
|
|
28612
|
-
|
|
28613
|
-
|
|
28614
|
-
|
|
28615
|
-
|
|
28616
|
-
|
|
28617
|
-
|
|
28618
|
-
|
|
28619
|
-
if (!
|
|
28653
|
+
for (const column of originalTable.columns) {
|
|
28654
|
+
if (column.references && !deferredForeignKeys.has(foreignKeyKey(originalTable.table, column.name)))
|
|
28655
|
+
operations.push({ kind: "add_foreign_key", table: originalTable.table, column: column.name, destructive: false, sql: createTableStatement });
|
|
28656
|
+
}
|
|
28657
|
+
if (tableIndex === orderedNewTables.length - 1) {
|
|
28658
|
+
for (const deferredTable of orderedNewTables) {
|
|
28659
|
+
for (const column of deferredTable.columns) {
|
|
28660
|
+
if (!column.references || !deferredForeignKeys.has(foreignKeyKey(deferredTable.table, column.name)))
|
|
28620
28661
|
continue;
|
|
28621
|
-
|
|
28622
|
-
|
|
28623
|
-
|
|
28662
|
+
const addForeignKey = driver.addForeignKey(deferredTable.table, column.name, column.references.table, column.references.column, column.references.onDelete, column.references.onUpdate);
|
|
28663
|
+
if (!addForeignKey)
|
|
28664
|
+
continue;
|
|
28665
|
+
tableStatements.push(addForeignKey);
|
|
28666
|
+
operations.push({ kind: "add_foreign_key", table: deferredTable.table, column: column.name, destructive: false, sql: addForeignKey });
|
|
28624
28667
|
}
|
|
28625
28668
|
}
|
|
28626
28669
|
}
|
|
28670
|
+
chunks.push(...tableStatements);
|
|
28671
|
+
operations.push({ kind: "create_table", table: t2.table, destructive: false, sql: createTableStatement });
|
|
28672
|
+
const combinedStatement = tableStatements.join(`
|
|
28673
|
+
|
|
28674
|
+
`);
|
|
28675
|
+
emit(combinedStatement, `create-${t2.table}-table`);
|
|
28627
28676
|
}
|
|
28628
28677
|
const enumTypes = new Set;
|
|
28629
28678
|
for (const tableName of Object.keys(nextTables)) {
|
package/package.json
CHANGED