rake-db 2.4.1 → 2.4.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 +11 -315
- package/dist/index.js +40 -15
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +41 -16
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { quote, getRaw, EnumColumn, columnTypes, getColumnTypes, getTableData, ColumnType, resetTableData, TransactionAdapter, logParamToLogObject, createDb as createDb$1, Adapter, columnsByType, instantiateColumn, codeToString, primaryKeyToCode, indexToCode, foreignKeyToCode, TimestampColumn, foreignKeyArgsToCode } from 'pqb';
|
|
2
|
-
import { singleQuote, isRaw, toArray, emptyObject, addCode, quoteObjectKey } from 'orchid-core';
|
|
2
|
+
import { singleQuote, isRaw, toArray, snakeCaseKey, nameKey, emptyObject, addCode, quoteObjectKey } from 'orchid-core';
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import { readdir, mkdir, writeFile } from 'fs/promises';
|
|
5
5
|
import prompts from 'prompts';
|
|
@@ -27,6 +27,7 @@ var __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
|
|
|
27
27
|
const migrationConfigDefaults = {
|
|
28
28
|
migrationsPath: path.join("src", "db", "migrations"),
|
|
29
29
|
migrationsTable: "schemaMigrations",
|
|
30
|
+
snakeCase: false,
|
|
30
31
|
commands: {},
|
|
31
32
|
import: (path2) => import(path2),
|
|
32
33
|
log: true,
|
|
@@ -268,7 +269,7 @@ var __spreadValues$5 = (a, b) => {
|
|
|
268
269
|
};
|
|
269
270
|
var __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b));
|
|
270
271
|
const columnToSql = (key, item, values, hasMultiplePrimaryKeys) => {
|
|
271
|
-
const line = [`"${key}" ${item.toSQL()}`];
|
|
272
|
+
const line = [`"${item.data.name || key}" ${item.toSQL()}`];
|
|
272
273
|
if (item.data.compression) {
|
|
273
274
|
line.push(`COMPRESSION ${item.data.compression}`);
|
|
274
275
|
}
|
|
@@ -481,6 +482,7 @@ var __objRest = (source, exclude) => {
|
|
|
481
482
|
};
|
|
482
483
|
const types = Object.assign(Object.create(columnTypes), tableMethods);
|
|
483
484
|
const createTable$1 = async (migration, up, tableName, options, fn) => {
|
|
485
|
+
types[snakeCaseKey] = migration.options.snakeCase;
|
|
484
486
|
const shape = getColumnTypes(types, fn);
|
|
485
487
|
const tableData = getTableData();
|
|
486
488
|
const ast = makeAst$1(
|
|
@@ -640,7 +642,14 @@ const mergeTableData = (a, b) => {
|
|
|
640
642
|
};
|
|
641
643
|
function add(item, options) {
|
|
642
644
|
if (item instanceof ColumnType) {
|
|
643
|
-
|
|
645
|
+
if (this[nameKey]) {
|
|
646
|
+
item.data.name = this[nameKey];
|
|
647
|
+
}
|
|
648
|
+
return {
|
|
649
|
+
type: "add",
|
|
650
|
+
item,
|
|
651
|
+
dropMode: options == null ? void 0 : options.dropMode
|
|
652
|
+
};
|
|
644
653
|
} else if (item === emptyObject) {
|
|
645
654
|
mergeTableData(changeTableData.add, getTableData());
|
|
646
655
|
resetTableData();
|
|
@@ -657,9 +666,16 @@ function add(item, options) {
|
|
|
657
666
|
return result;
|
|
658
667
|
}
|
|
659
668
|
}
|
|
660
|
-
const drop = (item, options)
|
|
669
|
+
const drop = function(item, options) {
|
|
661
670
|
if (item instanceof ColumnType) {
|
|
662
|
-
|
|
671
|
+
if (this[nameKey]) {
|
|
672
|
+
item.data.name = this[nameKey];
|
|
673
|
+
}
|
|
674
|
+
return {
|
|
675
|
+
type: "drop",
|
|
676
|
+
item,
|
|
677
|
+
dropMode: options == null ? void 0 : options.dropMode
|
|
678
|
+
};
|
|
663
679
|
} else if (item === emptyObject) {
|
|
664
680
|
mergeTableData(changeTableData.drop, getTableData());
|
|
665
681
|
resetTableData();
|
|
@@ -699,6 +715,7 @@ const tableChangeMethods = __spreadProps$2(__spreadValues$3({}, tableMethods), {
|
|
|
699
715
|
change(from, to, options) {
|
|
700
716
|
return __spreadValues$3({
|
|
701
717
|
type: "change",
|
|
718
|
+
name: this[nameKey],
|
|
702
719
|
from: columnTypeToColumnChange(from),
|
|
703
720
|
to: columnTypeToColumnChange(to)
|
|
704
721
|
}, options);
|
|
@@ -733,6 +750,7 @@ const changeTable = async (migration, up, tableName, options, fn) => {
|
|
|
733
750
|
resetChangeTableData();
|
|
734
751
|
const tableChanger = Object.create(columnTypes);
|
|
735
752
|
Object.assign(tableChanger, tableChangeMethods);
|
|
753
|
+
tableChanger[snakeCaseKey] = migration.options.snakeCase;
|
|
736
754
|
const changeData = (fn == null ? void 0 : fn(tableChanger)) || {};
|
|
737
755
|
const ast = makeAst(up, tableName, changeData, changeTableData, options);
|
|
738
756
|
const queries = astToQueries(ast);
|
|
@@ -838,28 +856,28 @@ const astToQueries = (ast) => {
|
|
|
838
856
|
} else if (item.type === "drop") {
|
|
839
857
|
addColumnIndex(dropIndexes, key, item.item);
|
|
840
858
|
alterTable.push(
|
|
841
|
-
`DROP COLUMN "${key}"${item.dropMode ? ` ${item.dropMode}` : ""}`
|
|
859
|
+
`DROP COLUMN "${item.item.data.name || key}"${item.dropMode ? ` ${item.dropMode}` : ""}`
|
|
842
860
|
);
|
|
843
861
|
} else if (item.type === "change") {
|
|
844
862
|
const { from, to } = item;
|
|
845
863
|
if (from.type !== to.type || from.collate !== to.collate) {
|
|
846
864
|
alterTable.push(
|
|
847
|
-
`ALTER COLUMN "${key}" TYPE ${to.type}${to.collate ? ` COLLATE ${quote(to.collate)}` : ""}${item.using ? ` USING ${getRaw(item.using, values)}` : ""}`
|
|
865
|
+
`ALTER COLUMN "${item.name || key}" TYPE ${to.type}${to.collate ? ` COLLATE ${quote(to.collate)}` : ""}${item.using ? ` USING ${getRaw(item.using, values)}` : ""}`
|
|
848
866
|
);
|
|
849
867
|
}
|
|
850
868
|
if (from.default !== to.default) {
|
|
851
869
|
const value = typeof to.default === "object" && to.default && isRaw(to.default) ? getRaw(to.default, values) : quote(to.default);
|
|
852
870
|
const expr = value === void 0 ? "DROP DEFAULT" : `SET DEFAULT ${value}`;
|
|
853
|
-
alterTable.push(`ALTER COLUMN "${key}" ${expr}`);
|
|
871
|
+
alterTable.push(`ALTER COLUMN "${item.name || key}" ${expr}`);
|
|
854
872
|
}
|
|
855
873
|
if (from.nullable !== to.nullable) {
|
|
856
874
|
alterTable.push(
|
|
857
|
-
`ALTER COLUMN "${key}" ${to.nullable ? "DROP" : "SET"} NOT NULL`
|
|
875
|
+
`ALTER COLUMN "${item.name || key}" ${to.nullable ? "DROP" : "SET"} NOT NULL`
|
|
858
876
|
);
|
|
859
877
|
}
|
|
860
878
|
if (from.compression !== to.compression) {
|
|
861
879
|
alterTable.push(
|
|
862
|
-
`ALTER COLUMN "${key}" SET COMPRESSION ${to.compression || "DEFAULT"}`
|
|
880
|
+
`ALTER COLUMN "${item.name || key}" SET COMPRESSION ${to.compression || "DEFAULT"}`
|
|
863
881
|
);
|
|
864
882
|
}
|
|
865
883
|
const foreignKeysLen = Math.max(
|
|
@@ -2019,6 +2037,7 @@ const pushTableAst = (ast, data, table, pendingTables, innerFKeys = data.foreign
|
|
|
2019
2037
|
}
|
|
2020
2038
|
);
|
|
2021
2039
|
}
|
|
2040
|
+
delete column.data.name;
|
|
2022
2041
|
shape[item.name] = column;
|
|
2023
2042
|
}
|
|
2024
2043
|
ast.push({
|
|
@@ -2072,7 +2091,7 @@ const foreignKeyToAst = (fkey) => ({
|
|
|
2072
2091
|
}
|
|
2073
2092
|
});
|
|
2074
2093
|
|
|
2075
|
-
const astToMigration = (ast) => {
|
|
2094
|
+
const astToMigration = (config, ast) => {
|
|
2076
2095
|
const first = [];
|
|
2077
2096
|
const tables = [];
|
|
2078
2097
|
const foreignKeys = [];
|
|
@@ -2088,7 +2107,7 @@ const astToMigration = (ast) => {
|
|
|
2088
2107
|
first.push([]);
|
|
2089
2108
|
first.push(...createEnum(item));
|
|
2090
2109
|
} else if (item.type === "table" && item.action === "create") {
|
|
2091
|
-
tables.push(createTable(item));
|
|
2110
|
+
tables.push(createTable(config, item));
|
|
2092
2111
|
} else if (item.type === "foreignKey") {
|
|
2093
2112
|
if (foreignKeys.length)
|
|
2094
2113
|
foreignKeys.push([]);
|
|
@@ -2154,13 +2173,16 @@ const createEnum = (ast) => {
|
|
|
2154
2173
|
addCode(code, ");");
|
|
2155
2174
|
return code;
|
|
2156
2175
|
};
|
|
2157
|
-
const createTable = (ast) => {
|
|
2176
|
+
const createTable = (config, ast) => {
|
|
2158
2177
|
const code = [];
|
|
2159
2178
|
addCode(code, `await db.createTable(${quoteSchemaTable(ast)}, (t) => ({`);
|
|
2160
|
-
const hasTimestamps = isTimestamp(ast.shape.createdAt) && isTimestamp(ast.shape.updatedAt);
|
|
2179
|
+
const hasTimestamps = !config.snakeCase && isTimestamp(ast.shape.createdAt) && isTimestamp(ast.shape.updatedAt);
|
|
2180
|
+
const hasTimestampsSnake = isTimestamp(ast.shape.created_at) && isTimestamp(ast.shape.updated_at);
|
|
2161
2181
|
for (const key in ast.shape) {
|
|
2162
2182
|
if (hasTimestamps && (key === "createdAt" || key === "updatedAt"))
|
|
2163
2183
|
continue;
|
|
2184
|
+
if (hasTimestampsSnake && (key === "created_at" || key === "updated_at"))
|
|
2185
|
+
continue;
|
|
2164
2186
|
const line = [`${quoteObjectKey(key)}: `];
|
|
2165
2187
|
for (const part of ast.shape[key].toCode("t")) {
|
|
2166
2188
|
addCode(line, part);
|
|
@@ -2168,9 +2190,12 @@ const createTable = (ast) => {
|
|
|
2168
2190
|
addCode(line, ",");
|
|
2169
2191
|
code.push(line);
|
|
2170
2192
|
}
|
|
2171
|
-
if (hasTimestamps) {
|
|
2193
|
+
if (hasTimestamps || config.snakeCase && hasTimestampsSnake) {
|
|
2172
2194
|
code.push(["...t.timestamps(),"]);
|
|
2173
2195
|
}
|
|
2196
|
+
if (hasTimestampsSnake && !config.snakeCase) {
|
|
2197
|
+
code.push(["...t.timestampsSnakeCase(),"]);
|
|
2198
|
+
}
|
|
2174
2199
|
if (ast.primaryKey) {
|
|
2175
2200
|
code.push([primaryKeyToCode(ast.primaryKey, "t")]);
|
|
2176
2201
|
}
|
|
@@ -2208,7 +2233,7 @@ const pullDbStructure = async (options, config) => {
|
|
|
2208
2233
|
const db = new DbStructure(adapter);
|
|
2209
2234
|
const ast = await structureToAst(db);
|
|
2210
2235
|
await adapter.close();
|
|
2211
|
-
const result = astToMigration(ast);
|
|
2236
|
+
const result = astToMigration(config, ast);
|
|
2212
2237
|
if (!result)
|
|
2213
2238
|
return;
|
|
2214
2239
|
await writeMigrationFile(config, "pull", result);
|