Skip to content

Commit

Permalink
fix: change-sets should be within up() calls
Browse files Browse the repository at this point in the history
  • Loading branch information
pateketrueke committed Apr 13, 2022
1 parent a895e20 commit 635e3e0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ E.g., if you've defined `PostTags` it will be used instead, otherwise the option
- `bundle(schemas, definitions[, description])` — Generate a bundle with all models and additional references as JSON-Schema
- `generate(dump, models, definitions[, squashMigrations])` — Generate javascript code of the current schema in form of migrations
- `resource(sequelize, options, model)`— Abstract CRUD wrapper for RESTful resources. It returns a functional API to read, update and delete from given model
- `migrate(sequelize, options[, bind])`— Executes a plain migration if `bind` is `true`, instantiate a umzug wrapper otherwise. When binding ensure you pass a valid object with `up/down/change` functions
- `migrate(sequelize, options[, bind])`— Executes a plain migration if `bind` is `true`, instantiate a umzug wrapper otherwise. When binding ensure you pass a valid object with `up/down` functions
- `sync(models[, options])`— WIll call `sequelize.sync()` by executing definitions in order, all dependencies are synced first, dependants last
- `clear(models[, options])`— Will call `model.destroy()` on each instance, providing a `truncate` or `where` option is mandatory
- `refs(directory[, prefix])` — Scan and load for `*.json` definitions. Set `prefix` to filter out scanned files, e.g. `**/PREFIX/*.json`
20 changes: 9 additions & 11 deletions lib/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ function buildSchema(reference, models, schema, source, prev, raw, op) {
// changes
const up = [];
const down = [];
const change = [];

// lazy-load
const types = require('./types');
Expand Down Expand Up @@ -516,15 +515,15 @@ function buildSchema(reference, models, schema, source, prev, raw, op) {
up.push(`${pad} }),`);
down.unshift(`${pad}() =>\n${pad} queryInterface.removeColumn('${tableName}', '${prop}'),`);
} else {
change.push(`${pad}() =>\n${pad} queryInterface.changeColumn('${tableName}', '${prop}', {`);
up.push(`${pad}() =>\n${pad} queryInterface.changeColumn('${tableName}', '${prop}', {`);

if (sourceObject.enum) {
addColumn(`${pad} `, getValues(sourceObject), prop, change);
addColumn(`${pad} `, getValues(sourceObject), prop, up);
} else {
addColumn(`${pad} `, _value, prop, change);
addColumn(`${pad} `, _value, prop, up);
}

change.push(`${pad} }),`);
up.push(`${pad} }),`);
}
}

Expand All @@ -544,9 +543,9 @@ function buildSchema(reference, models, schema, source, prev, raw, op) {

/* istanbul ignore else */
if (type === MODIFIED) {
change.push(`${pad}() =>\n${pad} queryInterface.changeColumn('${tableName}', '${prop}', {`);
addColumn(`${pad} `, getValues(sourceObject), prop, change);
change.push(`${pad} }),`);
up.push(`${pad}() =>\n${pad} queryInterface.changeColumn('${tableName}', '${prop}', {`);
addColumn(`${pad} `, getValues(sourceObject), prop, up);
up.push(`${pad} }),`);
}
}

Expand Down Expand Up @@ -597,22 +596,21 @@ function buildSchema(reference, models, schema, source, prev, raw, op) {
Array.prototype.push.apply(down, _idx.down);

/* istanbul ignore else */
if ((up.length + down.length + change.length) === 0) {
if ((up.length + down.length) === 0) {
return;
}

/* istanbul ignore else */
if (raw) {
return {
up, down, change, reference,
up, down, reference,
};
}

return [
"/* eslint-disable */\n'use strict';\nmodule.exports = {\n",
` up: (queryInterface, dataTypes) => [\n${up.length ? `${up.join('\n')}\n` : ''} ],\n`,
` down: (queryInterface, dataTypes) => [\n${down.length ? `${down.join('\n')}\n` : ''} ],\n`,
` change: (queryInterface, dataTypes) => [\n${change.length ? `${change.join('\n')}\n` : ''} ],\n`,
'};\n',
].join('');
}
Expand Down
5 changes: 1 addition & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ JSONSchemaSequelizer.generate = (dump, models, squash, globalOptions) => {
}
});

if (code.up.length + code.down.length + code.change.length) {
if (code.up.length + code.down.length) {
_changed.push(code.reference);
}
return;
Expand All @@ -479,9 +479,6 @@ JSONSchemaSequelizer.generate = (dump, models, squash, globalOptions) => {
_result.down && _result.down.length
? `\n down: (queryInterface, dataTypes) => [\n${_result.down.reverse().join('\n')}\n ],`
: '',
_result.change && _result.change.length
? `\n change: (queryInterface, dataTypes) => [\n${_result.change.join('\n')}\n ],`
: '',
'\n};\n',
].join(''),
models: Object.keys(fixedDeps),
Expand Down

0 comments on commit 635e3e0

Please sign in to comment.