Merge into one migration file

This commit is contained in:
Peter BALIVET 2025-06-30 15:15:25 +02:00
parent ab00b306e2
commit f7bf970938
3 changed files with 114 additions and 120 deletions

View File

@ -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
}
}

View File

@ -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')
}
}

View File

@ -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')
}
}