@sqb/migrator 4.9.1 → 4.10.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/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 +11 -13
- 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.0",
|
|
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.0",
|
|
40
|
+
"@sqb/connect": "^4.10.0",
|
|
41
|
+
"@sqb/postgres": "^4.10.0"
|
|
44
42
|
},
|
|
45
43
|
"engines": {
|
|
46
44
|
"node": ">=16.0",
|
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
|
-
}
|