85 lines
2.3 KiB
JavaScript
85 lines
2.3 KiB
JavaScript
'use strict';
|
|
|
|
/** @type {import('sequelize-cli').Migration} */
|
|
module.exports = {
|
|
async up(queryInterface, Sequelize) {
|
|
await queryInterface.createTable('user_bookmarks', {
|
|
id: {
|
|
type: Sequelize.CHAR(36),
|
|
primaryKey: true,
|
|
allowNull: false,
|
|
comment: 'UUID primary key'
|
|
},
|
|
user_id: {
|
|
type: Sequelize.CHAR(36),
|
|
allowNull: false,
|
|
references: {
|
|
model: 'users',
|
|
key: 'id'
|
|
},
|
|
onUpdate: 'CASCADE',
|
|
onDelete: 'CASCADE',
|
|
comment: 'Foreign key to users table'
|
|
},
|
|
question_id: {
|
|
type: Sequelize.CHAR(36),
|
|
allowNull: false,
|
|
references: {
|
|
model: 'questions',
|
|
key: 'id'
|
|
},
|
|
onUpdate: 'CASCADE',
|
|
onDelete: 'CASCADE',
|
|
comment: 'Foreign key to questions table'
|
|
},
|
|
notes: {
|
|
type: Sequelize.TEXT,
|
|
allowNull: true,
|
|
comment: 'Optional user notes about the bookmarked question'
|
|
},
|
|
created_at: {
|
|
type: Sequelize.DATE,
|
|
allowNull: false,
|
|
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
|
|
comment: 'When the bookmark was created'
|
|
},
|
|
updated_at: {
|
|
type: Sequelize.DATE,
|
|
allowNull: false,
|
|
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
|
|
comment: 'Record last update timestamp'
|
|
}
|
|
}, {
|
|
charset: 'utf8mb4',
|
|
collate: 'utf8mb4_unicode_ci',
|
|
comment: 'Junction table for users bookmarking questions'
|
|
});
|
|
|
|
// Add indexes
|
|
await queryInterface.addIndex('user_bookmarks', ['user_id'], {
|
|
name: 'idx_user_bookmarks_user_id'
|
|
});
|
|
|
|
await queryInterface.addIndex('user_bookmarks', ['question_id'], {
|
|
name: 'idx_user_bookmarks_question_id'
|
|
});
|
|
|
|
await queryInterface.addIndex('user_bookmarks', ['created_at'], {
|
|
name: 'idx_user_bookmarks_created_at'
|
|
});
|
|
|
|
// Unique composite index to prevent duplicate bookmarks
|
|
await queryInterface.addIndex('user_bookmarks', ['user_id', 'question_id'], {
|
|
name: 'idx_user_bookmarks_user_question',
|
|
unique: true
|
|
});
|
|
|
|
console.log('✅ User bookmarks table created with 5 fields and 4 indexes');
|
|
},
|
|
|
|
async down(queryInterface, Sequelize) {
|
|
await queryInterface.dropTable('user_bookmarks');
|
|
console.log('✅ User bookmarks table dropped');
|
|
}
|
|
};
|