miqro 1.6.8 → 1.7.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.
Files changed (108) hide show
  1. package/README.md +1 -11
  2. package/dist/cli.js +2 -3
  3. package/dist/cmds/config-bash.js +0 -1
  4. package/dist/cmds/config-env.js +0 -1
  5. package/dist/cmds/config-init.js +0 -1
  6. package/dist/cmds/config.js +0 -1
  7. package/dist/cmds/db-console.js +0 -1
  8. package/dist/cmds/db-createmodel.js +0 -1
  9. package/dist/cmds/db-dump-data.js +0 -1
  10. package/dist/cmds/db-init.js +0 -1
  11. package/dist/cmds/db-makemigrations.js +0 -1
  12. package/dist/cmds/db-push-data.js +0 -1
  13. package/dist/cmds/db-sync-makemigrations.js +0 -1
  14. package/dist/cmds/doc-json.js +0 -1
  15. package/dist/cmds/doc-md.js +0 -1
  16. package/dist/cmds/handler-apiroute-new.js +0 -1
  17. package/dist/cmds/handler-main-new.js +0 -1
  18. package/dist/cmds/new.js +0 -1
  19. package/dist/cmds/start.js +0 -1
  20. package/dist/cmds/test.js +56 -20
  21. package/dist/index.d.ts +0 -1
  22. package/dist/index.js +1 -13
  23. package/dist/utils/db/automigrations/index.js +14 -18
  24. package/dist/utils/db/automigrations/migrate.js +6 -10
  25. package/dist/utils/db/index.js +0 -1
  26. package/dist/utils/doc/index.d.ts +1 -1
  27. package/dist/utils/doc/index.js +1 -2
  28. package/dist/utils/index.d.ts +26 -2
  29. package/dist/utils/index.js +84 -7
  30. package/dist/utils/templates.js +0 -1
  31. package/package.json +9 -9
  32. package/dist/cli.js.map +0 -1
  33. package/dist/cmds/config-bash.js.map +0 -1
  34. package/dist/cmds/config-env.js.map +0 -1
  35. package/dist/cmds/config-init.js.map +0 -1
  36. package/dist/cmds/config.js.map +0 -1
  37. package/dist/cmds/db-console.js.map +0 -1
  38. package/dist/cmds/db-createmodel.js.map +0 -1
  39. package/dist/cmds/db-dump-data.js.map +0 -1
  40. package/dist/cmds/db-init.js.map +0 -1
  41. package/dist/cmds/db-makemigrations.js.map +0 -1
  42. package/dist/cmds/db-push-data.js.map +0 -1
  43. package/dist/cmds/db-sync-makemigrations.js.map +0 -1
  44. package/dist/cmds/doc-json.js.map +0 -1
  45. package/dist/cmds/doc-md.js.map +0 -1
  46. package/dist/cmds/handler-apiroute-new.js.map +0 -1
  47. package/dist/cmds/handler-main-new.js.map +0 -1
  48. package/dist/cmds/new.js.map +0 -1
  49. package/dist/cmds/start.js.map +0 -1
  50. package/dist/cmds/test.js.map +0 -1
  51. package/dist/index.js.map +0 -1
  52. package/dist/test/common/app.d.ts +0 -5
  53. package/dist/test/common/app.js +0 -51
  54. package/dist/test/common/app.js.map +0 -1
  55. package/dist/test/common/common.d.ts +0 -6
  56. package/dist/test/common/common.js +0 -15
  57. package/dist/test/common/common.js.map +0 -1
  58. package/dist/test/common/index.d.ts +0 -3
  59. package/dist/test/common/index.js +0 -16
  60. package/dist/test/common/index.js.map +0 -1
  61. package/dist/test/common/runner.d.ts +0 -34
  62. package/dist/test/common/runner.js +0 -88
  63. package/dist/test/common/runner.js.map +0 -1
  64. package/dist/test/index.d.ts +0 -1
  65. package/dist/test/index.js +0 -14
  66. package/dist/test/index.js.map +0 -1
  67. package/dist/utils/db/automigrations/deep-diff/index.d.ts +0 -24
  68. package/dist/utils/db/automigrations/deep-diff/index.js +0 -240
  69. package/dist/utils/db/automigrations/deep-diff/index.js.map +0 -1
  70. package/dist/utils/db/automigrations/index.js.map +0 -1
  71. package/dist/utils/db/automigrations/migrate.js.map +0 -1
  72. package/dist/utils/db/index.js.map +0 -1
  73. package/dist/utils/doc/index.js.map +0 -1
  74. package/dist/utils/index.js.map +0 -1
  75. package/dist/utils/templates.js.map +0 -1
  76. package/src/cli.ts +0 -62
  77. package/src/cmds/config-bash.ts +0 -18
  78. package/src/cmds/config-env.ts +0 -18
  79. package/src/cmds/config-init.ts +0 -35
  80. package/src/cmds/config.ts +0 -16
  81. package/src/cmds/db-console.ts +0 -35
  82. package/src/cmds/db-createmodel.ts +0 -44
  83. package/src/cmds/db-dump-data.ts +0 -44
  84. package/src/cmds/db-init.ts +0 -42
  85. package/src/cmds/db-makemigrations.ts +0 -12
  86. package/src/cmds/db-push-data.ts +0 -77
  87. package/src/cmds/db-sync-makemigrations.ts +0 -12
  88. package/src/cmds/doc-json.ts +0 -16
  89. package/src/cmds/doc-md.ts +0 -241
  90. package/src/cmds/handler-apiroute-new.ts +0 -67
  91. package/src/cmds/handler-main-new.ts +0 -85
  92. package/src/cmds/new.ts +0 -128
  93. package/src/cmds/start.ts +0 -16
  94. package/src/cmds/test.ts +0 -45
  95. package/src/index.ts +0 -1
  96. package/src/test/common/app.ts +0 -47
  97. package/src/test/common/common.ts +0 -17
  98. package/src/test/common/index.ts +0 -3
  99. package/src/test/common/runner.ts +0 -121
  100. package/src/test/index.ts +0 -1
  101. package/src/utils/db/automigrations/deep-diff/index.ts +0 -264
  102. package/src/utils/db/automigrations/index.ts +0 -151
  103. package/src/utils/db/automigrations/migrate.ts +0 -888
  104. package/src/utils/db/index.ts +0 -128
  105. package/src/utils/doc/index.ts +0 -36
  106. package/src/utils/index.ts +0 -9
  107. package/src/utils/templates.ts +0 -189
  108. package/tsconfig.json +0 -32
