@sqb/migrator 4.9.1 → 4.10.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/cjs/adapters/pg-migration-adapter.js +198 -0
- package/cjs/db-migrator.js +77 -184
- package/cjs/index.js +2 -0
- package/cjs/migration-adapter.js +14 -0
- package/cjs/migration-package.js +138 -0
- package/cjs/types.js +6 -0
- package/cjs/utils/get-calling-filename.js +23 -0
- package/esm/adapters/pg-migration-adapter.js +194 -0
- package/esm/db-migrator.js +77 -183
- package/esm/index.js +2 -0
- package/esm/migration-adapter.js +10 -0
- package/esm/migration-package.js +131 -0
- package/esm/types.js +5 -1
- package/esm/utils/get-calling-filename.js +19 -0
- package/package.json +12 -13
- package/types/adapters/pg-migration-adapter.d.ts +39 -0
- package/types/db-migrator.d.ts +15 -0
- package/types/index.d.ts +4 -0
- package/types/migration-adapter.d.ts +32 -0
- package/types/migration-package.d.ts +44 -0
- package/types/types.d.ts +9 -0
- package/types/utils/get-calling-filename.d.ts +1 -0
- package/cjs/load-task-files.js +0 -39
- package/esm/db-migrator.d.ts +0 -29
- package/esm/index.d.ts +0 -2
- package/esm/load-task-files.d.ts +0 -2
- package/esm/load-task-files.js +0 -34
- package/esm/types.d.ts +0 -36
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const PATH_PATTERN = /^(?:file:\/\/)?(.+)$/;
|
|
2
|
+
export function getCallingFilename(position = 0) {
|
|
3
|
+
position++;
|
|
4
|
+
if (position >= Error.stackTraceLimit)
|
|
5
|
+
return '';
|
|
6
|
+
const oldPrepareStackTrace = Error.prepareStackTrace;
|
|
7
|
+
Error.prepareStackTrace = (_, stack) => stack;
|
|
8
|
+
const stack = new Error().stack;
|
|
9
|
+
Error.prepareStackTrace = oldPrepareStackTrace;
|
|
10
|
+
if (stack !== null && typeof stack === 'object') {
|
|
11
|
+
// stack[0] holds this file
|
|
12
|
+
// stack[1] holds where this function was called
|
|
13
|
+
const s = stack[position] ?
|
|
14
|
+
stack[position].getFileName() : undefined;
|
|
15
|
+
const m = s ? PATH_PATTERN.exec(s) : undefined;
|
|
16
|
+
return m ? m[1] : '';
|
|
17
|
+
}
|
|
18
|
+
return '';
|
|
19
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sqb/migrator",
|
|
3
3
|
"description": "Database migrator for SQB",
|
|
4
|
-
"version": "4.
|
|
4
|
+
"version": "4.10.1",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"contributors": [
|
|
7
7
|
"Eray Hanoglu <e.hanoglu@panates.com>",
|
|
@@ -13,6 +13,10 @@
|
|
|
13
13
|
"url": "https://github.com/sqbjs/sqb.git",
|
|
14
14
|
"directory": "packages/migrator"
|
|
15
15
|
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"module": "./esm/index.js",
|
|
18
|
+
"main": "./cjs/index.js",
|
|
19
|
+
"types": "./types/index.d.ts",
|
|
16
20
|
"scripts": {
|
|
17
21
|
"compile": "tsc",
|
|
18
22
|
"prebuild": "npm run lint && npm run clean",
|
|
@@ -28,19 +32,13 @@
|
|
|
28
32
|
"clean:dist": "rimraf ../../build/migrator",
|
|
29
33
|
"clean:cover": "rimraf ../../coverage/migrator"
|
|
30
34
|
},
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"@sqb/connect": "^4.9.1"
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"ts-gems": "^2.5.0"
|
|
34
37
|
},
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
"
|
|
39
|
-
"require": "./cjs/index.js",
|
|
40
|
-
"default": "./esm/index.js"
|
|
41
|
-
},
|
|
42
|
-
"./cjs": "./cjs/index.js",
|
|
43
|
-
"./esm": "./esm/index.js"
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@sqb/builder": "^4.10.1",
|
|
40
|
+
"@sqb/connect": "^4.10.1",
|
|
41
|
+
"@sqb/postgres": "^4.10.1"
|
|
44
42
|
},
|
|
45
43
|
"engines": {
|
|
46
44
|
"node": ">=16.0",
|
|
@@ -50,6 +48,7 @@
|
|
|
50
48
|
"bin/",
|
|
51
49
|
"cjs/",
|
|
52
50
|
"esm/",
|
|
51
|
+
"types/",
|
|
53
52
|
"LICENSE",
|
|
54
53
|
"README.md"
|
|
55
54
|
],
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Connection } from 'postgresql-client';
|
|
2
|
+
import type { DbMigratorOptions } from '../db-migrator.js';
|
|
3
|
+
import { MigrationAdapter } from '../migration-adapter.js';
|
|
4
|
+
import { MigrationTask } from '../migration-package.js';
|
|
5
|
+
import { MigrationStatus } from '../types.js';
|
|
6
|
+
export declare class PgMigrationAdapter extends MigrationAdapter {
|
|
7
|
+
protected _connection: Connection;
|
|
8
|
+
protected _infoSchema: string;
|
|
9
|
+
protected _packageName: string;
|
|
10
|
+
protected _version: number;
|
|
11
|
+
protected _status: MigrationStatus;
|
|
12
|
+
protected defaultVariables: {
|
|
13
|
+
tablespace: string;
|
|
14
|
+
schema: string;
|
|
15
|
+
owner: string;
|
|
16
|
+
};
|
|
17
|
+
readonly summaryTable = "migration_summary";
|
|
18
|
+
readonly eventTable = "migration_events";
|
|
19
|
+
get packageName(): string;
|
|
20
|
+
get version(): number;
|
|
21
|
+
get status(): MigrationStatus;
|
|
22
|
+
get infoSchema(): string;
|
|
23
|
+
get summaryTableFull(): string;
|
|
24
|
+
get eventTableFull(): string;
|
|
25
|
+
static create(options: DbMigratorOptions): Promise<PgMigrationAdapter>;
|
|
26
|
+
close(): Promise<void>;
|
|
27
|
+
refresh(): Promise<void>;
|
|
28
|
+
update(info: {
|
|
29
|
+
status?: MigrationStatus;
|
|
30
|
+
version?: number;
|
|
31
|
+
}): Promise<void>;
|
|
32
|
+
writeEvent(event: MigrationAdapter.Event): Promise<void>;
|
|
33
|
+
executeTask(task: MigrationTask, variables: Record<string, any>): Promise<void>;
|
|
34
|
+
backupDatabase(): Promise<void>;
|
|
35
|
+
lockSchema(): Promise<void>;
|
|
36
|
+
restoreDatabase(): Promise<void>;
|
|
37
|
+
unlockSchema(): Promise<void>;
|
|
38
|
+
protected rowToSql(tableName: string, row: Object): string;
|
|
39
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AsyncEventEmitter } from 'strict-typed-events';
|
|
2
|
+
import { ClientConfiguration } from '@sqb/connect';
|
|
3
|
+
import { MigrationAdapter } from './migration-adapter.js';
|
|
4
|
+
import { MigrationPackage, MigrationPackageAsync } from './migration-package.js';
|
|
5
|
+
export interface DbMigratorOptions {
|
|
6
|
+
connection: ClientConfiguration;
|
|
7
|
+
migrationPackage: MigrationPackage | MigrationPackageAsync;
|
|
8
|
+
infoSchema?: string;
|
|
9
|
+
scriptVariables?: Record<string, string>;
|
|
10
|
+
targetVersion?: number;
|
|
11
|
+
}
|
|
12
|
+
export declare class DbMigrator extends AsyncEventEmitter {
|
|
13
|
+
protected adapter: MigrationAdapter;
|
|
14
|
+
execute(options: DbMigratorOptions): Promise<boolean>;
|
|
15
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { MigrationTask } from './migration-package.js';
|
|
2
|
+
import { MigrationStatus } from './types.js';
|
|
3
|
+
export declare abstract class MigrationAdapter {
|
|
4
|
+
abstract readonly packageName: string;
|
|
5
|
+
abstract readonly status: MigrationStatus;
|
|
6
|
+
abstract readonly version: number;
|
|
7
|
+
abstract close(): Promise<void>;
|
|
8
|
+
abstract refresh(): Promise<void>;
|
|
9
|
+
abstract update(info: {
|
|
10
|
+
status?: MigrationStatus;
|
|
11
|
+
version?: number;
|
|
12
|
+
}): Promise<void>;
|
|
13
|
+
abstract writeEvent(event: MigrationAdapter.Event): Promise<void>;
|
|
14
|
+
abstract executeTask(task: MigrationTask, variables: Record<string, any>): Promise<void>;
|
|
15
|
+
abstract lockSchema(): Promise<void>;
|
|
16
|
+
abstract unlockSchema(): Promise<void>;
|
|
17
|
+
abstract backupDatabase(): Promise<void>;
|
|
18
|
+
abstract restoreDatabase(): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
export declare namespace MigrationAdapter {
|
|
21
|
+
enum EventKind {
|
|
22
|
+
started = "started",
|
|
23
|
+
success = "success",
|
|
24
|
+
error = "error"
|
|
25
|
+
}
|
|
26
|
+
interface Event {
|
|
27
|
+
event: EventKind;
|
|
28
|
+
version: number;
|
|
29
|
+
message: string;
|
|
30
|
+
details?: string;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { StrictOmit } from 'ts-gems';
|
|
2
|
+
import type { MigrationAdapter } from './migration-adapter.js';
|
|
3
|
+
export interface MigrationPackage {
|
|
4
|
+
name: string;
|
|
5
|
+
description?: string;
|
|
6
|
+
migrations: Migration[];
|
|
7
|
+
informationTableName?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface Migration {
|
|
10
|
+
version: number;
|
|
11
|
+
tasks: MigrationTask[];
|
|
12
|
+
dirname?: string;
|
|
13
|
+
backup?: boolean;
|
|
14
|
+
}
|
|
15
|
+
export type MigrationTask = SqlScriptMigrationTask | CustomMigrationTask | InsertDataMigrationTask;
|
|
16
|
+
export interface BaseMigrationTask {
|
|
17
|
+
title: string;
|
|
18
|
+
filename?: string;
|
|
19
|
+
}
|
|
20
|
+
export interface SqlScriptMigrationTask extends BaseMigrationTask {
|
|
21
|
+
title: string;
|
|
22
|
+
script: string;
|
|
23
|
+
}
|
|
24
|
+
export interface InsertDataMigrationTask extends BaseMigrationTask {
|
|
25
|
+
title: string;
|
|
26
|
+
tableName: string;
|
|
27
|
+
rows: Record<string, any>[];
|
|
28
|
+
}
|
|
29
|
+
export interface CustomMigrationTask extends BaseMigrationTask {
|
|
30
|
+
title: string;
|
|
31
|
+
fn: (connection: any, adapter: MigrationAdapter) => void | Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
export declare function isSqlScriptMigrationTask(x: any): x is SqlScriptMigrationTask;
|
|
34
|
+
export declare function isInsertDataMigrationTask(x: any): x is InsertDataMigrationTask;
|
|
35
|
+
export declare function isCustomMigrationTask(x: any): x is CustomMigrationTask;
|
|
36
|
+
export interface MigrationPackageAsync extends StrictOmit<MigrationPackage, 'migrations'> {
|
|
37
|
+
migrations: (string | MigrationAsync | (() => MigrationAsync) | (() => Promise<MigrationAsync>))[];
|
|
38
|
+
}
|
|
39
|
+
export interface MigrationAsync extends StrictOmit<Migration, 'tasks'> {
|
|
40
|
+
tasks: (string | MigrationTask | (() => MigrationTask) | (() => Promise<MigrationTask>))[];
|
|
41
|
+
}
|
|
42
|
+
export declare namespace MigrationPackage {
|
|
43
|
+
function load(asyncConfig: MigrationPackageAsync): Promise<MigrationPackage>;
|
|
44
|
+
}
|
package/types/types.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getCallingFilename(position?: number): string;
|
package/cjs/load-task-files.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.loadTaskFiles = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const fast_glob_1 = tslib_1.__importDefault(require("fast-glob"));
|
|
6
|
-
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
7
|
-
const path_1 = tslib_1.__importDefault(require("path"));
|
|
8
|
-
function loadTaskFiles(pattern) {
|
|
9
|
-
const out = [];
|
|
10
|
-
const files = fast_glob_1.default.sync(pattern.replace(/\\/g, '/'), { absolute: true, onlyFiles: true });
|
|
11
|
-
for (const filename of files) {
|
|
12
|
-
const ext = path_1.default.extname(filename).toLowerCase();
|
|
13
|
-
if (ext === '.sql') {
|
|
14
|
-
const script = fs_1.default.readFileSync(filename, 'utf-8');
|
|
15
|
-
out.push({
|
|
16
|
-
title: path_1.default.basename(filename),
|
|
17
|
-
script
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
else if (ext === '.json') {
|
|
21
|
-
try {
|
|
22
|
-
const json = JSON.parse(fs_1.default.readFileSync(filename, 'utf-8'));
|
|
23
|
-
if (typeof json === 'object' && json.tableName && json.rows) {
|
|
24
|
-
if (!json.tableName) { // noinspection ExceptionCaughtLocallyJS
|
|
25
|
-
throw new Error('"tableName" property does not exists');
|
|
26
|
-
}
|
|
27
|
-
json.title = json.title || 'Migrate data into ' + json.tableName;
|
|
28
|
-
out.push(json);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
catch (e) {
|
|
32
|
-
e.message = e.message + '\n' + filename;
|
|
33
|
-
throw e;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return out;
|
|
38
|
-
}
|
|
39
|
-
exports.loadTaskFiles = loadTaskFiles;
|
package/esm/db-migrator.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { Connection } from 'postgresql-client';
|
|
2
|
-
import { InsertDataMigrationTask, Migration, MigrationPackage } from './types.js';
|
|
3
|
-
export interface MigrationExecuteOptions {
|
|
4
|
-
connection: Connection;
|
|
5
|
-
migrationPackage: MigrationPackage;
|
|
6
|
-
schema: string;
|
|
7
|
-
owner?: string;
|
|
8
|
-
tablespace?: string;
|
|
9
|
-
targetVersion?: number;
|
|
10
|
-
eventListener?: (event: string, ...args: any[]) => void;
|
|
11
|
-
}
|
|
12
|
-
interface LoadedMigrationPackage {
|
|
13
|
-
minVersion: number;
|
|
14
|
-
maxVersion: number;
|
|
15
|
-
migrations: Migration[];
|
|
16
|
-
}
|
|
17
|
-
export declare class DbMigrator {
|
|
18
|
-
execute(options: MigrationExecuteOptions): Promise<boolean>;
|
|
19
|
-
static loadMigrationPackage(pkg: MigrationPackage): Promise<LoadedMigrationPackage>;
|
|
20
|
-
private _lockSchema;
|
|
21
|
-
private _unlockSchema;
|
|
22
|
-
private _backup;
|
|
23
|
-
private _restore;
|
|
24
|
-
private _getSchemaInfo;
|
|
25
|
-
static objectDataToSql(task: InsertDataMigrationTask & {
|
|
26
|
-
schema: string;
|
|
27
|
-
}): string;
|
|
28
|
-
}
|
|
29
|
-
export {};
|
package/esm/index.d.ts
DELETED
package/esm/load-task-files.d.ts
DELETED
package/esm/load-task-files.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import glob from 'fast-glob';
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
export function loadTaskFiles(pattern) {
|
|
5
|
-
const out = [];
|
|
6
|
-
const files = glob.sync(pattern.replace(/\\/g, '/'), { absolute: true, onlyFiles: true });
|
|
7
|
-
for (const filename of files) {
|
|
8
|
-
const ext = path.extname(filename).toLowerCase();
|
|
9
|
-
if (ext === '.sql') {
|
|
10
|
-
const script = fs.readFileSync(filename, 'utf-8');
|
|
11
|
-
out.push({
|
|
12
|
-
title: path.basename(filename),
|
|
13
|
-
script
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
else if (ext === '.json') {
|
|
17
|
-
try {
|
|
18
|
-
const json = JSON.parse(fs.readFileSync(filename, 'utf-8'));
|
|
19
|
-
if (typeof json === 'object' && json.tableName && json.rows) {
|
|
20
|
-
if (!json.tableName) { // noinspection ExceptionCaughtLocallyJS
|
|
21
|
-
throw new Error('"tableName" property does not exists');
|
|
22
|
-
}
|
|
23
|
-
json.title = json.title || 'Migrate data into ' + json.tableName;
|
|
24
|
-
out.push(json);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
catch (e) {
|
|
28
|
-
e.message = e.message + '\n' + filename;
|
|
29
|
-
throw e;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return out;
|
|
34
|
-
}
|
package/esm/types.d.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
export type MigrationsThunk = (string | Migration)[] | (() => (string | Migration)[]) | (() => Promise<(string | Migration)[]>);
|
|
2
|
-
export interface MigrationPackage {
|
|
3
|
-
description: string;
|
|
4
|
-
informationTableName?: string;
|
|
5
|
-
migrations: MigrationsThunk | ((string | Migration)[]);
|
|
6
|
-
}
|
|
7
|
-
export interface SchemaOptions {
|
|
8
|
-
schema: string;
|
|
9
|
-
owner: string;
|
|
10
|
-
tablespace: string;
|
|
11
|
-
currentVersion: number;
|
|
12
|
-
targetVersion: number;
|
|
13
|
-
}
|
|
14
|
-
export interface Migration {
|
|
15
|
-
backup?: boolean;
|
|
16
|
-
version: number;
|
|
17
|
-
tasks: string[] | MigrationTask[];
|
|
18
|
-
}
|
|
19
|
-
export type MigrationTaskFunction = (connection: any) => void | Promise<void>;
|
|
20
|
-
export type MigrationTask = SqlScriptMigrationTask | CustomMigrationTask | InsertDataMigrationTask;
|
|
21
|
-
export interface BaseMigrationTask {
|
|
22
|
-
title: string;
|
|
23
|
-
}
|
|
24
|
-
export interface SqlScriptMigrationTask extends BaseMigrationTask {
|
|
25
|
-
title: string;
|
|
26
|
-
script: string;
|
|
27
|
-
}
|
|
28
|
-
export interface CustomMigrationTask extends BaseMigrationTask {
|
|
29
|
-
title: string;
|
|
30
|
-
fn: MigrationTaskFunction;
|
|
31
|
-
}
|
|
32
|
-
export interface InsertDataMigrationTask extends BaseMigrationTask {
|
|
33
|
-
title: string;
|
|
34
|
-
tableName: string;
|
|
35
|
-
rows: Record<string, any>[];
|
|
36
|
-
}
|