Spaces:
Runtime error
Runtime error
incognitolm commited on
Commit ·
cb54ec6
1
Parent(s): 03eabfb
step 3
Browse files- server/postgres.js +18 -5
- server/postgresMigration.js +10 -3
- server/postgresSchema.js +18 -0
server/postgres.js
CHANGED
|
@@ -44,16 +44,29 @@ export function buildPoolConfig() {
|
|
| 44 |
};
|
| 45 |
}
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
async function createPool() {
|
| 48 |
return createStandalonePostgresPool();
|
| 49 |
}
|
| 50 |
|
| 51 |
export async function createStandalonePostgresPool() {
|
| 52 |
-
const pool =
|
| 53 |
-
await
|
| 54 |
-
pool.on('error', (err) => {
|
| 55 |
-
console.error('PostgreSQL pool error:', err);
|
| 56 |
-
});
|
| 57 |
return pool;
|
| 58 |
}
|
| 59 |
|
|
|
|
| 44 |
};
|
| 45 |
}
|
| 46 |
|
| 47 |
+
function attachPoolErrorHandler(pool) {
|
| 48 |
+
pool.on('error', (err) => {
|
| 49 |
+
console.error('PostgreSQL pool error:', err);
|
| 50 |
+
});
|
| 51 |
+
return pool;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
export async function createRawPostgresPool() {
|
| 55 |
+
const pool = new Pool(buildPoolConfig());
|
| 56 |
+
return attachPoolErrorHandler(pool);
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
export async function applyPostgresSchema(pool) {
|
| 60 |
+
await pool.query(POSTGRES_SCHEMA_SQL);
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
async function createPool() {
|
| 64 |
return createStandalonePostgresPool();
|
| 65 |
}
|
| 66 |
|
| 67 |
export async function createStandalonePostgresPool() {
|
| 68 |
+
const pool = await createRawPostgresPool();
|
| 69 |
+
await applyPostgresSchema(pool);
|
|
|
|
|
|
|
|
|
|
| 70 |
return pool;
|
| 71 |
}
|
| 72 |
|
server/postgresMigration.js
CHANGED
|
@@ -9,12 +9,13 @@ import {
|
|
| 9 |
refreshStorageMode,
|
| 10 |
} from './dataPaths.js';
|
| 11 |
import {
|
| 12 |
-
|
|
|
|
| 13 |
encryptJsonPayload,
|
| 14 |
makeLookupToken,
|
| 15 |
makeOwnerLookup,
|
| 16 |
} from './postgres.js';
|
| 17 |
-
import { POSTGRES_SCHEMA_SQL } from './postgresSchema.js';
|
| 18 |
import { SUPABASE_URL } from './config.js';
|
| 19 |
|
| 20 |
const TEMP_TTL_MS = 24 * 60 * 60 * 1000;
|
|
@@ -158,6 +159,10 @@ async function truncateTargetTables(pool) {
|
|
| 158 |
`);
|
| 159 |
}
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
async function migrateVersions(pool, report) {
|
| 162 |
const data = await readJsonIfExists(VERSION_FILE);
|
| 163 |
const entries = Array.isArray(data) ? data : [];
|
|
@@ -546,7 +551,7 @@ export async function migrateLegacyDataToPostgres({ skipIfFolderExists = true }
|
|
| 546 |
|
| 547 |
await fs.mkdir(DATA_ROOT, { recursive: true });
|
| 548 |
|
| 549 |
-
const pool = await
|
| 550 |
const report = {
|
| 551 |
startedAt: nowIso(),
|
| 552 |
targetFolder: POSTGRES_STORAGE_DIR,
|
|
@@ -555,6 +560,8 @@ export async function migrateLegacyDataToPostgres({ skipIfFolderExists = true }
|
|
| 555 |
};
|
| 556 |
|
| 557 |
try {
|
|
|
|
|
|
|
| 558 |
await truncateTargetTables(pool);
|
| 559 |
await migrateVersions(pool, report);
|
| 560 |
await migrateTempSessions(pool, report);
|
|
|
|
| 9 |
refreshStorageMode,
|
| 10 |
} from './dataPaths.js';
|
| 11 |
import {
|
| 12 |
+
applyPostgresSchema,
|
| 13 |
+
createRawPostgresPool,
|
| 14 |
encryptJsonPayload,
|
| 15 |
makeLookupToken,
|
| 16 |
makeOwnerLookup,
|
| 17 |
} from './postgres.js';
|
| 18 |
+
import { APP_MANAGED_TABLES, POSTGRES_SCHEMA_SQL } from './postgresSchema.js';
|
| 19 |
import { SUPABASE_URL } from './config.js';
|
| 20 |
|
| 21 |
const TEMP_TTL_MS = 24 * 60 * 60 * 1000;
|
|
|
|
| 159 |
`);
|
| 160 |
}
|
| 161 |
|
| 162 |
+
async function dropTargetTables(pool) {
|
| 163 |
+
await pool.query(`DROP TABLE IF EXISTS ${APP_MANAGED_TABLES.join(', ')} CASCADE`);
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
async function migrateVersions(pool, report) {
|
| 167 |
const data = await readJsonIfExists(VERSION_FILE);
|
| 168 |
const entries = Array.isArray(data) ? data : [];
|
|
|
|
| 551 |
|
| 552 |
await fs.mkdir(DATA_ROOT, { recursive: true });
|
| 553 |
|
| 554 |
+
const pool = await createRawPostgresPool();
|
| 555 |
const report = {
|
| 556 |
startedAt: nowIso(),
|
| 557 |
targetFolder: POSTGRES_STORAGE_DIR,
|
|
|
|
| 560 |
};
|
| 561 |
|
| 562 |
try {
|
| 563 |
+
await dropTargetTables(pool);
|
| 564 |
+
await applyPostgresSchema(pool);
|
| 565 |
await truncateTargetTables(pool);
|
| 566 |
await migrateVersions(pool, report);
|
| 567 |
await migrateTempSessions(pool, report);
|
server/postgresSchema.js
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
export const POSTGRES_SCHEMA_SQL = `
|
| 2 |
CREATE TABLE IF NOT EXISTS app_versions (
|
| 3 |
public_url_lookup text PRIMARY KEY,
|
|
|
|
| 1 |
+
export const APP_MANAGED_TABLES = [
|
| 2 |
+
'media_blobs',
|
| 3 |
+
'media_entries',
|
| 4 |
+
'deleted_chats',
|
| 5 |
+
'memories',
|
| 6 |
+
'system_prompts',
|
| 7 |
+
'feedback_tickets',
|
| 8 |
+
'guest_request_counters',
|
| 9 |
+
'web_search_usage',
|
| 10 |
+
'user_settings',
|
| 11 |
+
'user_profiles',
|
| 12 |
+
'device_sessions',
|
| 13 |
+
'session_shares',
|
| 14 |
+
'chat_sessions',
|
| 15 |
+
'guest_state',
|
| 16 |
+
'app_versions',
|
| 17 |
+
];
|
| 18 |
+
|
| 19 |
export const POSTGRES_SCHEMA_SQL = `
|
| 20 |
CREATE TABLE IF NOT EXISTS app_versions (
|
| 21 |
public_url_lookup text PRIMARY KEY,
|