39172-vm/backend/src/db/migrations/1773403308528-create-files-table.js
Flatlogic Bot 9c59af97be 1
2026-03-13 13:29:01 +00:00

129 lines
3.5 KiB
JavaScript

module.exports = {
/**
* @param {import('sequelize').QueryInterface} queryInterface
* @param {import('sequelize').Sequelize} Sequelize
*/
async up(queryInterface, Sequelize) {
const transaction = await queryInterface.sequelize.transaction();
try {
const rawTables = await queryInterface.showAllTables({ transaction });
const tableNames = rawTables.map((table) => {
if (typeof table === 'string') {
return table;
}
return table.tableName || table.name;
});
if (!tableNames.includes('files')) {
await queryInterface.createTable(
'files',
{
id: {
type: Sequelize.DataTypes.UUID,
defaultValue: Sequelize.DataTypes.UUIDV4,
primaryKey: true,
},
belongsTo: {
type: Sequelize.DataTypes.STRING(255),
allowNull: true,
},
belongsToId: {
type: Sequelize.DataTypes.UUID,
allowNull: true,
},
belongsToColumn: {
type: Sequelize.DataTypes.STRING(255),
allowNull: true,
},
name: {
type: Sequelize.DataTypes.STRING(2083),
allowNull: false,
},
sizeInBytes: {
type: Sequelize.DataTypes.INTEGER,
allowNull: true,
},
privateUrl: {
type: Sequelize.DataTypes.STRING(2083),
allowNull: true,
},
publicUrl: {
type: Sequelize.DataTypes.STRING(2083),
allowNull: false,
},
createdById: {
type: Sequelize.DataTypes.UUID,
allowNull: true,
references: {
model: 'users',
key: 'id',
},
},
updatedById: {
type: Sequelize.DataTypes.UUID,
allowNull: true,
references: {
model: 'users',
key: 'id',
},
},
createdAt: {
type: Sequelize.DataTypes.DATE,
allowNull: false,
},
updatedAt: {
type: Sequelize.DataTypes.DATE,
allowNull: false,
},
deletedAt: {
type: Sequelize.DataTypes.DATE,
allowNull: true,
},
},
{ transaction },
);
await queryInterface.addIndex('files', ['belongsTo', 'belongsToId', 'belongsToColumn'], {
name: 'files_belongs_to_idx',
transaction,
});
}
await transaction.commit();
} catch (error) {
await transaction.rollback();
throw error;
}
},
/**
* @param {import('sequelize').QueryInterface} queryInterface
* @param {import('sequelize').Sequelize} Sequelize
*/
async down(queryInterface) {
const transaction = await queryInterface.sequelize.transaction();
try {
const rawTables = await queryInterface.showAllTables({ transaction });
const tableNames = rawTables.map((table) => {
if (typeof table === 'string') {
return table;
}
return table.tableName || table.name;
});
if (tableNames.includes('files')) {
await queryInterface.dropTable('files', { transaction });
}
await transaction.commit();
} catch (error) {
await transaction.rollback();
throw error;
}
},
};