Implement Phase 1.2: Database with encryption at rest and core infrastructure

Co-authored-by: southseact-3d <217551146+southseact-3d@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-02-09 19:33:00 +00:00
parent 95a2d1b98d
commit 650d849ad2
17 changed files with 2716 additions and 0 deletions

122
chat/scripts/setup-database.js Executable file
View File

@@ -0,0 +1,122 @@
#!/usr/bin/env node
/**
* Setup database script
* Initializes the SQLite database with the schema
*/
const fs = require('fs');
const path = require('path');
const { initDatabase, getDatabase, closeDatabase } = require('../src/database/connection');
const { initEncryption } = require('../src/utils/encryption');
const crypto = require('crypto');
const DATA_ROOT = process.env.CHAT_DATA_ROOT || path.join(__dirname, '..', '.data');
const DATABASE_PATH = process.env.DATABASE_PATH || path.join(DATA_ROOT, 'shopify_ai.db');
const DATABASE_ENCRYPTION_KEY = process.env.DATABASE_ENCRYPTION_KEY;
const WAL_MODE = process.env.DATABASE_WAL_MODE !== '0' && process.env.DATABASE_WAL_MODE !== 'false';
async function setupDatabase() {
console.log('🔧 Setting up database...');
console.log(' Database path:', DATABASE_PATH);
// Ensure data directory exists
const dataDir = path.dirname(DATABASE_PATH);
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
console.log(' Created data directory:', dataDir);
}
// Check if encryption key is provided
if (!DATABASE_ENCRYPTION_KEY) {
console.warn('⚠️ WARNING: No DATABASE_ENCRYPTION_KEY found!');
console.warn('⚠️ Generating a random key for this session (not persistent).');
console.warn('⚠️ For production, set DATABASE_ENCRYPTION_KEY environment variable.');
console.warn('⚠️ Generate one with: openssl rand -hex 32');
const generatedKey = crypto.randomBytes(32).toString('hex');
process.env.DATABASE_ENCRYPTION_KEY = generatedKey;
console.log('✅ Generated temporary encryption key');
} else {
console.log('✅ Using encryption key from environment');
}
// Initialize encryption
try {
initEncryption(process.env.DATABASE_ENCRYPTION_KEY);
console.log('✅ Encryption initialized');
} catch (error) {
console.error('❌ Failed to initialize encryption:', error.message);
process.exit(1);
}
// Initialize database
try {
initDatabase(DATABASE_PATH, {
verbose: false,
walMode: WAL_MODE
});
console.log('✅ Database initialized');
} catch (error) {
console.error('❌ Failed to initialize database:', error.message);
process.exit(1);
}
// Load and execute schema
try {
const schemaPath = path.join(__dirname, '..', 'src', 'database', 'schema.sql');
const schema = fs.readFileSync(schemaPath, 'utf8');
const db = getDatabase();
// Split by semicolon and execute each statement
const statements = schema
.split(';')
.map(s => s.trim())
.filter(s => s.length > 0 && !s.startsWith('--'));
for (const statement of statements) {
db.exec(statement);
}
console.log('✅ Database schema created');
console.log(` Executed ${statements.length} SQL statements`);
} catch (error) {
console.error('❌ Failed to create schema:', error.message);
closeDatabase();
process.exit(1);
}
// Verify tables
try {
const db = getDatabase();
const tables = db.prepare(`
SELECT name FROM sqlite_master
WHERE type='table' AND name NOT LIKE 'sqlite_%'
ORDER BY name
`).all();
console.log('✅ Database tables created:');
tables.forEach(table => {
console.log(` - ${table.name}`);
});
} catch (error) {
console.error('❌ Failed to verify tables:', error.message);
}
// Close database
closeDatabase();
console.log('');
console.log('✅ Database setup complete!');
console.log('');
console.log('Next steps:');
console.log(' 1. Run migration: node scripts/migrate-to-database.js');
console.log(' 2. Verify migration: node scripts/verify-migration.js');
console.log(' 3. Switch to database mode: unset USE_JSON_DATABASE');
console.log(' 4. Start server: npm start');
}
// Run setup
setupDatabase().catch(error => {
console.error('❌ Setup failed:', error);
process.exit(1);
});