@@ -1,121 +0,0 @@
1
- import { Console } from "console";
2
- import { format } from "util";
3
-
4
- export type TestFunction = () => void | Promise<void>;
5
- const DEFAULT_TIMEOUT = 2000;
6
-
7
- interface Test { run: TestFunction; title: string; category?: string; fullName: string; }
8
-
9
- const tests: Test[] = [];
10
-
11
- export const getTestCount = () => tests.length;
12
-
13
- export const it = (title: string, impl: TestFunction, options?: {
14
- category?: string;
15
- timeout?: number;
16
- before?: TestFunction;
17
- after?: TestFunction;
18
- }, logger: {
19
- log: (...args: any[]) => void
20
- } | Console = new Console(process.stdout)): void => {
21
- const category = options && options.category ? options.category : undefined;
22
- const fullName = `${category ? `${category} [` : ""}${title}${category ? "]" : ""}`;
23
- tests.push({
24
- run: () => new Promise((resolve, reject) => {
25
- const timeoutMS = options && options.timeout ? options.timeout : DEFAULT_TIMEOUT;
26
- const timeout = setTimeout(() => {
27
- reject(new Error(format("%s failed\x1b[31m\n%s timeout %o\x1b", fullName, fullName, timeoutMS)));
28
- }, timeoutMS);
29
- try {
30
- (async () => {
31
- if (options && options.before) {
32
- await options.before();
33
- }
34
- const startMS = Date.now();
35
- await impl();
36
- const took = Date.now() - startMS;
37
- if (options && options.after) {
38
- await options.after();
39
- }
40
- return took;
41
- })().then((took) => {
42
- clearTimeout(timeout);
43
- logger.log("\x1b[32mpassed %s (took %sms)\x1b[0m", fullName, took);
44
- resolve();
45
- }).catch(e => {
46
- clearTimeout(timeout);
47
- reject(new Error(format("%s failed\n%s finished with errors %o", fullName, fullName, e)));
48
- });
49
- } catch (e) {
50
- clearTimeout(timeout);
51
- reject(new Error(format("%s failed\n%s finished with errors %o", fullName, fullName, e)));
52
- }
53
- }), title,
54
- category,
55
- fullName
56
- });
57
- };
58
-
59
- export const runTests = async (title?: string | string[], logger: {
60
- error: (...args: any[]) => void;
61
- } | Console = new Console(process.stdout)): Promise<{
62
- passed: number;
63
- total: number;
64
- ignored: number;
65
- failed: {
66
- error: any;
67
- fullName: string;
68
- }[];
69
- }> => {
70
- const titles = title ? title instanceof Array ? title : [title] : [undefined];
71
- const ret: {
72
- passed: number;
73
- total: number;
74
- ignored: number;
75
- failed: {
76
- error: any;
77
- fullName: string;
78
- }[];
79
- } = {
80
- total: tests.length,
81
- ignored: 0,
82
- passed: 0,
83
- failed: []
84
- };
85
- for (const test of tests) {
86
- for (const title of titles) {
87
- if (title && test.fullName.indexOf(title) !== -1 || !title) {
88
- try {
89
- await test.run();
90
- ret.passed++;
91
- } catch (e) {
92
- logger.error("\x1b[31m%s\x1b[0m", e);
93
- ret.failed.push({
94
- error: e,
95
- fullName: test.fullName
96
- })
97
- }
98
- } else {
99
- ret.ignored++;
100
- }
101
- }
102
- }
103
- return ret;
104
- };
105
-
106
- export const runTestModules = async (modules: string[], title?: string | string[], logger: {
107
- error: (...args: any[]) => void;
108
- } | Console = new Console(process.stdout)): Promise<{
109
- passed: number;
110
- total: number;
111
- ignored: number;
112
- failed: {
113
- error: any;
114
- fullName: string;
115
- }[];
116
- }> => {
117
- for (const path of modules) {
118
- require(path);
119
- }
120
- return await runTests(title, logger);
121
- }
package/src/test/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./common";
@@ -1,264 +0,0 @@
1
- // https://github.com/flitbit/diff/blob/master/index.js
2
-
3
- class Diff<LHS = any, RHS = any> {
4
- public rhs: RHS | undefined;
5
- public lhs: LHS | undefined;
6
- constructor(public kind: "A" | "E" | "N" | "D", public path: string[] | undefined) {
7
- }
8
- }
9
-
10
- class DiffEdit<L, R> extends Diff<L, R> {
11
- constructor(path: string[], lhs: L, rhs: R) {
12
- super("E", path);
13
- this.lhs = lhs;
14
- this.rhs = rhs;
15
- }
16
- }
17
-
18
- class DiffNew<L, R> extends Diff<L, R> {
19
- constructor(path: string[] | undefined, rhs: R) {
20
- super("N", path);
21
- this.rhs = rhs;
22
- }
23
- }
24
-
25
- class DiffDeleted<L, R> extends Diff<L, R>{
26
- constructor(path: string[] | undefined, public lhs: L) {
27
- super("D", path);
28
- this.lhs = lhs;
29
- }
30
- }
31
-
32
- class DiffArray extends Diff {
33
- public item: any;
34
- constructor(path: string[], public index: number, item: any) {
35
- super("A", path);
36
- this.item = item;
37
- }
38
- }
39
-
40
- const realTypeOf = (subject: any) => {
41
- const type = typeof subject;
42
- if (type !== 'object') {
43
- return type;
44
- }
45
-
46
- if (subject === Math) {
47
- return 'math';
48
- } else if (subject === null) {
49
- return 'null';
50
- } else if (Array.isArray(subject)) {
51
- return 'array';
52
- } else if (Object.prototype.toString.call(subject) === '[object Date]') {
53
- return 'date';
54
- } else if (typeof subject.toString === 'function' && /^\/.*\//.test(subject.toString())) {
55
- return 'regexp';
56
- }
57
- return 'object';
58
- }
59
-
60
- // http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/
61
- const hashThisString = (string: string): number => {
62
- let hash = 0;
63
- if (string.length === 0) { return hash; }
64
- for (let i = 0; i < string.length; i++) {
65
- const char = string.charCodeAt(i);
66
- hash = ((hash << 5) - hash) + char;
67
- hash = hash & hash; // Convert to 32bit integer
68
- }
69
- return hash;
70
- }
71
-
72
- // Gets a hash of the given object in an array order-independent fashion
73
- // also object key order independent (easier since they can be alphabetized)
74
- const getOrderIndependentHash = (object: any): number => {
75
- let accum = 0;
76
- const type = realTypeOf(object);
77
-
78
- if (type === 'array') {
79
- object.forEach(function (item: any) {
80
- // Addition is commutative so this is order indep
81
- accum += getOrderIndependentHash(item);
82
- });
83
-
84
- const arrayString = '[type: array, hash: ' + accum + ']';
85
- return accum + hashThisString(arrayString);
86
- }
87
-
88
- if (type === 'object') {
89
- for (const key in object) {
90
- if (object.hasOwnProperty(key)) {
91
- const keyValueString = '[ type: object, key: ' + key + ', value hash: ' + getOrderIndependentHash(object[key]) + ']';
92
- accum += hashThisString(keyValueString);
93
- }
94
- }
95
-
96
- return accum;
97
- }
98
-
99
- // Non object, non array...should be good?
100
- const stringToHash = '[ type: ' + type + ' ; value: ' + object + ']';
101
- return accum + hashThisString(stringToHash);
102
- }
103
-
104
- const deepDiff = (lhs: any, rhs: any, changes: any[], prefilter?: PreFilter<any, any>, path?: string[] | null, key?: string | null | number, stack?: { lhs?: any; rhs?: any; }[] | null, orderIndependent?: boolean) => {
105
- changes = changes || [];
106
- path = path || [];
107
- stack = stack || [];
108
- const currentPath = path.slice(0);
109
- if (typeof key !== 'undefined' && key !== null) {
110
- if (prefilter) {
111
- if (typeof (prefilter) === 'function' && prefilter(currentPath, key)) {
112
- return;
113
- } else if (typeof (prefilter) === 'object') {
114
- if (prefilter.prefilter && prefilter.prefilter(currentPath, key)) {
115
- return;
116
- }
117
- if (prefilter.normalize) {
118
- const alt = prefilter.normalize(currentPath, key, lhs, rhs);
119
- if (alt) {
120
- lhs = alt[0];
121
- rhs = alt[1];
122
- }
123
- }
124
- }
125
- }
126
- currentPath.push(key as string);
127
- }
128
-
129
- // Use string comparison for regexes
130
- if (realTypeOf(lhs) === 'regexp' && realTypeOf(rhs) === 'regexp') {
131
- lhs = lhs.toString();
132
- rhs = rhs.toString();
133
- }
134
-
135
- const ltype = typeof lhs;
136
- const rtype = typeof rhs;
137
- let i, j, k, other;
138
-
139
- const ldefined = ltype !== 'undefined' ||
140
- (stack && (stack.length > 0) && stack[stack.length - 1].lhs &&
141
- Object.getOwnPropertyDescriptor(stack[stack.length - 1].lhs, key as string));
142
- const rdefined = rtype !== 'undefined' ||
143
- (stack && (stack.length > 0) && stack[stack.length - 1].rhs &&
144
- Object.getOwnPropertyDescriptor(stack[stack.length - 1].rhs, key as string));
145
-
146
- if (!ldefined && rdefined) {
147
- changes.push(new DiffNew(currentPath, rhs));
148
- } else if (!rdefined && ldefined) {
149
- changes.push(new DiffDeleted(currentPath, lhs));
150
- } else if (realTypeOf(lhs) !== realTypeOf(rhs)) {
151
- changes.push(new DiffEdit(currentPath, lhs, rhs));
152
- } else if (realTypeOf(lhs) === 'date' && (lhs - rhs) !== 0) {
153
- changes.push(new DiffEdit(currentPath, lhs, rhs));
154
- } else if (ltype === 'object' && lhs !== null && rhs !== null) {
155
- for (i = stack.length - 1; i > -1; --i) {
156
- if (stack[i].lhs === lhs) {
157
- other = true;
158
- break;
159
- }
160
- }
161
- if (!other) {
162
- stack.push({ lhs: lhs, rhs: rhs });
163
- if (Array.isArray(lhs)) {
164
- // If order doesn't matter, we need to sort our arrays
165
- if (orderIndependent) {
166
- lhs.sort(function (a, b) {
167
- return getOrderIndependentHash(a) - getOrderIndependentHash(b);
168
- });
169
-
170
- rhs.sort(function (a: any, b: any) {
171
- return getOrderIndependentHash(a) - getOrderIndependentHash(b);
172
- });
173
- }
174
- i = rhs.length - 1;
175
- j = lhs.length - 1;
176
- while (i > j) {
177
- changes.push(new DiffArray(currentPath, i, new DiffNew(undefined, rhs[i--])));
178
- }
179
- while (j > i) {
180
- changes.push(new DiffArray(currentPath, j, new DiffDeleted(undefined, lhs[j--])));
181
- }
182
- for (; i >= 0; --i) {
183
- deepDiff(lhs[i], rhs[i], changes, prefilter, currentPath, i, stack, orderIndependent);
184
- }
185
- } else {
186
-
187
- const akeys = Object.keys(lhs).concat(Object.getOwnPropertySymbols(lhs) as any[] as string[]);
188
- const pkeys = Object.keys(rhs).concat(Object.getOwnPropertySymbols(rhs) as any[] as string[]);
189
- for (i = 0; i < akeys.length; ++i) {
190
- k = akeys[i];
191
- other = pkeys.indexOf(k);
192
- if (other >= 0) {
193
- deepDiff(lhs[k], rhs[k], changes, prefilter, currentPath, k, stack, orderIndependent);
194
- pkeys[other] = null as any;
195
- } else {
196
- deepDiff(lhs[k], undefined, changes, prefilter, currentPath, k, stack, orderIndependent);
197
- }
198
- }
199
- for (i = 0; i < pkeys.length; ++i) {
200
- k = pkeys[i];
201
- if (k) {
202
- deepDiff(undefined, rhs[k], changes, prefilter, currentPath, k, stack, orderIndependent);
203
- }
204
- }
205
- }
206
- stack.length = stack.length - 1;
207
- } else if (lhs !== rhs) {
208
- // lhs is contains a cycle at this element and it differs from rhs
209
- changes.push(new DiffEdit(currentPath, lhs, rhs));
210
- }
211
- } else if (lhs !== rhs) {
212
- if (!(ltype === 'number' && isNaN(lhs) && isNaN(rhs))) {
213
- changes.push(new DiffEdit(currentPath, lhs, rhs));
214
- }
215
- }
216
- }
217
-
218
- /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
219
- const observableDiff = (lhs: any, rhs: any, observer?: (difference: any) => void, prefilter?: PreFilter<any, any>, orderIndependent?: boolean): Diff<any, any>[] => {
220
- const changes: Diff<any, any>[] = [];
221
- deepDiff(lhs, rhs, changes, prefilter, null, null, null, orderIndependent);
222
- if (observer) {
223
- for (let i = 0; i < changes.length; ++i) {
224
- observer(changes[i]);
225
- }
226
- }
227
- return changes;
228
- }
229
-
230
- /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
231
- const accumulateDiff = (lhs: any, rhs: any, prefilter?: PreFilter<any, any>, accum?: Diff<any, any>[] | undefined): Array<Diff<any, any>> | undefined => {
232
- const observer = (accum) ?
233
- function (difference: any) {
234
- if (difference) {
235
- accum.push(difference);
236
- }
237
- } : undefined;
238
- const changes = observableDiff(lhs, rhs, observer, prefilter);
239
- return (accum) ? accum : (changes.length) ? changes : undefined;
240
- }
241
-
242
- export type PreFilterFunction = (path: any[], key: any) => boolean;
243
- export interface PreFilterObject<LHS, RHS = LHS> {
244
- prefilter?(path: any[], key: any): boolean;
245
- normalize?(currentPath: any, key: any, lhs: LHS, rhs: RHS): [LHS, RHS] | undefined;
246
- }
247
- export type PreFilter<LHS, RHS = LHS> = PreFilterFunction | PreFilterObject<LHS, RHS>;
248
-
249
- export interface Accumulator<LHS, RHS = LHS> {
250
- push(diff: Diff<LHS, RHS>): void;
251
- length: number;
252
- }
253
-
254
- export type Observer<LHS, RHS = LHS> = (diff: Diff<LHS, RHS>) => void;
255
-
256
- export type Filter<LHS, RHS = LHS> = (target: LHS, source: RHS, change: Diff<LHS, RHS>) => boolean;
257
-
258
- export type DeepDiff<LHS, RHS = LHS> = (lhs: LHS, rhs: RHS, prefilter?: PreFilter<LHS, RHS>) => Array<Diff<LHS, RHS>> | undefined;
259
-
260
- export const DEEPDIFF: {
261
- diff: DeepDiff<any>
262
- } = {
263
- diff: accumulateDiff
264
- };
@@ -1,151 +0,0 @@
1
- import { getLogger } from "@miqro/core";
2
- import fs from "fs";
3
- import path from "path";
4
- import { loadModels, loadSequelizeRC } from "..";
5
- import { getMigration, MigrationState, parseDifference, reverseModels, sortActions, writeMigration } from "./migrate";
6
-
7
- export const syncMakeMigrationsImpl = (): void => {
8
- // Windows support
9
- if (!process.env.PWD) {
10
- process.env.PWD = process.cwd();
11
- }
12
-
13
- const sequelizeRC = loadSequelizeRC();
14
-
15
- // noinspection SpellCheckingInspection
16
- const logger = getLogger("makemigrations");
17
-
18
- try {
19
- if (!fs.existsSync(sequelizeRC["models-path"])) {
20
- logger.error("Can't find models directory. Use `sequelize init` to create it");
21
- return;
22
- }
23
-
24
- if (!fs.existsSync(sequelizeRC["migrations-path"])) {
25
- logger.error("Can't find migrations directory. Use `sequelize init` to create it");
26
- return;
27
- }
28
-
29
- // load last state
30
- let previousState: MigrationState;
31
-
32
- try {
33
- previousState = JSON.parse(fs.readFileSync(path.join(sequelizeRC["migrations-path"], "_current.json")).toString());
34
- } catch (e) {
35
- previousState = {
36
- revision: 0,
37
- tables: {}
38
- };
39
- }
40
-
41
- const { modelsModule } = loadModels({ ["models-path"]: sequelizeRC["models-path"] });
42
-
43
- // current state
44
- const currentState: MigrationState = {
45
- revision: previousState.revision + 1,
46
- tables: reverseModels(modelsModule.Sequelize, modelsModule.sequelize, modelsModule.sequelize.models, logger)
47
- };
48
-
49
-
50
- // backup _current file
51
- if (fs.existsSync(path.join(sequelizeRC["migrations-path"], "_current.json"))) {
52
- fs.writeFileSync(path.join(sequelizeRC["migrations-path"], "_current_bak.json"),
53
- fs.readFileSync(path.join(sequelizeRC["migrations-path"], "_current.json"))
54
- );
55
- }
56
-
57
- // save current state
58
- currentState.revision = previousState.revision + 1;
59
- fs.writeFileSync(path.join(sequelizeRC["migrations-path"], "_current.json"), JSON.stringify(currentState, null, 4));
60
- } catch (e) {
61
- logger.error(e);
62
- throw e;
63
- }
64
- };
65
-
66
- // noinspection SpellCheckingInspection
67
- export const makemigrationsImpl = (): string | undefined | null => {
68
-
69
- // Windows support
70
- if (!process.env.PWD) {
71
- process.env.PWD = process.cwd();
72
- }
73
-
74
- const sequelizeRC = loadSequelizeRC();
75
-
76
- // noinspection SpellCheckingInspection
77
- const logger = getLogger("makemigrations");
78
-
79
- try {
80
- if (!fs.existsSync(sequelizeRC["models-path"])) {
81
- logger.error("Can't find models directory. Use `sequelize init` to create it");
82
- return;
83
- }
84
-
85
- if (!fs.existsSync(sequelizeRC["migrations-path"])) {
86
- logger.error("Can't find migrations directory. Use `sequelize init` to create it");
87
- return;
88
- }
89
-
90
- // load last state
91
- let previousState: MigrationState;
92
-
93
- try {
94
- previousState = JSON.parse(fs.readFileSync(path.join(sequelizeRC["migrations-path"], "_current.json")).toString());
95
- } catch (e) {
96
- previousState = {
97
- revision: 0,
98
- tables: {}
99
- };
100
- }
101
-
102
- const { modelsModule } = loadModels({ ["models-path"]: sequelizeRC["models-path"] });
103
-
104
- // current state
105
- const currentState: MigrationState = {
106
- revision: previousState.revision + 1,
107
- tables: reverseModels(modelsModule.Sequelize, modelsModule.sequelize, modelsModule.sequelize.models, logger)
108
- };
109
-
110
- const actions = parseDifference(previousState.tables, currentState.tables, logger);
111
-
112
- // sort actions
113
- sortActions(actions);
114
-
115
- const migration = getMigration(actions);
116
-
117
- if (migration.commandsUp.length === 0) {
118
- logger.info("No changes found");
119
- return null;
120
- }
121
-
122
- // log migration actions
123
- migration.consoleOut.forEach((v) => {
124
- logger.info("[Actions] " + v);
125
- });
126
-
127
- // backup _current file
128
- if (fs.existsSync(path.join(sequelizeRC["migrations-path"], "_current.json"))) {
129
- fs.writeFileSync(path.join(sequelizeRC["migrations-path"], "_current_bak.json"),
130
- fs.readFileSync(path.join(sequelizeRC["migrations-path"], "_current.json"))
131
- );
132
- }
133
-
134
- // save current state
135
- currentState.revision = previousState.revision + 1;
136
- fs.writeFileSync(path.join(sequelizeRC["migrations-path"], "_current.json"), JSON.stringify(currentState, null, 4));
137
-
138
- // write migration to file
139
- const info = writeMigration(currentState.revision,
140
- migration,
141
- sequelizeRC["migrations-path"],
142
- "noname",
143
- "");
144
-
145
- logger.info(`New migration to revision ${currentState.revision} has been saved to file '${info.filename}'`);
146
- return info.filename;
147
- } catch (e) {
148
- logger.error(e);
149
- throw e;
150
- }
151
- };