mirror of
https://github.com/donpat1to/Schichtenplaner.git
synced 2025-12-01 15:05:45 +01:00
set template shift struc
This commit is contained in:
@@ -1,41 +1,103 @@
|
||||
import { db } from '../services/databaseService.js';
|
||||
import { readFile } from 'fs/promises';
|
||||
import { readFile, readdir } from 'fs/promises';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { dirname, join } from 'path';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
|
||||
// Helper function to ensure migrations are tracked
|
||||
async function ensureMigrationTable() {
|
||||
await db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS applied_migrations (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
applied_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
`);
|
||||
}
|
||||
|
||||
// Helper function to check if a migration has been applied
|
||||
async function isMigrationApplied(migrationName: string): Promise<boolean> {
|
||||
const result = await db.get<{ count: number }>(
|
||||
'SELECT COUNT(*) as count FROM applied_migrations WHERE name = ?',
|
||||
[migrationName]
|
||||
);
|
||||
return (result?.count ?? 0) > 0;
|
||||
}
|
||||
|
||||
// Helper function to mark a migration as applied
|
||||
async function markMigrationAsApplied(migrationName: string) {
|
||||
await db.run(
|
||||
'INSERT INTO applied_migrations (id, name) VALUES (?, ?)',
|
||||
[crypto.randomUUID(), migrationName]
|
||||
);
|
||||
}
|
||||
|
||||
export async function applyMigration() {
|
||||
try {
|
||||
console.log('📦 Starting database migration...');
|
||||
|
||||
// Read the migration file
|
||||
const migrationPath = join(__dirname, '../database/migrations/002_add_employee_fields.sql');
|
||||
const migrationSQL = await readFile(migrationPath, 'utf-8');
|
||||
// Ensure migration tracking table exists
|
||||
await ensureMigrationTable();
|
||||
|
||||
// Split into individual statements
|
||||
const statements = migrationSQL
|
||||
.split(';')
|
||||
.map(s => s.trim())
|
||||
.filter(s => s.length > 0);
|
||||
// Get all migration files
|
||||
const migrationsDir = join(__dirname, '../database/migrations');
|
||||
const files = await readdir(migrationsDir);
|
||||
|
||||
// Execute each statement
|
||||
for (const statement of statements) {
|
||||
// Sort files to ensure consistent order
|
||||
const migrationFiles = files
|
||||
.filter(f => f.endsWith('.sql'))
|
||||
.sort();
|
||||
|
||||
// Process each migration file
|
||||
for (const migrationFile of migrationFiles) {
|
||||
if (await isMigrationApplied(migrationFile)) {
|
||||
console.log(`ℹ️ Migration ${migrationFile} already applied, skipping...`);
|
||||
continue;
|
||||
}
|
||||
|
||||
console.log(`📄 Applying migration: ${migrationFile}`);
|
||||
const migrationPath = join(migrationsDir, migrationFile);
|
||||
const migrationSQL = await readFile(migrationPath, 'utf-8');
|
||||
|
||||
// Split into individual statements
|
||||
const statements = migrationSQL
|
||||
.split(';')
|
||||
.map(s => s.trim())
|
||||
.filter(s => s.length > 0);
|
||||
|
||||
// Start transaction for this migration
|
||||
await db.run('BEGIN TRANSACTION');
|
||||
|
||||
try {
|
||||
await db.exec(statement);
|
||||
console.log('✅ Executed:', statement.slice(0, 50) + '...');
|
||||
} catch (error) {
|
||||
const err = error as { code: string; message: string };
|
||||
if (err.code === 'SQLITE_ERROR' && err.message.includes('duplicate column name')) {
|
||||
console.log('ℹ️ Column already exists, skipping...');
|
||||
continue;
|
||||
// Execute each statement
|
||||
for (const statement of statements) {
|
||||
try {
|
||||
await db.exec(statement);
|
||||
console.log('✅ Executed:', statement.slice(0, 50) + '...');
|
||||
} catch (error) {
|
||||
const err = error as { code: string; message: string };
|
||||
if (err.code === 'SQLITE_ERROR' && err.message.includes('duplicate column name')) {
|
||||
console.log('ℹ️ Column already exists, skipping...');
|
||||
continue;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Mark migration as applied
|
||||
await markMigrationAsApplied(migrationFile);
|
||||
await db.run('COMMIT');
|
||||
console.log(`✅ Migration ${migrationFile} applied successfully`);
|
||||
|
||||
} catch (error) {
|
||||
await db.run('ROLLBACK');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
console.log('✅ Migration completed successfully');
|
||||
console.log('✅ All migrations completed successfully');
|
||||
} catch (error) {
|
||||
console.error('❌ Migration failed:', error);
|
||||
throw error;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { db } from '../services/databaseService.js';
|
||||
import { ShiftTemplate } from '../models/ShiftTemplate.js';
|
||||
import { TemplateShift } from '../models/ShiftTemplate.js';
|
||||
|
||||
async function checkTemplates() {
|
||||
try {
|
||||
const templates = await db.all<ShiftTemplate>(
|
||||
const templates = await db.all<TemplateShift>(
|
||||
`SELECT st.*, u.name as created_by_name
|
||||
FROM shift_templates st
|
||||
LEFT JOIN users u ON st.created_by = u.id`
|
||||
|
||||
@@ -55,7 +55,7 @@ export async function setupDefaultTemplate(): Promise<void> {
|
||||
console.log('Standard-Vorlage erstellt:', templateId);
|
||||
|
||||
// Vormittagsschicht Mo-Do
|
||||
for (let day = 1; day <= 4; day++) {
|
||||
for (let day = 1; day <= 5; day++) {
|
||||
await db.run(
|
||||
`INSERT INTO template_shifts (id, template_id, day_of_week, name, start_time, end_time, required_employees)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
@@ -63,7 +63,7 @@ export async function setupDefaultTemplate(): Promise<void> {
|
||||
);
|
||||
}
|
||||
|
||||
console.log('Vormittagsschichten Mo-Do erstellt');
|
||||
console.log('Vormittagsschichten Mo-Fr erstellt');
|
||||
|
||||
// Nachmittagsschicht Mo-Do
|
||||
for (let day = 1; day <= 4; day++) {
|
||||
@@ -76,15 +76,6 @@ export async function setupDefaultTemplate(): Promise<void> {
|
||||
|
||||
console.log('Nachmittagsschichten Mo-Do erstellt');
|
||||
|
||||
// Freitag nur Vormittagsschicht
|
||||
await db.run(
|
||||
`INSERT INTO template_shifts (id, template_id, day_of_week, name, start_time, end_time, required_employees)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
[uuidv4(), templateId, 5, 'Vormittagsschicht', '08:00', '12:00', 1]
|
||||
);
|
||||
|
||||
console.log('Freitag Vormittagsschicht erstellt');
|
||||
|
||||
await db.run('COMMIT');
|
||||
console.log('Standard-Vorlage erfolgreich initialisiert');
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user