rake-db 2.37.0 → 2.37.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/bun.js.map +1 -1
- package/dist/bun.mjs.map +1 -1
- package/dist/index.d.ts +21 -40
- package/dist/index.js +81 -34
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +80 -33
- package/dist/index.mjs.map +1 -1
- package/dist/node-postgres.js.map +1 -1
- package/dist/node-postgres.mjs.map +1 -1
- package/dist/postgres-js.js.map +1 -1
- package/dist/postgres-js.mjs.map +1 -1
- package/package.json +3 -3
package/dist/bun.js.map
CHANGED
|
@@ -1 +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,
|
|
1
|
+
{"version":3,"file":"bun.js","names":["rakeDbCliWithAdapter","toArray","AdapterClass","BunAdapter","setRakeDbCliRunFn"],"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,CAAC,MAAM;CACpE,IAAI,EAAE,oBAAoB,cACxB,CAAA,GAAA,QAAA,4BAAA,CAA4B;CAG9B,MAAM,UAAA,GAASA,QAAAA,qBAAAA,CAAqB,aAAa,IAAI;CACrD,OAAO;EACL,GAAG;EACH,IAAI,SAAS;GACX,MAAM,YAAA,GAAWC,aAAAA,QAAAA,CAAQ,OAAO,CAAC,CAAC,KAAK,WAAW;IAChD,OAAO,IAAIC,aAAAA,aAAa;KACtB,eAAeC,QAAAA;KACf;IACF,CAAC;GACH,CAAC;GACD,OAAO,OAAO,IAAI,QAAQ;EAC5B;CACF;AACF;CAEAC,GAAAA,QAAAA,kBAAAA,CAAkB,MAAM"}
|
package/dist/bun.mjs.map
CHANGED
|
@@ -1 +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,
|
|
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,CAAC,MAAM;CACpE,IAAI,EAAE,oBAAoB,cACxB,4BAA4B;CAG9B,MAAM,SAAS,qBAAqB,aAAa,IAAI;CACrD,OAAO;EACL,GAAG;EACH,IAAI,SAAS;GACX,MAAM,WAAW,QAAQ,OAAO,CAAC,CAAC,KAAK,WAAW;IAChD,OAAO,IAAI,aAAa;KACtB,eAAe;KACf;IACF,CAAC;GACH,CAAC;GACD,OAAO,OAAO,IAAI,QAAQ;EAC5B;CACF;AACF;AAEA,kBAAkB,MAAM"}
|
package/dist/index.d.ts
CHANGED
|
@@ -628,7 +628,7 @@ interface TableChangeMethods extends TableMethods, TableDataMethods<string> {
|
|
|
628
628
|
index(options?: TableData.Index.ColumnArg): ColumnIndexChangeInput;
|
|
629
629
|
unique<Columns extends [string | TableData.Index.ColumnOrExpressionOptions, ...(string | TableData.Index.ColumnOrExpressionOptions)[]], Name extends string>(columns: Columns, options?: TableData.Index.UniqueOptionsArg<Name>): {
|
|
630
630
|
tableDataItem: true;
|
|
631
|
-
columns: Columns extends (string | TableData.Index.ColumnOptionsForColumn<string>)[] ? { [I in keyof Columns]: 'column' extends keyof Columns[I] ? Columns[I]['column'] : Columns[I] } : never;
|
|
631
|
+
columns: Columns extends (string | TableData.Index.ColumnOptionsForColumn<string>)[] ? { [I in keyof Columns]: 'column' extends keyof Columns[I] ? Columns[I]['column'] : Columns[I]; } : never;
|
|
632
632
|
name: string extends Name ? never : Name;
|
|
633
633
|
};
|
|
634
634
|
unique(options?: TableData.Index.UniqueColumnArg): ColumnIndexChangeInput;
|
|
@@ -761,7 +761,7 @@ interface MigrationInterfaceResult {
|
|
|
761
761
|
* @param up - migrate or rollback
|
|
762
762
|
* @param config - config of `rakeDb`
|
|
763
763
|
*/
|
|
764
|
-
declare const createMigrationInterface: (tx: Adapter, up: boolean, config: Pick<RakeDbConfig,
|
|
764
|
+
declare const createMigrationInterface: (tx: Adapter, up: boolean, config: Pick<RakeDbConfig, 'log' | 'logger'>) => MigrationInterfaceResult;
|
|
765
765
|
type MigrationAdapter = Adapter;
|
|
766
766
|
declare class Migration<CT = unknown> {
|
|
767
767
|
adapter: MigrationAdapter;
|
|
@@ -1734,10 +1734,7 @@ interface RakeDbCtx {
|
|
|
1734
1734
|
migrationsPromise?: Promise<MigrationsSet>;
|
|
1735
1735
|
}
|
|
1736
1736
|
declare const getSchemaAndTableFromName: (schema: QuerySchema | undefined, name: string) => [string | undefined, string];
|
|
1737
|
-
declare const concatSchemaAndName: ({
|
|
1738
|
-
schema,
|
|
1739
|
-
name
|
|
1740
|
-
}: {
|
|
1737
|
+
declare const concatSchemaAndName: ({ schema, name }: {
|
|
1741
1738
|
schema?: string;
|
|
1742
1739
|
name: string;
|
|
1743
1740
|
}, excludeCurrentSchema?: string) => string;
|
|
@@ -1764,7 +1761,7 @@ interface OrmParam {
|
|
|
1764
1761
|
$getAdapter(): Adapter;
|
|
1765
1762
|
}
|
|
1766
1763
|
type DbParam = OrmParam | Adapter;
|
|
1767
|
-
declare const saveMigratedVersion: (db: SilentQueries, version: string, name: string, config: Pick<RakeDbConfig,
|
|
1764
|
+
declare const saveMigratedVersion: (db: SilentQueries, version: string, name: string, config: Pick<RakeDbConfig, 'migrationsTable'>) => Promise<void>;
|
|
1768
1765
|
declare const createMigrationsSchemaAndTable: (db: DbParam, config: {
|
|
1769
1766
|
migrationsTable: string;
|
|
1770
1767
|
logger?: QueryLogger;
|
|
@@ -2019,17 +2016,17 @@ interface RakeDbConfig<ColumnTypes = unknown> extends QueryLogOptions {
|
|
|
2019
2016
|
commands: RakeDbCommands;
|
|
2020
2017
|
}
|
|
2021
2018
|
declare const rakeDbConfigDefaults: {
|
|
2019
|
+
migrationId: {
|
|
2020
|
+
serial: number;
|
|
2021
|
+
};
|
|
2022
|
+
migrationsTable: string;
|
|
2023
|
+
transaction: string;
|
|
2022
2024
|
schemaConfig: DefaultSchemaConfig;
|
|
2023
2025
|
snakeCase: boolean;
|
|
2024
2026
|
commands: {};
|
|
2025
2027
|
log: boolean;
|
|
2026
2028
|
logger: Console;
|
|
2027
2029
|
import(): never;
|
|
2028
|
-
migrationId: {
|
|
2029
|
-
serial: number;
|
|
2030
|
-
};
|
|
2031
|
-
migrationsTable: string;
|
|
2032
|
-
transaction: string;
|
|
2033
2030
|
};
|
|
2034
2031
|
interface RakeDbCommandFn {
|
|
2035
2032
|
(adapters: Adapter[], config: RakeDbConfig, args: string[]): MaybePromise<unknown>;
|
|
@@ -2178,12 +2175,7 @@ declare const getMigrationsSchemaAndTable: (adapter: Adapter, config: {
|
|
|
2178
2175
|
schema?: string;
|
|
2179
2176
|
table: string;
|
|
2180
2177
|
};
|
|
2181
|
-
declare const promptSelect: ({
|
|
2182
|
-
message,
|
|
2183
|
-
options,
|
|
2184
|
-
active,
|
|
2185
|
-
inactive
|
|
2186
|
-
}: {
|
|
2178
|
+
declare const promptSelect: ({ message, options, active, inactive }: {
|
|
2187
2179
|
message: string;
|
|
2188
2180
|
options: string[];
|
|
2189
2181
|
active?: (s: string) => string;
|
|
@@ -2204,15 +2196,12 @@ interface StructureToAstTableData {
|
|
|
2204
2196
|
constraints: DbStructure.Constraint[];
|
|
2205
2197
|
roles?: DbStructure.Role[];
|
|
2206
2198
|
}
|
|
2207
|
-
declare const makeStructureToAstCtx: (config: Pick<RakeDbConfig,
|
|
2208
|
-
declare const structureToAst: (ctx: StructureToAstCtx, adapter: Adapter, config: Pick<RakeDbConfig,
|
|
2199
|
+
declare const makeStructureToAstCtx: (config: Pick<RakeDbConfig, 'snakeCase' | 'schemaConfig'>, currentSchema: string) => StructureToAstCtx;
|
|
2200
|
+
declare const structureToAst: (ctx: StructureToAstCtx, adapter: Adapter, config: Pick<RakeDbConfig, 'migrationsTable'>) => Promise<RakeDbAst[]>;
|
|
2209
2201
|
declare const makeDomainsMap: (ctx: StructureToAstCtx, data: IntrospectedStructure) => DbStructureDomainsMap;
|
|
2210
2202
|
declare const instantiateDbColumn: (ctx: StructureToAstCtx, data: IntrospectedStructure, domains: DbStructureDomainsMap, dbColumn: DbStructure.Column) => Column$1;
|
|
2211
|
-
declare const tableToAst: (ctx: StructureToAstCtx, data: IntrospectedStructure, table: DbStructure.Table, action:
|
|
2212
|
-
declare const getDbStructureTableData: (data: IntrospectedStructure, {
|
|
2213
|
-
name,
|
|
2214
|
-
schemaName
|
|
2215
|
-
}: DbStructure.Table) => StructureToAstTableData;
|
|
2203
|
+
declare const tableToAst: (ctx: StructureToAstCtx, data: IntrospectedStructure, table: DbStructure.Table, action: 'create' | 'drop', domains: DbStructureDomainsMap) => RakeDbAst.Table;
|
|
2204
|
+
declare const getDbStructureTableData: (data: IntrospectedStructure, { name, schemaName }: DbStructure.Table) => StructureToAstTableData;
|
|
2216
2205
|
interface ColumnChecks {
|
|
2217
2206
|
[K: string]: string[];
|
|
2218
2207
|
}
|
|
@@ -2238,10 +2227,7 @@ type CreateOrDropOk = 'done' | 'already';
|
|
|
2238
2227
|
* });
|
|
2239
2228
|
* ```
|
|
2240
2229
|
*/
|
|
2241
|
-
declare const createDatabase: (db: DbParam, {
|
|
2242
|
-
database,
|
|
2243
|
-
owner
|
|
2244
|
-
}: {
|
|
2230
|
+
declare const createDatabase: (db: DbParam, { database, owner }: {
|
|
2245
2231
|
database: string;
|
|
2246
2232
|
owner?: string;
|
|
2247
2233
|
}) => Promise<CreateOrDropOk>;
|
|
@@ -2264,9 +2250,7 @@ declare const createDatabase: (db: DbParam, {
|
|
|
2264
2250
|
* });
|
|
2265
2251
|
* ```
|
|
2266
2252
|
*/
|
|
2267
|
-
declare const dropDatabase: (db: DbParam, {
|
|
2268
|
-
database
|
|
2269
|
-
}: {
|
|
2253
|
+
declare const dropDatabase: (db: DbParam, { database }: {
|
|
2270
2254
|
database: string;
|
|
2271
2255
|
}) => Promise<CreateOrDropOk>;
|
|
2272
2256
|
/**
|
|
@@ -2280,7 +2264,7 @@ declare const dropDatabase: (db: DbParam, {
|
|
|
2280
2264
|
* const result: 'done' | 'already' = await createSchema(db, '"schema"');
|
|
2281
2265
|
* ```
|
|
2282
2266
|
*/
|
|
2283
|
-
declare const createSchema: (db: DbParam, sql: string) => Promise<
|
|
2267
|
+
declare const createSchema: (db: DbParam, sql: string) => Promise<'done' | 'already'>;
|
|
2284
2268
|
/**
|
|
2285
2269
|
* `dropSchema` uses a savepoint when it is called in a transaction to not break it if the schema does not exist.
|
|
2286
2270
|
*
|
|
@@ -2292,7 +2276,7 @@ declare const createSchema: (db: DbParam, sql: string) => Promise<"done" | "alre
|
|
|
2292
2276
|
* const result: 'done' | 'already' = await dropSchema(db, '"schema"');
|
|
2293
2277
|
* ```
|
|
2294
2278
|
*/
|
|
2295
|
-
declare const dropSchema: (db: DbParam, sql: string) => Promise<
|
|
2279
|
+
declare const dropSchema: (db: DbParam, sql: string) => Promise<'done' | 'already'>;
|
|
2296
2280
|
/**
|
|
2297
2281
|
* `createTable` uses a savepoint when it is called in a transaction to not break it if the table already exists.
|
|
2298
2282
|
*
|
|
@@ -2304,7 +2288,7 @@ declare const dropSchema: (db: DbParam, sql: string) => Promise<"done" | "alread
|
|
|
2304
2288
|
* const result: 'done' | 'already' = await createTable(db, '"table"');
|
|
2305
2289
|
* ```
|
|
2306
2290
|
*/
|
|
2307
|
-
declare const createTable: (db: DbParam, sql: string) => Promise<
|
|
2291
|
+
declare const createTable: (db: DbParam, sql: string) => Promise<'done' | 'already'>;
|
|
2308
2292
|
/**
|
|
2309
2293
|
* `dropTable` uses a savepoint when it is called in a transaction to not break it if the table does not exist.
|
|
2310
2294
|
*
|
|
@@ -2316,7 +2300,7 @@ declare const createTable: (db: DbParam, sql: string) => Promise<"done" | "alrea
|
|
|
2316
2300
|
* const result: 'done' | 'already' = await dropTable(db, '"table"');
|
|
2317
2301
|
* ```
|
|
2318
2302
|
*/
|
|
2319
|
-
declare const dropTable: (db: DbParam, sql: string) => Promise<
|
|
2303
|
+
declare const dropTable: (db: DbParam, sql: string) => Promise<'done' | 'already'>;
|
|
2320
2304
|
interface RakeDbTableFactoryConfigInput<CT> {
|
|
2321
2305
|
/**
|
|
2322
2306
|
* Function-style table factory metadata.
|
|
@@ -2327,9 +2311,6 @@ interface RakeDbTableFactoryConfigInput<CT> {
|
|
|
2327
2311
|
*/
|
|
2328
2312
|
baseTable?: RakeDbBaseTable<CT>;
|
|
2329
2313
|
}
|
|
2330
|
-
declare const getTableFactoryConfig: <CT>({
|
|
2331
|
-
baseTable,
|
|
2332
|
-
defineTable
|
|
2333
|
-
}: RakeDbTableFactoryConfigInput<CT>) => RakeDbTableFactoryConfig<CT> | undefined;
|
|
2314
|
+
declare const getTableFactoryConfig: <CT>({ baseTable, defineTable }: RakeDbTableFactoryConfigInput<CT>) => RakeDbTableFactoryConfig<CT> | undefined;
|
|
2334
2315
|
declare class RakeDbError extends Error {}
|
|
2335
2316
|
export { type ChangeCallback, type DbMigration, type DbStructure, type IntrospectedStructure, type MigrateConfig, type MigrateFn, type MigrationChangeFn, type RakeDbAst, type RakeDbBaseTable, type RakeDbChangeFnConfig, type RakeDbCliConfigInput, type RakeDbCliResult, type RakeDbConfig, type RakeDbDefineTable, RakeDbError, type RakeDbFn, type SilentQueries, type StructureToAstCtx, type StructureToAstTableData, astToMigration, concatSchemaAndName, createDatabase, createMigrationChangeFn, createMigrationInterface, createMigrationsSchemaAndTable, createSchema, createTable, dbColumnToAst, dropDatabase, dropSchema, dropTable, encodeColumnDefault, getConstraintName, getDbStructureTableData, getDbTableColumnsChecks, getDbVersion, getExcludeName, getIndexName, getMigrationsSchemaAndTable, getSchemaAndTableFromName, getTableFactoryConfig, incrementIntermediateCaller, instantiateDbColumn, introspectDbSchema, makeDomainsMap, makeFileVersion, makeStructureToAstCtx, migrate, migrateAndClose, promptSelect, rakeDbCliWithAdapter, rakeDbCommands, rakeDbConfigDefaults, redo, rollback, runMigration, saveMigratedVersion, setRakeDbCliRunFn, structureToAst, tableToAst, writeMigrationFile };
|
package/dist/index.js
CHANGED
|
@@ -15,7 +15,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
15
|
}
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
19
19
|
value: mod,
|
|
20
20
|
enumerable: true
|
|
21
21
|
}) : target, mod));
|
|
@@ -394,7 +394,8 @@ const primaryKeyToSql = (primaryKey) => {
|
|
|
394
394
|
};
|
|
395
395
|
const interpolateSqlValues = ({ text, values }) => {
|
|
396
396
|
return values?.length ? text.replace(/\$(\d+)/g, (_, n) => {
|
|
397
|
-
|
|
397
|
+
const i = +n - 1;
|
|
398
|
+
return (0, pqb_internal.escapeForMigration)(values[i]);
|
|
398
399
|
}) : text;
|
|
399
400
|
};
|
|
400
401
|
const nameColumnChecks = (table, column, checks) => checks.map((check, i) => ({
|
|
@@ -516,7 +517,7 @@ const makeAst$5 = (schema, up, tableName, shape, tableData, options, noPrimaryKe
|
|
|
516
517
|
...tableData,
|
|
517
518
|
primaryKey: shapePKeys.length <= 1 ? primaryKey : primaryKey ? {
|
|
518
519
|
...primaryKey,
|
|
519
|
-
columns: [
|
|
520
|
+
columns: [.../* @__PURE__ */ new Set([...shapePKeys, ...primaryKey.columns])]
|
|
520
521
|
} : { columns: shapePKeys },
|
|
521
522
|
...options,
|
|
522
523
|
noPrimaryKey: options.noPrimaryKey ? "ignore" : noPrimaryKey || "error"
|
|
@@ -902,6 +903,25 @@ const makeTableChangeMethods = (tableMethods) => ({
|
|
|
902
903
|
to: { comment }
|
|
903
904
|
};
|
|
904
905
|
},
|
|
906
|
+
/**
|
|
907
|
+
* Rename a column:
|
|
908
|
+
*
|
|
909
|
+
* ```ts
|
|
910
|
+
* import { change } from '../dbScript';
|
|
911
|
+
*
|
|
912
|
+
* change(async (db) => {
|
|
913
|
+
* await db.changeTable('table', (t) => ({
|
|
914
|
+
* oldColumnName: t.rename('newColumnName'),
|
|
915
|
+
* }));
|
|
916
|
+
* });
|
|
917
|
+
* ```
|
|
918
|
+
*
|
|
919
|
+
* Note that the renaming `ALTER TABLE` is executed before the rest of alterations,
|
|
920
|
+
* so if you're also adding a new constraint on this column inside the same `changeTable`,
|
|
921
|
+
* refer to it with a new name.
|
|
922
|
+
*
|
|
923
|
+
* @param name
|
|
924
|
+
*/
|
|
905
925
|
rename(name) {
|
|
906
926
|
return {
|
|
907
927
|
type: "rename",
|
|
@@ -920,7 +940,8 @@ const changeTable = async (migration, tableChangeMethods, up, tableName, options
|
|
|
920
940
|
standaloneCheckChanges.length = 0;
|
|
921
941
|
const changeData = fn?.(tableChanger) || {};
|
|
922
942
|
const schema = migration.adapter.getSchema();
|
|
923
|
-
const
|
|
943
|
+
const ast = makeAst$4(schema, up, tableName, changeData, changeTableData, options);
|
|
944
|
+
const queries = astToQueries(schema, ast, snakeCase, language);
|
|
924
945
|
for (const query of queries) {
|
|
925
946
|
const result = await migration.adapter.arrays(interpolateSqlValues(query));
|
|
926
947
|
query.then?.(result);
|
|
@@ -1199,7 +1220,9 @@ const renameColumnSql = (from, to) => {
|
|
|
1199
1220
|
return `RENAME COLUMN "${from}" TO "${to}"`;
|
|
1200
1221
|
};
|
|
1201
1222
|
const createView = async (migration, up, name, options, sql) => {
|
|
1202
|
-
const
|
|
1223
|
+
const schema = migration.adapter.getSchema();
|
|
1224
|
+
const ast = makeAst$3(schema, up, name, options, sql);
|
|
1225
|
+
const query = astToQuery$2(ast);
|
|
1203
1226
|
await migration.adapter.arrays(interpolateSqlValues(query));
|
|
1204
1227
|
};
|
|
1205
1228
|
const makeAst$3 = (schema, up, fullName, options, sql) => {
|
|
@@ -1246,12 +1269,15 @@ const astToQuery$2 = (ast) => {
|
|
|
1246
1269
|
};
|
|
1247
1270
|
};
|
|
1248
1271
|
const createMaterializedView = async (migration, up, name, options, sql) => {
|
|
1249
|
-
const
|
|
1272
|
+
const schema = migration.adapter.getSchema();
|
|
1273
|
+
const ast = makeAst$2(schema, up, name, options, sql);
|
|
1274
|
+
const query = astToQuery$1(ast);
|
|
1250
1275
|
await migration.adapter.arrays(interpolateSqlValues(query));
|
|
1251
1276
|
};
|
|
1252
1277
|
const refreshMaterializedView = async (migration, name, options = {}) => {
|
|
1253
1278
|
if (options.concurrently && options.withData === false) throw new Error("Cannot refresh a materialized view concurrently with WITH NO DATA");
|
|
1254
|
-
const
|
|
1279
|
+
const schema = migration.adapter.getSchema();
|
|
1280
|
+
const [s, viewName] = getSchemaAndTableFromName(schema, name);
|
|
1255
1281
|
const sql = ["REFRESH MATERIALIZED VIEW"];
|
|
1256
1282
|
if (options.concurrently) sql.push("CONCURRENTLY");
|
|
1257
1283
|
sql.push(`${s ? `"${s}".` : ""}"${viewName}"`);
|
|
@@ -1309,7 +1335,8 @@ const serializers = {
|
|
|
1309
1335
|
validUntil: (value) => `VALID UNTIL '${value === void 0 ? "infinity" : value}'`
|
|
1310
1336
|
};
|
|
1311
1337
|
const createOrDropRole = async (migration, up, name, params) => {
|
|
1312
|
-
const
|
|
1338
|
+
const ast = makeAst$1(up, name, params);
|
|
1339
|
+
const sql = astToQuery(ast);
|
|
1313
1340
|
await migration.adapter.arrays(sql);
|
|
1314
1341
|
};
|
|
1315
1342
|
const makeAst$1 = (up, name, params) => {
|
|
@@ -1361,7 +1388,8 @@ const changeRole = async (migration, up, name, from, to) => {
|
|
|
1361
1388
|
for (const key in from.config) if (!(key in config)) config[key] = void 0;
|
|
1362
1389
|
}
|
|
1363
1390
|
}
|
|
1364
|
-
const
|
|
1391
|
+
const ast = makeChangeAst(name, from, to);
|
|
1392
|
+
const sql = changeAstToQuery(ast);
|
|
1365
1393
|
if (sql) await migration.adapter.arrays(sql);
|
|
1366
1394
|
};
|
|
1367
1395
|
const makeChangeAst = (name, from, to) => {
|
|
@@ -1400,7 +1428,8 @@ const renameRole = async (migration, up, from, to) => {
|
|
|
1400
1428
|
from = to;
|
|
1401
1429
|
to = f;
|
|
1402
1430
|
}
|
|
1403
|
-
const
|
|
1431
|
+
const ast = makeRenameAst(from, to);
|
|
1432
|
+
const sql = renameAstToQuery(ast);
|
|
1404
1433
|
if (sql) await migration.adapter.arrays(sql);
|
|
1405
1434
|
};
|
|
1406
1435
|
const makeRenameAst = (from, to) => ({
|
|
@@ -1452,7 +1481,8 @@ const filterAndTransformConfig = (config, schema) => {
|
|
|
1452
1481
|
return Object.keys(result).length ? result : void 0;
|
|
1453
1482
|
};
|
|
1454
1483
|
const changeDefaultPrivileges = async (migration, up, arg) => {
|
|
1455
|
-
const
|
|
1484
|
+
const ast = makeAst(up, arg);
|
|
1485
|
+
const sql = astToSql(ast);
|
|
1456
1486
|
if (sql.length) await migration.adapter.arrays(sql.join(";\n"));
|
|
1457
1487
|
};
|
|
1458
1488
|
const makeAst = (up, arg) => {
|
|
@@ -1605,7 +1635,9 @@ const recreatePolicy = async (migration, tableName, policyName, from, to) => {
|
|
|
1605
1635
|
const fromTable = from.table ?? tableName;
|
|
1606
1636
|
const fromName = from.name ?? policyName;
|
|
1607
1637
|
await migration.adapter.arrays(dropPolicySql(migration, fromTable, fromName));
|
|
1608
|
-
const
|
|
1638
|
+
const toTable = to.table ?? tableName;
|
|
1639
|
+
const toName = to.name ?? policyName;
|
|
1640
|
+
const { text, values } = createPolicySql(migration, toTable, toName, to);
|
|
1609
1641
|
await migration.adapter.arrays(text, values);
|
|
1610
1642
|
};
|
|
1611
1643
|
const changePolicy = async (migration, up, tableName, policyName, params) => {
|
|
@@ -1633,18 +1665,19 @@ const schemaOrDatabaseTargetKeyToSql = {
|
|
|
1633
1665
|
schemas: "SCHEMA",
|
|
1634
1666
|
databases: "DATABASE"
|
|
1635
1667
|
};
|
|
1636
|
-
const specialRoleSpecs = new Set([
|
|
1668
|
+
const specialRoleSpecs = /* @__PURE__ */ new Set([
|
|
1637
1669
|
"PUBLIC",
|
|
1638
1670
|
"CURRENT_ROLE",
|
|
1639
1671
|
"CURRENT_USER",
|
|
1640
1672
|
"SESSION_USER"
|
|
1641
1673
|
]);
|
|
1642
1674
|
const changeGrant = async (migration, up, params) => {
|
|
1643
|
-
const
|
|
1675
|
+
const ast = {
|
|
1644
1676
|
...params,
|
|
1645
1677
|
to: typeof params.to === "string" ? [params.to] : params.to,
|
|
1646
1678
|
action: up ? "grant" : "revoke"
|
|
1647
|
-
}
|
|
1679
|
+
};
|
|
1680
|
+
const sql = privilegeToSql(migration, ast);
|
|
1648
1681
|
if (sql.length) await migration.adapter.arrays(sql.join(";\n"));
|
|
1649
1682
|
};
|
|
1650
1683
|
const privilegeToSql = (migration, ast) => {
|
|
@@ -1769,7 +1802,10 @@ const createMigrationInterface = (tx, up, config) => {
|
|
|
1769
1802
|
var Migration = class {
|
|
1770
1803
|
getTableMethods() {
|
|
1771
1804
|
let { tableMethods } = this;
|
|
1772
|
-
if (!tableMethods)
|
|
1805
|
+
if (!tableMethods) {
|
|
1806
|
+
const schemaConfig = (0, pqb_internal.defaultSchemaConfig)();
|
|
1807
|
+
tableMethods = makeTableMethods(schemaConfig);
|
|
1808
|
+
}
|
|
1773
1809
|
return tableMethods;
|
|
1774
1810
|
}
|
|
1775
1811
|
getTableChangeMethods() {
|
|
@@ -2850,7 +2886,8 @@ const addOrDropEnumValues = async (migration, up, enumName, values, options) =>
|
|
|
2850
2886
|
return;
|
|
2851
2887
|
}
|
|
2852
2888
|
const { rows: valuesRows } = await migration.adapter.query(`SELECT unnest(enum_range(NULL::${quotedName}))::text value`);
|
|
2853
|
-
|
|
2889
|
+
const existingValues = valuesRows.map((r) => r.value);
|
|
2890
|
+
await recreateEnum(migration, ast, existingValues.filter((v) => !ast.values.includes(v)), (quotedName, table, column) => `Cannot drop ${quotedName} enum values [${ast.values.map(pqb_internal.singleQuote).join(", ")}]: table ${table} has a row with such value in the column "${column}"`);
|
|
2854
2891
|
};
|
|
2855
2892
|
const changeEnumValues = async (migration, enumName, fromValues, toValues) => {
|
|
2856
2893
|
const [schema, name] = getSchemaAndTableFromName(migration.adapter.getSchema(), enumName);
|
|
@@ -2907,11 +2944,13 @@ GROUP BY n.nspname, c.relname`);
|
|
|
2907
2944
|
const writeMigrationFile = async (config, version, name, migrationCode) => {
|
|
2908
2945
|
await (0, node_fs_promises.mkdir)(config.migrationsPath, { recursive: true });
|
|
2909
2946
|
const filePath = node_path.default.resolve(config.migrationsPath, `${version}_${name.replaceAll(" ", "-")}.ts`);
|
|
2910
|
-
|
|
2947
|
+
const importPath = (0, pqb_internal.getImportPath)(filePath, node_path.default.join(config.basePath, config.dbScript));
|
|
2948
|
+
await (0, node_fs_promises.writeFile)(filePath, `import { change } from '${importPath}';\n${migrationCode}`);
|
|
2911
2949
|
config.logger?.log(`Created ${(0, pqb_internal.pathToLog)(filePath)}`);
|
|
2912
2950
|
};
|
|
2913
2951
|
const newMigration = async (config, name) => {
|
|
2914
|
-
|
|
2952
|
+
const version = await makeFileVersion({}, config);
|
|
2953
|
+
await writeMigrationFile(config, version, name, makeContent(name));
|
|
2915
2954
|
};
|
|
2916
2955
|
const makeFileVersion = async (ctx, config) => {
|
|
2917
2956
|
if (config.migrationId === "timestamp") return generateTimeStamp();
|
|
@@ -3226,7 +3265,8 @@ const saveMigratedVersion = async (db, version, name, config) => {
|
|
|
3226
3265
|
await db.silentArrays(`INSERT INTO ${migrationsSchemaTableSql(db, config)}(version, name) VALUES ($1, $2)`, [version, name]);
|
|
3227
3266
|
};
|
|
3228
3267
|
const createMigrationsSchemaAndTable = async (db, config) => {
|
|
3229
|
-
const
|
|
3268
|
+
const adapter = getMaybeTransactionAdapter(db);
|
|
3269
|
+
const { schema, table } = getMigrationsSchemaAndTable(adapter, config);
|
|
3230
3270
|
if (schema) {
|
|
3231
3271
|
if (await createSchema(db, schema) === "done") config.logger?.log(`Created schema "${schema}"`);
|
|
3232
3272
|
}
|
|
@@ -3339,12 +3379,14 @@ function makeMigrateFn(up, defaultCount, fn) {
|
|
|
3339
3379
|
let migrations;
|
|
3340
3380
|
try {
|
|
3341
3381
|
await transactionIfSingle(adapter, config, async (trx) => {
|
|
3342
|
-
|
|
3382
|
+
const versions = await getMigratedVersionsMap(ctx, trx, config, set.renameTo);
|
|
3383
|
+
migrations = await fn(trx, config, set, versions, count, force);
|
|
3343
3384
|
});
|
|
3344
3385
|
} catch (err) {
|
|
3345
3386
|
if (err instanceof NoMigrationsTableError) await transactionIfSingle(adapter, config, async (trx) => {
|
|
3346
3387
|
await createMigrationsSchemaAndTable(trx, config);
|
|
3347
|
-
|
|
3388
|
+
const versions = await getMigratedVersionsMap(ctx, trx, config, set.renameTo);
|
|
3389
|
+
migrations = await fn(trx, config, set, versions, count, force);
|
|
3348
3390
|
});
|
|
3349
3391
|
else throw err;
|
|
3350
3392
|
}
|
|
@@ -3380,9 +3422,11 @@ async function runMigration(db, ...args) {
|
|
|
3380
3422
|
...rawConfig,
|
|
3381
3423
|
...handleConfigLogger(rawConfig)
|
|
3382
3424
|
};
|
|
3383
|
-
|
|
3425
|
+
const adapter = getMaybeTransactionAdapter(db);
|
|
3426
|
+
await transaction(adapter, config, async (trx) => {
|
|
3384
3427
|
clearChanges();
|
|
3385
|
-
|
|
3428
|
+
const changes = await getChanges({ load: migration });
|
|
3429
|
+
await applyMigration(trx, true, changes, config);
|
|
3386
3430
|
});
|
|
3387
3431
|
}
|
|
3388
3432
|
/**
|
|
@@ -3465,7 +3509,8 @@ const migrateOrRollback = async (trx, config, set, versions, count, up, redo, fo
|
|
|
3465
3509
|
loggedAboutStarting = true;
|
|
3466
3510
|
config.logger?.log(`${redo ? "Reapplying migrations for" : up ? "Migrating" : "Rolling back"} database ${getAdapterDatabase$1(trx)}\n`);
|
|
3467
3511
|
}
|
|
3468
|
-
|
|
3512
|
+
const adapter = await migrationRunner(trx, up, await getChanges(file, config), config);
|
|
3513
|
+
await changeMigratedVersion(adapter, up, file, config);
|
|
3469
3514
|
(migrations ??= []).push(file);
|
|
3470
3515
|
if (up) {
|
|
3471
3516
|
const name = node_path.default.basename(file.path);
|
|
@@ -3846,11 +3891,12 @@ const astToGenerateItem = (config, ast, currentSchema) => {
|
|
|
3846
3891
|
const keys = ast.action === "create" ? add : drop;
|
|
3847
3892
|
keys.push(table);
|
|
3848
3893
|
deps.push(schema);
|
|
3849
|
-
|
|
3894
|
+
const columns = Object.entries(ast.shape).map(([name, column]) => [
|
|
3850
3895
|
keys,
|
|
3851
3896
|
name,
|
|
3852
3897
|
{ column }
|
|
3853
|
-
])
|
|
3898
|
+
]);
|
|
3899
|
+
analyzeTableColumns(config, currentSchema, schema, table, deps, resolveType, columns);
|
|
3854
3900
|
if (ast.type === "table") analyzeTableData(config, currentSchema, schema, table, keys, deps, ast);
|
|
3855
3901
|
else deps.push(...ast.deps.map(({ schemaName, name }) => `${schemaName}.${name}`));
|
|
3856
3902
|
} else {
|
|
@@ -5710,9 +5756,7 @@ const viewToAst = (ctx, data, domains, view) => {
|
|
|
5710
5756
|
case "securityBarrier":
|
|
5711
5757
|
options.securityBarrier = value === "true";
|
|
5712
5758
|
break;
|
|
5713
|
-
case "securityInvoker":
|
|
5714
|
-
options.securityInvoker = value === "true";
|
|
5715
|
-
break;
|
|
5759
|
+
case "securityInvoker": options.securityInvoker = value === "true";
|
|
5716
5760
|
}
|
|
5717
5761
|
}
|
|
5718
5762
|
return {
|
|
@@ -5860,14 +5904,16 @@ const checkIfIsOuterRecursiveFkey = (data, table, references) => {
|
|
|
5860
5904
|
const pullDbStructure = async (adapter, config) => {
|
|
5861
5905
|
const currentSchema = adapter.getSearchPath?.() ?? adapter.searchPath ?? "public";
|
|
5862
5906
|
const ctx = makeStructureToAstCtx(config, currentSchema);
|
|
5863
|
-
const
|
|
5907
|
+
const ast = await structureToAst(ctx, adapter, config);
|
|
5908
|
+
const result = astToMigration(currentSchema, config, ast);
|
|
5864
5909
|
if (!result) return;
|
|
5865
5910
|
const version = await makeFileVersion({}, config);
|
|
5866
5911
|
await writeMigrationFile(config, version, "pull", result);
|
|
5867
|
-
|
|
5912
|
+
const silentQueries = Object.assign(adapter, {
|
|
5868
5913
|
silentQuery: adapter.query,
|
|
5869
5914
|
silentArrays: adapter.arrays
|
|
5870
|
-
})
|
|
5915
|
+
});
|
|
5916
|
+
await saveMigratedVersion(silentQueries, version, "pull", config);
|
|
5871
5917
|
const unsupportedEntries = Object.entries(ctx.unsupportedTypes);
|
|
5872
5918
|
if (unsupportedEntries.length) {
|
|
5873
5919
|
let count = 0;
|
|
@@ -6117,7 +6163,8 @@ const rakeDbCommands = {
|
|
|
6117
6163
|
},
|
|
6118
6164
|
status: {
|
|
6119
6165
|
run(adapters, config, args) {
|
|
6120
|
-
|
|
6166
|
+
const showUrl = args.includes("p") || args.includes("path");
|
|
6167
|
+
return listMigrationsStatuses(adapters, config, { showUrl });
|
|
6121
6168
|
},
|
|
6122
6169
|
help: "list migrations statuses",
|
|
6123
6170
|
helpArguments: {
|