rake-db 2.4.44 → 2.6.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/dist/index.d.ts +1 -1
- package/dist/index.js +49 -113
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +50 -114
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { quote, getRaw, EnumColumn, columnTypes, getColumnTypes, getTableData, ColumnType, resetTableData, UnknownColumn, TransactionAdapter, logParamToLogObject, createDb as createDb$1, Adapter, simplifyColumnDefault, columnsByType, instantiateColumn, DomainColumn, CustomTypeColumn, ArrayColumn, getConstraintKind, primaryKeyToCode, indexToCode, constraintToCode, TimestampTzColumn, referencesArgsToCode, constraintPropsToCode } from 'pqb';
|
|
2
|
-
import { singleQuote, toSnakeCase, isRaw, toArray, snakeCaseKey, nameKey, emptyObject, deepCompare, raw, pathToLog, toCamelCase, codeToString, addCode, rawToCode, quoteObjectKey, backtickQuote } from 'orchid-core';
|
|
2
|
+
import { singleQuote, toSnakeCase, isRaw, toArray, snakeCaseKey, nameKey, emptyObject, deepCompare, raw, pathToLog, emptyArray, toCamelCase, codeToString, addCode, rawToCode, quoteObjectKey, backtickQuote } from 'orchid-core';
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import { readdir, mkdir, writeFile } from 'fs/promises';
|
|
5
5
|
import prompts from 'prompts';
|
|
@@ -1662,9 +1662,13 @@ const migrateOrRollback = async (options, config, args, up) => {
|
|
|
1662
1662
|
}
|
|
1663
1663
|
};
|
|
1664
1664
|
const changeCache = {};
|
|
1665
|
+
const begin = {
|
|
1666
|
+
text: "BEGIN",
|
|
1667
|
+
values: emptyArray
|
|
1668
|
+
};
|
|
1665
1669
|
const processMigration = async (db, up, file, config, options, appCodeUpdaterCache) => {
|
|
1666
1670
|
var _a;
|
|
1667
|
-
const asts = await db.transaction(async (tx) => {
|
|
1671
|
+
const asts = await db.transaction(begin, async (tx) => {
|
|
1668
1672
|
const db2 = createMigrationInterface(tx, up, config);
|
|
1669
1673
|
clearChanges();
|
|
1670
1674
|
let changes = changeCache[file.path];
|
|
@@ -1828,17 +1832,11 @@ const writeMigrationFile = async (config, version, name, content) => {
|
|
|
1828
1832
|
await writeFile(filePath, content);
|
|
1829
1833
|
(_a = config.logger) == null ? void 0 : _a.log(`Created ${pathToLog(filePath)}`);
|
|
1830
1834
|
};
|
|
1831
|
-
const generate = async (config,
|
|
1832
|
-
const name = args[0];
|
|
1835
|
+
const generate = async (config, [name]) => {
|
|
1833
1836
|
if (!name)
|
|
1834
1837
|
throw new Error("Migration name is missing");
|
|
1835
1838
|
const version = makeFileTimeStamp();
|
|
1836
|
-
await writeMigrationFile(
|
|
1837
|
-
config,
|
|
1838
|
-
version,
|
|
1839
|
-
name,
|
|
1840
|
-
makeContent(name, args.slice(1))
|
|
1841
|
-
);
|
|
1839
|
+
await writeMigrationFile(config, version, name, makeContent(name));
|
|
1842
1840
|
};
|
|
1843
1841
|
const makeFileTimeStamp = () => {
|
|
1844
1842
|
const now = /* @__PURE__ */ new Date();
|
|
@@ -1851,7 +1849,7 @@ const makeFileTimeStamp = () => {
|
|
|
1851
1849
|
now.getUTCSeconds()
|
|
1852
1850
|
].map((value) => value < 10 ? `0${value}` : value).join("");
|
|
1853
1851
|
};
|
|
1854
|
-
const makeContent = (name
|
|
1852
|
+
const makeContent = (name) => {
|
|
1855
1853
|
let content = `import { change } from 'rake-db';
|
|
1856
1854
|
|
|
1857
1855
|
change(async (db) => {`;
|
|
@@ -1859,42 +1857,27 @@ change(async (db) => {`;
|
|
|
1859
1857
|
if (rest) {
|
|
1860
1858
|
if (first === "create" || first === "drop") {
|
|
1861
1859
|
content += `
|
|
1862
|
-
await db.${first === "create" ? "createTable" : "dropTable"}('${rest}', (t) => ({
|
|
1863
|
-
|
|
1864
|
-
|
|
1860
|
+
await db.${first === "create" ? "createTable" : "dropTable"}('${rest}', (t) => ({
|
|
1861
|
+
|
|
1862
|
+
}));`;
|
|
1865
1863
|
} else if (first === "change") {
|
|
1866
1864
|
content += `
|
|
1867
|
-
await db.changeTable('${rest}', (t) => ({
|
|
1868
|
-
|
|
1865
|
+
await db.changeTable('${rest}', (t) => ({
|
|
1866
|
+
|
|
1867
|
+
}));`;
|
|
1869
1868
|
} else if (first === "add" || first === "remove") {
|
|
1870
1869
|
const table = first === "add" ? getTextAfterTo(rest) : getTextAfterFrom(rest);
|
|
1871
1870
|
content += `
|
|
1872
|
-
await db.changeTable(${table ? `'${table}'` : "tableName"}, (t) => ({
|
|
1873
|
-
|
|
1874
|
-
|
|
1871
|
+
await db.changeTable(${table ? `'${table}'` : "tableName"}, (t) => ({
|
|
1872
|
+
|
|
1873
|
+
}));`;
|
|
1875
1874
|
}
|
|
1876
1875
|
}
|
|
1877
1876
|
return content + "\n});\n";
|
|
1878
1877
|
};
|
|
1879
|
-
const makeColumnsContent = (args, method) => {
|
|
1880
|
-
let content = "";
|
|
1881
|
-
const prepend = method ? `t.${method}(` : "";
|
|
1882
|
-
const append = method ? ")" : "";
|
|
1883
|
-
for (const arg of args) {
|
|
1884
|
-
const [name, def] = arg.split(":");
|
|
1885
|
-
if (!def) {
|
|
1886
|
-
throw new Error(
|
|
1887
|
-
`Column argument should be similar to name:type, name:type.method1.method2, name:type(arg).method(arg). Example: name:varchar(20).nullable. Received: ${arg}`
|
|
1888
|
-
);
|
|
1889
|
-
}
|
|
1890
|
-
const methods = def.split(".").map((method2) => method2.endsWith(")") ? `.${method2}` : `.${method2}()`);
|
|
1891
|
-
content += `
|
|
1892
|
-
${name}: ${prepend}t${methods.join("")}${append},`;
|
|
1893
|
-
}
|
|
1894
|
-
return content;
|
|
1895
|
-
};
|
|
1896
1878
|
|
|
1897
1879
|
const filterSchema = (table) => `${table} !~ '^pg_' AND ${table} != 'information_schema'`;
|
|
1880
|
+
const jsonAgg = (sql2, as) => `(SELECT coalesce(json_agg(t.*), '[]') FROM (${sql2}) t) AS "${as}"`;
|
|
1898
1881
|
const columnsSql = ({
|
|
1899
1882
|
schema,
|
|
1900
1883
|
table,
|
|
@@ -1976,50 +1959,33 @@ WHERE a.attnum > 0
|
|
|
1976
1959
|
AND NOT a.attisdropped
|
|
1977
1960
|
AND ${where}
|
|
1978
1961
|
ORDER BY a.attnum`;
|
|
1979
|
-
|
|
1980
|
-
constructor(db) {
|
|
1981
|
-
this.db = db;
|
|
1982
|
-
}
|
|
1983
|
-
async getSchemas() {
|
|
1984
|
-
const { rows } = await this.db.arrays(
|
|
1985
|
-
`SELECT n.nspname "name"
|
|
1962
|
+
const schemasSql = `SELECT coalesce(json_agg(nspname ORDER BY nspname), '[]')
|
|
1986
1963
|
FROM pg_catalog.pg_namespace n
|
|
1987
|
-
WHERE ${filterSchema("
|
|
1988
|
-
|
|
1989
|
-
);
|
|
1990
|
-
return rows.flat();
|
|
1991
|
-
}
|
|
1992
|
-
async getTables() {
|
|
1993
|
-
const { rows } = await this.db.query(
|
|
1994
|
-
`SELECT
|
|
1964
|
+
WHERE ${filterSchema("nspname")}`;
|
|
1965
|
+
const tablesSql = `SELECT
|
|
1995
1966
|
nspname AS "schemaName",
|
|
1996
1967
|
relname AS "name",
|
|
1997
1968
|
obj_description(c.oid) AS comment,
|
|
1998
1969
|
(SELECT coalesce(json_agg(t), '[]') FROM (${columnsSql({
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
1970
|
+
schema: "n",
|
|
1971
|
+
table: "c",
|
|
1972
|
+
where: "a.attrelid = c.oid"
|
|
1973
|
+
})}) t) AS "columns"
|
|
2003
1974
|
FROM pg_class c
|
|
2004
1975
|
JOIN pg_catalog.pg_namespace n ON n.oid = relnamespace
|
|
2005
1976
|
WHERE relkind = 'r'
|
|
2006
1977
|
AND ${filterSchema("nspname")}
|
|
2007
|
-
ORDER BY relname
|
|
2008
|
-
|
|
2009
|
-
return rows;
|
|
2010
|
-
}
|
|
2011
|
-
async getViews() {
|
|
2012
|
-
const { rows } = await this.db.query(
|
|
2013
|
-
`SELECT
|
|
1978
|
+
ORDER BY relname`;
|
|
1979
|
+
const viewsSql = `SELECT
|
|
2014
1980
|
nc.nspname AS "schemaName",
|
|
2015
1981
|
c.relname AS "name",
|
|
2016
1982
|
right(substring(r.ev_action from ':hasRecursive w'), 1)::bool AS "isRecursive",
|
|
2017
1983
|
array_to_json(c.reloptions) AS "with",
|
|
2018
1984
|
(SELECT coalesce(json_agg(t), '[]') FROM (${columnsSql({
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
1985
|
+
schema: "nc",
|
|
1986
|
+
table: "c",
|
|
1987
|
+
where: "a.attrelid = c.oid"
|
|
1988
|
+
})}) t) AS "columns",
|
|
2023
1989
|
pg_get_viewdef(c.oid) AS "sql"
|
|
2024
1990
|
FROM pg_namespace nc
|
|
2025
1991
|
JOIN pg_class c
|
|
@@ -2028,38 +1994,20 @@ JOIN pg_class c
|
|
|
2028
1994
|
AND c.relpersistence != 't'
|
|
2029
1995
|
JOIN pg_rewrite r ON r.ev_class = c.oid
|
|
2030
1996
|
WHERE ${filterSchema("nc.nspname")}
|
|
2031
|
-
ORDER BY c.relname
|
|
2032
|
-
|
|
2033
|
-
|
|
1997
|
+
ORDER BY c.relname`;
|
|
1998
|
+
const sql = `SELECT (${schemasSql}) AS "schemas", ${jsonAgg(
|
|
1999
|
+
tablesSql,
|
|
2000
|
+
"tables"
|
|
2001
|
+
)}, ${jsonAgg(viewsSql, "views")}`;
|
|
2002
|
+
class DbStructure {
|
|
2003
|
+
constructor(db) {
|
|
2004
|
+
this.db = db;
|
|
2034
2005
|
}
|
|
2035
|
-
async
|
|
2036
|
-
const {
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
proretset AS "returnSet",
|
|
2041
|
-
(
|
|
2042
|
-
SELECT typname FROM pg_type WHERE oid = prorettype
|
|
2043
|
-
) AS "returnType",
|
|
2044
|
-
prokind AS "kind",
|
|
2045
|
-
coalesce((
|
|
2046
|
-
SELECT true FROM information_schema.triggers
|
|
2047
|
-
WHERE n.nspname = trigger_schema AND trigger_name = proname
|
|
2048
|
-
LIMIT 1
|
|
2049
|
-
), false) AS "isTrigger",
|
|
2050
|
-
coalesce((
|
|
2051
|
-
SELECT json_agg(pg_type.typname)
|
|
2052
|
-
FROM unnest(coalesce(proallargtypes, proargtypes)) typeId
|
|
2053
|
-
JOIN pg_type ON pg_type.oid = typeId
|
|
2054
|
-
), '[]') AS "types",
|
|
2055
|
-
coalesce(to_json(proallargtypes::int[]), to_json(proargtypes::int[])) AS "argTypes",
|
|
2056
|
-
coalesce(to_json(proargmodes), '[]') AS "argModes",
|
|
2057
|
-
to_json(proargnames) AS "argNames"
|
|
2058
|
-
FROM pg_proc p
|
|
2059
|
-
JOIN pg_namespace n ON p.pronamespace = n.oid
|
|
2060
|
-
WHERE ${filterSchema("n.nspname")}`
|
|
2061
|
-
);
|
|
2062
|
-
return rows;
|
|
2006
|
+
async getStructure() {
|
|
2007
|
+
const {
|
|
2008
|
+
rows: [structure]
|
|
2009
|
+
} = await this.db.query(sql);
|
|
2010
|
+
return structure;
|
|
2063
2011
|
}
|
|
2064
2012
|
async getIndexes() {
|
|
2065
2013
|
const { rows } = await this.db.query(
|
|
@@ -2469,18 +2417,14 @@ const structureToAst = async (ctx, db) => {
|
|
|
2469
2417
|
};
|
|
2470
2418
|
const getData = async (db) => {
|
|
2471
2419
|
const [
|
|
2472
|
-
schemas,
|
|
2473
|
-
tables,
|
|
2474
|
-
views,
|
|
2420
|
+
{ schemas, tables, views },
|
|
2475
2421
|
constraints,
|
|
2476
2422
|
indexes,
|
|
2477
2423
|
extensions,
|
|
2478
2424
|
enums,
|
|
2479
2425
|
domains
|
|
2480
2426
|
] = await Promise.all([
|
|
2481
|
-
db.
|
|
2482
|
-
db.getTables(),
|
|
2483
|
-
db.getViews(),
|
|
2427
|
+
db.getStructure(),
|
|
2484
2428
|
db.getConstraints(),
|
|
2485
2429
|
db.getIndexes(),
|
|
2486
2430
|
db.getExtensions(),
|
|
@@ -3081,6 +3025,9 @@ const rakeDb = async (options, partialConfig = {}, args = process.argv.slice(2))
|
|
|
3081
3025
|
};
|
|
3082
3026
|
const help = `Usage: rake-db [command] [arguments]
|
|
3083
3027
|
|
|
3028
|
+
See documentation at:
|
|
3029
|
+
https://orchid-orm.netlify.app/guide/migration-commands.html
|
|
3030
|
+
|
|
3084
3031
|
Commands:
|
|
3085
3032
|
create create databases
|
|
3086
3033
|
drop drop databases
|
|
@@ -3103,17 +3050,6 @@ Rollback arguments:
|
|
|
3103
3050
|
Migrate and rollback common arguments:
|
|
3104
3051
|
--code run code updater, overrides \`useCodeUpdater\` option
|
|
3105
3052
|
--code false do not run code updater
|
|
3106
|
-
|
|
3107
|
-
New migration file arguments:
|
|
3108
|
-
- (required) first argument is migration name
|
|
3109
|
-
* create* template for create table
|
|
3110
|
-
* change* template for change table
|
|
3111
|
-
* add*To* template for add columns
|
|
3112
|
-
* remove*From* template for remove columns
|
|
3113
|
-
* drop* template for drop table
|
|
3114
|
-
|
|
3115
|
-
- other arguments considered as columns with types and optional methods:
|
|
3116
|
-
rake-db new createTable id:serial.primaryKey name:text.nullable
|
|
3117
3053
|
`;
|
|
3118
3054
|
|
|
3119
3055
|
export { MigrationBase, change, changeCache, createDb, createMigrationInterface, dropDb, generate, getMigratedVersionsMap, makeFileTimeStamp, migrate, migrateOrRollback, rakeDb, redo, removeMigratedVersion, resetDb, rollback, saveMigratedVersion, writeMigrationFile };
|