mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-09-01 13:51:27 +02:00
Merge into one migration file
This commit is contained in:
parent
ab00b306e2
commit
f7bf970938
@ -1,9 +1,14 @@
|
|||||||
const { DataTypes } = require('sequelize')
|
const { DataTypes } = require('sequelize')
|
||||||
|
|
||||||
|
const migrationName = 'v2.25.2-add-book-ratings'
|
||||||
|
const loggerPrefix = `[${migrationName} migration]`
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
up: async ({ context: queryInterface }) => {
|
up: async ({ context: { queryInterface, logger } }) => {
|
||||||
|
logger.info(`${loggerPrefix} UPGRADE BEGIN: ${migrationName}`)
|
||||||
const transaction = await queryInterface.sequelize.transaction()
|
const transaction = await queryInterface.sequelize.transaction()
|
||||||
try {
|
try {
|
||||||
|
logger.info(`${loggerPrefix} adding columns to books table`)
|
||||||
await queryInterface.addColumn(
|
await queryInterface.addColumn(
|
||||||
'books',
|
'books',
|
||||||
'providerRating',
|
'providerRating',
|
||||||
@ -28,21 +33,128 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
{ transaction }
|
{ 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()
|
await transaction.commit()
|
||||||
|
logger.info(`${loggerPrefix} UPGRADE END: ${migrationName}`)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await transaction.rollback()
|
await transaction.rollback()
|
||||||
|
logger.error(`${loggerPrefix} UPGRADE FAILED: ${migrationName}`, { error: err })
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
down: async ({ context: queryInterface }) => {
|
down: async ({ context: { queryInterface, logger } }) => {
|
||||||
|
logger.info(`${loggerPrefix} DOWNGRADE BEGIN: ${migrationName}`)
|
||||||
const transaction = await queryInterface.sequelize.transaction()
|
const transaction = await queryInterface.sequelize.transaction()
|
||||||
try {
|
try {
|
||||||
|
logger.info(`${loggerPrefix} removing columns from books table`)
|
||||||
await queryInterface.removeColumn('books', 'providerRating', { transaction })
|
await queryInterface.removeColumn('books', 'providerRating', { transaction })
|
||||||
await queryInterface.removeColumn('books', 'provider', { transaction })
|
await queryInterface.removeColumn('books', 'provider', { transaction })
|
||||||
await queryInterface.removeColumn('books', 'providerId', { 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()
|
await transaction.commit()
|
||||||
|
logger.info(`${loggerPrefix} DOWNGRADE END: ${migrationName}`)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await transaction.rollback()
|
await transaction.rollback()
|
||||||
|
logger.error(`${loggerPrefix} DOWNGRADE FAILED: ${migrationName}`, { error: err })
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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')
|
|
||||||
}
|
|
||||||
}
|
|
@ -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')
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user