Files
linkding/migrations/001-initial-schema.js
2025-11-16 10:33:59 +01:00

137 lines
3.3 KiB
JavaScript

/**
* Initial database schema migration
* Creates links, lists, and link_lists tables
*/
module.exports = {
up: async (queryInterface, Sequelize) => {
// Create links table
await queryInterface.createTable('links', {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
primaryKey: true
},
url: {
type: Sequelize.TEXT,
allowNull: false,
unique: true
},
title: {
type: Sequelize.TEXT,
allowNull: true
},
description: {
type: Sequelize.TEXT,
allowNull: true
},
image: {
type: Sequelize.TEXT,
allowNull: true
},
created_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
},
created_by: {
type: Sequelize.TEXT,
allowNull: true
},
modified_at: {
type: Sequelize.DATE,
allowNull: true
},
modified_by: {
type: Sequelize.TEXT,
allowNull: true
},
archived: {
type: Sequelize.BOOLEAN,
defaultValue: false,
allowNull: false
}
});
// Create lists table
await queryInterface.createTable('lists', {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
primaryKey: true
},
name: {
type: Sequelize.TEXT,
allowNull: false
},
created_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
},
created_by: {
type: Sequelize.TEXT,
allowNull: true
},
modified_at: {
type: Sequelize.DATE,
allowNull: true
},
modified_by: {
type: Sequelize.TEXT,
allowNull: true
},
public: {
type: Sequelize.BOOLEAN,
defaultValue: false,
allowNull: false
}
});
// Create link_lists junction table
await queryInterface.createTable('link_lists', {
link_id: {
type: Sequelize.UUID,
allowNull: false,
references: {
model: 'links',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
},
list_id: {
type: Sequelize.UUID,
allowNull: false,
references: {
model: 'lists',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
}
});
// Add composite primary key
await queryInterface.addConstraint('link_lists', {
fields: ['link_id', 'list_id'],
type: 'primary key',
name: 'link_lists_pkey'
});
// Create indexes for better performance
await queryInterface.addIndex('links', ['url'], { unique: true });
await queryInterface.addIndex('links', ['created_at']);
await queryInterface.addIndex('links', ['archived']);
await queryInterface.addIndex('lists', ['name']);
await queryInterface.addIndex('link_lists', ['link_id']);
await queryInterface.addIndex('link_lists', ['list_id']);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('link_lists');
await queryInterface.dropTable('lists');
await queryInterface.dropTable('links');
}
};