feat: database

This commit is contained in:
2025-11-16 10:33:59 +01:00
parent 3cf9601a71
commit 1417023395
14 changed files with 1436 additions and 192 deletions

View File

@@ -0,0 +1,136 @@
/**
* 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');
}
};

104
migrations/runner.js Normal file
View File

@@ -0,0 +1,104 @@
const { QueryInterface } = require('sequelize');
const fs = require('fs').promises;
const path = require('path');
/**
* Simple migration runner for Sequelize
* Checks which migrations have been run and executes pending ones
*/
class MigrationRunner {
constructor(sequelize) {
this.sequelize = sequelize;
this.migrationsPath = path.join(__dirname);
}
async ensureMigrationsTable() {
const queryInterface = this.sequelize.getQueryInterface();
// Check if migrations table exists
const [results] = await this.sequelize.query(`
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'SequelizeMeta'
);
`);
if (!results[0].exists) {
// Create migrations table
await queryInterface.createTable('SequelizeMeta', {
name: {
type: require('sequelize').DataTypes.STRING,
allowNull: false,
primaryKey: true
}
});
}
}
async getExecutedMigrations() {
await this.ensureMigrationsTable();
const [results] = await this.sequelize.query(
'SELECT name FROM "SequelizeMeta" ORDER BY name'
);
return results.map(row => row.name);
}
async getAllMigrations() {
const files = await fs.readdir(this.migrationsPath);
return files
.filter(file => file.endsWith('.js') && file !== 'runner.js')
.sort();
}
async runMigrations() {
const executed = await this.getExecutedMigrations();
const allMigrations = await this.getAllMigrations();
const pending = allMigrations.filter(m => !executed.includes(m));
if (pending.length === 0) {
console.log('No pending migrations');
return;
}
console.log(`Running ${pending.length} pending migration(s)...`);
for (const migrationFile of pending) {
const migration = require(path.join(this.migrationsPath, migrationFile));
if (!migration.up || typeof migration.up !== 'function') {
throw new Error(`Migration ${migrationFile} does not export an 'up' function`);
}
const queryInterface = this.sequelize.getQueryInterface();
const transaction = await this.sequelize.transaction();
try {
console.log(`Running migration: ${migrationFile}`);
await migration.up(queryInterface, this.sequelize.constructor);
// Record migration as executed
await this.sequelize.query(
`INSERT INTO "SequelizeMeta" (name) VALUES (:name)`,
{
replacements: { name: migrationFile },
transaction
}
);
await transaction.commit();
console.log(`Completed migration: ${migrationFile}`);
} catch (error) {
await transaction.rollback();
throw new Error(`Migration ${migrationFile} failed: ${error.message}`);
}
}
console.log('All migrations completed successfully');
}
}
module.exports = MigrationRunner;