rake-db 2.35.1 → 2.36.1
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/bun.d.ts +5 -0
- package/dist/bun.js +24 -0
- package/dist/bun.js.map +1 -0
- package/dist/bun.mjs +23 -0
- package/dist/bun.mjs.map +1 -0
- package/dist/index.d.ts +7 -5
- package/dist/index.js +69 -70
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +70 -71
- package/dist/index.mjs.map +1 -1
- package/package.json +15 -3
package/dist/bun.d.ts
ADDED
package/dist/bun.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let pqb_bun = require("pqb/bun");
|
|
3
|
+
let pqb_internal = require("pqb/internal");
|
|
4
|
+
let rake_db = require("rake-db");
|
|
5
|
+
const rakeDb = ((inputConfig, args = process.argv.slice(2)) => {
|
|
6
|
+
if (!("__rakeDbConfig" in inputConfig)) (0, rake_db.incrementIntermediateCaller)();
|
|
7
|
+
const rakeDb = (0, rake_db.rakeDbCliWithAdapter)(inputConfig, args);
|
|
8
|
+
return {
|
|
9
|
+
...rakeDb,
|
|
10
|
+
run(options) {
|
|
11
|
+
const adapters = (0, pqb_internal.toArray)(options).map((config) => {
|
|
12
|
+
return new pqb_internal.AdapterClass({
|
|
13
|
+
driverAdapter: pqb_bun.BunAdapter,
|
|
14
|
+
config
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
return rakeDb.run(adapters);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
(0, rake_db.setRakeDbCliRunFn)(rakeDb);
|
|
22
|
+
exports.rakeDb = rakeDb;
|
|
23
|
+
|
|
24
|
+
//# sourceMappingURL=bun.js.map
|
package/dist/bun.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bun.js","names":["AdapterClass","BunAdapter"],"sources":["../src/adapters/bun.ts"],"sourcesContent":["import { BunAdapter, BunAdapterOptions } from 'pqb/bun';\nimport { AdapterClass, MaybeArray, toArray } from 'pqb/internal';\nimport {\n rakeDbCliWithAdapter,\n RakeDbFn,\n setRakeDbCliRunFn,\n incrementIntermediateCaller,\n} from 'rake-db';\n\nexport const rakeDb = ((inputConfig, args = process.argv.slice(2)) => {\n if (!('__rakeDbConfig' in inputConfig)) {\n incrementIntermediateCaller();\n }\n\n const rakeDb = rakeDbCliWithAdapter(inputConfig, args);\n return {\n ...rakeDb,\n run(options) {\n const adapters = toArray(options).map((config) => {\n return new AdapterClass({\n driverAdapter: BunAdapter,\n config,\n });\n });\n return rakeDb.run(adapters);\n },\n };\n}) as RakeDbFn<MaybeArray<BunAdapterOptions>>;\n\nsetRakeDbCliRunFn(rakeDb);\n"],"mappings":";;;;AASA,MAAa,WAAW,aAAa,OAAO,QAAQ,KAAK,MAAM,EAAE,KAAK;AACpE,KAAI,EAAE,oBAAoB,aACxB,EAAA,GAAA,QAAA,8BAA6B;CAG/B,MAAM,UAAA,GAAA,QAAA,sBAA8B,aAAa,KAAK;AACtD,QAAO;EACL,GAAG;EACH,IAAI,SAAS;GACX,MAAM,YAAA,GAAA,aAAA,SAAmB,QAAQ,CAAC,KAAK,WAAW;AAChD,WAAO,IAAIA,aAAAA,aAAa;KACtB,eAAeC,QAAAA;KACf;KACD,CAAC;KACF;AACF,UAAO,OAAO,IAAI,SAAS;;EAE9B;;+BAGe,OAAO"}
|
package/dist/bun.mjs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { BunAdapter } from "pqb/bun";
|
|
2
|
+
import { AdapterClass, toArray } from "pqb/internal";
|
|
3
|
+
import { incrementIntermediateCaller, rakeDbCliWithAdapter, setRakeDbCliRunFn } from "rake-db";
|
|
4
|
+
const rakeDb = ((inputConfig, args = process.argv.slice(2)) => {
|
|
5
|
+
if (!("__rakeDbConfig" in inputConfig)) incrementIntermediateCaller();
|
|
6
|
+
const rakeDb = rakeDbCliWithAdapter(inputConfig, args);
|
|
7
|
+
return {
|
|
8
|
+
...rakeDb,
|
|
9
|
+
run(options) {
|
|
10
|
+
const adapters = toArray(options).map((config) => {
|
|
11
|
+
return new AdapterClass({
|
|
12
|
+
driverAdapter: BunAdapter,
|
|
13
|
+
config
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
return rakeDb.run(adapters);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
|
+
setRakeDbCliRunFn(rakeDb);
|
|
21
|
+
export { rakeDb };
|
|
22
|
+
|
|
23
|
+
//# sourceMappingURL=bun.mjs.map
|
package/dist/bun.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bun.mjs","names":[],"sources":["../src/adapters/bun.ts"],"sourcesContent":["import { BunAdapter, BunAdapterOptions } from 'pqb/bun';\nimport { AdapterClass, MaybeArray, toArray } from 'pqb/internal';\nimport {\n rakeDbCliWithAdapter,\n RakeDbFn,\n setRakeDbCliRunFn,\n incrementIntermediateCaller,\n} from 'rake-db';\n\nexport const rakeDb = ((inputConfig, args = process.argv.slice(2)) => {\n if (!('__rakeDbConfig' in inputConfig)) {\n incrementIntermediateCaller();\n }\n\n const rakeDb = rakeDbCliWithAdapter(inputConfig, args);\n return {\n ...rakeDb,\n run(options) {\n const adapters = toArray(options).map((config) => {\n return new AdapterClass({\n driverAdapter: BunAdapter,\n config,\n });\n });\n return rakeDb.run(adapters);\n },\n };\n}) as RakeDbFn<MaybeArray<BunAdapterOptions>>;\n\nsetRakeDbCliRunFn(rakeDb);\n"],"mappings":";;;AASA,MAAa,WAAW,aAAa,OAAO,QAAQ,KAAK,MAAM,EAAE,KAAK;AACpE,KAAI,EAAE,oBAAoB,aACxB,8BAA6B;CAG/B,MAAM,SAAS,qBAAqB,aAAa,KAAK;AACtD,QAAO;EACL,GAAG;EACH,IAAI,SAAS;GACX,MAAM,WAAW,QAAQ,QAAQ,CAAC,KAAK,WAAW;AAChD,WAAO,IAAI,aAAa;KACtB,eAAe;KACf;KACD,CAAC;KACF;AACF,UAAO,OAAO,IAAI,SAAS;;EAE9B;;AAGH,kBAAkB,OAAO"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _$pqb_internal0 from "pqb/internal";
|
|
2
|
-
import { Adapter, Column as Column$1, ColumnSchemaConfig, ColumnsByType, ColumnsShape, DbDomainArg, DbResult, DbStructureDomainsMap, DefaultColumnTypes, DefaultPrivileges, DefaultSchemaConfig, EmptyObject, EnumColumn, Grant, MaybeArray, MaybePromise, NoPrimaryKeyOption, NonUniqDataItem, QueryLogObject, QueryLogOptions, QueryLogger, QuerySchema, RawSqlBase, RecordOptionalString, RecordString, RlsPolicy, SearchWeight, TableData, TableDataFn, TableDataItem, TableDataMethods, raw } from "pqb/internal";
|
|
2
|
+
import { Adapter, Column as Column$1, ColumnSchemaConfig, ColumnTypeSchemaArg, ColumnsByType, ColumnsShape, DbDomainArg, DbResult, DbStructureDomainsMap, DefaultColumnTypes, DefaultPrivileges, DefaultSchemaConfig, EmptyObject, EnumColumn, Grant, MaybeArray, MaybePromise, NoPrimaryKeyOption, NonUniqDataItem, QueryLogObject, QueryLogOptions, QueryLogger, QuerySchema, RawSqlBase, RecordOptionalString, RecordString, RlsPolicy, SearchWeight, TableData, TableDataFn, TableDataItem, TableDataMethods, raw } from "pqb/internal";
|
|
3
3
|
import { Db } from "pqb";
|
|
4
4
|
declare namespace DbStructure {
|
|
5
5
|
interface TableNameAndSchemaName {
|
|
@@ -494,7 +494,7 @@ declare namespace RakeDbAst {
|
|
|
494
494
|
export {};
|
|
495
495
|
}
|
|
496
496
|
interface TableMethods {
|
|
497
|
-
enum(name: string): EnumColumn<
|
|
497
|
+
enum(name: string): EnumColumn<ColumnTypeSchemaArg, undefined, [string, ...string[]]>;
|
|
498
498
|
}
|
|
499
499
|
type Add = typeof add;
|
|
500
500
|
declare function add(item: Column$1, options?: {
|
|
@@ -680,6 +680,10 @@ declare class Migration<CT = unknown> {
|
|
|
680
680
|
up: boolean;
|
|
681
681
|
options: RakeDbConfig;
|
|
682
682
|
columnTypes: CT;
|
|
683
|
+
private tableMethods;
|
|
684
|
+
private getTableMethods;
|
|
685
|
+
private tableChangeMethods;
|
|
686
|
+
private getTableChangeMethods;
|
|
683
687
|
/**
|
|
684
688
|
* `createTable` accepts a string for a table name, optional options, and a callback to specify columns.
|
|
685
689
|
*
|
|
@@ -1624,9 +1628,7 @@ interface RakeDbChangeFnConfig {
|
|
|
1624
1628
|
interface MigrationChangeFn<ColumnTypes> {
|
|
1625
1629
|
(fn: ChangeCallback<ColumnTypes>): MigrationChange;
|
|
1626
1630
|
}
|
|
1627
|
-
declare const createMigrationChangeFn: <ColumnTypes
|
|
1628
|
-
columnTypes?: ColumnTypes;
|
|
1629
|
-
}) => MigrationChangeFn<ColumnTypes>;
|
|
1631
|
+
declare const createMigrationChangeFn: <ColumnTypes>(config: RakeDbConfig<ColumnTypes>) => MigrationChangeFn<ColumnTypes>;
|
|
1630
1632
|
interface MigrationChange {
|
|
1631
1633
|
fn: ChangeCallback<unknown>;
|
|
1632
1634
|
config: RakeDbChangeFnConfig;
|
package/dist/index.js
CHANGED
|
@@ -100,11 +100,10 @@ const getCliParam = (args, name) => {
|
|
|
100
100
|
}
|
|
101
101
|
};
|
|
102
102
|
const createMigrationChangeFn = (config) => {
|
|
103
|
-
const conf = config.columnTypes ? config : { columnTypes: (0, pqb_internal.makeColumnTypes)(pqb_internal.defaultSchemaConfig) };
|
|
104
103
|
return (fn) => {
|
|
105
104
|
const change = {
|
|
106
105
|
fn,
|
|
107
|
-
config
|
|
106
|
+
config
|
|
108
107
|
};
|
|
109
108
|
pushChange(change);
|
|
110
109
|
return change;
|
|
@@ -149,9 +148,15 @@ const columnToSql = (schema, name, item, values, hasMultiplePrimaryKeys, snakeCa
|
|
|
149
148
|
}
|
|
150
149
|
return line.join(" ");
|
|
151
150
|
};
|
|
151
|
+
const getDefaultEncode = (column) => {
|
|
152
|
+
return column.data.encode ?? (column.dataType === "jsonb" ? JSON.stringify : void 0);
|
|
153
|
+
};
|
|
152
154
|
const encodeColumnDefault = (def, values, column) => {
|
|
153
155
|
if (def !== void 0 && def !== null && typeof def !== "function") if ((0, pqb_internal.isRawSQL)(def)) return `(${def.toSQL({ values })})`;
|
|
154
|
-
else
|
|
156
|
+
else {
|
|
157
|
+
let encode;
|
|
158
|
+
return (0, pqb_internal.escapeForMigration)(column instanceof pqb_internal.ArrayColumn && Array.isArray(def) ? "{" + ((encode = getDefaultEncode(column.data.item)) ? def.map((x) => encode(x)) : def).join(",") + "}" : column && (encode = getDefaultEncode(column)) ? encode(def) : def);
|
|
159
|
+
}
|
|
155
160
|
return null;
|
|
156
161
|
};
|
|
157
162
|
const identityToSql = (schema, identity) => {
|
|
@@ -393,12 +398,9 @@ const migrationsSchemaTableSql = (adapter, config) => {
|
|
|
393
398
|
const { schema, table } = getMigrationsSchemaAndTable(adapter, config);
|
|
394
399
|
return `${schema ? `"${schema}".` : ""}"${table}"`;
|
|
395
400
|
};
|
|
396
|
-
const tableMethods = { enum(name) {
|
|
397
|
-
return new pqb_internal.EnumColumn(pqb_internal.defaultSchemaConfig, name, [], void 0);
|
|
398
|
-
} };
|
|
399
401
|
var RakeDbError = class extends Error {};
|
|
400
402
|
var NoPrimaryKey = class extends RakeDbError {};
|
|
401
|
-
const createTable$1 = async (migration, up, tableName, first, second, third) => {
|
|
403
|
+
const createTable$1 = async (migration, tableMethods, up, tableName, first, second, third) => {
|
|
402
404
|
let options;
|
|
403
405
|
let fn;
|
|
404
406
|
let dataFn;
|
|
@@ -786,7 +788,7 @@ function exclude(...args) {
|
|
|
786
788
|
options: { ...options }
|
|
787
789
|
} };
|
|
788
790
|
}
|
|
789
|
-
const
|
|
791
|
+
const makeTableChangeMethods = (tableMethods) => ({
|
|
790
792
|
...tableMethods,
|
|
791
793
|
...pqb_internal.tableDataMethods,
|
|
792
794
|
name(name) {
|
|
@@ -846,8 +848,8 @@ const tableChangeMethods = {
|
|
|
846
848
|
name
|
|
847
849
|
};
|
|
848
850
|
}
|
|
849
|
-
};
|
|
850
|
-
const changeTable = async (migration, up, tableName, options, fn) => {
|
|
851
|
+
});
|
|
852
|
+
const changeTable = async (migration, tableChangeMethods, up, tableName, options, fn) => {
|
|
851
853
|
const snakeCase = "snakeCase" in options ? options.snakeCase : migration.options.snakeCase;
|
|
852
854
|
const language = "language" in options ? options.language : migration.options.language;
|
|
853
855
|
(0, pqb_internal.setDefaultLanguage)(language);
|
|
@@ -1602,6 +1604,9 @@ const buildQuery = (ast, privileges, targetSql, grantable, isRevoke) => {
|
|
|
1602
1604
|
const formatRoleSpec = (role) => {
|
|
1603
1605
|
return specialRoleSpecs.has(role) ? role : `"${role}"`;
|
|
1604
1606
|
};
|
|
1607
|
+
const makeTableMethods = (schemaConfig) => ({ enum(name) {
|
|
1608
|
+
return new pqb_internal.EnumColumn(schemaConfig, name, [], void 0);
|
|
1609
|
+
} });
|
|
1605
1610
|
/**
|
|
1606
1611
|
* Creates a new `db` instance that is an instance of `pqb` with mixed in migration methods from the `Migration` class.
|
|
1607
1612
|
* It overrides `query` and `array` db adapter methods to intercept SQL for the logging.
|
|
@@ -1651,15 +1656,25 @@ const createMigrationInterface = (tx, up, config) => {
|
|
|
1651
1656
|
};
|
|
1652
1657
|
};
|
|
1653
1658
|
var Migration = class {
|
|
1659
|
+
getTableMethods() {
|
|
1660
|
+
let { tableMethods } = this;
|
|
1661
|
+
if (!tableMethods) tableMethods = makeTableMethods((0, pqb_internal.defaultSchemaConfig)());
|
|
1662
|
+
return tableMethods;
|
|
1663
|
+
}
|
|
1664
|
+
getTableChangeMethods() {
|
|
1665
|
+
let { tableChangeMethods } = this;
|
|
1666
|
+
if (!tableChangeMethods) tableChangeMethods = makeTableChangeMethods(this.getTableMethods());
|
|
1667
|
+
return tableChangeMethods;
|
|
1668
|
+
}
|
|
1654
1669
|
createTable(tableName, first, second, third) {
|
|
1655
|
-
return createTable$1(this, this.up, tableName, first, second, third);
|
|
1670
|
+
return createTable$1(this, this.getTableMethods(), this.up, tableName, first, second, third);
|
|
1656
1671
|
}
|
|
1657
1672
|
dropTable(tableName, first, second, third) {
|
|
1658
|
-
return createTable$1(this, !this.up, tableName, first, second, third);
|
|
1673
|
+
return createTable$1(this, this.getTableMethods(), !this.up, tableName, first, second, third);
|
|
1659
1674
|
}
|
|
1660
1675
|
changeTable(tableName, cbOrOptions, cb) {
|
|
1661
1676
|
const [fn, options] = typeof cbOrOptions === "function" ? [cbOrOptions, {}] : [cb, cbOrOptions];
|
|
1662
|
-
return changeTable(this, this.up, tableName, options, fn);
|
|
1677
|
+
return changeTable(this, this.getTableChangeMethods(), this.up, tableName, options, fn);
|
|
1663
1678
|
}
|
|
1664
1679
|
/**
|
|
1665
1680
|
* Rename a table:
|
|
@@ -1726,7 +1741,7 @@ var Migration = class {
|
|
|
1726
1741
|
* @param fn - function returning a type of the column
|
|
1727
1742
|
*/
|
|
1728
1743
|
addColumn(tableName, columnName, fn) {
|
|
1729
|
-
return addColumn(this, this.up, tableName, columnName, fn);
|
|
1744
|
+
return addColumn(this, this.getTableChangeMethods(), this.up, tableName, columnName, fn);
|
|
1730
1745
|
}
|
|
1731
1746
|
/**
|
|
1732
1747
|
* Drop the schema, create it on rollback. See {@link addColumn}.
|
|
@@ -1736,7 +1751,7 @@ var Migration = class {
|
|
|
1736
1751
|
* @param fn - function returning a type of the column
|
|
1737
1752
|
*/
|
|
1738
1753
|
dropColumn(tableName, columnName, fn) {
|
|
1739
|
-
return addColumn(this, !this.up, tableName, columnName, fn);
|
|
1754
|
+
return addColumn(this, this.getTableChangeMethods(), !this.up, tableName, columnName, fn);
|
|
1740
1755
|
}
|
|
1741
1756
|
/**
|
|
1742
1757
|
* Add an index to the table on migrating, and remove it on rollback.
|
|
@@ -1764,7 +1779,7 @@ var Migration = class {
|
|
|
1764
1779
|
* @param args - index options
|
|
1765
1780
|
*/
|
|
1766
1781
|
addIndex(tableName, columns, ...args) {
|
|
1767
|
-
return addIndex(this, this.up, tableName, columns, ...args);
|
|
1782
|
+
return addIndex(this, this.getTableChangeMethods(), this.up, tableName, columns, ...args);
|
|
1768
1783
|
}
|
|
1769
1784
|
/**
|
|
1770
1785
|
* Drop the schema, create it on rollback. See {@link addIndex}.
|
|
@@ -1774,7 +1789,7 @@ var Migration = class {
|
|
|
1774
1789
|
* @param args - index options
|
|
1775
1790
|
*/
|
|
1776
1791
|
dropIndex(tableName, columns, ...args) {
|
|
1777
|
-
return addIndex(this, !this.up, tableName, columns, ...args);
|
|
1792
|
+
return addIndex(this, this.getTableChangeMethods(), !this.up, tableName, columns, ...args);
|
|
1778
1793
|
}
|
|
1779
1794
|
/**
|
|
1780
1795
|
* Rename index:
|
|
@@ -1839,7 +1854,7 @@ var Migration = class {
|
|
|
1839
1854
|
* @param options - foreign key options
|
|
1840
1855
|
*/
|
|
1841
1856
|
addForeignKey(tableName, columns, foreignTable, foreignColumns, options) {
|
|
1842
|
-
return addForeignKey(this, this.up, tableName, columns, foreignTable, foreignColumns, options);
|
|
1857
|
+
return addForeignKey(this, this.getTableChangeMethods(), this.up, tableName, columns, foreignTable, foreignColumns, options);
|
|
1843
1858
|
}
|
|
1844
1859
|
/**
|
|
1845
1860
|
* Drop the schema, create it on rollback. See {@link addForeignKey}.
|
|
@@ -1851,7 +1866,7 @@ var Migration = class {
|
|
|
1851
1866
|
* @param options - foreign key options
|
|
1852
1867
|
*/
|
|
1853
1868
|
dropForeignKey(tableName, columns, foreignTable, foreignColumns, options) {
|
|
1854
|
-
return addForeignKey(this, !this.up, tableName, columns, foreignTable, foreignColumns, options);
|
|
1869
|
+
return addForeignKey(this, this.getTableChangeMethods(), !this.up, tableName, columns, foreignTable, foreignColumns, options);
|
|
1855
1870
|
}
|
|
1856
1871
|
/**
|
|
1857
1872
|
* Add a primary key to a table on migrate, and remove it on rollback.
|
|
@@ -1876,7 +1891,7 @@ var Migration = class {
|
|
|
1876
1891
|
* @param name - optionally, set a primary key constraint name
|
|
1877
1892
|
*/
|
|
1878
1893
|
addPrimaryKey(tableName, columns, name) {
|
|
1879
|
-
return addPrimaryKey(this, this.up, tableName, columns, name);
|
|
1894
|
+
return addPrimaryKey(this, this.getTableChangeMethods(), this.up, tableName, columns, name);
|
|
1880
1895
|
}
|
|
1881
1896
|
/**
|
|
1882
1897
|
* Drop the schema, create it on rollback. See {@link addPrimaryKey}.
|
|
@@ -1886,7 +1901,7 @@ var Migration = class {
|
|
|
1886
1901
|
* @param name - optionally, set a primary key constraint name
|
|
1887
1902
|
*/
|
|
1888
1903
|
dropPrimaryKey(tableName, columns, name) {
|
|
1889
|
-
return addPrimaryKey(this, !this.up, tableName, columns, name);
|
|
1904
|
+
return addPrimaryKey(this, this.getTableChangeMethods(), !this.up, tableName, columns, name);
|
|
1890
1905
|
}
|
|
1891
1906
|
/**
|
|
1892
1907
|
* Add or drop a check for multiple columns.
|
|
@@ -1903,7 +1918,7 @@ var Migration = class {
|
|
|
1903
1918
|
* @param check - raw SQL for the check
|
|
1904
1919
|
*/
|
|
1905
1920
|
addCheck(tableName, check) {
|
|
1906
|
-
return addCheck(this, this.up, tableName, check);
|
|
1921
|
+
return addCheck(this, this.getTableChangeMethods(), this.up, tableName, check);
|
|
1907
1922
|
}
|
|
1908
1923
|
/**
|
|
1909
1924
|
* Drop the schema, create it on rollback. See {@link addCheck}.
|
|
@@ -1912,7 +1927,7 @@ var Migration = class {
|
|
|
1912
1927
|
* @param check - raw SQL for the check
|
|
1913
1928
|
*/
|
|
1914
1929
|
dropCheck(tableName, check) {
|
|
1915
|
-
return addCheck(this, !this.up, tableName, check);
|
|
1930
|
+
return addCheck(this, this.getTableChangeMethods(), !this.up, tableName, check);
|
|
1916
1931
|
}
|
|
1917
1932
|
/**
|
|
1918
1933
|
* Rename a table constraint such as a primary key or a database check.
|
|
@@ -2495,10 +2510,7 @@ const wrapWithEnhancingError = async (text, values, promise) => {
|
|
|
2495
2510
|
try {
|
|
2496
2511
|
return await promise;
|
|
2497
2512
|
} catch (err) {
|
|
2498
|
-
if (err && typeof err === "object" && "message" in err && typeof err.message === "string" && (err.constructor.name === "PostgresError" || err.constructor.name === "DatabaseError")) {
|
|
2499
|
-
err.message += `\nSQL: ${text}${values ? `\nVariables: ${JSON.stringify(values)}` : ""}`;
|
|
2500
|
-
throw new err.constructor(err);
|
|
2501
|
-
}
|
|
2513
|
+
if (err && typeof err === "object" && "message" in err && typeof err.message === "string" && (err.constructor.name === "PostgresError" || err.constructor.name === "DatabaseError")) err.message += `\nSQL: ${text}${values ? `\nVariables: ${JSON.stringify(values)}` : ""}`;
|
|
2502
2514
|
throw err;
|
|
2503
2515
|
}
|
|
2504
2516
|
};
|
|
@@ -2531,32 +2543,32 @@ const wrapWithLog = async (log, text, values, fn) => {
|
|
|
2531
2543
|
/**
|
|
2532
2544
|
* See {@link Migration.prototype.addColumn}
|
|
2533
2545
|
*/
|
|
2534
|
-
const addColumn = (migration, up, tableName, columnName, fn) => {
|
|
2535
|
-
return changeTable(migration, up, tableName, {}, (t) => ({ [columnName]: t.add(fn(t)) }));
|
|
2546
|
+
const addColumn = (migration, tableChangeMethods, up, tableName, columnName, fn) => {
|
|
2547
|
+
return changeTable(migration, tableChangeMethods, up, tableName, {}, (t) => ({ [columnName]: t.add(fn(t)) }));
|
|
2536
2548
|
};
|
|
2537
2549
|
/**
|
|
2538
2550
|
* See {@link Migration.prototype.addIndex}
|
|
2539
2551
|
*/
|
|
2540
|
-
const addIndex = (migration, up, tableName, columns, ...args) => {
|
|
2541
|
-
return changeTable(migration, up, tableName, {}, (t) => ({ ...t.add(t.index(columns, ...args)) }));
|
|
2552
|
+
const addIndex = (migration, tableChangeMethods, up, tableName, columns, ...args) => {
|
|
2553
|
+
return changeTable(migration, tableChangeMethods, up, tableName, {}, (t) => ({ ...t.add(t.index(columns, ...args)) }));
|
|
2542
2554
|
};
|
|
2543
2555
|
/**
|
|
2544
2556
|
* See {@link Migration.prototype.addForeignKey}
|
|
2545
2557
|
*/
|
|
2546
|
-
const addForeignKey = (migration, up, tableName, columns, foreignTable, foreignColumns, options) => {
|
|
2547
|
-
return changeTable(migration, up, tableName, {}, (t) => ({ ...t.add(t.foreignKey(columns, foreignTable, foreignColumns, options)) }));
|
|
2558
|
+
const addForeignKey = (migration, tableChangeMethods, up, tableName, columns, foreignTable, foreignColumns, options) => {
|
|
2559
|
+
return changeTable(migration, tableChangeMethods, up, tableName, {}, (t) => ({ ...t.add(t.foreignKey(columns, foreignTable, foreignColumns, options)) }));
|
|
2548
2560
|
};
|
|
2549
2561
|
/**
|
|
2550
2562
|
* See {@link Migration.prototype.addPrimaryKey}
|
|
2551
2563
|
*/
|
|
2552
|
-
const addPrimaryKey = (migration, up, tableName, columns, name) => {
|
|
2553
|
-
return changeTable(migration, up, tableName, {}, (t) => ({ ...t.add(t.primaryKey(columns, name)) }));
|
|
2564
|
+
const addPrimaryKey = (migration, tableChangeMethods, up, tableName, columns, name) => {
|
|
2565
|
+
return changeTable(migration, tableChangeMethods, up, tableName, {}, (t) => ({ ...t.add(t.primaryKey(columns, name)) }));
|
|
2554
2566
|
};
|
|
2555
2567
|
/**
|
|
2556
2568
|
* See {@link Migration.prototype.addCheck}
|
|
2557
2569
|
*/
|
|
2558
|
-
const addCheck = (migration, up, tableName, check) => {
|
|
2559
|
-
return changeTable(migration, up, tableName, {}, (t) => ({ ...t.add(t.check(check)) }));
|
|
2570
|
+
const addCheck = (migration, tableChangeMethods, up, tableName, check) => {
|
|
2571
|
+
return changeTable(migration, tableChangeMethods, up, tableName, {}, (t) => ({ ...t.add(t.check(check)) }));
|
|
2560
2572
|
};
|
|
2561
2573
|
/**
|
|
2562
2574
|
* See {@link Migration.prototype.createSchema}
|
|
@@ -2960,11 +2972,6 @@ To keep using timestamp ids, set \`migrationId\` option of rake-db to 'timestamp
|
|
|
2960
2972
|
function getMigrationVersion(config, name) {
|
|
2961
2973
|
return (config.migrationId === "timestamp" ? name.match(/^(\d{14})(_|\b)/) : name.match(/^(\d{4})(_|\b)/))?.[1];
|
|
2962
2974
|
}
|
|
2963
|
-
function getDigitsPrefix(name) {
|
|
2964
|
-
const value = name.match(/^(\d+)\D/)?.[1];
|
|
2965
|
-
if (!value) throw new Error(`Migration file should be prefixed with a serial number, received ${name}`);
|
|
2966
|
-
return value;
|
|
2967
|
-
}
|
|
2968
2975
|
const getMaybeTransactionAdapter = (db) => "$getAdapter" in db ? db.$getAdapter() : db;
|
|
2969
2976
|
const runSqlInSavePoint = async (db, sql, code) => {
|
|
2970
2977
|
const adapter = getMaybeTransactionAdapter(db);
|
|
@@ -3101,39 +3108,22 @@ const deleteMigratedVersion = async (adapter, version, name, config) => {
|
|
|
3101
3108
|
if ((await adapter.silentArrays(`DELETE FROM ${migrationsSchemaTableSql(adapter, config)} WHERE version = $1 AND name = $2`, [version, name])).rowCount === 0) throw new Error(`Migration ${version}_${name} was not found in db`);
|
|
3102
3109
|
};
|
|
3103
3110
|
var NoMigrationsTableError = class extends Error {};
|
|
3104
|
-
const getMigratedVersionsMap = async (
|
|
3111
|
+
const getMigratedVersionsMap = async (_ctx, adapter, config, renameTo) => {
|
|
3105
3112
|
const table = migrationsSchemaTableSql(adapter, config);
|
|
3106
|
-
const queryVersion = () => adapter.
|
|
3113
|
+
const queryVersion = () => adapter.query(`SELECT * FROM ${table} ORDER BY version`);
|
|
3107
3114
|
let result;
|
|
3108
3115
|
try {
|
|
3109
3116
|
if (adapter.isInTransaction()) result = await adapter.savepoint("check_migrations_table", queryVersion);
|
|
3110
3117
|
else result = await queryVersion();
|
|
3111
3118
|
} catch (err) {
|
|
3112
|
-
if (err.
|
|
3119
|
+
if (err && typeof err === "object" && (0, pqb_internal.getDriverErrorCode)(err) === "42P01") throw new NoMigrationsTableError();
|
|
3113
3120
|
else throw err;
|
|
3114
3121
|
}
|
|
3115
|
-
|
|
3116
|
-
const { migrations } = await getMigrations(ctx, config, true);
|
|
3117
|
-
const map = {};
|
|
3118
|
-
for (const item of migrations) {
|
|
3119
|
-
const name = node_path.default.basename(item.path);
|
|
3120
|
-
map[item.version] = name.slice(getDigitsPrefix(name).length + 1);
|
|
3121
|
-
}
|
|
3122
|
-
for (const row of result.rows) {
|
|
3123
|
-
const [version] = row;
|
|
3124
|
-
const name = map[version];
|
|
3125
|
-
if (!name) throw new Error(`Migration for version ${version} is stored in db but is not found among available migrations`);
|
|
3126
|
-
row[1] = name;
|
|
3127
|
-
}
|
|
3128
|
-
await adapter.arrays(`ALTER TABLE ${table} ADD COLUMN name TEXT`);
|
|
3129
|
-
await Promise.all(result.rows.map(([version, name]) => adapter.arrays(`UPDATE ${table} SET name = $2 WHERE version = $1`, [version, name])));
|
|
3130
|
-
await adapter.arrays(`ALTER TABLE ${table} ALTER COLUMN name SET NOT NULL`);
|
|
3131
|
-
}
|
|
3132
|
-
let versions = Object.fromEntries(result.rows);
|
|
3122
|
+
let versions = Object.fromEntries(result.rows.map(({ version, name }) => [version, name]));
|
|
3133
3123
|
if (renameTo) versions = await renameMigrations(config, adapter, versions, renameTo);
|
|
3134
3124
|
return {
|
|
3135
3125
|
map: versions,
|
|
3136
|
-
sequence: result.rows.map((row) => +row
|
|
3126
|
+
sequence: result.rows.map((row) => +row.version)
|
|
3137
3127
|
};
|
|
3138
3128
|
};
|
|
3139
3129
|
async function renameMigrations(config, trx, versions, renameTo) {
|
|
@@ -3438,7 +3428,7 @@ const getAdapterDatabase$1 = (adapter) => {
|
|
|
3438
3428
|
};
|
|
3439
3429
|
const rakeDbConfigDefaults = {
|
|
3440
3430
|
...migrateConfigDefaults,
|
|
3441
|
-
schemaConfig: pqb_internal.defaultSchemaConfig,
|
|
3431
|
+
schemaConfig: (0, pqb_internal.defaultSchemaConfig)(),
|
|
3442
3432
|
snakeCase: false,
|
|
3443
3433
|
commands: {},
|
|
3444
3434
|
log: true,
|
|
@@ -4751,8 +4741,11 @@ const constraintsSql = `SELECT
|
|
|
4751
4741
|
t.relname AS "tableName",
|
|
4752
4742
|
c.conname AS "name",
|
|
4753
4743
|
(
|
|
4754
|
-
SELECT json_agg(ccu.column_name)
|
|
4744
|
+
SELECT json_agg(ccu.column_name ORDER BY a.attnum)
|
|
4755
4745
|
FROM information_schema.constraint_column_usage ccu
|
|
4746
|
+
JOIN pg_catalog.pg_attribute a
|
|
4747
|
+
ON a.attrelid = c.conrelid
|
|
4748
|
+
AND a.attname = ccu.column_name
|
|
4756
4749
|
WHERE contype = 'p'
|
|
4757
4750
|
AND ccu.constraint_name = c.conname
|
|
4758
4751
|
AND ccu.table_schema = s.nspname
|
|
@@ -4766,15 +4759,21 @@ const constraintsSql = `SELECT
|
|
|
4766
4759
|
ft.relname,
|
|
4767
4760
|
'columns',
|
|
4768
4761
|
(
|
|
4769
|
-
SELECT json_agg(ccu.column_name)
|
|
4762
|
+
SELECT json_agg(ccu.column_name ORDER BY a.attnum)
|
|
4770
4763
|
FROM information_schema.key_column_usage ccu
|
|
4764
|
+
JOIN pg_catalog.pg_attribute a
|
|
4765
|
+
ON a.attrelid = c.conrelid
|
|
4766
|
+
AND a.attname = ccu.column_name
|
|
4771
4767
|
WHERE ccu.constraint_name = c.conname
|
|
4772
4768
|
AND ccu.table_schema = cs.nspname
|
|
4773
4769
|
),
|
|
4774
4770
|
'foreignColumns',
|
|
4775
4771
|
(
|
|
4776
|
-
SELECT json_agg(ccu.column_name)
|
|
4772
|
+
SELECT json_agg(ccu.column_name ORDER BY a.attnum)
|
|
4777
4773
|
FROM information_schema.constraint_column_usage ccu
|
|
4774
|
+
JOIN pg_catalog.pg_attribute a
|
|
4775
|
+
ON a.attrelid = c.confrelid
|
|
4776
|
+
AND a.attname = ccu.column_name
|
|
4778
4777
|
WHERE ccu.constraint_name = c.conname
|
|
4779
4778
|
AND ccu.table_schema = cs.nspname
|
|
4780
4779
|
),
|
|
@@ -5926,10 +5925,10 @@ const makeRakeDbConfig = (config, args) => {
|
|
|
5926
5925
|
if (result.recurrentPath && !node_path.default.isAbsolute(result.recurrentPath)) result.recurrentPath = node_path.default.resolve(result.basePath, result.recurrentPath);
|
|
5927
5926
|
if ("baseTable" in config && config.baseTable) {
|
|
5928
5927
|
const { types } = config.baseTable.prototype;
|
|
5929
|
-
result.columnTypes = types || (0, pqb_internal.makeColumnTypes)(pqb_internal.defaultSchemaConfig);
|
|
5928
|
+
result.columnTypes = types || (0, pqb_internal.makeColumnTypes)((0, pqb_internal.defaultSchemaConfig)());
|
|
5930
5929
|
} else {
|
|
5931
5930
|
const ct = "columnTypes" in config && config.columnTypes;
|
|
5932
|
-
result.columnTypes = (typeof ct === "function" ? ct((0, pqb_internal.makeColumnTypes)(pqb_internal.defaultSchemaConfig)) : ct) || (0, pqb_internal.makeColumnTypes)(pqb_internal.defaultSchemaConfig);
|
|
5931
|
+
result.columnTypes = (typeof ct === "function" ? ct((0, pqb_internal.makeColumnTypes)((0, pqb_internal.defaultSchemaConfig)())) : ct) || (0, pqb_internal.makeColumnTypes)((0, pqb_internal.defaultSchemaConfig)());
|
|
5933
5932
|
}
|
|
5934
5933
|
if (config.migrationId === "serial") result.migrationId = { serial: 4 };
|
|
5935
5934
|
const transaction = getCliParam(args, "transaction");
|