From f7bf970938a23d40514b01e49e859d2a65fa86e1 Mon Sep 17 00:00:00 2001 From: Peter BALIVET Date: Mon, 30 Jun 2025 15:15:25 +0200 Subject: [PATCH] Merge into one migration file --- server/migrations/v2.25.2-add-book-ratings.js | 116 +++++++++++++++++- .../v2.25.3-add-user-book-ratings.js | 59 --------- .../v2.25.4-add-user-book-explicit-ratings.js | 59 --------- 3 files changed, 114 insertions(+), 120 deletions(-) delete mode 100644 server/migrations/v2.25.3-add-user-book-ratings.js delete mode 100644 server/migrations/v2.25.4-add-user-book-explicit-ratings.js diff --git a/server/migrations/v2.25.2-add-book-ratings.js b/server/migrations/v2.25.2-add-book-ratings.js index 1d15d545a..54ff60ab0 100644 --- a/server/migrations/v2.25.2-add-book-ratings.js +++ b/server/migrations/v2.25.2-add-book-ratings.js @@ -1,9 +1,14 @@ const { DataTypes } = require('sequelize') +const migrationName = 'v2.25.2-add-book-ratings' +const loggerPrefix = `[${migrationName} migration]` + module.exports = { - up: async ({ context: queryInterface }) => { + up: async ({ context: { queryInterface, logger } }) => { + logger.info(`${loggerPrefix} UPGRADE BEGIN: ${migrationName}`) const transaction = await queryInterface.sequelize.transaction() try { + logger.info(`${loggerPrefix} adding columns to books table`) await queryInterface.addColumn( 'books', 'providerRating', @@ -28,21 +33,128 @@ module.exports = { }, { transaction } ) + logger.info(`${loggerPrefix} added columns to books table`) + + logger.info(`${loggerPrefix} creating userBookRatings table`) + await queryInterface.createTable( + 'userBookRatings', + { + id: { + type: DataTypes.INTEGER, + primaryKey: true, + autoIncrement: true + }, + userId: { + type: DataTypes.STRING, + allowNull: false, + references: { model: 'users', key: 'id' }, + onUpdate: 'CASCADE', + onDelete: 'CASCADE' + }, + bookId: { + type: DataTypes.STRING, + allowNull: false, + references: { model: 'books', key: 'id' }, + onUpdate: 'CASCADE', + onDelete: 'CASCADE' + }, + rating: { + type: DataTypes.FLOAT, + allowNull: false + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false + } + }, + { transaction } + ) + await queryInterface.addConstraint('userBookRatings', { + fields: ['userId', 'bookId'], + type: 'unique', + name: 'user_book_ratings_unique_constraint', + transaction + }) + logger.info(`${loggerPrefix} created userBookRatings table`) + + logger.info(`${loggerPrefix} creating userBookExplicitRatings table`) + await queryInterface.createTable( + 'userBookExplicitRatings', + { + id: { + type: DataTypes.INTEGER, + primaryKey: true, + autoIncrement: true + }, + userId: { + type: DataTypes.STRING, + allowNull: false, + references: { model: 'users', key: 'id' }, + onUpdate: 'CASCADE', + onDelete: 'CASCADE' + }, + bookId: { + type: DataTypes.STRING, + allowNull: false, + references: { model: 'books', key: 'id' }, + onUpdate: 'CASCADE', + onDelete: 'CASCADE' + }, + rating: { + type: DataTypes.FLOAT, + allowNull: false + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false + } + }, + { transaction } + ) + await queryInterface.addConstraint('userBookExplicitRatings', { + fields: ['userId', 'bookId'], + type: 'unique', + name: 'user_book_explicit_ratings_unique_constraint', + transaction + }) + logger.info(`${loggerPrefix} created userBookExplicitRatings table`) + await transaction.commit() + logger.info(`${loggerPrefix} UPGRADE END: ${migrationName}`) } catch (err) { await transaction.rollback() + logger.error(`${loggerPrefix} UPGRADE FAILED: ${migrationName}`, { error: err }) throw err } }, - down: async ({ context: queryInterface }) => { + down: async ({ context: { queryInterface, logger } }) => { + logger.info(`${loggerPrefix} DOWNGRADE BEGIN: ${migrationName}`) const transaction = await queryInterface.sequelize.transaction() try { + logger.info(`${loggerPrefix} removing columns from books table`) await queryInterface.removeColumn('books', 'providerRating', { transaction }) await queryInterface.removeColumn('books', 'provider', { transaction }) await queryInterface.removeColumn('books', 'providerId', { transaction }) + logger.info(`${loggerPrefix} removed columns from books table`) + logger.info(`${loggerPrefix} dropping userBookRatings table`) + await queryInterface.dropTable('userBookRatings', { transaction }) + logger.info(`${loggerPrefix} dropped userBookRatings table`) + logger.info(`${loggerPrefix} dropping userBookExplicitRatings table`) + await queryInterface.dropTable('userBookExplicitRatings', { transaction }) + logger.info(`${loggerPrefix} dropped userBookExplicitRatings table`) await transaction.commit() + logger.info(`${loggerPrefix} DOWNGRADE END: ${migrationName}`) } catch (err) { await transaction.rollback() + logger.error(`${loggerPrefix} DOWNGRADE FAILED: ${migrationName}`, { error: err }) throw err } } diff --git a/server/migrations/v2.25.3-add-user-book-ratings.js b/server/migrations/v2.25.3-add-user-book-ratings.js deleted file mode 100644 index 9b63d4b21..000000000 --- a/server/migrations/v2.25.3-add-user-book-ratings.js +++ /dev/null @@ -1,59 +0,0 @@ -const { DataTypes } = require('sequelize') - -module.exports = { - up: async ({ context: queryInterface }) => { - const transaction = await queryInterface.sequelize.transaction() - try { - await queryInterface.createTable( - 'userBookRatings', - { - id: { - type: DataTypes.INTEGER, - primaryKey: true, - autoIncrement: true - }, - userId: { - type: DataTypes.STRING, - allowNull: false, - references: { model: 'users', key: 'id' }, - onUpdate: 'CASCADE', - onDelete: 'CASCADE' - }, - bookId: { - type: DataTypes.STRING, - allowNull: false, - references: { model: 'books', key: 'id' }, - onUpdate: 'CASCADE', - onDelete: 'CASCADE' - }, - rating: { - type: DataTypes.FLOAT, - allowNull: false - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false - } - }, - { transaction } - ) - await queryInterface.addConstraint('userBookRatings', { - fields: ['userId', 'bookId'], - type: 'unique', - name: 'user_book_ratings_unique_constraint', - transaction - }) - await transaction.commit() - } catch (err) { - await transaction.rollback() - throw err - } - }, - down: async ({ context: queryInterface }) => { - await queryInterface.dropTable('userBookRatings') - } -} diff --git a/server/migrations/v2.25.4-add-user-book-explicit-ratings.js b/server/migrations/v2.25.4-add-user-book-explicit-ratings.js deleted file mode 100644 index 8a1f69acb..000000000 --- a/server/migrations/v2.25.4-add-user-book-explicit-ratings.js +++ /dev/null @@ -1,59 +0,0 @@ -const { DataTypes } = require('sequelize') - -module.exports = { - up: async ({ context: queryInterface }) => { - const transaction = await queryInterface.sequelize.transaction() - try { - await queryInterface.createTable( - 'userBookExplicitRatings', - { - id: { - type: DataTypes.INTEGER, - primaryKey: true, - autoIncrement: true - }, - userId: { - type: DataTypes.STRING, - allowNull: false, - references: { model: 'users', key: 'id' }, - onUpdate: 'CASCADE', - onDelete: 'CASCADE' - }, - bookId: { - type: DataTypes.STRING, - allowNull: false, - references: { model: 'books', key: 'id' }, - onUpdate: 'CASCADE', - onDelete: 'CASCADE' - }, - rating: { - type: DataTypes.FLOAT, - allowNull: false - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false - } - }, - { transaction } - ) - await queryInterface.addConstraint('userBookExplicitRatings', { - fields: ['userId', 'bookId'], - type: 'unique', - name: 'user_book_explicit_ratings_unique_constraint', - transaction - }) - await transaction.commit() - } catch (err) { - await transaction.rollback() - throw err - } - }, - down: async ({ context: queryInterface }) => { - await queryInterface.dropTable('userBookExplicitRatings') - } -}