rake-db 2.1.0 → 2.1.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.d.ts +2 -2
- package/dist/index.esm.js +142 -54
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +141 -53
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/migration/changeTable.test.ts +169 -2
- package/src/migration/changeTable.ts +141 -13
- package/src/migration/migration.test.ts +1 -1
- package/src/migration/migrationUtils.ts +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as pqb from 'pqb';
|
|
2
|
-
import { QueryLogOptions, MaybeArray, AdapterOptions, ColumnTypes, EmptyObject, RawExpression, ColumnType, raw, ColumnsShape, IndexColumnOptions, IndexOptions, TransactionAdapter, QueryLogObject, QueryResultRow, QueryInput, TypeParsers, QueryResult, QueryArraysResult, ForeignKeyOptions } from 'pqb';
|
|
2
|
+
import { QueryLogOptions, MaybeArray, AdapterOptions, ColumnTypes, EmptyObject, RawExpression, ColumnType, ForeignKey, SingleColumnIndexOptions, raw, ColumnsShape, IndexColumnOptions, IndexOptions, TransactionAdapter, QueryLogObject, QueryResultRow, QueryInput, TypeParsers, QueryResult, QueryArraysResult, ForeignKeyOptions } from 'pqb';
|
|
3
3
|
|
|
4
4
|
declare type MigrationConfig = {
|
|
5
5
|
migrationsPath: string;
|
|
@@ -27,7 +27,7 @@ declare type ChangeOptions = {
|
|
|
27
27
|
usingUp?: RawExpression;
|
|
28
28
|
usingDown?: RawExpression;
|
|
29
29
|
};
|
|
30
|
-
declare type ChangeArg = ColumnType | ['default', unknown | RawExpression] | ['nullable', boolean] | ['comment', string | null];
|
|
30
|
+
declare type ChangeArg = ColumnType | ['default', unknown | RawExpression] | ['nullable', boolean] | ['comment', string | null] | ['compression', string] | ['foreignKey', ForeignKey<string, string[]>] | ['index', Omit<SingleColumnIndexOptions, 'column'>];
|
|
31
31
|
declare type TableChangeMethods = typeof tableChangeMethods;
|
|
32
32
|
declare const tableChangeMethods: {
|
|
33
33
|
raw: (sql: string, ...values: unknown[]) => RawExpression<ColumnType<unknown, pqb.Operators, unknown>>;
|
package/dist/index.esm.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { quote, isRaw, getRaw, toArray, columnTypes, raw, getColumnTypes, getTableData, ColumnType, Operators, resetTableData, emptyObject, TransactionAdapter, logParamToLogObject, Adapter } from 'pqb';
|
|
1
|
+
import { quote, isRaw, getRaw, toArray, columnTypes, raw, getColumnTypes, getTableData, ColumnType, Operators, resetTableData, newTableData, emptyObject, TransactionAdapter, logParamToLogObject, Adapter } from 'pqb';
|
|
2
2
|
import Enquirer from 'enquirer';
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import { readdir, mkdir, writeFile } from 'fs/promises';
|
|
5
5
|
import { singular } from 'pluralize';
|
|
6
6
|
|
|
7
|
-
var __defProp$
|
|
7
|
+
var __defProp$4 = Object.defineProperty;
|
|
8
8
|
var __defProps$3 = Object.defineProperties;
|
|
9
9
|
var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
|
|
10
|
-
var __getOwnPropSymbols$
|
|
11
|
-
var __hasOwnProp$
|
|
12
|
-
var __propIsEnum$
|
|
13
|
-
var __defNormalProp$
|
|
14
|
-
var __spreadValues$
|
|
10
|
+
var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
|
|
11
|
+
var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
|
|
12
|
+
var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
|
|
13
|
+
var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
14
|
+
var __spreadValues$4 = (a, b) => {
|
|
15
15
|
for (var prop in b || (b = {}))
|
|
16
|
-
if (__hasOwnProp$
|
|
17
|
-
__defNormalProp$
|
|
18
|
-
if (__getOwnPropSymbols$
|
|
19
|
-
for (var prop of __getOwnPropSymbols$
|
|
20
|
-
if (__propIsEnum$
|
|
21
|
-
__defNormalProp$
|
|
16
|
+
if (__hasOwnProp$4.call(b, prop))
|
|
17
|
+
__defNormalProp$4(a, prop, b[prop]);
|
|
18
|
+
if (__getOwnPropSymbols$4)
|
|
19
|
+
for (var prop of __getOwnPropSymbols$4(b)) {
|
|
20
|
+
if (__propIsEnum$4.call(b, prop))
|
|
21
|
+
__defNormalProp$4(a, prop, b[prop]);
|
|
22
22
|
}
|
|
23
23
|
return a;
|
|
24
24
|
};
|
|
@@ -31,7 +31,7 @@ const migrationConfigDefaults = {
|
|
|
31
31
|
logger: console
|
|
32
32
|
};
|
|
33
33
|
const getMigrationConfigWithDefaults = (config) => {
|
|
34
|
-
return __spreadValues$
|
|
34
|
+
return __spreadValues$4(__spreadValues$4({}, migrationConfigDefaults), config);
|
|
35
35
|
};
|
|
36
36
|
const getDatabaseAndUserFromOptions = (options) => {
|
|
37
37
|
if (options.connectionString) {
|
|
@@ -59,9 +59,9 @@ const setAdapterOptions = (options, set) => {
|
|
|
59
59
|
if (set.password !== void 0) {
|
|
60
60
|
url.password = set.password;
|
|
61
61
|
}
|
|
62
|
-
return __spreadProps$3(__spreadValues$
|
|
62
|
+
return __spreadProps$3(__spreadValues$4({}, options), { connectionString: url.toString() });
|
|
63
63
|
} else {
|
|
64
|
-
return __spreadValues$
|
|
64
|
+
return __spreadValues$4(__spreadValues$4({}, options), set);
|
|
65
65
|
}
|
|
66
66
|
};
|
|
67
67
|
const askAdminCredentials = async () => {
|
|
@@ -200,21 +200,21 @@ const setCurrentMigrationUp = (up) => {
|
|
|
200
200
|
const getCurrentPromise = () => currentPromise;
|
|
201
201
|
const getCurrentChangeCallback = () => currentChangeCallback;
|
|
202
202
|
|
|
203
|
-
var __defProp$
|
|
203
|
+
var __defProp$3 = Object.defineProperty;
|
|
204
204
|
var __defProps$2 = Object.defineProperties;
|
|
205
205
|
var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
|
|
206
|
-
var __getOwnPropSymbols$
|
|
207
|
-
var __hasOwnProp$
|
|
208
|
-
var __propIsEnum$
|
|
209
|
-
var __defNormalProp$
|
|
210
|
-
var __spreadValues$
|
|
206
|
+
var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
|
|
207
|
+
var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
|
|
208
|
+
var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
|
|
209
|
+
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
210
|
+
var __spreadValues$3 = (a, b) => {
|
|
211
211
|
for (var prop in b || (b = {}))
|
|
212
|
-
if (__hasOwnProp$
|
|
213
|
-
__defNormalProp$
|
|
214
|
-
if (__getOwnPropSymbols$
|
|
215
|
-
for (var prop of __getOwnPropSymbols$
|
|
216
|
-
if (__propIsEnum$
|
|
217
|
-
__defNormalProp$
|
|
212
|
+
if (__hasOwnProp$3.call(b, prop))
|
|
213
|
+
__defNormalProp$3(a, prop, b[prop]);
|
|
214
|
+
if (__getOwnPropSymbols$3)
|
|
215
|
+
for (var prop of __getOwnPropSymbols$3(b)) {
|
|
216
|
+
if (__propIsEnum$3.call(b, prop))
|
|
217
|
+
__defNormalProp$3(a, prop, b[prop]);
|
|
218
218
|
}
|
|
219
219
|
return a;
|
|
220
220
|
};
|
|
@@ -255,7 +255,7 @@ const addColumnIndex = (indexes, key, item) => {
|
|
|
255
255
|
if (item.data) {
|
|
256
256
|
if (item.data.index) {
|
|
257
257
|
indexes.push({
|
|
258
|
-
columns: [__spreadProps$2(__spreadValues$
|
|
258
|
+
columns: [__spreadProps$2(__spreadValues$3({}, item.data.index), { column: key })],
|
|
259
259
|
options: item.data.index
|
|
260
260
|
});
|
|
261
261
|
}
|
|
@@ -274,12 +274,12 @@ const getForeignKeyTable = (fnOrTable) => {
|
|
|
274
274
|
return new klass().table;
|
|
275
275
|
};
|
|
276
276
|
const constraintToSql = (tableName, up, foreignKey) => {
|
|
277
|
-
const
|
|
278
|
-
const constraintName = foreignKey.options.name || joinWords(tableName, "to", table);
|
|
277
|
+
const constraintName = foreignKey.options.name || `${tableName}_${foreignKey.columns.join("_")}_fkey`;
|
|
279
278
|
if (!up) {
|
|
280
279
|
const { dropMode } = foreignKey.options;
|
|
281
280
|
return `CONSTRAINT "${constraintName}"${dropMode ? ` ${dropMode}` : ""}`;
|
|
282
281
|
}
|
|
282
|
+
const table = getForeignKeyTable(foreignKey.fnOrTable);
|
|
283
283
|
return `CONSTRAINT "${constraintName}" FOREIGN KEY (${joinColumns(
|
|
284
284
|
foreignKey.columns
|
|
285
285
|
)}) ${referencesToSql(table, foreignKey.foreignColumns, foreignKey.options)}`;
|
|
@@ -394,21 +394,21 @@ WHERE
|
|
|
394
394
|
return rows;
|
|
395
395
|
};
|
|
396
396
|
|
|
397
|
-
var __defProp$
|
|
397
|
+
var __defProp$2 = Object.defineProperty;
|
|
398
398
|
var __defProps$1 = Object.defineProperties;
|
|
399
399
|
var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
|
|
400
|
-
var __getOwnPropSymbols$
|
|
401
|
-
var __hasOwnProp$
|
|
402
|
-
var __propIsEnum$
|
|
403
|
-
var __defNormalProp$
|
|
404
|
-
var __spreadValues$
|
|
400
|
+
var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
|
|
401
|
+
var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
|
|
402
|
+
var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
|
|
403
|
+
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
404
|
+
var __spreadValues$2 = (a, b) => {
|
|
405
405
|
for (var prop in b || (b = {}))
|
|
406
|
-
if (__hasOwnProp$
|
|
407
|
-
__defNormalProp$
|
|
408
|
-
if (__getOwnPropSymbols$
|
|
409
|
-
for (var prop of __getOwnPropSymbols$
|
|
410
|
-
if (__propIsEnum$
|
|
411
|
-
__defNormalProp$
|
|
406
|
+
if (__hasOwnProp$2.call(b, prop))
|
|
407
|
+
__defNormalProp$2(a, prop, b[prop]);
|
|
408
|
+
if (__getOwnPropSymbols$2)
|
|
409
|
+
for (var prop of __getOwnPropSymbols$2(b)) {
|
|
410
|
+
if (__propIsEnum$2.call(b, prop))
|
|
411
|
+
__defNormalProp$2(a, prop, b[prop]);
|
|
412
412
|
}
|
|
413
413
|
return a;
|
|
414
414
|
};
|
|
@@ -428,7 +428,7 @@ const createJoinTable = async (migration, up, tables, options, fn) => {
|
|
|
428
428
|
const tablesWithPrimaryKeys = await Promise.all(
|
|
429
429
|
tables.map(async (table) => {
|
|
430
430
|
const primaryKeys = await getPrimaryKeysOfTable(migration, table).then(
|
|
431
|
-
(items) => items.map((item) => __spreadProps$1(__spreadValues$
|
|
431
|
+
(items) => items.map((item) => __spreadProps$1(__spreadValues$2({}, item), {
|
|
432
432
|
joinedName: joinWords(singular(table), item.name)
|
|
433
433
|
}))
|
|
434
434
|
);
|
|
@@ -520,6 +520,22 @@ const createTable = async (migration, up, tableName, options, fn) => {
|
|
|
520
520
|
}
|
|
521
521
|
};
|
|
522
522
|
|
|
523
|
+
var __defProp$1 = Object.defineProperty;
|
|
524
|
+
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
|
|
525
|
+
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
|
|
526
|
+
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
|
|
527
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
528
|
+
var __spreadValues$1 = (a, b) => {
|
|
529
|
+
for (var prop in b || (b = {}))
|
|
530
|
+
if (__hasOwnProp$1.call(b, prop))
|
|
531
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
532
|
+
if (__getOwnPropSymbols$1)
|
|
533
|
+
for (var prop of __getOwnPropSymbols$1(b)) {
|
|
534
|
+
if (__propIsEnum$1.call(b, prop))
|
|
535
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
536
|
+
}
|
|
537
|
+
return a;
|
|
538
|
+
};
|
|
523
539
|
const newChangeTableData = () => ({
|
|
524
540
|
add: [],
|
|
525
541
|
drop: []
|
|
@@ -620,11 +636,11 @@ const changeTable = async (migration, up, tableName, options, fn) => {
|
|
|
620
636
|
}
|
|
621
637
|
}
|
|
622
638
|
}
|
|
623
|
-
changeTableData
|
|
624
|
-
handleTableData(state,
|
|
639
|
+
changeTableData[up ? "drop" : "add"].forEach((tableData) => {
|
|
640
|
+
handleTableData(state, false, tableName, tableData);
|
|
625
641
|
});
|
|
626
|
-
changeTableData
|
|
627
|
-
handleTableData(state,
|
|
642
|
+
changeTableData[up ? "add" : "drop"].forEach((tableData) => {
|
|
643
|
+
handleTableData(state, true, tableName, tableData);
|
|
628
644
|
});
|
|
629
645
|
if (state.alterTable.length) {
|
|
630
646
|
await migration.query(
|
|
@@ -632,10 +648,8 @@ const changeTable = async (migration, up, tableName, options, fn) => {
|
|
|
632
648
|
${state.alterTable.join(",\n ")}`
|
|
633
649
|
);
|
|
634
650
|
}
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
await migrateIndexes(state, createIndexes, up);
|
|
638
|
-
await migrateIndexes(state, dropIndexes, !up);
|
|
651
|
+
await migrateIndexes(state, state.dropIndexes, false);
|
|
652
|
+
await migrateIndexes(state, state.indexes, true);
|
|
639
653
|
await migrateComments(state, state.comments);
|
|
640
654
|
};
|
|
641
655
|
const changeActions = {
|
|
@@ -686,6 +700,68 @@ const changeActions = {
|
|
|
686
700
|
`ALTER COLUMN "${key}" ${to.nullable ? "DROP" : "SET"} NOT NULL`
|
|
687
701
|
);
|
|
688
702
|
}
|
|
703
|
+
if (from.compression !== to.compression) {
|
|
704
|
+
state.alterTable.push(
|
|
705
|
+
`ALTER COLUMN "${key}" SET COMPRESSION ${to.compression || "DEFAULT"}`
|
|
706
|
+
);
|
|
707
|
+
}
|
|
708
|
+
const fromFkey = from.foreignKey;
|
|
709
|
+
const toFkey = to.foreignKey;
|
|
710
|
+
if (fromFkey || toFkey) {
|
|
711
|
+
if (fromFkey && "fn" in fromFkey || toFkey && "fn" in toFkey) {
|
|
712
|
+
throw new Error("Callback in foreignKey is not allowed in migration");
|
|
713
|
+
}
|
|
714
|
+
if (checkIfForeignKeysAreDifferent(fromFkey, toFkey)) {
|
|
715
|
+
if (fromFkey) {
|
|
716
|
+
const data = newTableData();
|
|
717
|
+
data.foreignKeys.push({
|
|
718
|
+
columns: [key],
|
|
719
|
+
fnOrTable: fromFkey.table,
|
|
720
|
+
foreignColumns: fromFkey.columns,
|
|
721
|
+
options: fromFkey
|
|
722
|
+
});
|
|
723
|
+
changeTableData[up ? "drop" : "add"].push(data);
|
|
724
|
+
}
|
|
725
|
+
if (toFkey) {
|
|
726
|
+
const data = newTableData();
|
|
727
|
+
data.foreignKeys.push({
|
|
728
|
+
columns: [key],
|
|
729
|
+
fnOrTable: toFkey.table,
|
|
730
|
+
foreignColumns: toFkey.columns,
|
|
731
|
+
options: toFkey
|
|
732
|
+
});
|
|
733
|
+
changeTableData[up ? "add" : "drop"].push(data);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
const fromIndex = from.index;
|
|
738
|
+
const toIndex = to.index;
|
|
739
|
+
if ((fromIndex || toIndex) && checkIfIndexesAreDifferent(fromIndex, toIndex)) {
|
|
740
|
+
if (fromIndex) {
|
|
741
|
+
const data = newTableData();
|
|
742
|
+
data.indexes.push({
|
|
743
|
+
columns: [
|
|
744
|
+
__spreadValues$1({
|
|
745
|
+
column: key
|
|
746
|
+
}, fromIndex)
|
|
747
|
+
],
|
|
748
|
+
options: fromIndex
|
|
749
|
+
});
|
|
750
|
+
changeTableData[up ? "drop" : "add"].push(data);
|
|
751
|
+
}
|
|
752
|
+
if (toIndex) {
|
|
753
|
+
const data = newTableData();
|
|
754
|
+
data.indexes.push({
|
|
755
|
+
columns: [
|
|
756
|
+
__spreadValues$1({
|
|
757
|
+
column: key
|
|
758
|
+
}, toIndex)
|
|
759
|
+
],
|
|
760
|
+
options: toIndex
|
|
761
|
+
});
|
|
762
|
+
changeTableData[up ? "add" : "drop"].push(data);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
689
765
|
if (from.comment !== to.comment) {
|
|
690
766
|
state.comments.push({ column: key, comment: to.comment || null });
|
|
691
767
|
}
|
|
@@ -695,6 +771,12 @@ const changeActions = {
|
|
|
695
771
|
state.alterTable.push(`RENAME COLUMN "${from}" TO "${to}"`);
|
|
696
772
|
}
|
|
697
773
|
};
|
|
774
|
+
const checkIfForeignKeysAreDifferent = (from, to) => {
|
|
775
|
+
return !from || !to || from.name !== to.name || from.match !== to.match || from.onUpdate !== to.onUpdate || from.onDelete !== to.onDelete || from.dropMode !== to.dropMode || from.table !== to.table || from.columns.join(",") !== to.columns.join(",");
|
|
776
|
+
};
|
|
777
|
+
const checkIfIndexesAreDifferent = (from, to) => {
|
|
778
|
+
return !from || !to || from.expression !== to.expression || from.collate !== to.collate || from.operator !== to.operator || from.order !== to.order || from.name !== to.name || from.unique !== to.unique || from.using !== to.using || from.include !== to.include || Array.isArray(from.include) && Array.isArray(to.include) && from.include.join(",") !== to.include.join(",") || from.with !== to.with || from.tablespace !== to.tablespace || from.where !== to.where || from.dropMode !== to.dropMode;
|
|
779
|
+
};
|
|
698
780
|
const getChangeProperties = (item) => {
|
|
699
781
|
if (item instanceof ColumnType) {
|
|
700
782
|
return {
|
|
@@ -702,7 +784,10 @@ const getChangeProperties = (item) => {
|
|
|
702
784
|
collate: item.data.collate,
|
|
703
785
|
default: item.data.default,
|
|
704
786
|
nullable: item.isNullable,
|
|
705
|
-
comment: item.data.comment
|
|
787
|
+
comment: item.data.comment,
|
|
788
|
+
compression: item.data.compression,
|
|
789
|
+
foreignKey: item.data.foreignKey,
|
|
790
|
+
index: item.data.index
|
|
706
791
|
};
|
|
707
792
|
} else {
|
|
708
793
|
return {
|
|
@@ -710,7 +795,10 @@ const getChangeProperties = (item) => {
|
|
|
710
795
|
collate: void 0,
|
|
711
796
|
default: item[0] === "default" ? item[1] : void 0,
|
|
712
797
|
nullable: item[0] === "nullable" ? item[1] : void 0,
|
|
713
|
-
comment: item[0] === "comment" ? item[1] : void 0
|
|
798
|
+
comment: item[0] === "comment" ? item[1] : void 0,
|
|
799
|
+
compression: item[0] === "compression" ? item[1] : void 0,
|
|
800
|
+
foreignKey: item[0] === "foreignKey" ? item[1] : void 0,
|
|
801
|
+
index: item[0] === "index" ? item[1] : void 0
|
|
714
802
|
};
|
|
715
803
|
}
|
|
716
804
|
};
|