db-migrate 1.0.0-beta.16

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 (51) hide show
  1. package/CHANGELOG.md +487 -0
  2. package/LICENSE +22 -0
  3. package/README.md +100 -0
  4. package/api.js +463 -0
  5. package/bin/db-migrate +39 -0
  6. package/connect.js +198 -0
  7. package/get.js +67 -0
  8. package/index.js +85 -0
  9. package/lib/chain.js +157 -0
  10. package/lib/class.js +75 -0
  11. package/lib/commands/check.js +52 -0
  12. package/lib/commands/create-migration.js +252 -0
  13. package/lib/commands/db.js +63 -0
  14. package/lib/commands/down.js +45 -0
  15. package/lib/commands/dump.js +22 -0
  16. package/lib/commands/fn/plugin.js +45 -0
  17. package/lib/commands/generated.js +32 -0
  18. package/lib/commands/helper/assert.js +15 -0
  19. package/lib/commands/helper/load-config.js +26 -0
  20. package/lib/commands/helper/migration-hook.js +4 -0
  21. package/lib/commands/helper/register-events.js +19 -0
  22. package/lib/commands/index.js +9 -0
  23. package/lib/commands/on-complete.js +28 -0
  24. package/lib/commands/run.js +122 -0
  25. package/lib/commands/seed.js +48 -0
  26. package/lib/commands/set-default-argv.js +169 -0
  27. package/lib/commands/sync.js +52 -0
  28. package/lib/commands/undo-seed.js +54 -0
  29. package/lib/commands/up.js +52 -0
  30. package/lib/config.js +207 -0
  31. package/lib/driver/index.js +156 -0
  32. package/lib/driver/shadow.js +74 -0
  33. package/lib/executors/versioned/v1.js +48 -0
  34. package/lib/executors/versioned/v2.js +78 -0
  35. package/lib/file.js +190 -0
  36. package/lib/interface/migratorInterface.js +127 -0
  37. package/lib/interface/seederInterface.js +145 -0
  38. package/lib/learn.js +369 -0
  39. package/lib/methods/v2/conventions.js +47 -0
  40. package/lib/methods/v2/migrate.js +5 -0
  41. package/lib/methods/v2/statetravel.js +86 -0
  42. package/lib/methods/v2/translatestate.js +117 -0
  43. package/lib/relation.js +37 -0
  44. package/lib/state.js +154 -0
  45. package/lib/template.js +312 -0
  46. package/lib/templates/v2.js +12 -0
  47. package/lib/temputils.js +62 -0
  48. package/lib/walker.js +266 -0
  49. package/package.json +87 -0
  50. package/pnpm-lock.yaml +2574 -0
  51. package/te.js +70 -0
