Add database migration scripts and configuration files
- Add verify-migration.js script for testing database migrations - Add database config module for centralized configuration - Add chutes.txt prompt for system responses - Update database implementation and testing documentation - Add database migration and setup scripts - Update session system and LLM tool configuration - Update deployment checklist and environment example - Update Dockerfile and docker-compose configuration
This commit is contained in:
@@ -7,9 +7,9 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const crypto = require('crypto');
|
||||
const { DATA_ROOT, DB_PATH, KEY_FILE } = require('../src/database/config');
|
||||
|
||||
const DATA_ROOT = process.env.CHAT_DATA_ROOT || '/home/web/data/.data';
|
||||
const DATABASE_PATH = process.env.DATABASE_PATH || path.join(DATA_ROOT, 'shopify_ai.db');
|
||||
const DATABASE_PATH = DB_PATH;
|
||||
const USE_JSON_DATABASE = process.env.USE_JSON_DATABASE === '1' || process.env.USE_JSON_DATABASE === 'true';
|
||||
|
||||
async function initializeDatabase() {
|
||||
@@ -33,7 +33,15 @@ async function initializeDatabase() {
|
||||
|
||||
if (dbExists) {
|
||||
console.log('✅ Database already exists:', DATABASE_PATH);
|
||||
|
||||
|
||||
if (!process.env.DATABASE_ENCRYPTION_KEY && KEY_FILE && fs.existsSync(KEY_FILE)) {
|
||||
const persistedKey = fs.readFileSync(KEY_FILE, 'utf8').trim();
|
||||
if (persistedKey) {
|
||||
process.env.DATABASE_ENCRYPTION_KEY = persistedKey;
|
||||
console.log('✅ Loaded encryption key from file');
|
||||
}
|
||||
}
|
||||
|
||||
// Verify encryption key is set
|
||||
if (!process.env.DATABASE_ENCRYPTION_KEY) {
|
||||
console.error('❌ DATABASE_ENCRYPTION_KEY not set!');
|
||||
@@ -48,6 +56,14 @@ async function initializeDatabase() {
|
||||
console.log('🔧 Database not found, setting up new database...');
|
||||
|
||||
// Generate encryption key if not provided
|
||||
if (!process.env.DATABASE_ENCRYPTION_KEY && KEY_FILE && fs.existsSync(KEY_FILE)) {
|
||||
const persistedKey = fs.readFileSync(KEY_FILE, 'utf8').trim();
|
||||
if (persistedKey) {
|
||||
process.env.DATABASE_ENCRYPTION_KEY = persistedKey;
|
||||
console.log('✅ Loaded encryption key from file');
|
||||
}
|
||||
}
|
||||
|
||||
if (!process.env.DATABASE_ENCRYPTION_KEY) {
|
||||
const generatedKey = crypto.randomBytes(32).toString('hex');
|
||||
process.env.DATABASE_ENCRYPTION_KEY = generatedKey;
|
||||
@@ -57,7 +73,7 @@ async function initializeDatabase() {
|
||||
console.log('⚠️ Add this to your environment configuration to persist it!');
|
||||
|
||||
// Save to a file for persistence
|
||||
const keyFile = path.join(dataDir, '.encryption_key');
|
||||
const keyFile = KEY_FILE || path.join(dataDir, '.encryption_key');
|
||||
fs.writeFileSync(keyFile, generatedKey, { mode: 0o600 });
|
||||
console.log('⚠️ Saved to:', keyFile);
|
||||
}
|
||||
@@ -86,8 +102,8 @@ async function initializeDatabase() {
|
||||
}
|
||||
|
||||
// Check if there are JSON files to migrate
|
||||
const usersFile = path.join(DATA_ROOT, 'users.json');
|
||||
const sessionsFile = path.join(DATA_ROOT, 'user-sessions.json');
|
||||
const usersFile = path.join(path.join(DATA_ROOT, '.opencode-chat'), 'users.json');
|
||||
const sessionsFile = path.join(path.join(DATA_ROOT, '.opencode-chat'), 'user-sessions.json');
|
||||
|
||||
const hasJsonData = fs.existsSync(usersFile) || fs.existsSync(sessionsFile);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user