UNPKG

sequelize-typescript-migration-rafaeltab

Version:
178 lines 7.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const deep_diff_1 = require("deep-diff"); const sortActions_1 = require("./sortActions"); function getDiffActionsFromTables(previousStateTables, currentStateTables) { const actions = []; let difference = (0, deep_diff_1.diff)(previousStateTables, currentStateTables); if (difference === undefined) { return actions; } difference.forEach(df => { switch (df.kind) { case "N": { if (df.path.length === 1) { const depends = []; const tableName = df.rhs.tableName; Object.values(df.rhs.schema).forEach((v) => { if (v.references) { depends.push(v.references.model); } }); actions.push({ actionType: "createTable", tableName, attributes: df.rhs.schema, options: {}, depends: depends, }); if (df.rhs.indexes) { for (const _i in df.rhs.indexes) { const copied = JSON.parse(JSON.stringify(df.rhs.indexes[_i])); actions.push(Object.assign({ actionType: "addIndex", tableName, depends: [tableName], }, copied)); } } break; } const tableName = df.path[0]; const depends = [tableName]; if (df.path[1] === "schema") { if (df.path.length === 3) { if (df.rhs && df.rhs.references) { depends.push(df.rhs.references.model); } actions.push({ actionType: "addColumn", tableName: tableName, attributeName: df.path[2], options: df.rhs, depends: depends, }); break; } if (df.path.length > 3) { if (df.path[1] === "schema") { const options = currentStateTables[tableName].schema[df.path[2]]; if (options.references) { depends.push(options.references.nodel); } actions.push({ actionType: "changeColumn", tableName: tableName, attributeName: df.path[2], options: options, depends: depends, }); break; } } } if (df.path[1] === "indexes") { const tableName = df.path[0]; const copied = df.rhs ? JSON.parse(JSON.stringify(df.rhs)) : undefined; const index = copied; index.actionType = "addIndex"; index.tableName = tableName; index.depends = [tableName]; actions.push(index); break; } } break; case "D": { const tableName = df.path[0]; if (df.path.length === 1) { const depends = []; Object.values(df.lhs.schema).forEach((v) => { if (v.references) { depends.push(v.references.model); } }); actions.push({ actionType: "dropTable", tableName: tableName, depends: depends, }); break; } if (df.path[1] === "schema") { if (df.path.length === 3) { actions.push({ actionType: "removeColumn", tableName, columnName: df.path[2], depends: [tableName], }); break; } if (df.path.length > 3) { const depends = [tableName]; const options = currentStateTables[tableName].schema[df.path[2]]; if (options.references) { depends.push(options.references.model); } actions.push({ actionType: "changeColumn", tableName, attributeName: df.path[2], options, depends, }); break; } } if (df.path[1] === "indexes") { actions.push({ actionType: "removeIndex", tableName, fields: df.lhs.fields, options: df.lhs.options, depends: [tableName], }); break; } } break; case "E": { const tableName = df.path[0]; const depends = [tableName]; if (df.path[1] === "schema") { const options = currentStateTables[tableName].schema[df.path[2]]; if (options.references) { depends.push(options.references.nodel); } actions.push({ actionType: "changeColumn", tableName, attributeName: df.path[2], options, depends, }); } } break; case "A": { console.log("[Not supported] Array model changes! Problems are possible. Please, check result more carefully!"); console.log("[Not supported] Difference: "); console.log(JSON.stringify(df, null, 4)); } break; default: break; } }); const result = (0, sortActions_1.default)(actions); return result; } exports.default = getDiffActionsFromTables; //# sourceMappingURL=getDiffActionsFromTables.js.map