UNPKG

@grouparoo/core

Version:
120 lines (119 loc) 5.93 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const migration_1 = require("../utils/migration"); // Note: In some future world this import might break. We no longer need to use this after a few months from when this migration is deployed in Grouparoo v0.7 const pluginDetails_1 = require("../modules/pluginDetails"); const configLoaders_1 = require("../modules/configLoaders"); const uuid = __importStar(require("uuid")); exports.default = { up: async (queryInterface, DataTypes) => { let codeConfigInUse = false; if ((await migration_1.MigrationUtils.countRows(queryInterface, "sources", `"locked" = 'config:code'`)) > 0 || (await migration_1.MigrationUtils.countRows(queryInterface, "destinations", `"locked" = 'config:code'`)) > 0) { codeConfigInUse = true; } let default_model = { id: `mod_${uuid.v4()}`, name: "Profiles", type: "profile", }; const countRecords = await migration_1.MigrationUtils.countRows(queryInterface, "records"); if (countRecords === 0) { // OK, we don't have any records we need to worry about updating } else if (codeConfigInUse === true) { const configDir = await (0, pluginDetails_1.getConfigDir)(); const { configObjects, errors } = await (0, configLoaders_1.loadConfigObjects)(configDir); if (errors.length > 0) throw new Error(errors.join("; ")); const modelConfigObjects = configObjects.filter((o) => o.class === "Model"); if (modelConfigObjects.length === 0) { throw new Error("There are no Models configured. When first upgrading to v0.7, Grouparoo requires a single Model to be created to define where existing Records should be migrated. See https://www.grouparoo.com/docs/support/upgrading-grouparoo/v06-v07 for more information."); } else if (modelConfigObjects.length > 1) { throw new Error("There is more than one Model configured. When first upgrading to v0.7, Grouparoo requires a single Model to be created to define where existing Records should be migrated. More Models can then be created after that. See https://www.grouparoo.com/docs/support/upgrading-grouparoo/v06-v07 for more information."); } // Use the model file to migrate all the existing records, but do not create the model (code config will handle that later) default_model = modelConfigObjects[0]; if (!default_model.id) throw new Error(`Model does not have an id (${JSON.stringify(default_model)})`); } else { // Not using Code Config - Create the default Model await queryInterface.sequelize.query(` INSERT INTO "models" ("id", "name", "type", "createdAt", "updatedAt") VALUES ('${default_model.id}', '${default_model.name}', '${default_model.type}', NOW(), NOW()) `); } await queryInterface.addColumn("models", "locked", { type: DataTypes.STRING(191), allowNull: true, }); await queryInterface.addColumn("records", "modelId", { type: DataTypes.STRING(191), defaultValue: default_model.id, allowNull: true, }); await queryInterface.changeColumn("records", "modelId", { type: DataTypes.STRING(191), allowNull: false, }); await queryInterface.addColumn("sources", "modelId", { type: DataTypes.STRING(191), defaultValue: default_model.id, allowNull: true, }); await queryInterface.changeColumn("sources", "modelId", { type: DataTypes.STRING(191), allowNull: false, }); await queryInterface.addColumn("groups", "modelId", { type: DataTypes.STRING(191), defaultValue: default_model.id, allowNull: true, }); await queryInterface.changeColumn("groups", "modelId", { type: DataTypes.STRING(191), allowNull: false, }); await queryInterface.addColumn("destinations", "modelId", { type: DataTypes.STRING(191), defaultValue: default_model.id, allowNull: true, }); await queryInterface.changeColumn("destinations", "modelId", { type: DataTypes.STRING(191), allowNull: false, }); }, down: async (queryInterface) => { await queryInterface.removeColumn("models", "locked"); await queryInterface.removeColumn("records", "modelId"); await queryInterface.removeColumn("sources", "modelId"); await queryInterface.removeColumn("groups", "modelId"); await queryInterface.removeColumn("destinations", "modelId"); }, };