dbgate-tools 5.2.2-alpha.13 → 5.2.3

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.
@@ -228,6 +228,9 @@ class DatabaseAnalyser {
228
228
  if (this.pool.feedback) {
229
229
  this.pool.feedback(obj);
230
230
  }
231
+ if (obj && obj.analysingMessage) {
232
+ logger.debug(obj.analysingMessage);
233
+ }
231
234
  }
232
235
  getModifications() {
233
236
  return __awaiter(this, void 0, void 0, function* () {
@@ -13,6 +13,7 @@ export declare class ScriptWriter {
13
13
  copyStream(sourceVar: any, targetVar: any, colmapVar?: any): void;
14
14
  dumpDatabase(options: any): void;
15
15
  importDatabase(options: any): void;
16
+ dataDuplicator(options: any): void;
16
17
  comment(s: any): void;
17
18
  getScript(schedule?: any): string;
18
19
  }
@@ -30,6 +31,7 @@ export declare class ScriptWriterJson {
30
31
  comment(text: any): void;
31
32
  dumpDatabase(options: any): void;
32
33
  importDatabase(options: any): void;
34
+ dataDuplicator(options: any): void;
33
35
  getScript(schedule?: any): {
34
36
  type: string;
35
37
  schedule: any;
@@ -51,6 +51,9 @@ class ScriptWriter {
51
51
  importDatabase(options) {
52
52
  this._put(`await dbgateApi.importDatabase(${JSON.stringify(options)});`);
53
53
  }
54
+ dataDuplicator(options) {
55
+ this._put(`await dbgateApi.dataDuplicator(${JSON.stringify(options)});`);
56
+ }
54
57
  comment(s) {
55
58
  this._put(`// ${s}`);
56
59
  }
@@ -126,6 +129,12 @@ class ScriptWriterJson {
126
129
  options,
127
130
  });
128
131
  }
132
+ dataDuplicator(options) {
133
+ this.commands.push({
134
+ type: 'dataDuplicator',
135
+ options,
136
+ });
137
+ }
129
138
  getScript(schedule = null) {
130
139
  return {
131
140
  type: 'json',
@@ -168,6 +177,9 @@ function jsonScriptToJavascript(json) {
168
177
  case 'importDatabase':
169
178
  script.importDatabase(cmd.options);
170
179
  break;
180
+ case 'dataDuplicator':
181
+ script.dataDuplicator(cmd.options);
182
+ break;
171
183
  }
172
184
  }
173
185
  return script.getScript(schedule);
@@ -21,6 +21,7 @@ export declare class SqlDumper implements AlterProcessor {
21
21
  createDatabase(name: string): void;
22
22
  dropDatabase(name: string): void;
23
23
  specialColumnOptions(column: any): void;
24
+ selectScopeIdentity(table: TableInfo): void;
24
25
  columnDefinition(column: ColumnInfo, { includeDefault, includeNullable, includeCollate }?: {
25
26
  includeDefault?: boolean;
26
27
  includeNullable?: boolean;
@@ -29,6 +30,7 @@ export declare class SqlDumper implements AlterProcessor {
29
30
  columnDefault(column: ColumnInfo): void;
30
31
  putCollection<T>(delimiter: string, collection: T[], lambda: (col: T) => void): void;
31
32
  createTable(table: TableInfo): void;
33
+ createTablePrimaryKeyCore(table: TableInfo): void;
32
34
  createForeignKeyFore(fk: ForeignKeyInfo): void;
33
35
  transform(type: TransformType, dumpExpr: any): void;
34
36
  allowIdentityInsert(table: NamedObjectInfo, allow: boolean): void;
@@ -98,6 +100,7 @@ export declare class SqlDumper implements AlterProcessor {
98
100
  renameTable(obj: TableInfo, newname: string): void;
99
101
  beginTransaction(): void;
100
102
  commitTransaction(): void;
103
+ rollbackTransaction(): void;
101
104
  alterProlog(): void;
102
105
  alterEpilog(): void;
103
106
  selectTableIntoNewTable(sourceName: NamedObjectInfo, targetName: NamedObjectInfo): void;
package/lib/SqlDumper.js CHANGED
@@ -183,6 +183,7 @@ class SqlDumper {
183
183
  this.putCmd('^drop ^database %i', name);
184
184
  }
185
185
  specialColumnOptions(column) { }
186
+ selectScopeIdentity(table) { }
186
187
  columnDefinition(column, { includeDefault = true, includeNullable = true, includeCollate = true } = {}) {
187
188
  var _a;
188
189
  if (column.computedExpression) {
@@ -229,13 +230,7 @@ class SqlDumper {
229
230
  this.put('%i ', col.columnName);
230
231
  this.columnDefinition(col);
231
232
  });
232
- if (table.primaryKey) {
233
- this.put(',&n');
234
- if (table.primaryKey.constraintName) {
235
- this.put('^constraint %i', table.primaryKey.constraintName);
236
- }
237
- this.put(' ^primary ^key (%,i)', table.primaryKey.columns.map(x => x.columnName));
238
- }
233
+ this.createTablePrimaryKeyCore(table);
239
234
  (table.foreignKeys || []).forEach(fk => {
240
235
  this.put(',&n');
241
236
  this.createForeignKeyFore(fk);
@@ -254,6 +249,15 @@ class SqlDumper {
254
249
  this.createIndex(ix);
255
250
  });
256
251
  }
252
+ createTablePrimaryKeyCore(table) {
253
+ if (table.primaryKey) {
254
+ this.put(',&n');
255
+ if (table.primaryKey.constraintName) {
256
+ this.put('^constraint %i', table.primaryKey.constraintName);
257
+ }
258
+ this.put(' ^primary ^key (%,i)', table.primaryKey.columns.map(x => x.columnName));
259
+ }
260
+ }
257
261
  createForeignKeyFore(fk) {
258
262
  if (fk.constraintName != null)
259
263
  this.put('^constraint %i ', fk.constraintName);
@@ -481,6 +485,9 @@ class SqlDumper {
481
485
  commitTransaction() {
482
486
  this.putCmd('^commit');
483
487
  }
488
+ rollbackTransaction() {
489
+ this.putCmd('^rollback');
490
+ }
484
491
  alterProlog() { }
485
492
  alterEpilog() { }
486
493
  selectTableIntoNewTable(sourceName, targetName) {
@@ -0,0 +1,4 @@
1
+ export interface AsyncWriteStreamOptions {
2
+ processItem: (chunk: any) => Promise<void>;
3
+ }
4
+ export declare function createAsyncWriteStream(stream: any, options: AsyncWriteStreamOptions): any;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.createAsyncWriteStream = void 0;
13
+ const getLogger_1 = require("./getLogger");
14
+ const logger = (0, getLogger_1.getLogger)('asyncWriteStream');
15
+ function createAsyncWriteStream(stream, options) {
16
+ const writable = new stream.Writable({
17
+ objectMode: true,
18
+ });
19
+ writable._write = (chunk, encoding, callback) => __awaiter(this, void 0, void 0, function* () {
20
+ try {
21
+ yield options.processItem(chunk);
22
+ callback(null);
23
+ }
24
+ catch (err) {
25
+ callback(err);
26
+ }
27
+ });
28
+ // writable._final = async callback => {
29
+ // callback();
30
+ // };
31
+ return writable;
32
+ }
33
+ exports.createAsyncWriteStream = createAsyncWriteStream;
@@ -1 +1,2 @@
1
- export declare function createBulkInsertStreamBase(driver: any, stream: any, pool: any, name: any, options: any): any;
1
+ import { EngineDriver, WriteTableOptions } from 'dbgate-types';
2
+ export declare function createBulkInsertStreamBase(driver: EngineDriver, stream: any, pool: any, name: any, options: WriteTableOptions): any;
@@ -75,7 +75,7 @@ function createBulkInsertStreamBase(driver, stream, pool, name, options) {
75
75
  dmp.putRaw(';');
76
76
  // require('fs').writeFileSync('/home/jena/test.sql', dmp.s);
77
77
  // console.log(dmp.s);
78
- yield driver.query(pool, dmp.s);
78
+ yield driver.query(pool, dmp.s, { discardResult: true });
79
79
  });
80
80
  writable.sendIfFull = () => __awaiter(this, void 0, void 0, function* () {
81
81
  if (writable.buffer.length > 100) {
@@ -17,6 +17,7 @@ export interface DbDiffOptions {
17
17
  ignoreDataTypes?: boolean;
18
18
  }
19
19
  export declare function generateTablePairingId(table: TableInfo): TableInfo;
20
+ export declare function removeTablePairingId(table: TableInfo): TableInfo;
20
21
  export declare function generateDbPairingId(db: DatabaseInfo): DatabaseInfo;
21
22
  export declare function testEqualColumns(a: ColumnInfo, b: ColumnInfo, checkName: boolean, checkDefault: boolean, opts?: DbDiffOptions): boolean;
22
23
  export declare function testEqualTypes(a: ColumnInfo, b: ColumnInfo, opts?: DbDiffOptions): boolean;
package/lib/diffTools.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.modelCompareDbDiffOptions = exports.matchPairedObjects = exports.getAlterDatabaseScript = exports.getAlterTableScript = exports.createAlterDatabasePlan = exports.createAlterTablePlan = exports.testEqualSqlObjects = exports.testEqualTables = exports.testEqualTypes = exports.testEqualColumns = exports.generateDbPairingId = exports.generateTablePairingId = void 0;
6
+ exports.modelCompareDbDiffOptions = exports.matchPairedObjects = exports.getAlterDatabaseScript = exports.getAlterTableScript = exports.createAlterDatabasePlan = exports.createAlterTablePlan = exports.testEqualSqlObjects = exports.testEqualTables = exports.testEqualTypes = exports.testEqualColumns = exports.generateDbPairingId = exports.removeTablePairingId = exports.generateTablePairingId = void 0;
7
7
  const v1_1 = __importDefault(require("uuid/v1"));
8
8
  const alterPlan_1 = require("./alterPlan");
9
9
  const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
@@ -21,6 +21,13 @@ function generateTablePairingId(table) {
21
21
  return table;
22
22
  }
23
23
  exports.generateTablePairingId = generateTablePairingId;
24
+ function removeTablePairingId(table) {
25
+ var _a, _b, _c, _d, _e;
26
+ if (!table)
27
+ return table;
28
+ return Object.assign(Object.assign({}, table), { columns: (_a = table.columns) === null || _a === void 0 ? void 0 : _a.map(col => (Object.assign(Object.assign({}, col), { pairingId: undefined }))), foreignKeys: (_b = table.foreignKeys) === null || _b === void 0 ? void 0 : _b.map(cnt => (Object.assign(Object.assign({}, cnt), { pairingId: undefined }))), checks: (_c = table.checks) === null || _c === void 0 ? void 0 : _c.map(cnt => (Object.assign(Object.assign({}, cnt), { pairingId: undefined }))), indexes: (_d = table.indexes) === null || _d === void 0 ? void 0 : _d.map(cnt => (Object.assign(Object.assign({}, cnt), { pairingId: undefined }))), uniques: (_e = table.uniques) === null || _e === void 0 ? void 0 : _e.map(cnt => (Object.assign(Object.assign({}, cnt), { pairingId: undefined }))), pairingId: undefined });
29
+ }
30
+ exports.removeTablePairingId = removeTablePairingId;
24
31
  function generateObjectPairingId(obj) {
25
32
  if (obj.objectTypeField)
26
33
  return Object.assign(Object.assign({}, obj), { pairingId: obj.pairingId || (0, v1_1.default)() });
@@ -375,11 +382,7 @@ function getAlterTableScript(oldTable, newTable, opts, wholeOldDb, wholeNewDb, d
375
382
  }
376
383
  const plan = createAlterTablePlan(oldTable, newTable, opts, wholeOldDb, wholeNewDb, driver);
377
384
  const dmp = driver.createDumper({ useHardSeparator: true });
378
- if (!driver.dialect.disableExplicitTransaction)
379
- dmp.beginTransaction();
380
385
  plan.run(dmp);
381
- if (!driver.dialect.disableExplicitTransaction)
382
- dmp.commitTransaction();
383
386
  return {
384
387
  sql: dmp.s,
385
388
  recreates: plan.recreates,
@@ -389,11 +392,7 @@ exports.getAlterTableScript = getAlterTableScript;
389
392
  function getAlterDatabaseScript(oldDb, newDb, opts, wholeOldDb, wholeNewDb, driver) {
390
393
  const plan = createAlterDatabasePlan(oldDb, newDb, opts, wholeOldDb, wholeNewDb, driver);
391
394
  const dmp = driver.createDumper({ useHardSeparator: true });
392
- if (!driver.dialect.disableExplicitTransaction)
393
- dmp.beginTransaction();
394
395
  plan.run(dmp);
395
- if (!driver.dialect.disableExplicitTransaction)
396
- dmp.commitTransaction();
397
396
  return {
398
397
  sql: dmp.s,
399
398
  recreates: plan.recreates,
@@ -1,4 +1,7 @@
1
1
  import { SqlDumper } from './SqlDumper';
2
+ import { EngineDriver, QueryResult, RunScriptOptions } from 'dbgate-types';
3
+ export declare function runCommandOnDriver(pool: any, driver: EngineDriver, cmd: (dmp: SqlDumper) => void): Promise<void>;
4
+ export declare function runQueryOnDriver(pool: any, driver: EngineDriver, cmd: (dmp: SqlDumper) => void): Promise<QueryResult>;
2
5
  export declare const driverBase: {
3
6
  analyserClass: any;
4
7
  dumperClass: typeof SqlDumper;
@@ -21,8 +24,8 @@ export declare const driverBase: {
21
24
  analyseSingleObject(pool: any, name: any, typeField?: string): Promise<any>;
22
25
  analyseSingleTable(pool: any, name: any): any;
23
26
  analyseIncremental(pool: any, structure: any, version: any): Promise<any>;
24
- createDumper(options?: any): any;
25
- script(pool: any, sql: any): Promise<void>;
27
+ createDumper(options?: any): SqlDumper;
28
+ script(pool: any, sql: any, options: RunScriptOptions): Promise<void>;
26
29
  getNewObjectTemplates(): {
27
30
  label: string;
28
31
  sql: string;
package/lib/driverBase.js CHANGED
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.driverBase = void 0;
15
+ exports.driverBase = exports.runQueryOnDriver = exports.runCommandOnDriver = void 0;
16
16
  const compact_1 = __importDefault(require("lodash/compact"));
17
17
  const SqlDumper_1 = require("./SqlDumper");
18
18
  const dbgate_query_splitter_1 = require("dbgate-query-splitter");
@@ -32,6 +32,24 @@ const dialect = {
32
32
  },
33
33
  defaultSchemaName: null,
34
34
  };
35
+ function runCommandOnDriver(pool, driver, cmd) {
36
+ return __awaiter(this, void 0, void 0, function* () {
37
+ const dmp = driver.createDumper();
38
+ cmd(dmp);
39
+ // console.log('CMD:', dmp.s);
40
+ yield driver.query(pool, dmp.s, { discardResult: true });
41
+ });
42
+ }
43
+ exports.runCommandOnDriver = runCommandOnDriver;
44
+ function runQueryOnDriver(pool, driver, cmd) {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ const dmp = driver.createDumper();
47
+ cmd(dmp);
48
+ // console.log('QUERY:', dmp.s);
49
+ return yield driver.query(pool, dmp.s);
50
+ });
51
+ }
52
+ exports.runQueryOnDriver = runQueryOnDriver;
35
53
  exports.driverBase = {
36
54
  analyserClass: null,
37
55
  dumperClass: SqlDumper_1.SqlDumper,
@@ -62,10 +80,24 @@ exports.driverBase = {
62
80
  createDumper(options = null) {
63
81
  return new this.dumperClass(this, options);
64
82
  },
65
- script(pool, sql) {
83
+ script(pool, sql, options) {
66
84
  return __awaiter(this, void 0, void 0, function* () {
85
+ if (options === null || options === void 0 ? void 0 : options.useTransaction) {
86
+ runCommandOnDriver(pool, this, dmp => dmp.beginTransaction());
87
+ }
67
88
  for (const sqlItem of (0, dbgate_query_splitter_1.splitQuery)(sql, this.getQuerySplitterOptions('script'))) {
68
- yield this.query(pool, sqlItem, { discardResult: true });
89
+ try {
90
+ yield this.query(pool, sqlItem, { discardResult: true });
91
+ }
92
+ catch (err) {
93
+ if (options === null || options === void 0 ? void 0 : options.useTransaction) {
94
+ runCommandOnDriver(pool, this, dmp => dmp.rollbackTransaction());
95
+ }
96
+ throw err;
97
+ }
98
+ }
99
+ if (options === null || options === void 0 ? void 0 : options.useTransaction) {
100
+ runCommandOnDriver(pool, this, dmp => dmp.commitTransaction());
69
101
  }
70
102
  });
71
103
  },
@@ -131,5 +163,5 @@ exports.driverBase = {
131
163
  return this.readQuery(pool, dmp.s, structure);
132
164
  },
133
165
  showConnectionField: (field, values) => false,
134
- showConnectionTab: (field) => true,
166
+ showConnectionTab: field => true,
135
167
  };
package/lib/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export * from './nameTools';
3
3
  export * from './tableTransforms';
4
4
  export * from './packageTools';
5
5
  export * from './createBulkInsertStreamBase';
6
+ export * from './createAsyncWriteStream';
6
7
  export * from './DatabaseAnalyser';
7
8
  export * from './driverBase';
8
9
  export * from './SqlDumper';
package/lib/index.js CHANGED
@@ -19,6 +19,7 @@ __exportStar(require("./nameTools"), exports);
19
19
  __exportStar(require("./tableTransforms"), exports);
20
20
  __exportStar(require("./packageTools"), exports);
21
21
  __exportStar(require("./createBulkInsertStreamBase"), exports);
22
+ __exportStar(require("./createAsyncWriteStream"), exports);
22
23
  __exportStar(require("./DatabaseAnalyser"), exports);
23
24
  __exportStar(require("./driverBase"), exports);
24
25
  __exportStar(require("./SqlDumper"), exports);
@@ -1,11 +1,18 @@
1
1
  import type { ColumnInfo, ConstraintInfo, TableInfo } from 'dbgate-types';
2
+ export interface JsonDataObjectUpdateCommand {
3
+ type: 'renameField' | 'deleteField' | 'setField' | 'setFieldIfNull';
4
+ oldField?: string;
5
+ newField?: string;
6
+ value?: any;
7
+ }
2
8
  export interface EditorColumnInfo extends ColumnInfo {
3
9
  isPrimaryKey?: boolean;
4
10
  }
5
11
  export declare function fillEditorColumnInfo(column: ColumnInfo, table: TableInfo): EditorColumnInfo;
6
- export declare function editorAddColumn(table: TableInfo, column: EditorColumnInfo): TableInfo;
7
- export declare function editorModifyColumn(table: TableInfo, column: EditorColumnInfo): TableInfo;
8
- export declare function editorDeleteColumn(table: TableInfo, column: EditorColumnInfo): TableInfo;
12
+ export declare function processJsonDataUpdateCommands(obj: any, commands?: JsonDataObjectUpdateCommand[]): any;
13
+ export declare function editorAddColumn(table: TableInfo, column: EditorColumnInfo, addDataCommand?: boolean): TableInfo;
14
+ export declare function editorModifyColumn(table: TableInfo, column: EditorColumnInfo, addDataCommand?: boolean): TableInfo;
15
+ export declare function editorDeleteColumn(table: TableInfo, column: EditorColumnInfo, addDataCommand?: boolean): TableInfo;
9
16
  export declare function editorAddConstraint(table: TableInfo, constraint: ConstraintInfo): TableInfo;
10
17
  export declare function editorModifyConstraint(table: TableInfo, constraint: ConstraintInfo): TableInfo;
11
18
  export declare function editorDeleteConstraint(table: TableInfo, constraint: ConstraintInfo): TableInfo;
@@ -3,15 +3,43 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.editorDeleteConstraint = exports.editorModifyConstraint = exports.editorAddConstraint = exports.editorDeleteColumn = exports.editorModifyColumn = exports.editorAddColumn = exports.fillEditorColumnInfo = void 0;
6
+ exports.editorDeleteConstraint = exports.editorModifyConstraint = exports.editorAddConstraint = exports.editorDeleteColumn = exports.editorModifyColumn = exports.editorAddColumn = exports.processJsonDataUpdateCommands = exports.fillEditorColumnInfo = void 0;
7
7
  const v1_1 = __importDefault(require("uuid/v1"));
8
8
  const omit_1 = __importDefault(require("lodash/omit"));
9
9
  const lodash_1 = __importDefault(require("lodash"));
10
+ const stringTools_1 = require("./stringTools");
10
11
  function fillEditorColumnInfo(column, table) {
11
12
  var _a, _b;
12
13
  return Object.assign({ isPrimaryKey: !!((_b = (_a = table === null || table === void 0 ? void 0 : table.primaryKey) === null || _a === void 0 ? void 0 : _a.columns) === null || _b === void 0 ? void 0 : _b.find(x => x.columnName == column.columnName)), dataType: lodash_1.default.isEmpty(column) ? 'int' : undefined }, column);
13
14
  }
14
15
  exports.fillEditorColumnInfo = fillEditorColumnInfo;
16
+ function processJsonDataUpdateCommands(obj, commands = []) {
17
+ for (const cmd of commands) {
18
+ switch (cmd.type) {
19
+ case 'deleteField':
20
+ obj = Object.assign({}, obj);
21
+ delete obj[cmd.oldField];
22
+ break;
23
+ case 'renameField':
24
+ obj = Object.assign({}, obj);
25
+ obj[cmd.newField] = obj[cmd.oldField];
26
+ delete obj[cmd.oldField];
27
+ break;
28
+ case 'setField':
29
+ obj = Object.assign({}, obj);
30
+ obj[cmd.newField] = cmd.value;
31
+ break;
32
+ case 'setFieldIfNull':
33
+ obj = Object.assign({}, obj);
34
+ if (obj[cmd.newField] == null) {
35
+ obj[cmd.newField] = cmd.value;
36
+ }
37
+ break;
38
+ }
39
+ }
40
+ return obj;
41
+ }
42
+ exports.processJsonDataUpdateCommands = processJsonDataUpdateCommands;
15
43
  function processPrimaryKey(table, oldColumn, newColumn) {
16
44
  if (!(oldColumn === null || oldColumn === void 0 ? void 0 : oldColumn.isPrimaryKey) && (newColumn === null || newColumn === void 0 ? void 0 : newColumn.isPrimaryKey)) {
17
45
  let primaryKey = table === null || table === void 0 ? void 0 : table.primaryKey;
@@ -42,23 +70,53 @@ function processPrimaryKey(table, oldColumn, newColumn) {
42
70
  }
43
71
  return table;
44
72
  }
45
- function editorAddColumn(table, column) {
73
+ function defineDataCommand(table, cmd) {
74
+ table['__addDataCommands'] = [...(table['__addDataCommands'] || []), cmd()];
75
+ }
76
+ function editorAddColumn(table, column, addDataCommand) {
46
77
  let res = Object.assign(Object.assign({}, table), { columns: [...((table === null || table === void 0 ? void 0 : table.columns) || []), Object.assign(Object.assign({}, column), { pairingId: (0, v1_1.default)() })] });
47
78
  res = processPrimaryKey(res, null, column);
79
+ if (addDataCommand && column.defaultValue) {
80
+ defineDataCommand(res, () => ({
81
+ type: 'setField',
82
+ newField: column.columnName,
83
+ value: (0, stringTools_1.parseSqlDefaultValue)(column.defaultValue),
84
+ }));
85
+ }
48
86
  return res;
49
87
  }
50
88
  exports.editorAddColumn = editorAddColumn;
51
- function editorModifyColumn(table, column) {
89
+ function editorModifyColumn(table, column, addDataCommand) {
52
90
  var _a;
53
91
  const oldColumn = (_a = table === null || table === void 0 ? void 0 : table.columns) === null || _a === void 0 ? void 0 : _a.find(x => x.pairingId == column.pairingId);
54
92
  let res = Object.assign(Object.assign({}, table), { columns: table.columns.map(col => (col.pairingId == column.pairingId ? (0, omit_1.default)(column, ['isPrimaryKey']) : col)) });
55
93
  res = processPrimaryKey(res, fillEditorColumnInfo(oldColumn, table), column);
94
+ if (addDataCommand && oldColumn.columnName != column.columnName) {
95
+ defineDataCommand(res, () => ({
96
+ type: 'renameField',
97
+ oldField: oldColumn.columnName,
98
+ newField: column.columnName,
99
+ }));
100
+ }
101
+ if (addDataCommand && !oldColumn.defaultValue && column.defaultValue) {
102
+ defineDataCommand(res, () => ({
103
+ type: 'setFieldIfNull',
104
+ newField: column.columnName,
105
+ value: (0, stringTools_1.parseSqlDefaultValue)(column.defaultValue),
106
+ }));
107
+ }
56
108
  return res;
57
109
  }
58
110
  exports.editorModifyColumn = editorModifyColumn;
59
- function editorDeleteColumn(table, column) {
111
+ function editorDeleteColumn(table, column, addDataCommand) {
60
112
  let res = Object.assign(Object.assign({}, table), { columns: table.columns.filter(col => col.pairingId != column.pairingId) });
61
113
  res = processPrimaryKey(res, column, null);
114
+ if (addDataCommand) {
115
+ defineDataCommand(res, () => ({
116
+ type: 'deleteField',
117
+ oldField: column.columnName,
118
+ }));
119
+ }
62
120
  return res;
63
121
  }
64
122
  exports.editorDeleteColumn = editorDeleteColumn;
@@ -8,3 +8,4 @@ export declare function getIconForRedisType(type: any): "img folder" | "img type
8
8
  export declare function isWktGeometry(s: any): boolean;
9
9
  export declare function arrayBufferToBase64(buffer: any): string;
10
10
  export declare function getAsImageSrc(obj: any): string;
11
+ export declare function parseSqlDefaultValue(value: string): string | number;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getAsImageSrc = exports.arrayBufferToBase64 = exports.isWktGeometry = exports.getIconForRedisType = exports.isJsonLikeLongString = exports.safeJsonParse = exports.stringifyCellValue = exports.parseCellValue = exports.hexStringToArray = exports.arrayToHexString = void 0;
6
+ exports.parseSqlDefaultValue = exports.getAsImageSrc = exports.arrayBufferToBase64 = exports.isWktGeometry = exports.getIconForRedisType = exports.isJsonLikeLongString = exports.safeJsonParse = exports.stringifyCellValue = exports.parseCellValue = exports.hexStringToArray = exports.arrayToHexString = void 0;
7
7
  const isString_1 = __importDefault(require("lodash/isString"));
8
8
  const isArray_1 = __importDefault(require("lodash/isArray"));
9
9
  const isPlainObject_1 = __importDefault(require("lodash/isPlainObject"));
@@ -121,3 +121,17 @@ function getAsImageSrc(obj) {
121
121
  return null;
122
122
  }
123
123
  exports.getAsImageSrc = getAsImageSrc;
124
+ function parseSqlDefaultValue(value) {
125
+ if (!value)
126
+ return undefined;
127
+ if (!(0, isString_1.default)(value))
128
+ return undefined;
129
+ if (value.startsWith("'") && value.endsWith("'")) {
130
+ return value.slice(1, -1);
131
+ }
132
+ if (!isNaN(value) && !isNaN(parseFloat(value))) {
133
+ return parseFloat(value);
134
+ }
135
+ return undefined;
136
+ }
137
+ exports.parseSqlDefaultValue = parseSqlDefaultValue;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "5.2.2-alpha.13",
2
+ "version": "5.2.3",
3
3
  "name": "dbgate-tools",
4
4
  "main": "lib/index.js",
5
5
  "typings": "lib/index.d.ts",
@@ -25,14 +25,14 @@
25
25
  ],
26
26
  "devDependencies": {
27
27
  "@types/node": "^13.7.0",
28
- "dbgate-types": "^5.2.2-alpha.13",
28
+ "dbgate-types": "^5.2.3",
29
29
  "jest": "^24.9.0",
30
30
  "ts-jest": "^25.2.1",
31
31
  "typescript": "^4.4.3"
32
32
  },
33
33
  "dependencies": {
34
34
  "dbgate-query-splitter": "^4.9.3",
35
- "dbgate-sqltree": "^5.2.2-alpha.13",
35
+ "dbgate-sqltree": "^5.2.3",
36
36
  "debug": "^4.3.4",
37
37
  "json-stable-stringify": "^1.0.1",
38
38
  "lodash": "^4.17.21",