rake-db 2.2.6 → 2.3.0
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 +13 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.esm.js +82 -64
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +82 -64
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/ast.ts +3 -3
- package/src/migration/changeTable.test.ts +10 -15
- package/src/migration/changeTable.ts +90 -77
- package/src/migration/createTable.test.ts +2 -3
- package/src/migration/migrationUtils.ts +28 -22
- package/src/pull/dbStructure.test.ts +14 -6
- package/src/pull/dbStructure.ts +167 -47
- package/src/pull/structureToAst.test.ts +203 -35
- package/src/pull/structureToAst.ts +69 -18
package/dist/index.js
CHANGED
|
@@ -251,26 +251,28 @@ const columnToSql = (key, item, values, hasMultiplePrimaryKeys) => {
|
|
|
251
251
|
line.push(`DEFAULT ${pqb.quote(item.data.default)}`);
|
|
252
252
|
}
|
|
253
253
|
}
|
|
254
|
-
const {
|
|
255
|
-
if (
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
254
|
+
const { foreignKeys } = item.data;
|
|
255
|
+
if (foreignKeys) {
|
|
256
|
+
for (const foreignKey of foreignKeys) {
|
|
257
|
+
const [schema, table] = getForeignKeyTable(
|
|
258
|
+
"fn" in foreignKey ? foreignKey.fn : foreignKey.table
|
|
259
|
+
);
|
|
260
|
+
if (foreignKey.name) {
|
|
261
|
+
line.push(`CONSTRAINT "${foreignKey.name}"`);
|
|
262
|
+
}
|
|
263
|
+
line.push(referencesToSql(schema, table, foreignKey.columns, foreignKey));
|
|
261
264
|
}
|
|
262
|
-
line.push(referencesToSql(schema, table, foreignKey.columns, foreignKey));
|
|
263
265
|
}
|
|
264
266
|
return line.join(" ");
|
|
265
267
|
};
|
|
266
268
|
const addColumnIndex = (indexes, key, item) => {
|
|
267
|
-
if (item.data) {
|
|
268
|
-
|
|
269
|
-
indexes.
|
|
270
|
-
columns: [__spreadProps$4(__spreadValues$5({},
|
|
271
|
-
options:
|
|
272
|
-
})
|
|
273
|
-
|
|
269
|
+
if (item.data.indexes) {
|
|
270
|
+
indexes.push(
|
|
271
|
+
...item.data.indexes.map((index) => ({
|
|
272
|
+
columns: [__spreadProps$4(__spreadValues$5({}, index), { column: key })],
|
|
273
|
+
options: index
|
|
274
|
+
}))
|
|
275
|
+
);
|
|
274
276
|
}
|
|
275
277
|
};
|
|
276
278
|
const addColumnComment = (comments, key, item) => {
|
|
@@ -320,7 +322,11 @@ const referencesToSql = (schema, table, columns, foreignKey) => {
|
|
|
320
322
|
};
|
|
321
323
|
const indexesToQuery = (up, { schema, name }, indexes) => {
|
|
322
324
|
return indexes.map(({ columns, options }) => {
|
|
323
|
-
const indexName = options.name || joinWords(
|
|
325
|
+
const indexName = options.name || joinWords(
|
|
326
|
+
name,
|
|
327
|
+
...columns.filter((it) => "column" in it).map((it) => it.column),
|
|
328
|
+
"index"
|
|
329
|
+
);
|
|
324
330
|
if (!up) {
|
|
325
331
|
return {
|
|
326
332
|
text: `DROP INDEX "${indexName}"${options.dropMode ? ` ${options.dropMode}` : ""}`,
|
|
@@ -339,13 +345,13 @@ const indexesToQuery = (up, { schema, name }, indexes) => {
|
|
|
339
345
|
const columnsSql = [];
|
|
340
346
|
columns.forEach((column) => {
|
|
341
347
|
const columnSql = [
|
|
342
|
-
`"${column.column}"
|
|
348
|
+
"column" in column ? `"${column.column}"` : `(${column.expression})`
|
|
343
349
|
];
|
|
344
350
|
if (column.collate) {
|
|
345
351
|
columnSql.push(`COLLATE '${column.collate}'`);
|
|
346
352
|
}
|
|
347
|
-
if (column.
|
|
348
|
-
columnSql.push(column.
|
|
353
|
+
if (column.opclass) {
|
|
354
|
+
columnSql.push(column.opclass);
|
|
349
355
|
}
|
|
350
356
|
if (column.order) {
|
|
351
357
|
columnSql.push(column.order);
|
|
@@ -621,8 +627,8 @@ const drop = (item, options) => {
|
|
|
621
627
|
};
|
|
622
628
|
const columnTypeToColumnChange = (item) => {
|
|
623
629
|
if (item instanceof pqb.ColumnType) {
|
|
624
|
-
const
|
|
625
|
-
if (
|
|
630
|
+
const foreignKeys = item.data.foreignKeys;
|
|
631
|
+
if (foreignKeys == null ? void 0 : foreignKeys.some((it) => "fn" in it)) {
|
|
626
632
|
throw new Error("Callback in foreignKey is not allowed in migration");
|
|
627
633
|
}
|
|
628
634
|
return __spreadProps$2(__spreadValues$3({
|
|
@@ -631,7 +637,7 @@ const columnTypeToColumnChange = (item) => {
|
|
|
631
637
|
nullable: item.data.isNullable,
|
|
632
638
|
primaryKey: item.isPrimaryKey
|
|
633
639
|
}, item.data), {
|
|
634
|
-
|
|
640
|
+
foreignKeys
|
|
635
641
|
});
|
|
636
642
|
}
|
|
637
643
|
return item.to;
|
|
@@ -711,7 +717,7 @@ const makeAst = (up, name, changeData, changeTableData2, options) => {
|
|
|
711
717
|
}, up ? changeTableData2 : { add: changeTableData2.drop, drop: changeTableData2.add });
|
|
712
718
|
};
|
|
713
719
|
const astToQueries = (ast) => {
|
|
714
|
-
var _a;
|
|
720
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
715
721
|
const result = [];
|
|
716
722
|
if (ast.comment !== void 0) {
|
|
717
723
|
result.push({
|
|
@@ -793,48 +799,60 @@ const astToQueries = (ast) => {
|
|
|
793
799
|
`ALTER COLUMN "${key}" SET COMPRESSION ${to.compression || "DEFAULT"}`
|
|
794
800
|
);
|
|
795
801
|
}
|
|
796
|
-
const
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
802
|
+
const foreignKeysLen = Math.max(
|
|
803
|
+
((_a = from.foreignKeys) == null ? void 0 : _a.length) || 0,
|
|
804
|
+
((_b = to.foreignKeys) == null ? void 0 : _b.length) || 0
|
|
805
|
+
);
|
|
806
|
+
for (let i = 0; i < foreignKeysLen; i++) {
|
|
807
|
+
const fromFkey = (_c = from.foreignKeys) == null ? void 0 : _c[i];
|
|
808
|
+
const toFkey = (_d = to.foreignKeys) == null ? void 0 : _d[i];
|
|
809
|
+
if ((fromFkey || toFkey) && (!fromFkey || !toFkey || fromFkey.name !== toFkey.name || fromFkey.match !== toFkey.match || fromFkey.onUpdate !== toFkey.onUpdate || fromFkey.onDelete !== toFkey.onDelete || fromFkey.dropMode !== toFkey.dropMode || fromFkey.table !== toFkey.table || fromFkey.columns.join(",") !== toFkey.columns.join(","))) {
|
|
810
|
+
if (fromFkey) {
|
|
811
|
+
dropForeignKeys.push({
|
|
812
|
+
columns: [key],
|
|
813
|
+
fnOrTable: fromFkey.table,
|
|
814
|
+
foreignColumns: fromFkey.columns,
|
|
815
|
+
options: fromFkey
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
if (toFkey) {
|
|
819
|
+
addForeignKeys.push({
|
|
820
|
+
columns: [key],
|
|
821
|
+
fnOrTable: toFkey.table,
|
|
822
|
+
foreignColumns: toFkey.columns,
|
|
823
|
+
options: toFkey
|
|
824
|
+
});
|
|
825
|
+
}
|
|
814
826
|
}
|
|
815
827
|
}
|
|
816
|
-
const
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
828
|
+
const indexesLen = Math.max(
|
|
829
|
+
((_e = from.indexes) == null ? void 0 : _e.length) || 0,
|
|
830
|
+
((_f = to.indexes) == null ? void 0 : _f.length) || 0
|
|
831
|
+
);
|
|
832
|
+
for (let i = 0; i < indexesLen; i++) {
|
|
833
|
+
const fromIndex = (_g = from.indexes) == null ? void 0 : _g[i];
|
|
834
|
+
const toIndex = (_h = to.indexes) == null ? void 0 : _h[i];
|
|
835
|
+
if ((fromIndex || toIndex) && (!fromIndex || !toIndex || fromIndex.collate !== toIndex.collate || fromIndex.opclass !== toIndex.opclass || fromIndex.order !== toIndex.order || fromIndex.name !== toIndex.name || fromIndex.unique !== toIndex.unique || fromIndex.using !== toIndex.using || fromIndex.include !== toIndex.include || Array.isArray(fromIndex.include) && Array.isArray(toIndex.include) && fromIndex.include.join(",") !== toIndex.include.join(",") || fromIndex.with !== toIndex.with || fromIndex.tablespace !== toIndex.tablespace || fromIndex.where !== toIndex.where || fromIndex.dropMode !== toIndex.dropMode)) {
|
|
836
|
+
if (fromIndex) {
|
|
837
|
+
dropIndexes.push({
|
|
838
|
+
columns: [
|
|
839
|
+
__spreadValues$3({
|
|
840
|
+
column: key
|
|
841
|
+
}, fromIndex)
|
|
842
|
+
],
|
|
843
|
+
options: fromIndex
|
|
844
|
+
});
|
|
845
|
+
}
|
|
846
|
+
if (toIndex) {
|
|
847
|
+
addIndexes.push({
|
|
848
|
+
columns: [
|
|
849
|
+
__spreadValues$3({
|
|
850
|
+
column: key
|
|
851
|
+
}, toIndex)
|
|
852
|
+
],
|
|
853
|
+
options: toIndex
|
|
854
|
+
});
|
|
855
|
+
}
|
|
838
856
|
}
|
|
839
857
|
}
|
|
840
858
|
if (from.comment !== to.comment) {
|
|
@@ -846,7 +864,7 @@ const astToQueries = (ast) => {
|
|
|
846
864
|
}
|
|
847
865
|
const prependAlterTable = [];
|
|
848
866
|
if (ast.drop.primaryKey || dropPrimaryKeys.change || dropPrimaryKeys.columns.length > 1) {
|
|
849
|
-
const name = ((
|
|
867
|
+
const name = ((_i = dropPrimaryKeys.options) == null ? void 0 : _i.name) || `${ast.name}_pkey`;
|
|
850
868
|
prependAlterTable.push(`DROP CONSTRAINT "${name}"`);
|
|
851
869
|
}
|
|
852
870
|
prependAlterTable.push(
|