@sqb/migrator 4.19.4 → 4.19.6

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/README.md CHANGED
@@ -20,12 +20,12 @@ SQB is an extensible, multi-dialect SQL query builder and Database connection wr
20
20
 
21
21
  ## Main goals
22
22
 
23
- - Single code base for any sql based database
24
- - Powerful and simplified query coding scheme
25
- - Fast applications with low memory requirements
26
- - Let applications work with large data tables efficiently
27
- - Support latest JavaScript language standards
28
- - Lightweight and extensible framework.
23
+ - Single code base for any sql based database
24
+ - Powerful and simplified query coding scheme
25
+ - Fast applications with low memory requirements
26
+ - Let applications work with large data tables efficiently
27
+ - Support latest JavaScript language standards
28
+ - Lightweight and extensible framework.
29
29
 
30
30
  You can report bugs and discuss features on the [GitHub issues](https://github.com/sqbjs/sqb/issues) page
31
31
 
@@ -41,7 +41,7 @@ $ npm install @sqb/migrator --save
41
41
 
42
42
  ## Node Compatibility
43
43
 
44
- - node >= 16.x
44
+ - node >= 16.x
45
45
 
46
46
  ### License
47
47
 
@@ -43,7 +43,8 @@ class PgMigrationAdapter extends migration_adapter_js_1.MigrationAdapter {
43
43
  }
44
44
  static async create(options) {
45
45
  // Create connection
46
- const connection = (await pgAdapter.connect(options.connection)).intlcon;
46
+ const connection = (await pgAdapter.connect(options.connection))
47
+ .intlcon;
47
48
  try {
48
49
  const adapter = new PgMigrationAdapter();
49
50
  adapter._connection = connection;
@@ -51,8 +52,11 @@ class PgMigrationAdapter extends migration_adapter_js_1.MigrationAdapter {
51
52
  adapter._infoSchema = options.infoSchema || '__migration';
52
53
  adapter.defaultVariables.schema = options.connection.schema || '';
53
54
  if (!adapter.defaultVariables.schema) {
54
- const r = await connection.query('SELECT CURRENT_SCHEMA ', { objectRows: true });
55
- adapter.defaultVariables.schema = r.rows?.[0]?.current_schema || 'public';
55
+ const r = await connection.query('SELECT CURRENT_SCHEMA ', {
56
+ objectRows: true,
57
+ });
58
+ adapter.defaultVariables.schema =
59
+ r.rows?.[0]?.current_schema || 'public';
56
60
  }
57
61
  // Check if migration schema
58
62
  await connection.query(`CREATE SCHEMA IF NOT EXISTS ${adapter.infoSchema} AUTHORIZATION postgres;`);
@@ -144,7 +148,15 @@ CREATE TABLE IF NOT EXISTS ${adapter.eventTableFull}
144
148
  '(package_name, version, event, event_time, title, message, filename, details) ' +
145
149
  'values ($1, $2, $3, CURRENT_TIMESTAMP, $4, $5, $6, $7)';
146
150
  await this._connection.query(sql, {
147
- params: [this.packageName, event.version, event.event, event.title, event.message, event.filename, event.details],
151
+ params: [
152
+ this.packageName,
153
+ event.version,
154
+ event.event,
155
+ event.title,
156
+ event.message,
157
+ event.filename,
158
+ event.details,
159
+ ],
148
160
  });
149
161
  }
150
162
  async executeTask(migrationPackage, migration, task, variables) {
@@ -156,7 +168,12 @@ CREATE TABLE IF NOT EXISTS ${adapter.eventTableFull}
156
168
  try {
157
169
  let script;
158
170
  if (typeof task.script === 'function') {
159
- script = await task.script({ migrationPackage, migration, task, variables });
171
+ script = await task.script({
172
+ migrationPackage,
173
+ migration,
174
+ task,
175
+ variables,
176
+ });
160
177
  }
161
178
  else
162
179
  script = task.script;
@@ -168,7 +185,8 @@ CREATE TABLE IF NOT EXISTS ${adapter.eventTableFull}
168
185
  catch (e) {
169
186
  let msg = `Error in task "${task.title}"`;
170
187
  if (task.filename)
171
- msg += '\n at ' + path_1.default.relative(migrationPackage.baseDir, task.filename);
188
+ msg +=
189
+ '\n at ' + path_1.default.relative(migrationPackage.baseDir, task.filename);
172
190
  if (e.lineNr) {
173
191
  if (!task.filename)
174
192
  e.message += '\n at';
@@ -187,7 +205,9 @@ CREATE TABLE IF NOT EXISTS ${adapter.eventTableFull}
187
205
  }
188
206
  if ((0, migration_package_js_1.isInsertDataMigrationTask)(task)) {
189
207
  const tableName = this.replaceVariables(task.tableName, variables);
190
- const script = task.rows.map(row => this.rowToSql(tableName, row)).join('\n');
208
+ const script = task.rows
209
+ .map(row => this.rowToSql(tableName, row))
210
+ .join('\n');
191
211
  await this._connection.execute(script);
192
212
  }
193
213
  }
@@ -24,7 +24,10 @@ class DbMigrator extends strict_typed_events_1.AsyncEventEmitter {
24
24
  let migrationAdapter;
25
25
  switch (options.connection.dialect) {
26
26
  case 'postgres': {
27
- migrationAdapter = await pg_migration_adapter_js_1.PgMigrationAdapter.create({ ...options, migrationPackage });
27
+ migrationAdapter = await pg_migration_adapter_js_1.PgMigrationAdapter.create({
28
+ ...options,
29
+ migrationPackage,
30
+ });
28
31
  break;
29
32
  }
30
33
  default:
@@ -32,7 +35,8 @@ class DbMigrator extends strict_typed_events_1.AsyncEventEmitter {
32
35
  }
33
36
  let needBackup = false;
34
37
  try {
35
- if (migrationAdapter.version && migrationAdapter.version < minVersion - 1) {
38
+ if (migrationAdapter.version &&
39
+ migrationAdapter.version < minVersion - 1) {
36
40
  // noinspection ExceptionCaughtLocallyJS
37
41
  throw new Error(`This package can migrate starting from ${minVersion - 1} but current version is ${migrationAdapter.version}`);
38
42
  }
@@ -51,7 +55,8 @@ class DbMigrator extends strict_typed_events_1.AsyncEventEmitter {
51
55
  let migrationIndex = -1;
52
56
  for (const migration of migrations) {
53
57
  migrationIndex++;
54
- if (migration.version > targetVersion || migrationAdapter.version >= migration.version)
58
+ if (migration.version > targetVersion ||
59
+ migrationAdapter.version >= migration.version)
55
60
  continue;
56
61
  await this.emitAsync('migration-start', {
57
62
  migration,
@@ -99,9 +104,17 @@ class DbMigrator extends strict_typed_events_1.AsyncEventEmitter {
99
104
  // noinspection ExceptionCaughtLocallyJS
100
105
  throw e;
101
106
  }
102
- await this.emitAsync('task-finish', { migration, task, total, index });
107
+ await this.emitAsync('task-finish', {
108
+ migration,
109
+ task,
110
+ total,
111
+ index,
112
+ });
103
113
  }
104
- await migrationAdapter.update({ version: migration.version, status: types_js_1.MigrationStatus.idle });
114
+ await migrationAdapter.update({
115
+ version: migration.version,
116
+ status: types_js_1.MigrationStatus.idle,
117
+ });
105
118
  await this.emitAsync('migration-finish', {
106
119
  migration,
107
120
  total: migrations.length,
@@ -10,10 +10,13 @@ const promises_1 = tslib_1.__importDefault(require("fs/promises"));
10
10
  const path_1 = tslib_1.__importDefault(require("path"));
11
11
  const get_calling_filename_js_1 = require("./utils/get-calling-filename.js");
12
12
  function isSqlScriptMigrationTask(x) {
13
- return typeof x === 'object' && (typeof x.script === 'string' || typeof x.script === 'function');
13
+ return (typeof x === 'object' &&
14
+ (typeof x.script === 'string' || typeof x.script === 'function'));
14
15
  }
15
16
  function isInsertDataMigrationTask(x) {
16
- return typeof x === 'object' && typeof x.tableName === 'string' && Array.isArray(x.rows);
17
+ return (typeof x === 'object' &&
18
+ typeof x.tableName === 'string' &&
19
+ Array.isArray(x.rows));
17
20
  }
18
21
  function isCustomMigrationTask(x) {
19
22
  return typeof x === 'object' && typeof x.fn === 'function';
@@ -45,7 +48,11 @@ var MigrationPackage;
45
48
  }
46
49
  srcMigrations.sort((a, b) => a.version - b.version);
47
50
  for (const migration of srcMigrations) {
48
- const trgMigration = { baseDir: '', ...migration, tasks: [] };
51
+ const trgMigration = {
52
+ baseDir: '',
53
+ ...migration,
54
+ tasks: [],
55
+ };
49
56
  trgMigrations.push(trgMigration);
50
57
  const srcTasks = migration.tasks;
51
58
  trgMigration.tasks = [];
@@ -75,7 +82,9 @@ var MigrationPackage;
75
82
  }
76
83
  else if (['.json', '.js', '.ts', '.cjs', '.mjs'].includes(ext)) {
77
84
  try {
78
- let json = ext === '.json' ? JSON.parse(await promises_1.default.readFile(filename, 'utf-8')) : await Promise.resolve(`${filename}`).then(s => tslib_1.__importStar(require(s)));
85
+ let json = ext === '.json'
86
+ ? JSON.parse(await promises_1.default.readFile(filename, 'utf-8'))
87
+ : await Promise.resolve(`${filename}`).then(s => tslib_1.__importStar(require(s)));
79
88
  if (typeof json !== 'object')
80
89
  continue;
81
90
  if (json.__esModule)
@@ -87,7 +96,8 @@ var MigrationPackage;
87
96
  continue;
88
97
  }
89
98
  if (json.tableName && json.rows) {
90
- json.title = json.title || 'Migrate data into ' + json.tableName;
99
+ json.title =
100
+ json.title || 'Migrate data into ' + json.tableName;
91
101
  json.filename = filename;
92
102
  trgMigration.tasks.push(json);
93
103
  continue;
@@ -114,7 +124,10 @@ var MigrationPackage;
114
124
  })(MigrationPackage || (exports.MigrationPackage = MigrationPackage = {}));
115
125
  async function loadMigrations(baseDir, pattern) {
116
126
  const out = [];
117
- const files = await (0, fast_glob_1.default)(path_1.default.join(baseDir, pattern), { absolute: true, onlyFiles: true });
127
+ const files = await (0, fast_glob_1.default)(path_1.default.join(baseDir, pattern), {
128
+ absolute: true,
129
+ onlyFiles: true,
130
+ });
118
131
  for (const filename of files) {
119
132
  const ext = path_1.default.extname(filename).toLowerCase();
120
133
  if (path_1.default.basename(filename, ext) !== 'migration')
@@ -134,7 +147,10 @@ async function loadMigrations(baseDir, pattern) {
134
147
  throw e;
135
148
  }
136
149
  }
137
- if (json && typeof json === 'object' && json.version && Array.isArray(json.tasks)) {
150
+ if (json &&
151
+ typeof json === 'object' &&
152
+ json.version &&
153
+ Array.isArray(json.tasks)) {
138
154
  json.baseDir = path_1.default.relative(baseDir, path_1.default.dirname(filename));
139
155
  out.push(json);
140
156
  }
@@ -13,7 +13,9 @@ function getCallingFilename(position = 0) {
13
13
  if (stack !== null && typeof stack === 'object') {
14
14
  // stack[0] holds this file
15
15
  // stack[1] holds where this function was called
16
- const s = stack[position] ? stack[position].getFileName() : undefined;
16
+ const s = stack[position]
17
+ ? stack[position].getFileName()
18
+ : undefined;
17
19
  const m = s ? PATH_PATTERN.exec(s) : undefined;
18
20
  return m ? m[1] : '';
19
21
  }
@@ -39,7 +39,8 @@ export class PgMigrationAdapter extends MigrationAdapter {
39
39
  }
40
40
  static async create(options) {
41
41
  // Create connection
42
- const connection = (await pgAdapter.connect(options.connection)).intlcon;
42
+ const connection = (await pgAdapter.connect(options.connection))
43
+ .intlcon;
43
44
  try {
44
45
  const adapter = new PgMigrationAdapter();
45
46
  adapter._connection = connection;
@@ -47,8 +48,11 @@ export class PgMigrationAdapter extends MigrationAdapter {
47
48
  adapter._infoSchema = options.infoSchema || '__migration';
48
49
  adapter.defaultVariables.schema = options.connection.schema || '';
49
50
  if (!adapter.defaultVariables.schema) {
50
- const r = await connection.query('SELECT CURRENT_SCHEMA ', { objectRows: true });
51
- adapter.defaultVariables.schema = r.rows?.[0]?.current_schema || 'public';
51
+ const r = await connection.query('SELECT CURRENT_SCHEMA ', {
52
+ objectRows: true,
53
+ });
54
+ adapter.defaultVariables.schema =
55
+ r.rows?.[0]?.current_schema || 'public';
52
56
  }
53
57
  // Check if migration schema
54
58
  await connection.query(`CREATE SCHEMA IF NOT EXISTS ${adapter.infoSchema} AUTHORIZATION postgres;`);
@@ -140,7 +144,15 @@ CREATE TABLE IF NOT EXISTS ${adapter.eventTableFull}
140
144
  '(package_name, version, event, event_time, title, message, filename, details) ' +
141
145
  'values ($1, $2, $3, CURRENT_TIMESTAMP, $4, $5, $6, $7)';
142
146
  await this._connection.query(sql, {
143
- params: [this.packageName, event.version, event.event, event.title, event.message, event.filename, event.details],
147
+ params: [
148
+ this.packageName,
149
+ event.version,
150
+ event.event,
151
+ event.title,
152
+ event.message,
153
+ event.filename,
154
+ event.details,
155
+ ],
144
156
  });
145
157
  }
146
158
  async executeTask(migrationPackage, migration, task, variables) {
@@ -152,7 +164,12 @@ CREATE TABLE IF NOT EXISTS ${adapter.eventTableFull}
152
164
  try {
153
165
  let script;
154
166
  if (typeof task.script === 'function') {
155
- script = await task.script({ migrationPackage, migration, task, variables });
167
+ script = await task.script({
168
+ migrationPackage,
169
+ migration,
170
+ task,
171
+ variables,
172
+ });
156
173
  }
157
174
  else
158
175
  script = task.script;
@@ -164,7 +181,8 @@ CREATE TABLE IF NOT EXISTS ${adapter.eventTableFull}
164
181
  catch (e) {
165
182
  let msg = `Error in task "${task.title}"`;
166
183
  if (task.filename)
167
- msg += '\n at ' + path.relative(migrationPackage.baseDir, task.filename);
184
+ msg +=
185
+ '\n at ' + path.relative(migrationPackage.baseDir, task.filename);
168
186
  if (e.lineNr) {
169
187
  if (!task.filename)
170
188
  e.message += '\n at';
@@ -183,7 +201,9 @@ CREATE TABLE IF NOT EXISTS ${adapter.eventTableFull}
183
201
  }
184
202
  if (isInsertDataMigrationTask(task)) {
185
203
  const tableName = this.replaceVariables(task.tableName, variables);
186
- const script = task.rows.map(row => this.rowToSql(tableName, row)).join('\n');
204
+ const script = task.rows
205
+ .map(row => this.rowToSql(tableName, row))
206
+ .join('\n');
187
207
  await this._connection.execute(script);
188
208
  }
189
209
  }
@@ -1,7 +1,7 @@
1
1
  import { AsyncEventEmitter } from 'strict-typed-events';
2
2
  import { PgMigrationAdapter } from './adapters/pg-migration-adapter.js';
3
3
  import { MigrationAdapter } from './migration-adapter.js';
4
- import { MigrationPackage } from './migration-package.js';
4
+ import { MigrationPackage, } from './migration-package.js';
5
5
  import { MigrationStatus } from './types.js';
6
6
  export class DbMigrator extends AsyncEventEmitter {
7
7
  async execute(options) {
@@ -21,7 +21,10 @@ export class DbMigrator extends AsyncEventEmitter {
21
21
  let migrationAdapter;
22
22
  switch (options.connection.dialect) {
23
23
  case 'postgres': {
24
- migrationAdapter = await PgMigrationAdapter.create({ ...options, migrationPackage });
24
+ migrationAdapter = await PgMigrationAdapter.create({
25
+ ...options,
26
+ migrationPackage,
27
+ });
25
28
  break;
26
29
  }
27
30
  default:
@@ -29,7 +32,8 @@ export class DbMigrator extends AsyncEventEmitter {
29
32
  }
30
33
  let needBackup = false;
31
34
  try {
32
- if (migrationAdapter.version && migrationAdapter.version < minVersion - 1) {
35
+ if (migrationAdapter.version &&
36
+ migrationAdapter.version < minVersion - 1) {
33
37
  // noinspection ExceptionCaughtLocallyJS
34
38
  throw new Error(`This package can migrate starting from ${minVersion - 1} but current version is ${migrationAdapter.version}`);
35
39
  }
@@ -48,7 +52,8 @@ export class DbMigrator extends AsyncEventEmitter {
48
52
  let migrationIndex = -1;
49
53
  for (const migration of migrations) {
50
54
  migrationIndex++;
51
- if (migration.version > targetVersion || migrationAdapter.version >= migration.version)
55
+ if (migration.version > targetVersion ||
56
+ migrationAdapter.version >= migration.version)
52
57
  continue;
53
58
  await this.emitAsync('migration-start', {
54
59
  migration,
@@ -96,9 +101,17 @@ export class DbMigrator extends AsyncEventEmitter {
96
101
  // noinspection ExceptionCaughtLocallyJS
97
102
  throw e;
98
103
  }
99
- await this.emitAsync('task-finish', { migration, task, total, index });
104
+ await this.emitAsync('task-finish', {
105
+ migration,
106
+ task,
107
+ total,
108
+ index,
109
+ });
100
110
  }
101
- await migrationAdapter.update({ version: migration.version, status: MigrationStatus.idle });
111
+ await migrationAdapter.update({
112
+ version: migration.version,
113
+ status: MigrationStatus.idle,
114
+ });
102
115
  await this.emitAsync('migration-finish', {
103
116
  migration,
104
117
  total: migrations.length,
@@ -3,10 +3,13 @@ import fs from 'fs/promises';
3
3
  import path from 'path';
4
4
  import { getCallingFilename } from './utils/get-calling-filename.js';
5
5
  export function isSqlScriptMigrationTask(x) {
6
- return typeof x === 'object' && (typeof x.script === 'string' || typeof x.script === 'function');
6
+ return (typeof x === 'object' &&
7
+ (typeof x.script === 'string' || typeof x.script === 'function'));
7
8
  }
8
9
  export function isInsertDataMigrationTask(x) {
9
- return typeof x === 'object' && typeof x.tableName === 'string' && Array.isArray(x.rows);
10
+ return (typeof x === 'object' &&
11
+ typeof x.tableName === 'string' &&
12
+ Array.isArray(x.rows));
10
13
  }
11
14
  export function isCustomMigrationTask(x) {
12
15
  return typeof x === 'object' && typeof x.fn === 'function';
@@ -38,7 +41,11 @@ export var MigrationPackage;
38
41
  }
39
42
  srcMigrations.sort((a, b) => a.version - b.version);
40
43
  for (const migration of srcMigrations) {
41
- const trgMigration = { baseDir: '', ...migration, tasks: [] };
44
+ const trgMigration = {
45
+ baseDir: '',
46
+ ...migration,
47
+ tasks: [],
48
+ };
42
49
  trgMigrations.push(trgMigration);
43
50
  const srcTasks = migration.tasks;
44
51
  trgMigration.tasks = [];
@@ -68,7 +75,9 @@ export var MigrationPackage;
68
75
  }
69
76
  else if (['.json', '.js', '.ts', '.cjs', '.mjs'].includes(ext)) {
70
77
  try {
71
- let json = ext === '.json' ? JSON.parse(await fs.readFile(filename, 'utf-8')) : await import(filename);
78
+ let json = ext === '.json'
79
+ ? JSON.parse(await fs.readFile(filename, 'utf-8'))
80
+ : await import(filename);
72
81
  if (typeof json !== 'object')
73
82
  continue;
74
83
  if (json.__esModule)
@@ -80,7 +89,8 @@ export var MigrationPackage;
80
89
  continue;
81
90
  }
82
91
  if (json.tableName && json.rows) {
83
- json.title = json.title || 'Migrate data into ' + json.tableName;
92
+ json.title =
93
+ json.title || 'Migrate data into ' + json.tableName;
84
94
  json.filename = filename;
85
95
  trgMigration.tasks.push(json);
86
96
  continue;
@@ -107,7 +117,10 @@ export var MigrationPackage;
107
117
  })(MigrationPackage || (MigrationPackage = {}));
108
118
  async function loadMigrations(baseDir, pattern) {
109
119
  const out = [];
110
- const files = await glob(path.join(baseDir, pattern), { absolute: true, onlyFiles: true });
120
+ const files = await glob(path.join(baseDir, pattern), {
121
+ absolute: true,
122
+ onlyFiles: true,
123
+ });
111
124
  for (const filename of files) {
112
125
  const ext = path.extname(filename).toLowerCase();
113
126
  if (path.basename(filename, ext) !== 'migration')
@@ -127,7 +140,10 @@ async function loadMigrations(baseDir, pattern) {
127
140
  throw e;
128
141
  }
129
142
  }
130
- if (json && typeof json === 'object' && json.version && Array.isArray(json.tasks)) {
143
+ if (json &&
144
+ typeof json === 'object' &&
145
+ json.version &&
146
+ Array.isArray(json.tasks)) {
131
147
  json.baseDir = path.relative(baseDir, path.dirname(filename));
132
148
  out.push(json);
133
149
  }
@@ -10,7 +10,9 @@ export function getCallingFilename(position = 0) {
10
10
  if (stack !== null && typeof stack === 'object') {
11
11
  // stack[0] holds this file
12
12
  // stack[1] holds where this function was called
13
- const s = stack[position] ? stack[position].getFileName() : undefined;
13
+ const s = stack[position]
14
+ ? stack[position].getFileName()
15
+ : undefined;
14
16
  const m = s ? PATH_PATTERN.exec(s) : undefined;
15
17
  return m ? m[1] : '';
16
18
  }
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "@sqb/migrator",
3
3
  "description": "Database migrator for SQB",
4
- "version": "4.19.4",
4
+ "version": "4.19.6",
5
5
  "author": "Panates",
6
6
  "license": "Apache-2.0",
7
7
  "dependencies": {
8
8
  "fast-glob": "^3.3.2",
9
9
  "strict-typed-events": "^2.8.0",
10
- "ts-gems": "^3.5.1",
11
- "tslib": "^2.8.0"
10
+ "ts-gems": "^3.6.0",
11
+ "tslib": "^2.8.1"
12
12
  },
13
13
  "peerDependencies": {
14
- "@sqb/builder": "^4.19.4",
15
- "@sqb/connect": "^4.19.4",
16
- "@sqb/postgres": "^4.19.4"
14
+ "@sqb/builder": "^4.19.6",
15
+ "@sqb/connect": "^4.19.6",
16
+ "@sqb/postgres": "^4.19.6"
17
17
  },
18
18
  "type": "module",
19
19
  "exports": {
@@ -62,5 +62,8 @@
62
62
  "db",
63
63
  "migrate",
64
64
  "migrator"
65
- ]
65
+ ],
66
+ "publishConfig": {
67
+ "access": "public"
68
+ }
66
69
  }