// db/init.js const db = require('./knex'); async function init() { // ------------------------------- // 1) Tabella PHOTOS // ------------------------------- const existsPhotos = await db.schema.hasTable('photos'); if (!existsPhotos) { await db.schema.createTable('photos', (t) => { t.string('id').primary(); t.string('user'); t.string('cartella'); t.string('name'); t.string('path'); t.string('thub1'); t.string('thub2'); t.string('mime_type'); t.integer('width'); t.integer('height'); t.integer('rotation'); t.integer('size_bytes'); t.integer('mtimeMs'); t.integer('duration_ms'); t.string('taken_at'); t.string('data'); t.float('lat'); t.float('lon'); t.float('alt'); t.text('location'); // JSON string // Hash lento (metadati) t.string('_indexHash'); // Hash veloce (size-mtime) t.string('fast_hash'); }); console.log("✔ Tabella 'photos' creata correttamente (con _indexHash + fast_hash)"); } else { console.log("✔ Tabella 'photos' già esistente"); // Controllo colonna _indexHash const hasIndexHash = await db.schema.hasColumn('photos', '_indexHash'); if (!hasIndexHash) { await db.schema.alterTable('photos', (t) => { t.string('_indexHash'); }); console.log("✔ Colonna '_indexHash' aggiunta"); } // Controllo colonna fast_hash const hasFastHash = await db.schema.hasColumn('photos', 'fast_hash'); if (!hasFastHash) { await db.schema.alterTable('photos', (t) => { t.string('fast_hash'); }); console.log("✔ Colonna 'fast_hash' aggiunta"); } } // ------------------------------- // 2) Tabella PHOTO_CHANGES // ------------------------------- const existsChanges = await db.schema.hasTable('photo_changes'); if (!existsChanges) { await db.schema.createTable('photo_changes', (t) => { t.increments('id').primary(); t.string('photo_id'); t.string('user'); t.string('change_type'); // added | updated | removed t.string('timestamp'); // ISO string }); console.log("✔ Tabella 'photo_changes' creata correttamente"); } else { console.log("✔ Tabella 'photo_changes' già esistente"); } process.exit(); } init();