package/lib/walker.js ADDED
@@ -0,0 +1,266 @@
1
+ 'use strict';
2
+
3
+ const dbmUtil = require('db-migrate-shared').util;
4
+ const State = require('./state');
5
+ const log = require('db-migrate-shared').log;
6
+ const Promise = require('bluebird');
7
+ const File = require('./file.js');
8
+
9
+ // Not sure what will happen to this yet
10
+ function SeedLink (driver, internals) {
11
+ this.seeder = require('./seeder.js')(
12
+ driver,
13
+ internals.argv['vcseeder-dir'],
14
+ true,
15
+ internals
16
+ );
17
+ this.internals = internals;
18
+ this.links = [];
19
+ }
20
+
21
+ const INTERFACES = {
22
+ migration: require('./interface/migratorInterface.js'),
23
+ seed: require('./interface/seederInterface.js'),
24
+ 'static-seed': require('./interface/seederInterface.js')
25
+ };
26
+
27
+ const Walker = function (driver, directory, mode, intern, prefix) {
28
+ this.driver = dbmUtil.reduceToInterface(driver, INTERFACES[prefix]);
29
+ this._driver = driver;
30
+ Promise.promisifyAll(this._driver);
31
+ this.directory = directory;
32
+ this.internals = intern;
33
+ /**
34
+ * i: index
35
+ * c: schema
36
+ * f: foreignKey
37
+ * e: extra items for the schema (like ENUM tyes)
38
+ */
39
+ this.internals.schema = { i: {}, c: {}, f: {}, e: {} };
40
+ /**
41
+ * s: commands
42
+ * i: index
43
+ * f: foreignKey
44
+ * c: schema
45
+ */
46
+ this.internals.modSchema = { i: {}, c: {}, f: {}, s: [] };
47
+ this.mode = mode;
48
+
49
+ if (!this.mode) this.prefix = `static-${prefix}`;
50
+ else this.prefix = prefix;
51
+
52
+ this.title = `[${prefix}] `;
53
+
54
+ // keep it until we decide how we do the cross linking
55
+ if (intern.linked === false) {
56
+ this.seedLink = new SeedLink(driver, intern);
57
+ intern.linked = true;
58
+ }
59
+ };
60
+
61
+ Walker.prototype = {
62
+ createTables: async function () {
63
+ await State.init(this._driver, this.internals);
64
+ return this._driver._createList(this.internals.migrationTable);
65
+ },
66
+
67
+ createMigrationsTable: function () {
68
+ if (
69
+ typeof this._driver._createList !== 'function' ||
70
+ typeof this._driver._getList !== 'function' ||
71
+ typeof this._driver._createKV !== 'function' ||
72
+ typeof this._driver._getKV !== 'function' ||
73
+ typeof this._driver._deleteKV !== 'function' ||
74
+ typeof this._driver._deleteEntry !== 'function' ||
75
+ typeof this._driver._insertEntry !== 'function' ||
76
+ typeof this._driver._insertKV !== 'function'
77
+ ) {
78
+ log.warn(
79
+ 'The driver you are using does not support the new state management. ' +
80
+ 'Please raise an issue in the repository of your driver maintainer'
81
+ );
82
+
83
+ if (typeof this._driver._createList !== 'function') {
84
+ return this._driver.createMigrationsTableAsync();
85
+ }
86
+ }
87
+
88
+ return this.createTables();
89
+ },
90
+
91
+ writeMigrationRecord: function (migration, callback) {
92
+ const onComplete = err => {
93
+ if (err) {
94
+ log.error(this.title + migration.name, err);
95
+ } else {
96
+ log.info(this.title + 'Processed', migration.name);
97
+ }
98
+ callback(err);
99
+ };
100
+ this._driver.addMigrationRecord(
101
+ this.internals.matching + '/' + migration.name,
102
+ onComplete
103
+ );
104
+ },
105
+
106
+ deleteMigrationRecord: function (migration, callback) {
107
+ const onComplete = err => {
108
+ if (err) {
109
+ log.error(this.title + migration.name, err);
110
+ } else {
111
+ log.info(this.title + 'Processed', migration.name);
112
+ }
113
+ callback(err);
114
+ };
115
+ this._driver.deleteMigration(
116
+ this.internals.matching + '/' + migration.name,
117
+ function (err) {
118
+ if (!this.internals.matching) {
119
+ this._driver.deleteMigration(migration.name, onComplete);
120
+ } else {
121
+ onComplete.apply(err);
122
+ }
123
+ }.bind(this)
124
+ );
125
+ },
126
+
127
+ sync: function (options, callback) {
128
+ return File.loadFromDatabase(
129
+ this.directory,
130
+ this.prefix,
131
+ this._driver,
132
+ this.internals
133
+ )
134
+ .then(completedFiles => {
135
+ const mode = dbmUtil.syncMode(completedFiles, options.destination);
136
+ if (mode === 1) {
137
+ log.info(this.title + 'Syncing upwards.');
138
+ return this.up(options);
139
+ } else {
140
+ log.info(this.title + 'Syncing downwards.');
141
+ return this.down(options);
142
+ }
143
+ })
144
+ .nodeify(callback);
145
+ },
146
+
147
+ up: function (options, callback) {
148
+ const partialName = options.destination;
149
+ const count = options.count;
150
+ const files = Promise.all([
151
+ File.loadFromFileystem(this.directory, this.prefix, this.internals),
152
+ File.loadFromDatabase(
153
+ this.directory,
154
+ this.prefix,
155
+ this._driver,
156
+ this.internals
157
+ )
158
+ ]).spread((allFiles, completedFiles) => {
159
+ const toRun = dbmUtil.filterUp(
160
+ allFiles,
161
+ completedFiles,
162
+ partialName,
163
+ count
164
+ );
165
+
166
+ if (toRun.length === 0) {
167
+ log.info(this.title + 'Nothing to run');
168
+ }
169
+
170
+ if (this.internals.check) {
171
+ const toRunNames = toRun.map(migration => migration.name);
172
+ log.info(this.title + 'run:', toRunNames);
173
+ return toRunNames;
174
+ }
175
+
176
+ return toRun;
177
+ });
178
+
179
+ if (this.internals.check) {
180
+ return files.nodeify(callback);
181
+ }
182
+
183
+ return files
184
+ .each(file => {
185
+ log.verbose(this.title + 'preparing to run up:', file.name);
186
+ const _meta = file.get()._meta || {};
187
+ const version = _meta.version || 1;
188
+ this.internals.modSchema = { i: {}, c: {}, f: {}, s: [] };
189
+ return require(`./executors/versioned/v${version}`).up(
190
+ this,
191
+ this.driver,
192
+ file
193
+ );
194
+ })
195
+ .nodeify(callback);
196
+ },
197
+
198
+ down: function (options, callback) {
199
+ const partialName = options.destination;
200
+ const count = options.count;
201
+
202
+ const files = File.loadFromDatabase(
203
+ this.directory,
204
+ this.prefix,
205
+ this._driver,
206
+ this.internals
207
+ ).then(completedFiles => {
208
+ const toRun = dbmUtil.filterDown(completedFiles, partialName, count);
209
+
210
+ if (toRun.length === 0) {
211
+ log.info(this.title + 'Nothing to run');
212
+ }
213
+
214
+ if (this.internals.check) {
215
+ const toRunNames = toRun.map(migration => migration.name);
216
+ log.info(this.title + 'run:', toRunNames);
217
+ return toRunNames;
218
+ }
219
+
220
+ return toRun;
221
+ });
222
+
223
+ if (this.internals.check) {
224
+ return files.nodeify(callback);
225
+ }
226
+
227
+ return files
228
+ .each(file => {
229
+ log.verbose(this.title + 'preparing to run down:', file.name);
230
+ const _meta = file.get()._meta || {};
231
+ const version = _meta.version || 1;
232
+ this.internals.modSchema = { i: {}, c: {}, f: {}, s: [] };
233
+ return require(`./executors/versioned/v${version}`).down(
234
+ this,
235
+ this.driver,
236
+ file
237
+ );
238
+ })
239
+ .nodeify(callback);
240
+ },
241
+
242
+ check: function (options, callback) {
243
+ return Promise.all([
244
+ File.loadFromDatabase(
245
+ this.directory,
246
+ this.prefix,
247
+ this._driver,
248
+ this.internals
249
+ ),
250
+ File.loadFromFileystem(this.directory, this.prefix, this.internals)
251
+ ])
252
+ .spread((completedFiles, allFiles) => {
253
+ // Requires pr to export filterCompleted from db-migrate-shared
254
+ const toRun = dbmUtil.filterCompleted(allFiles, completedFiles);
255
+
256
+ log.info(
257
+ 'Files to run:',
258
+ toRun.map(migration => migration.name)
259
+ );
260
+ return toRun;
261
+ })
262
+ .nodeify(callback);
263
+ }
264
+ };
265
+
266
+ module.exports = Walker;
package/package.json ADDED
@@ -0,0 +1,87 @@
1
+ {
2
+ "name": "db-migrate",
3
+ "description": "Database migration framework for node.js",
4
+ "author": "Tobias Gurtick",
5
+ "bin": "./bin/db-migrate",
6
+ "keywords": [
7
+ "database",
8
+ "db",
9
+ "migrate",
10
+ "migration",
11
+ "sqlite",
12
+ "mysql",
13
+ "pg",
14
+ "postgre",
15
+ "sql",
16
+ "migrations",
17
+ "db-migrate",
18
+ "postgresql",
19
+ "extensible",
20
+ "expandable",
21
+ "nosql",
22
+ "coffee",
23
+ "coffescript",
24
+ "mongo",
25
+ "mongodb",
26
+ "seed",
27
+ "seeds",
28
+ "seeder",
29
+ "migrator",
30
+ "programatic",
31
+ "programable",
32
+ "api"
33
+ ],
34
+ "version": "1.0.0-beta.16",
35
+ "engines": {
36
+ "node": ">=8.0.0"
37
+ },
38
+ "bugs": {
39
+ "url": "https://github.com/db-migrate/node-db-migrate/issues"
40
+ },
41
+ "license": "MIT",
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "https://github.com/db-migrate/node-db-migrate.git"
45
+ },
46
+ "dependencies": {
47
+ "bluebird": "^3.1.1",
48
+ "db-migrate-shared": "^1.2.0",
49
+ "deep-extend": "^0.6.0",
50
+ "dotenv": "^5.0.1",
51
+ "final-fs": "^1.6.0",
52
+ "inflection": "^1.10.0",
53
+ "mkdirp": "~0.5.0",
54
+ "parse-database-url": "~0.3.0",
55
+ "rc": "^1.2.8",
56
+ "resolve": "^1.1.6",
57
+ "semver": "^5.3.0",
58
+ "tunnel-ssh": "^4.0.0",
59
+ "yargs": "^15.3.1"
60
+ },
61
+ "devDependencies": {
62
+ "@hapi/code": "5.x",
63
+ "@hapi/lab": "18.x",
64
+ "db-migrate-base": "^2.1.1",
65
+ "db-migrate-mysql": "^2.1.1",
66
+ "db-migrate-sqlite3": "^0.4.0",
67
+ "eslint": "^6.8.0",
68
+ "eslint-config-standard": "^14.1.1",
69
+ "eslint-plugin-import": "^2.20.2",
70
+ "eslint-plugin-node": "^11.1.0",
71
+ "eslint-plugin-promise": "^4.2.1",
72
+ "eslint-plugin-standard": "^4.0.1",
73
+ "proxyquire": "^1.4.0",
74
+ "rimraf": "^3.0.2",
75
+ "sinon": "^9.0.2"
76
+ },
77
+ "funding": {
78
+ "type": "opencollective",
79
+ "url": "https://opencollective.com/node-db-migrate"
80
+ },
81
+ "scripts": {
82
+ "prepublishOnly": "node generateLoader.js",
83
+ "pretest": "eslint *.js lib/*.js",
84
+ "test": "lab",
85
+ "test:coverage": "lab -r html -o coverage.html"
86
+ }
87
+ }