Files
Filamenteka/api/migrate.js
DaX 1eec8c8b4a Add Bambu Lab predefined colors and finishes
- Parse Bambu Lab PDF to extract all official colors and finishes
- Add 77 predefined Bambu Lab colors with accurate hex codes
- Update finish dropdown to show only Bambu Lab finishes
- Make color selection dynamic based on brand and finish
- Show Bambu Lab colors only when BambuLab brand is selected
- Auto-fill hex codes for Bambu Lab colors
- Allow custom colors for other brands
- Add database migrations for finish types and colors
- Import all Bambu Lab colors to database
2025-06-23 19:33:15 +02:00

94 lines
2.9 KiB
JavaScript

const { Pool } = require('pg');
const fs = require('fs');
const path = require('path');
require('dotenv').config();
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: process.env.DATABASE_URL?.includes('amazonaws.com') ? { rejectUnauthorized: false } : false
});
async function migrate() {
try {
// Read schema file
const schemaPath = path.join(__dirname, '..', 'database', 'schema.sql');
const schema = fs.readFileSync(schemaPath, 'utf8');
// Execute schema
await pool.query(schema);
console.log('Database migration completed successfully');
// Run additional migrations
const migrationsPath = path.join(__dirname, '..', 'database', 'migrations');
if (fs.existsSync(migrationsPath)) {
const migrationFiles = fs.readdirSync(migrationsPath)
.filter(file => file.endsWith('.sql'))
.sort();
for (const file of migrationFiles) {
console.log(`Running migration: ${file}`);
const migrationSQL = fs.readFileSync(path.join(migrationsPath, file), 'utf8');
try {
await pool.query(migrationSQL);
console.log(`✓ Migration ${file} completed`);
} catch (err) {
console.log(`⚠ Migration ${file} skipped:`, err.message);
}
}
}
// Import legacy data if available
try {
const dataPath = path.join(__dirname, '..', 'data.json');
if (fs.existsSync(dataPath)) {
const legacyData = JSON.parse(fs.readFileSync(dataPath, 'utf8'));
// Import colors
const colors = new Set();
legacyData.forEach(item => {
if (item.boja) colors.add(item.boja);
});
for (const color of colors) {
const hex = legacyData.find(item => item.boja === color)?.bojaHex || '#000000';
await pool.query(
'INSERT INTO colors (name, hex) VALUES ($1, $2) ON CONFLICT (name) DO UPDATE SET hex = $2',
[color, hex]
);
}
// Import filaments
for (const item of legacyData) {
await pool.query(
`INSERT INTO filaments (brand, tip, finish, boja, boja_hex, refill, vakum, otvoreno, kolicina, cena)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`,
[
item.brand,
item.tip,
item.finish,
item.boja,
item.bojaHex,
item.refill,
item.vakum,
item.otvoreno,
item.kolicina || 1,
item.cena
]
);
}
console.log('Legacy data imported successfully');
}
} catch (error) {
console.error('Error importing legacy data:', error);
}
process.exit(0);
} catch (error) {
console.error('Migration failed:', error);
process.exit(1);
}
}
migrate();