Skip to content

Commit

Permalink
rearanged sequence of runnign migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
soleil00 committed Jun 25, 2024
1 parent 9599ce7 commit d191b31
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 65 deletions.
65 changes: 0 additions & 65 deletions src/sequelize/migrations/20240529192329-add-private-Chat-Model.js

This file was deleted.

71 changes: 71 additions & 0 deletions src/sequelize/migrations/d20240529192329-add-private-Chat-Model.js
Original file line number Diff line number Diff line change
@@ -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");
},
};

0 comments on commit d191b31

Please sign in to comment.