From d191b3171f8d7de655f22ca71dc1306dc16d44b4 Mon Sep 17 00:00:00 2001 From: soleil00 Date: Tue, 25 Jun 2024 18:56:55 +0200 Subject: [PATCH] rearanged sequence of runnign migrations --- .../20240529192329-add-private-Chat-Model.js | 65 ----------------- .../d20240529192329-add-private-Chat-Model.js | 71 +++++++++++++++++++ 2 files changed, 71 insertions(+), 65 deletions(-) delete mode 100644 src/sequelize/migrations/20240529192329-add-private-Chat-Model.js create mode 100644 src/sequelize/migrations/d20240529192329-add-private-Chat-Model.js diff --git a/src/sequelize/migrations/20240529192329-add-private-Chat-Model.js b/src/sequelize/migrations/20240529192329-add-private-Chat-Model.js deleted file mode 100644 index b977eb5..0000000 --- a/src/sequelize/migrations/20240529192329-add-private-Chat-Model.js +++ /dev/null @@ -1,65 +0,0 @@ -'use strict'; - -/** @type {import('sequelize-cli').Migration} */ -module.exports = { - async up (queryInterface, Sequelize) { - await queryInterface.createTable('PrivateChats', { - id: { - allowNull: false, - autoIncrement: true, - primaryKey: true, - type: Sequelize.INTEGER - }, - userId: { - type: Sequelize.INTEGER, - allowNull: false, - references: { - model: 'users', - key: 'id' - } - }, - receiverId: { - type: Sequelize.INTEGER, - allowNull: false, - references: { - model: 'users', - key: 'id' - } - }, - createdAt: { - allowNull: false, - type: Sequelize.DATE - }, - updatedAt: { - allowNull: false, - type: Sequelize.DATE - } - }); - - await queryInterface.addColumn('messages', 'privateChatId', { - type: Sequelize.INTEGER, - allowNull: true, - references: { - model: 'PrivateChats', - key: 'id' - }, - onUpdate: 'CASCADE', - onDelete: 'SET NULL', - }); - await queryInterface.addColumn('messages', 'isPrivate',{ - type: Sequelize.BOOLEAN, - allowNull: false - }) - - - - }, - - - async down (queryInterface, Sequelize) { - await queryInterface.dropTable('PrivateChats'); - await queryInterface.removeColumn('messages', 'privateChatId'); - await queryInterface.removeColumn('messages', 'isPrivate') - - } -}; diff --git a/src/sequelize/migrations/d20240529192329-add-private-Chat-Model.js b/src/sequelize/migrations/d20240529192329-add-private-Chat-Model.js new file mode 100644 index 0000000..cf9e30b --- /dev/null +++ b/src/sequelize/migrations/d20240529192329-add-private-Chat-Model.js @@ -0,0 +1,71 @@ +"use strict"; + +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.createTable("PrivateChats", { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER, + }, + userId: { + type: Sequelize.INTEGER, + allowNull: false, + references: { + model: "users", + key: "id", + }, + }, + receiverId: { + type: Sequelize.INTEGER, + allowNull: false, + references: { + model: "users", + key: "id", + }, + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE, + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE, + }, + }); + + const isPrivateChatID = await queryInterface.describeTable("messages").then((columns) => { + return "privateChatId" in columns; + }); + const isPrivate = await queryInterface.describeTable("messages").then((columns) => { + return "isPrivate" in columns; + }); + + if (!isPrivateChatID) { + await queryInterface.addColumn("messages", "privateChatId", { + type: Sequelize.INTEGER, + allowNull: true, + references: { + model: "PrivateChats", + key: "id", + }, + onUpdate: "CASCADE", + onDelete: "SET NULL", + }); + } + if (!isPrivate) { + await queryInterface.addColumn("messages", "isPrivate", { + type: Sequelize.BOOLEAN, + allowNull: false, + }); + } + }, + + async down(queryInterface, Sequelize) { + await queryInterface.dropTable("PrivateChats"); + await queryInterface.removeColumn("messages", "privateChatId"); + await queryInterface.removeColumn("messages", "isPrivate"); + }, +};