diff --git a/lib/learn.js b/lib/learn.js index ca2eae06..7f74515a 100644 --- a/lib/learn.js +++ b/lib/learn.js @@ -2,7 +2,7 @@ const Promise = require('bluebird'); const Shadow = require('./driver/shadow'); class STD { - constructor ({ schema, modSchema: mod }, driver) { + constructor ({ schema, modSchema: mod, unlearn }, driver) { this.checkColumn = function (t, c) { if (!this.schema[t]) { throw new Error(`There is no ${t} table in schema!`); @@ -12,6 +12,7 @@ class STD { throw new Error(`There is no ${c} column in schema!`); } }; + this.unlearn = unlearn; this.validations = {}; this.driver = driver; this.indizies = schema.i; @@ -21,10 +22,14 @@ class STD { schema.e = {}; } this.extra = schema.e; + if (unlearn) { + this.modC = []; + } else { + this.modC = mod.s; + } this.modS = mod.c; this.modI = mod.i; this.modF = mod.f; - this.modC = mod.s; } dropTable (t) { @@ -124,7 +129,7 @@ class STD { const hasColumns = this.schema[t].columns !== undefined; const columns = hasColumns ? this.schema[t].columns : this.schema[t]; - if (columns[c].notNull === true) { + if (columns[c].notNull === true && !this.unlearn) { if (this.validations.columnStrategies !== true) { if ( this.driver._meta && @@ -172,7 +177,7 @@ class STD { this.modS[t] = {}; - if (columns[c].notNull === true) { + if (columns[c].notNull === true && !this.unlearn) { switch (o.columnStrategy) { case 'delay': this.modS[t][c] = columns[c]; @@ -196,7 +201,7 @@ class STD { this.modC.push({ t: 1, a: 'addColumn', c: [t, c, o] }); break; } - } else { + } else if (!this.unlearn) { this.modS[t][c] = columns[c]; this.modC.push({ t: 1, a: 'addColumn', c: [t, c, o] }); diff --git a/lib/methods/v2/translatestate.js b/lib/methods/v2/translatestate.js index 84cf1b15..9ca64000 100644 --- a/lib/methods/v2/translatestate.js +++ b/lib/methods/v2/translatestate.js @@ -2,6 +2,7 @@ const Promise = require('bluebird'); const State = require('../../state'); const Chain = require('../../chain'); const Migrate = require('./migrate'); +const Learn = require('../../learn'); const log = require('db-migrate-shared').log; const methods = { @@ -107,7 +108,9 @@ module.exports = async (context, file, driver, internals) => { const mod = internals.modSchema; const chain = new Chain(context, file, driver, internals); - // chain.addChain(Learn); + internals.unlearn = true; + + chain.addChain(Learn); chain.addChain(Migrate); await Promise.resolve(mod.s.reverse()).each(args => processEntry(context, file, chain, internals, args)