rake-db 2.3.26 → 2.3.28
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/CHANGELOG.md +6 -0
- package/dist/index.d.ts +9 -6
- package/dist/index.js +224 -112
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +224 -114
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/commands/migrateOrRollback.test.ts +43 -1
- package/src/commands/migrateOrRollback.ts +13 -15
- package/src/common.ts +16 -0
- package/src/errors.ts +3 -0
- package/src/migration/change.test.ts +16 -0
- package/src/migration/change.ts +5 -16
- package/src/migration/changeTable.test.ts +236 -58
- package/src/migration/changeTable.ts +34 -14
- package/src/migration/createTable.test.ts +31 -7
- package/src/migration/createTable.ts +44 -25
- package/src/migration/migration.test.ts +1 -1
- package/src/migration/migration.ts +6 -8
- package/src/migration/tableMethods.ts +8 -0
- package/src/pull/astToMigration.test.ts +97 -28
- package/src/pull/astToMigration.ts +55 -12
- package/src/pull/dbStructure.test.ts +14 -0
- package/src/pull/dbStructure.ts +21 -0
- package/src/pull/pull.test.ts +4 -0
- package/src/pull/structureToAst.test.ts +37 -0
- package/src/pull/structureToAst.ts +24 -10
- package/src/rakeDb.test.ts +20 -0
- package/src/rakeDb.ts +27 -18
- package/src/test-utils.ts +1 -0
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { singleQuote, quote, isRaw, getRaw, toArray,
|
|
1
|
+
import { singleQuote, quote, isRaw, getRaw, toArray, raw, EnumColumn, columnTypes, getColumnTypes, getTableData, ColumnType, emptyObject, resetTableData, TransactionAdapter, logParamToLogObject, createDb as createDb$1, Adapter, columnsByType, instantiateColumn, codeToString, addCode, quoteObjectKey, primaryKeyToCode, indexToCode, foreignKeyToCode, TimestampColumn, foreignKeyArgsToCode } from 'pqb';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import { readdir, mkdir, writeFile } from 'fs/promises';
|
|
4
4
|
import prompts from 'prompts';
|
|
@@ -225,25 +225,27 @@ const quoteSchemaTable = ({
|
|
|
225
225
|
}) => {
|
|
226
226
|
return singleQuote(schema ? `${schema}.${name}` : name);
|
|
227
227
|
};
|
|
228
|
+
const makePopulateEnumQuery = (item) => {
|
|
229
|
+
const [schema, name] = getSchemaAndTableFromName(item.enumName);
|
|
230
|
+
return {
|
|
231
|
+
text: `SELECT unnest(enum_range(NULL::${quoteWithSchema({
|
|
232
|
+
schema,
|
|
233
|
+
name
|
|
234
|
+
})}))::text`,
|
|
235
|
+
then(result) {
|
|
236
|
+
item.options.push(...result.rows.map(([value]) => value));
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
};
|
|
228
240
|
|
|
229
|
-
|
|
230
|
-
let currentPromise;
|
|
231
|
-
let currentUp = true;
|
|
232
|
-
let currentChangeCallback;
|
|
241
|
+
const currentChanges = [];
|
|
233
242
|
const change = (fn) => {
|
|
234
|
-
|
|
235
|
-
throw new Error("Database instance is not set");
|
|
236
|
-
currentPromise = fn(currentMigration, currentUp);
|
|
237
|
-
currentChangeCallback = fn;
|
|
238
|
-
};
|
|
239
|
-
const setCurrentMigration = (db) => {
|
|
240
|
-
currentMigration = db;
|
|
243
|
+
currentChanges.push(fn);
|
|
241
244
|
};
|
|
242
|
-
const
|
|
243
|
-
|
|
245
|
+
const clearChanges = () => {
|
|
246
|
+
currentChanges.length = 0;
|
|
244
247
|
};
|
|
245
|
-
const
|
|
246
|
-
const getCurrentChangeCallback = () => currentChangeCallback;
|
|
248
|
+
const getCurrentChanges = () => currentChanges;
|
|
247
249
|
|
|
248
250
|
var __defProp$5 = Object.defineProperty;
|
|
249
251
|
var __defProps$4 = Object.defineProperties;
|
|
@@ -434,6 +436,16 @@ const primaryKeyToSql = (primaryKey) => {
|
|
|
434
436
|
)})`;
|
|
435
437
|
};
|
|
436
438
|
|
|
439
|
+
const tableMethods = {
|
|
440
|
+
raw,
|
|
441
|
+
enum: (name) => new EnumColumn(name, [])
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
class RakeDbError extends Error {
|
|
445
|
+
}
|
|
446
|
+
class NoPrimaryKey extends RakeDbError {
|
|
447
|
+
}
|
|
448
|
+
|
|
437
449
|
var __defProp$4 = Object.defineProperty;
|
|
438
450
|
var __defProps$3 = Object.defineProperties;
|
|
439
451
|
var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
|
|
@@ -453,9 +465,19 @@ var __spreadValues$4 = (a, b) => {
|
|
|
453
465
|
return a;
|
|
454
466
|
};
|
|
455
467
|
var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
468
|
+
var __objRest = (source, exclude) => {
|
|
469
|
+
var target = {};
|
|
470
|
+
for (var prop in source)
|
|
471
|
+
if (__hasOwnProp$4.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
472
|
+
target[prop] = source[prop];
|
|
473
|
+
if (source != null && __getOwnPropSymbols$4)
|
|
474
|
+
for (var prop of __getOwnPropSymbols$4(source)) {
|
|
475
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum$4.call(source, prop))
|
|
476
|
+
target[prop] = source[prop];
|
|
477
|
+
}
|
|
478
|
+
return target;
|
|
479
|
+
};
|
|
480
|
+
const types = Object.assign(Object.create(columnTypes), tableMethods);
|
|
459
481
|
const createTable$1 = async (migration, up, tableName, options, fn) => {
|
|
460
482
|
const shape = getColumnTypes(types, fn);
|
|
461
483
|
const tableData = getTableData();
|
|
@@ -469,8 +491,10 @@ const createTable$1 = async (migration, up, tableName, options, fn) => {
|
|
|
469
491
|
);
|
|
470
492
|
validatePrimaryKey(ast);
|
|
471
493
|
const queries = astToQueries$1(ast);
|
|
472
|
-
for (const
|
|
473
|
-
|
|
494
|
+
for (const _a of queries) {
|
|
495
|
+
const _b = _a, { then } = _b, query = __objRest(_b, ["then"]);
|
|
496
|
+
const result = await migration.adapter.arrays(query);
|
|
497
|
+
then == null ? void 0 : then(result);
|
|
474
498
|
}
|
|
475
499
|
await runCodeUpdater(migration, ast);
|
|
476
500
|
};
|
|
@@ -508,23 +532,31 @@ const validatePrimaryKey = (ast) => {
|
|
|
508
532
|
}
|
|
509
533
|
}
|
|
510
534
|
if (!hasPrimaryKey) {
|
|
511
|
-
const
|
|
535
|
+
const error = new NoPrimaryKey(
|
|
536
|
+
`Table ${ast.name} has no primary key.
|
|
537
|
+
You can suppress this error by setting { noPrimaryKey: true } after a table name.`
|
|
538
|
+
);
|
|
512
539
|
if (ast.noPrimaryKey === "error") {
|
|
513
|
-
throw
|
|
540
|
+
throw error;
|
|
514
541
|
} else {
|
|
515
|
-
console.warn(message);
|
|
542
|
+
console.warn(error.message);
|
|
516
543
|
}
|
|
517
544
|
}
|
|
518
545
|
}
|
|
519
546
|
};
|
|
520
547
|
const astToQueries$1 = (ast) => {
|
|
548
|
+
const queries = [];
|
|
549
|
+
for (const key in ast.shape) {
|
|
550
|
+
const item = ast.shape[key];
|
|
551
|
+
if (!(item instanceof EnumColumn))
|
|
552
|
+
continue;
|
|
553
|
+
queries.push(makePopulateEnumQuery(item));
|
|
554
|
+
}
|
|
521
555
|
if (ast.action === "drop") {
|
|
522
|
-
|
|
523
|
-
{
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
}
|
|
527
|
-
];
|
|
556
|
+
queries.push({
|
|
557
|
+
text: `DROP TABLE ${quoteWithSchema(ast)}${ast.dropMode ? ` ${ast.dropMode}` : ""}`
|
|
558
|
+
});
|
|
559
|
+
return queries;
|
|
528
560
|
}
|
|
529
561
|
const lines = [];
|
|
530
562
|
const values = [];
|
|
@@ -546,7 +578,7 @@ const astToQueries$1 = (ast) => {
|
|
|
546
578
|
${constraintToSql(ast, true, foreignKey)}`);
|
|
547
579
|
});
|
|
548
580
|
indexes.push(...ast.indexes);
|
|
549
|
-
|
|
581
|
+
queries.push(
|
|
550
582
|
{
|
|
551
583
|
text: `CREATE TABLE ${quoteWithSchema(ast)} (${lines.join(",")}
|
|
552
584
|
)`,
|
|
@@ -554,14 +586,13 @@ const astToQueries$1 = (ast) => {
|
|
|
554
586
|
},
|
|
555
587
|
...indexesToQuery(true, ast, indexes),
|
|
556
588
|
...commentsToQuery(ast, comments)
|
|
557
|
-
|
|
589
|
+
);
|
|
558
590
|
if (ast.comment) {
|
|
559
|
-
|
|
560
|
-
text: `COMMENT ON TABLE ${quoteWithSchema(ast)} IS ${quote(ast.comment)}
|
|
561
|
-
values: []
|
|
591
|
+
queries.push({
|
|
592
|
+
text: `COMMENT ON TABLE ${quoteWithSchema(ast)} IS ${quote(ast.comment)}`
|
|
562
593
|
});
|
|
563
594
|
}
|
|
564
|
-
return
|
|
595
|
+
return queries;
|
|
565
596
|
};
|
|
566
597
|
|
|
567
598
|
var __defProp$3 = Object.defineProperty;
|
|
@@ -660,8 +691,7 @@ const columnTypeToColumnChange = (item) => {
|
|
|
660
691
|
}
|
|
661
692
|
return item.to;
|
|
662
693
|
};
|
|
663
|
-
const tableChangeMethods = {
|
|
664
|
-
raw,
|
|
694
|
+
const tableChangeMethods = __spreadProps$2(__spreadValues$3({}, tableMethods), {
|
|
665
695
|
add,
|
|
666
696
|
drop,
|
|
667
697
|
change(from, to, options) {
|
|
@@ -694,8 +724,9 @@ const tableChangeMethods = {
|
|
|
694
724
|
rename(name) {
|
|
695
725
|
return { type: "rename", name };
|
|
696
726
|
}
|
|
697
|
-
};
|
|
727
|
+
});
|
|
698
728
|
const changeTable = async (migration, up, tableName, options, fn) => {
|
|
729
|
+
var _a;
|
|
699
730
|
resetTableData();
|
|
700
731
|
resetChangeTableData();
|
|
701
732
|
const tableChanger = Object.create(columnTypes);
|
|
@@ -704,7 +735,8 @@ const changeTable = async (migration, up, tableName, options, fn) => {
|
|
|
704
735
|
const ast = makeAst(up, tableName, changeData, changeTableData, options);
|
|
705
736
|
const queries = astToQueries(ast);
|
|
706
737
|
for (const query of queries) {
|
|
707
|
-
await migration.adapter.
|
|
738
|
+
const result = await migration.adapter.arrays(query);
|
|
739
|
+
(_a = query.then) == null ? void 0 : _a.call(query, result);
|
|
708
740
|
}
|
|
709
741
|
await runCodeUpdater(migration, ast);
|
|
710
742
|
};
|
|
@@ -736,11 +768,10 @@ const makeAst = (up, name, changeData, changeTableData2, options) => {
|
|
|
736
768
|
};
|
|
737
769
|
const astToQueries = (ast) => {
|
|
738
770
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
739
|
-
const
|
|
771
|
+
const queries = [];
|
|
740
772
|
if (ast.comment !== void 0) {
|
|
741
|
-
|
|
742
|
-
text: `COMMENT ON TABLE ${quoteWithSchema(ast)} IS ${quote(ast.comment)}
|
|
743
|
-
values: []
|
|
773
|
+
queries.push({
|
|
774
|
+
text: `COMMENT ON TABLE ${quoteWithSchema(ast)} IS ${quote(ast.comment)}`
|
|
744
775
|
});
|
|
745
776
|
}
|
|
746
777
|
const addPrimaryKeys = ast.add.primaryKey ? __spreadValues$3({}, ast.add.primaryKey) : {
|
|
@@ -751,6 +782,12 @@ const astToQueries = (ast) => {
|
|
|
751
782
|
};
|
|
752
783
|
for (const key in ast.shape) {
|
|
753
784
|
const item = ast.shape[key];
|
|
785
|
+
if ("item" in item) {
|
|
786
|
+
const { item: column } = item;
|
|
787
|
+
if (column instanceof EnumColumn) {
|
|
788
|
+
queries.push(makePopulateEnumQuery(column));
|
|
789
|
+
}
|
|
790
|
+
}
|
|
754
791
|
if (item.type === "add") {
|
|
755
792
|
if (item.item.isPrimaryKey) {
|
|
756
793
|
addPrimaryKeys.columns.push(key);
|
|
@@ -760,6 +797,12 @@ const astToQueries = (ast) => {
|
|
|
760
797
|
dropPrimaryKeys.columns.push(key);
|
|
761
798
|
}
|
|
762
799
|
} else if (item.type === "change") {
|
|
800
|
+
if (item.from.column instanceof EnumColumn) {
|
|
801
|
+
queries.push(makePopulateEnumQuery(item.from.column));
|
|
802
|
+
}
|
|
803
|
+
if (item.to.column instanceof EnumColumn) {
|
|
804
|
+
queries.push(makePopulateEnumQuery(item.to.column));
|
|
805
|
+
}
|
|
763
806
|
if (item.from.primaryKey) {
|
|
764
807
|
dropPrimaryKeys.columns.push(key);
|
|
765
808
|
dropPrimaryKeys.change = true;
|
|
@@ -902,16 +945,16 @@ const astToQueries = (ast) => {
|
|
|
902
945
|
)
|
|
903
946
|
);
|
|
904
947
|
if (alterTable.length) {
|
|
905
|
-
|
|
948
|
+
queries.push({
|
|
906
949
|
text: `ALTER TABLE ${quoteWithSchema(ast)}
|
|
907
950
|
${alterTable.join(",\n ")}`,
|
|
908
951
|
values
|
|
909
952
|
});
|
|
910
953
|
}
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
return
|
|
954
|
+
queries.push(...indexesToQuery(false, ast, dropIndexes));
|
|
955
|
+
queries.push(...indexesToQuery(true, ast, addIndexes));
|
|
956
|
+
queries.push(...commentsToQuery(ast, comments));
|
|
957
|
+
return queries;
|
|
915
958
|
};
|
|
916
959
|
|
|
917
960
|
var __defProp$2 = Object.defineProperty;
|
|
@@ -1051,10 +1094,10 @@ class MigrationBase {
|
|
|
1051
1094
|
return createExtension$1(this, !this.up, name, options);
|
|
1052
1095
|
}
|
|
1053
1096
|
createEnum(name, values, options) {
|
|
1054
|
-
return createEnum(this, this.up, name, values, options);
|
|
1097
|
+
return createEnum$1(this, this.up, name, values, options);
|
|
1055
1098
|
}
|
|
1056
1099
|
dropEnum(name, values, options) {
|
|
1057
|
-
return createEnum(this, !this.up, name, values, options);
|
|
1100
|
+
return createEnum$1(this, !this.up, name, values, options);
|
|
1058
1101
|
}
|
|
1059
1102
|
async tableExists(tableName) {
|
|
1060
1103
|
return queryExists(this, {
|
|
@@ -1131,7 +1174,7 @@ const createExtension$1 = async (migration, up, name, options) => {
|
|
|
1131
1174
|
await migration.adapter.query(query);
|
|
1132
1175
|
await runCodeUpdater(migration, ast);
|
|
1133
1176
|
};
|
|
1134
|
-
const createEnum = async (migration, up, name, values, options = {}) => {
|
|
1177
|
+
const createEnum$1 = async (migration, up, name, values, options = {}) => {
|
|
1135
1178
|
const [schema, enumName] = getSchemaAndTableFromName(name);
|
|
1136
1179
|
const ast = __spreadValues$2({
|
|
1137
1180
|
type: "enum",
|
|
@@ -1140,17 +1183,14 @@ const createEnum = async (migration, up, name, values, options = {}) => {
|
|
|
1140
1183
|
name: enumName,
|
|
1141
1184
|
values
|
|
1142
1185
|
}, options);
|
|
1143
|
-
let
|
|
1186
|
+
let query;
|
|
1144
1187
|
const quotedName = quoteWithSchema(ast);
|
|
1145
1188
|
if (ast.action === "create") {
|
|
1146
|
-
|
|
1189
|
+
query = `CREATE TYPE ${quotedName} AS ENUM (${values.map(quote).join(", ")})`;
|
|
1147
1190
|
} else {
|
|
1148
|
-
|
|
1191
|
+
query = `DROP TYPE${ast.dropIfExists ? " IF EXISTS" : ""} ${quotedName}${ast.cascade ? " CASCADE" : ""}`;
|
|
1149
1192
|
}
|
|
1150
|
-
await migration.adapter.query(
|
|
1151
|
-
text,
|
|
1152
|
-
values
|
|
1153
|
-
});
|
|
1193
|
+
await migration.adapter.query(query);
|
|
1154
1194
|
await runCodeUpdater(migration, ast);
|
|
1155
1195
|
};
|
|
1156
1196
|
const queryExists = (db, sql) => {
|
|
@@ -1248,16 +1288,16 @@ const processMigration = async (db, up, file, config, options, appCodeUpdaterCac
|
|
|
1248
1288
|
options,
|
|
1249
1289
|
appCodeUpdaterCache
|
|
1250
1290
|
);
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
if (callback) {
|
|
1255
|
-
change(callback);
|
|
1256
|
-
} else {
|
|
1291
|
+
clearChanges();
|
|
1292
|
+
let changes = changeCache[file.path];
|
|
1293
|
+
if (!changes) {
|
|
1257
1294
|
await config.import(pathToFileURL(file.path).pathname);
|
|
1258
|
-
|
|
1295
|
+
changes = getCurrentChanges();
|
|
1296
|
+
changeCache[file.path] = changes;
|
|
1297
|
+
}
|
|
1298
|
+
for (const fn of up ? changes : changes.reverse()) {
|
|
1299
|
+
await fn(db2, up);
|
|
1259
1300
|
}
|
|
1260
|
-
await getCurrentPromise();
|
|
1261
1301
|
await (up ? saveMigratedVersion : removeMigratedVersion)(
|
|
1262
1302
|
db2.adapter,
|
|
1263
1303
|
file.version,
|
|
@@ -1742,6 +1782,20 @@ JOIN pg_catalog.pg_namespace n ON n.oid = extnamespace
|
|
|
1742
1782
|
);
|
|
1743
1783
|
return rows;
|
|
1744
1784
|
}
|
|
1785
|
+
async getEnums() {
|
|
1786
|
+
const { rows } = await this.db.query(
|
|
1787
|
+
`SELECT
|
|
1788
|
+
n.nspname as "schemaName",
|
|
1789
|
+
t.typname as name,
|
|
1790
|
+
json_agg(e.enumlabel) as values
|
|
1791
|
+
FROM pg_type t
|
|
1792
|
+
JOIN pg_enum e ON t.oid = e.enumtypid
|
|
1793
|
+
JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
|
|
1794
|
+
WHERE ${filterSchema("n.nspname")}
|
|
1795
|
+
GROUP BY n.nspname, t.typname`
|
|
1796
|
+
);
|
|
1797
|
+
return rows;
|
|
1798
|
+
}
|
|
1745
1799
|
}
|
|
1746
1800
|
|
|
1747
1801
|
var __defProp = Object.defineProperty;
|
|
@@ -1808,6 +1862,24 @@ const structureToAst = async (db) => {
|
|
|
1808
1862
|
}
|
|
1809
1863
|
}
|
|
1810
1864
|
const outerFKeys = [];
|
|
1865
|
+
for (const it of data.extensions) {
|
|
1866
|
+
ast.push({
|
|
1867
|
+
type: "extension",
|
|
1868
|
+
action: "create",
|
|
1869
|
+
name: it.name,
|
|
1870
|
+
schema: it.schemaName === "public" ? void 0 : it.schemaName,
|
|
1871
|
+
version: it.version
|
|
1872
|
+
});
|
|
1873
|
+
}
|
|
1874
|
+
for (const it of data.enums) {
|
|
1875
|
+
ast.push({
|
|
1876
|
+
type: "enum",
|
|
1877
|
+
action: "create",
|
|
1878
|
+
name: it.name,
|
|
1879
|
+
schema: it.schemaName === "public" ? void 0 : it.schemaName,
|
|
1880
|
+
values: it.values
|
|
1881
|
+
});
|
|
1882
|
+
}
|
|
1811
1883
|
for (const key in pendingTables) {
|
|
1812
1884
|
const innerFKeys = [];
|
|
1813
1885
|
const { table } = pendingTables[key];
|
|
@@ -1831,15 +1903,6 @@ const structureToAst = async (db) => {
|
|
|
1831
1903
|
tableName: fkey.tableName
|
|
1832
1904
|
}));
|
|
1833
1905
|
}
|
|
1834
|
-
for (const it of data.extensions) {
|
|
1835
|
-
ast.push({
|
|
1836
|
-
type: "extension",
|
|
1837
|
-
action: "create",
|
|
1838
|
-
name: it.name,
|
|
1839
|
-
schema: it.schemaName === "public" ? void 0 : it.schemaName,
|
|
1840
|
-
version: it.version
|
|
1841
|
-
});
|
|
1842
|
-
}
|
|
1843
1906
|
return ast;
|
|
1844
1907
|
};
|
|
1845
1908
|
const getData = async (db) => {
|
|
@@ -1850,7 +1913,8 @@ const getData = async (db) => {
|
|
|
1850
1913
|
primaryKeys,
|
|
1851
1914
|
indexes,
|
|
1852
1915
|
foreignKeys,
|
|
1853
|
-
extensions
|
|
1916
|
+
extensions,
|
|
1917
|
+
enums
|
|
1854
1918
|
] = await Promise.all([
|
|
1855
1919
|
db.getSchemas(),
|
|
1856
1920
|
db.getTables(),
|
|
@@ -1858,7 +1922,8 @@ const getData = async (db) => {
|
|
|
1858
1922
|
db.getPrimaryKeys(),
|
|
1859
1923
|
db.getIndexes(),
|
|
1860
1924
|
db.getForeignKeys(),
|
|
1861
|
-
db.getExtensions()
|
|
1925
|
+
db.getExtensions(),
|
|
1926
|
+
db.getEnums()
|
|
1862
1927
|
]);
|
|
1863
1928
|
return {
|
|
1864
1929
|
schemas,
|
|
@@ -1867,7 +1932,8 @@ const getData = async (db) => {
|
|
|
1867
1932
|
primaryKeys,
|
|
1868
1933
|
indexes,
|
|
1869
1934
|
foreignKeys,
|
|
1870
|
-
extensions
|
|
1935
|
+
extensions,
|
|
1936
|
+
enums
|
|
1871
1937
|
};
|
|
1872
1938
|
};
|
|
1873
1939
|
const makeBelongsToTable = (schema, table) => (item) => item.schemaName === schema && item.tableName === table;
|
|
@@ -1999,32 +2065,56 @@ const foreignKeyToAst = (fkey) => ({
|
|
|
1999
2065
|
});
|
|
2000
2066
|
|
|
2001
2067
|
const astToMigration = (ast) => {
|
|
2002
|
-
const
|
|
2068
|
+
const first = [];
|
|
2069
|
+
const tables = [];
|
|
2070
|
+
const foreignKeys = [];
|
|
2003
2071
|
for (const item of ast) {
|
|
2004
2072
|
if (item.type === "schema" && item.action === "create") {
|
|
2005
|
-
|
|
2073
|
+
first.push(createSchema(item));
|
|
2006
2074
|
} else if (item.type === "extension" && item.action === "create") {
|
|
2007
|
-
if (
|
|
2008
|
-
|
|
2009
|
-
|
|
2075
|
+
if (first.length)
|
|
2076
|
+
first.push([]);
|
|
2077
|
+
first.push(...createExtension(item));
|
|
2078
|
+
} else if (item.type === "enum" && item.action === "create") {
|
|
2079
|
+
if (first.length)
|
|
2080
|
+
first.push([]);
|
|
2081
|
+
first.push(...createEnum(item));
|
|
2010
2082
|
} else if (item.type === "table" && item.action === "create") {
|
|
2011
|
-
|
|
2012
|
-
code.push([]);
|
|
2013
|
-
code.push(...createTable(item));
|
|
2083
|
+
tables.push(createTable(item));
|
|
2014
2084
|
} else if (item.type === "foreignKey") {
|
|
2015
|
-
if (
|
|
2016
|
-
|
|
2017
|
-
|
|
2085
|
+
if (foreignKeys.length)
|
|
2086
|
+
foreignKeys.push([]);
|
|
2087
|
+
foreignKeys.push(...createForeignKey(item));
|
|
2018
2088
|
}
|
|
2019
2089
|
}
|
|
2020
|
-
if (!
|
|
2090
|
+
if (!first.length && !tables.length && !foreignKeys.length)
|
|
2021
2091
|
return;
|
|
2022
|
-
|
|
2023
|
-
|
|
2092
|
+
let code = `import { change } from 'rake-db';
|
|
2093
|
+
`;
|
|
2094
|
+
if (first.length) {
|
|
2095
|
+
code += `
|
|
2024
2096
|
change(async (db) => {
|
|
2025
|
-
${codeToString(
|
|
2097
|
+
${codeToString(first, " ", " ")}
|
|
2026
2098
|
});
|
|
2027
2099
|
`;
|
|
2100
|
+
}
|
|
2101
|
+
if (tables.length) {
|
|
2102
|
+
for (const table of tables) {
|
|
2103
|
+
code += `
|
|
2104
|
+
change(async (db) => {
|
|
2105
|
+
${codeToString(table, " ", " ")}
|
|
2106
|
+
});
|
|
2107
|
+
`;
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2110
|
+
if (foreignKeys.length) {
|
|
2111
|
+
code += `
|
|
2112
|
+
change(async (db) => {
|
|
2113
|
+
${codeToString(foreignKeys, " ", " ")}
|
|
2114
|
+
});
|
|
2115
|
+
`;
|
|
2116
|
+
}
|
|
2117
|
+
return code;
|
|
2028
2118
|
};
|
|
2029
2119
|
const createSchema = (ast) => {
|
|
2030
2120
|
return `await db.createSchema(${singleQuote(ast.name)});`;
|
|
@@ -2041,7 +2131,19 @@ const createExtension = (ast) => {
|
|
|
2041
2131
|
}
|
|
2042
2132
|
addCode(code, "}");
|
|
2043
2133
|
}
|
|
2044
|
-
addCode(code, ")");
|
|
2134
|
+
addCode(code, ");");
|
|
2135
|
+
return code;
|
|
2136
|
+
};
|
|
2137
|
+
const createEnum = (ast) => {
|
|
2138
|
+
const code = [
|
|
2139
|
+
`await db.createEnum(${singleQuote(ast.name)}, [${ast.values.map(singleQuote).join(", ")}]`
|
|
2140
|
+
];
|
|
2141
|
+
if (ast.schema) {
|
|
2142
|
+
addCode(code, ", {");
|
|
2143
|
+
code.push([`schema: ${singleQuote(ast.schema)},`]);
|
|
2144
|
+
addCode(code, "}");
|
|
2145
|
+
}
|
|
2146
|
+
addCode(code, ");");
|
|
2045
2147
|
return code;
|
|
2046
2148
|
};
|
|
2047
2149
|
const createTable = (ast) => {
|
|
@@ -2108,24 +2210,32 @@ const rakeDb = async (options, partialConfig = {}, args = process.argv.slice(2))
|
|
|
2108
2210
|
var _a;
|
|
2109
2211
|
const config = processRakeDbConfig(partialConfig);
|
|
2110
2212
|
const command = (_a = args[0]) == null ? void 0 : _a.split(":")[0];
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2213
|
+
try {
|
|
2214
|
+
if (command === "create") {
|
|
2215
|
+
await createDb(options, config);
|
|
2216
|
+
} else if (command === "drop") {
|
|
2217
|
+
await dropDb(options);
|
|
2218
|
+
} else if (command === "reset") {
|
|
2219
|
+
await resetDb(options, config);
|
|
2220
|
+
} else if (command === "migrate") {
|
|
2221
|
+
await migrate(options, config, args.slice(1));
|
|
2222
|
+
} else if (command === "rollback") {
|
|
2223
|
+
await rollback(options, config, args.slice(1));
|
|
2224
|
+
} else if (command === "g" || command === "generate") {
|
|
2225
|
+
await generate(config, args.slice(1));
|
|
2226
|
+
} else if (command === "pull") {
|
|
2227
|
+
await pullDbStructure(toArray(options)[0], config);
|
|
2228
|
+
} else if (config.commands[command]) {
|
|
2229
|
+
await config.commands[command](toArray(options), config, args.slice(1));
|
|
2230
|
+
} else {
|
|
2231
|
+
printHelp();
|
|
2232
|
+
}
|
|
2233
|
+
} catch (err) {
|
|
2234
|
+
if (err instanceof RakeDbError) {
|
|
2235
|
+
console.error(err.message);
|
|
2236
|
+
process.exit(1);
|
|
2237
|
+
}
|
|
2238
|
+
throw err;
|
|
2129
2239
|
}
|
|
2130
2240
|
};
|
|
2131
2241
|
const printHelp = () => console.log(
|
|
@@ -2166,5 +2276,5 @@ Generate arguments:
|
|
|
2166
2276
|
`
|
|
2167
2277
|
);
|
|
2168
2278
|
|
|
2169
|
-
export { MigrationBase, change, createDb, createMigrationInterface, dropDb, generate, migrate, rakeDb, resetDb, rollback, runCodeUpdater, writeMigrationFile };
|
|
2279
|
+
export { MigrationBase, change, changeCache, createDb, createMigrationInterface, dropDb, generate, migrate, migrateOrRollback, rakeDb, resetDb, rollback, runCodeUpdater, writeMigrationFile };
|
|
2170
2280
|
//# sourceMappingURL=index.mjs.map
|