feat(database): add migrations.ts with migration framework
This commit is contained in:
@@ -0,0 +1,48 @@
|
|||||||
|
import { readFileSync } from 'fs';
|
||||||
|
import { join } from 'path';
|
||||||
|
|
||||||
|
export type Migration = {
|
||||||
|
version: number;
|
||||||
|
name: string;
|
||||||
|
up: string; // SQL script for migration
|
||||||
|
};
|
||||||
|
|
||||||
|
export class MigrationManager {
|
||||||
|
private migrations: Migration[] = [];
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.loadMigrations();
|
||||||
|
}
|
||||||
|
|
||||||
|
private loadMigrations() {
|
||||||
|
// In a real implementation, this would load migration files from a directory
|
||||||
|
// For now, we'll just create a single migration for the initial schema
|
||||||
|
|
||||||
|
const schemaPath = join(__dirname, 'schema.sql');
|
||||||
|
const schemaSql = readFileSync(schemaPath, 'utf8');
|
||||||
|
|
||||||
|
const initialMigration: Migration = {
|
||||||
|
version: 1,
|
||||||
|
name: 'initial_schema',
|
||||||
|
up: schemaSql
|
||||||
|
};
|
||||||
|
|
||||||
|
this.migrations.push(initialMigration);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getMigrations(): Migration[] {
|
||||||
|
return this.migrations;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getMigrationByVersion(version: number): Migration | undefined {
|
||||||
|
return this.migrations.find(m => m.version === version);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getLatestVersion(): number {
|
||||||
|
if (this.migrations.length === 0) return 0;
|
||||||
|
return Math.max(...this.migrations.map(m => m.version));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Export a singleton instance
|
||||||
|
export const migrationManager = new MigrationManager();
|
||||||
Reference in New Issue
Block a user