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
|
# DATABASE_URL=postgresql://user:pass@localhost:5432/luckychit
|
||||||
|
|
||||||
# One-time copy Postgres → SQLite: npm run migrate:pg-to-sqlite
|
# 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
|
# SOURCE_DATABASE_URL=postgresql://user:pass@localhost:5432/luckychit
|
||||||
|
|
||||||
JWT_SECRET=change-me-in-production
|
JWT_SECRET=change-me-in-production
|
||||||
|
|
|
||||||
|
|
@ -10,17 +10,20 @@
|
||||||
* Usage (from backend/):
|
* Usage (from backend/):
|
||||||
* node scripts/migrate-pg-to-sqlite.js
|
* node scripts/migrate-pg-to-sqlite.js
|
||||||
*
|
*
|
||||||
* Env (backend/.env):
|
* Env files (backend/): loads `.env`, then merges `.env.prod` and `.env.local` without
|
||||||
* SOURCE_DATABASE_URL=postgresql://user:pass@localhost:5432/luckychit (recommended for migrate)
|
* overriding keys already set (so sqlite-only `.env` can still pick up Postgres from `.env.prod`).
|
||||||
* Or set DATABASE_URL to your Postgres URL temporarily.
|
|
||||||
* SQLITE_STORAGE=./data/luckychit.sqlite (destination; default shown)
|
|
||||||
*
|
*
|
||||||
* If SOURCE_DATABASE_URL and DATABASE_URL are unset, builds URL from:
|
* SOURCE_DATABASE_URL=postgresql://... (recommended if DATABASE_URL in .env is sqlite)
|
||||||
* DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD
|
* 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');
|
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');
|
const { Sequelize, QueryTypes } = require('sequelize');
|
||||||
|
|
||||||
|
|
@ -36,28 +39,41 @@ const TABLES_ORDER = [
|
||||||
|
|
||||||
const TABLES_DELETE_REVERSE = [...TABLES_ORDER].reverse();
|
const TABLES_DELETE_REVERSE = [...TABLES_ORDER].reverse();
|
||||||
|
|
||||||
|
function isPostgresConnectionString(s) {
|
||||||
|
const t = String(s).trim();
|
||||||
|
return /^postgres(ql)?:\/\//i.test(t);
|
||||||
|
}
|
||||||
|
|
||||||
function getPostgresUrl() {
|
function getPostgresUrl() {
|
||||||
const u = process.env.SOURCE_DATABASE_URL;
|
const candidates = [
|
||||||
if (u && String(u).startsWith('postgres')) return u;
|
process.env.SOURCE_DATABASE_URL,
|
||||||
|
process.env.POSTGRES_URL,
|
||||||
|
process.env.DATABASE_URL,
|
||||||
|
].filter(Boolean);
|
||||||
|
|
||||||
const du = process.env.DATABASE_URL;
|
for (const u of candidates) {
|
||||||
if (du && String(du).startsWith('postgres')) return du;
|
if (isPostgresConnectionString(u)) return String(u).trim();
|
||||||
|
}
|
||||||
|
|
||||||
const host = process.env.DB_HOST;
|
const host = process.env.DB_HOST || process.env.PGHOST;
|
||||||
const port = process.env.DB_PORT || 5432;
|
const port = process.env.DB_PORT || process.env.PGPORT || 5432;
|
||||||
const name = process.env.DB_NAME;
|
const name = process.env.DB_NAME || process.env.PGDATABASE;
|
||||||
const user = process.env.DB_USER;
|
const user = process.env.DB_USER || process.env.PGUSER;
|
||||||
const pass = process.env.DB_PASSWORD ?? '';
|
const pass = process.env.DB_PASSWORD ?? process.env.PGPASSWORD ?? '';
|
||||||
|
|
||||||
if (host && name && user !== undefined) {
|
if (host && name && user != null && String(user).length > 0) {
|
||||||
const userEnc = encodeURIComponent(user);
|
const userEnc = encodeURIComponent(String(user));
|
||||||
const passEnc = encodeURIComponent(pass);
|
const passEnc = encodeURIComponent(String(pass));
|
||||||
return `postgresql://${userEnc}:${passEnc}@${host}:${port}/${name}`;
|
return `postgresql://${userEnc}:${passEnc}@${host}:${port}/${name}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Could not build PostgreSQL URL. Set SOURCE_DATABASE_URL=postgresql://... ' +
|
'Could not build PostgreSQL URL. Your .env likely uses SQLite only.\n' +
|
||||||
'or DATABASE_URL (postgres), or DB_HOST, DB_NAME, DB_USER, DB_PASSWORD.'
|
' 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