vr-migrations 1.0.35 ā 1.0.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/db.js +103 -19
- package/package.json +1 -1
package/dist/db.js
CHANGED
|
@@ -17,30 +17,30 @@ function getSequelize() {
|
|
|
17
17
|
async function initializeDatabase(config) {
|
|
18
18
|
if (_isInitialized)
|
|
19
19
|
return _sequelize;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const shouldUseSSL =
|
|
27
|
-
|
|
20
|
+
console.log("\nš [VR-MIGRATIONS] Database initialization started...");
|
|
21
|
+
console.log(` NODE_ENV: ${config.NODE_ENV || "not set"}`);
|
|
22
|
+
console.log(` DATABASE_URL provided: ${!!config.DATABASE_URL}`);
|
|
23
|
+
console.log(` Individual params provided: ${!!config.DB_HOST}`);
|
|
24
|
+
// Determine if SSL should be used
|
|
25
|
+
const sslEnvironments = ["prestaging", "staging", "production"];
|
|
26
|
+
const shouldUseSSL = sslEnvironments.includes(config.NODE_ENV || "");
|
|
27
|
+
const hasSSLParam = config.DATABASE_URL?.includes("sslmode=require") || false;
|
|
28
|
+
const enableSSL = shouldUseSSL || hasSSLParam;
|
|
29
|
+
// Configure SSL
|
|
28
30
|
const dialectOptions = {};
|
|
29
|
-
if (
|
|
31
|
+
if (enableSSL) {
|
|
30
32
|
dialectOptions.ssl = {
|
|
31
33
|
require: true,
|
|
32
34
|
rejectUnauthorized: false,
|
|
33
35
|
};
|
|
34
|
-
console.log(
|
|
35
|
-
console.log("connecting to DATABASE_URL:", process.env.DATABASE_URL);
|
|
36
|
+
console.log(` š SSL enabled`);
|
|
36
37
|
}
|
|
37
|
-
|
|
38
|
+
else {
|
|
39
|
+
console.log(` š SSL disabled`);
|
|
40
|
+
}
|
|
41
|
+
// Build Sequelize configuration
|
|
42
|
+
let sequelizeConfig = {
|
|
38
43
|
dialect: "postgres",
|
|
39
|
-
host: config.DB_HOST,
|
|
40
|
-
port: config.DB_PORT,
|
|
41
|
-
username: config.DB_USER,
|
|
42
|
-
password: config.DB_PASSWORD,
|
|
43
|
-
database: config.DB_NAME,
|
|
44
44
|
logging: false,
|
|
45
45
|
pool: {
|
|
46
46
|
max: 20,
|
|
@@ -49,14 +49,98 @@ async function initializeDatabase(config) {
|
|
|
49
49
|
idle: 10000,
|
|
50
50
|
},
|
|
51
51
|
dialectOptions,
|
|
52
|
-
}
|
|
52
|
+
};
|
|
53
|
+
// Prefer DATABASE_URL over individual parameters
|
|
54
|
+
if (config.DATABASE_URL) {
|
|
55
|
+
// Ensure the URL has a valid password
|
|
56
|
+
let url = config.DATABASE_URL;
|
|
57
|
+
// Debug: Parse URL to check password
|
|
58
|
+
try {
|
|
59
|
+
const parsedUrl = new URL(url);
|
|
60
|
+
console.log(` š Parsed URL:`);
|
|
61
|
+
console.log(` - Host: ${parsedUrl.hostname}`);
|
|
62
|
+
console.log(` - Port: ${parsedUrl.port || "5432"}`);
|
|
63
|
+
console.log(` - Username: ${parsedUrl.username}`);
|
|
64
|
+
console.log(` - Password: ${parsedUrl.password ? "***" : "MISSING!"}`);
|
|
65
|
+
console.log(` - Database: ${parsedUrl.pathname.slice(1)}`);
|
|
66
|
+
// CRITICAL FIX: Ensure password is properly encoded in URL
|
|
67
|
+
if (parsedUrl.password &&
|
|
68
|
+
parsedUrl.password !== decodeURIComponent(parsedUrl.password)) {
|
|
69
|
+
// Password has special characters, reconstruct URL with encoded password
|
|
70
|
+
const encodedPassword = encodeURIComponent(parsedUrl.password);
|
|
71
|
+
url = url.replace(parsedUrl.password, encodedPassword);
|
|
72
|
+
console.log(` š§ Re-encoded password in URL`);
|
|
73
|
+
}
|
|
74
|
+
// CRITICAL FIX: If password is missing but we have individual password, add it
|
|
75
|
+
if (!parsedUrl.password && config.DB_PASSWORD) {
|
|
76
|
+
// Reconstruct URL with password
|
|
77
|
+
const encodedPassword = encodeURIComponent(config.DB_PASSWORD);
|
|
78
|
+
url = `postgresql://${parsedUrl.username}:${encodedPassword}@${parsedUrl.hostname}:${parsedUrl.port || "5432"}${parsedUrl.pathname}${parsedUrl.search}`;
|
|
79
|
+
console.log(` š§ Added missing password to URL`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
console.error(` ā Failed to parse DATABASE_URL:`, error);
|
|
84
|
+
}
|
|
85
|
+
sequelizeConfig.url = url;
|
|
86
|
+
console.log(` ā
Using DATABASE_URL connection`);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
// Use individual parameters
|
|
90
|
+
sequelizeConfig.host = config.DB_HOST;
|
|
91
|
+
sequelizeConfig.port = config.DB_PORT;
|
|
92
|
+
sequelizeConfig.username = config.DB_USER;
|
|
93
|
+
// CRITICAL FIX: Ensure password is a string
|
|
94
|
+
sequelizeConfig.password = config.DB_PASSWORD
|
|
95
|
+
? String(config.DB_PASSWORD)
|
|
96
|
+
: "";
|
|
97
|
+
sequelizeConfig.database = config.DB_NAME;
|
|
98
|
+
console.log(` ā
Using individual connection parameters`);
|
|
99
|
+
console.log(` - Host: ${sequelizeConfig.host}:${sequelizeConfig.port}`);
|
|
100
|
+
console.log(` - User: ${sequelizeConfig.username}`);
|
|
101
|
+
console.log(` - Password: ${sequelizeConfig.password ? "***" : "(empty)"}`);
|
|
102
|
+
console.log(` - Database: ${sequelizeConfig.database}`);
|
|
103
|
+
}
|
|
104
|
+
// Final debug before connection
|
|
105
|
+
console.log(`\n š Attempting database connection...`);
|
|
53
106
|
try {
|
|
107
|
+
_sequelize = new sequelize_1.Sequelize(sequelizeConfig);
|
|
54
108
|
await _sequelize.authenticate();
|
|
55
109
|
_isInitialized = true;
|
|
110
|
+
console.log(` ā
Database connected successfully!\n`);
|
|
56
111
|
return _sequelize;
|
|
57
112
|
}
|
|
58
113
|
catch (error) {
|
|
59
|
-
console.error(
|
|
114
|
+
console.error(` ā Database connection failed:`, error);
|
|
115
|
+
// Helpful error messages
|
|
116
|
+
if (error instanceof Error) {
|
|
117
|
+
if (error.message.includes("password must be a string")) {
|
|
118
|
+
console.error(`
|
|
119
|
+
ā ļø PASSWORD ISSUE DETECTED!
|
|
120
|
+
|
|
121
|
+
The password is not being passed correctly to the database.
|
|
122
|
+
|
|
123
|
+
Possible solutions:
|
|
124
|
+
1. Check your .env file for special characters in passwords (@, #, $, etc.)
|
|
125
|
+
2. URL-encode special characters: @ -> %40, # -> %23, $ -> %24
|
|
126
|
+
3. Ensure DATABASE_URL format: postgresql://user:password@host:port/db
|
|
127
|
+
4. Try using individual DB_* variables instead of DATABASE_URL
|
|
128
|
+
|
|
129
|
+
Current config:
|
|
130
|
+
- Using DATABASE_URL: ${!!config.DATABASE_URL}
|
|
131
|
+
- DB_PASSWORD provided: ${!!config.DB_PASSWORD}
|
|
132
|
+
`);
|
|
133
|
+
}
|
|
134
|
+
else if (error.message.includes("does not support SSL")) {
|
|
135
|
+
console.error(`
|
|
136
|
+
ā ļø SSL ERROR DETECTED!
|
|
137
|
+
|
|
138
|
+
You're trying to connect with SSL to a database that doesn't support it.
|
|
139
|
+
|
|
140
|
+
Solution: Set NODE_ENV=development for local development
|
|
141
|
+
`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
60
144
|
throw error;
|
|
61
145
|
}
|
|
62
146
|
}
|