Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: auto-upgrade PostgreSQL schema on startup

Adam Eivyc207f2b96e16
diff --git a/server/lib/db.js b/server/lib/db.js
index c573964ca8..7084d29d40 100644
--- a/server/lib/db.js
+++ b/server/lib/db.js
@@ -85,6 +85,24 @@ export async function checkHealth() {
}
}
+/**
+ * Apply idempotent schema upgrades to an existing database.
+ * Each statement uses IF NOT EXISTS so it's safe to run on every startup.
+ * Add new ALTER TABLE statements here when the schema evolves.
+ */
+export async function ensureSchema() {
+ const upgrades = [
+ `ALTER TABLE memories ADD COLUMN IF NOT EXISTS sync_sequence BIGSERIAL`,
+ `ALTER TABLE memories ADD COLUMN IF NOT EXISTS origin_instance_id VARCHAR(36)`,
+ `CREATE INDEX IF NOT EXISTS idx_memories_origin_instance ON memories (origin_instance_id)`,
+ `CREATE INDEX IF NOT EXISTS idx_memories_sync_sequence ON memories (sync_sequence)`,
+ ];
+ for (const sql of upgrades) {
+ await pool.query(sql);
+ }
+ console.log('🗄️ Database schema upgrades applied');
+}
+
/**
* Gracefully shut down the pool.
*/
diff --git a/server/services/memoryBackend.js b/server/services/memoryBackend.js
index 1789bc21cb..f05686e019 100644
--- a/server/services/memoryBackend.js
+++ b/server/services/memoryBackend.js
@@ -9,7 +9,7 @@
* // All functions are the same regardless of backend
*/
-import { checkHealth } from '../lib/db.js';
+import { checkHealth, ensureSchema } from '../lib/db.js';
import { DEFAULT_MEMORY_CONFIG } from './memoryConfig.js';
export { DEFAULT_MEMORY_CONFIG };
@@ -34,6 +34,7 @@ async function getBackend() {
}
if (envBackend === 'postgres') {
+ await ensureSchema();
backend = await import('./memoryDB.js');
backendName = 'postgres';
console.log('🧠 Memory backend: PostgreSQL + pgvector');
@@ -43,6 +44,7 @@ async function getBackend() {
// Auto-detect: try PostgreSQL first, fall back to file
const health = await checkHealth();
if (health.connected && health.hasSchema) {
+ await ensureSchema();
backend = await import('./memoryDB.js');
backendName = 'postgres';
console.log('🧠 Memory backend: PostgreSQL + pgvector (auto-detected)');