updated postgres creds fix
This commit is contained in:
parent
7c6be1f5da
commit
8a4529066d
|
|
@ -16,6 +16,7 @@ SQLITE_STORAGE=./data/luckychit.sqlite
|
|||
# DATABASE_URL=postgresql://user:pass@localhost:5432/luckychit
|
||||
|
||||
# One-time copy Postgres → SQLite: npm run migrate:pg-to-sqlite
|
||||
# Also merges .env.prod / .env.local (without overriding .env) so Postgres creds can live in .env.prod
|
||||
# SOURCE_DATABASE_URL=postgresql://user:pass@localhost:5432/luckychit
|
||||
|
||||
JWT_SECRET=change-me-in-production
|
||||
|
|
|
|||
|
|
@ -10,17 +10,20 @@
|
|||
* Usage (from backend/):
|
||||
* node scripts/migrate-pg-to-sqlite.js
|
||||
*
|
||||
* Env (backend/.env):
|
||||
* SOURCE_DATABASE_URL=postgresql://user:pass@localhost:5432/luckychit (recommended for migrate)
|
||||
* Or set DATABASE_URL to your Postgres URL temporarily.
|
||||
* SQLITE_STORAGE=./data/luckychit.sqlite (destination; default shown)
|
||||
* Env files (backend/): loads `.env`, then merges `.env.prod` and `.env.local` without
|
||||
* overriding keys already set (so sqlite-only `.env` can still pick up Postgres from `.env.prod`).
|
||||
*
|
||||
* If SOURCE_DATABASE_URL and DATABASE_URL are unset, builds URL from:
|
||||
* DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD
|
||||
* SOURCE_DATABASE_URL=postgresql://... (recommended if DATABASE_URL in .env is sqlite)
|
||||
* Or POSTGRES_URL / DATABASE_URL if postgres:// or postgresql://
|
||||
* SQLITE_STORAGE=./data/luckychit.sqlite (destination)
|
||||
* Or DB_* / PGHOST, PGDATABASE, PGUSER, PGPASSWORD
|
||||
*/
|
||||
|
||||
const path = require('path');
|
||||
require('dotenv').config({ path: path.join(__dirname, '..', '.env') });
|
||||
const backendRoot = path.join(__dirname, '..');
|
||||
require('dotenv').config({ path: path.join(backendRoot, '.env') });
|
||||
require('dotenv').config({ path: path.join(backendRoot, '.env.prod'), override: false });
|
||||
require('dotenv').config({ path: path.join(backendRoot, '.env.local'), override: false });
|
||||
|
||||
const { Sequelize, QueryTypes } = require('sequelize');
|
||||
|
||||
|
|
@ -36,28 +39,41 @@ const TABLES_ORDER = [
|
|||
|
||||
const TABLES_DELETE_REVERSE = [...TABLES_ORDER].reverse();
|
||||
|
||||
function isPostgresConnectionString(s) {
|
||||
const t = String(s).trim();
|
||||
return /^postgres(ql)?:\/\//i.test(t);
|
||||
}
|
||||
|
||||
function getPostgresUrl() {
|
||||
const u = process.env.SOURCE_DATABASE_URL;
|
||||
if (u && String(u).startsWith('postgres')) return u;
|
||||
const candidates = [
|
||||
process.env.SOURCE_DATABASE_URL,
|
||||
process.env.POSTGRES_URL,
|
||||
process.env.DATABASE_URL,
|
||||
].filter(Boolean);
|
||||
|
||||
const du = process.env.DATABASE_URL;
|
||||
if (du && String(du).startsWith('postgres')) return du;
|
||||
for (const u of candidates) {
|
||||
if (isPostgresConnectionString(u)) return String(u).trim();
|
||||
}
|
||||
|
||||
const host = process.env.DB_HOST;
|
||||
const port = process.env.DB_PORT || 5432;
|
||||
const name = process.env.DB_NAME;
|
||||
const user = process.env.DB_USER;
|
||||
const pass = process.env.DB_PASSWORD ?? '';
|
||||
const host = process.env.DB_HOST || process.env.PGHOST;
|
||||
const port = process.env.DB_PORT || process.env.PGPORT || 5432;
|
||||
const name = process.env.DB_NAME || process.env.PGDATABASE;
|
||||
const user = process.env.DB_USER || process.env.PGUSER;
|
||||
const pass = process.env.DB_PASSWORD ?? process.env.PGPASSWORD ?? '';
|
||||
|
||||
if (host && name && user !== undefined) {
|
||||
const userEnc = encodeURIComponent(user);
|
||||
const passEnc = encodeURIComponent(pass);
|
||||
if (host && name && user != null && String(user).length > 0) {
|
||||
const userEnc = encodeURIComponent(String(user));
|
||||
const passEnc = encodeURIComponent(String(pass));
|
||||
return `postgresql://${userEnc}:${passEnc}@${host}:${port}/${name}`;
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
'Could not build PostgreSQL URL. Set SOURCE_DATABASE_URL=postgresql://... ' +
|
||||
'or DATABASE_URL (postgres), or DB_HOST, DB_NAME, DB_USER, DB_PASSWORD.'
|
||||
'Could not build PostgreSQL URL. Your .env likely uses SQLite only.\n' +
|
||||
' Add one line to backend/.env (use your real Postgres host/user/db), then run again:\n' +
|
||||
' SOURCE_DATABASE_URL=postgresql://USER:PASSWORD@HOST:5432/DBNAME\n' +
|
||||
' Or set POSTGRES_URL / DATABASE_URL to a postgresql:// or postgres:// URL.\n' +
|
||||
' Or set DB_HOST, DB_NAME, DB_USER, DB_PASSWORD (and optional DB_PORT),\n' +
|
||||
' or libpq vars: PGHOST, PGDATABASE, PGUSER, PGPASSWORD (optional PGPORT).'
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue