vr-migrations 1.0.36 → 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.
Files changed (2) hide show
  1. package/dist/db.js +86 -11
  2. package/package.json +1 -1
package/dist/db.js CHANGED
@@ -17,25 +17,28 @@ function getSequelize() {
17
17
  async function initializeDatabase(config) {
18
18
  if (_isInitialized)
19
19
  return _sequelize;
20
- // SSL only for prestaging, staging, and production (Neon databases)
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
21
25
  const sslEnvironments = ["prestaging", "staging", "production"];
22
26
  const shouldUseSSL = sslEnvironments.includes(config.NODE_ENV || "");
23
- // Also check if DATABASE_URL explicitly contains sslmode=require
24
27
  const hasSSLParam = config.DATABASE_URL?.includes("sslmode=require") || false;
25
28
  const enableSSL = shouldUseSSL || hasSSLParam;
26
- // Configure dialect options for SSL
29
+ // Configure SSL
27
30
  const dialectOptions = {};
28
31
  if (enableSSL) {
29
32
  dialectOptions.ssl = {
30
33
  require: true,
31
34
  rejectUnauthorized: false,
32
35
  };
33
- console.log(`šŸ”’ SSL enabled for ${config.NODE_ENV} environment`);
36
+ console.log(` šŸ”’ SSL enabled`);
34
37
  }
35
38
  else {
36
- console.log(`šŸ”“ SSL disabled for ${config.NODE_ENV} environment (local database)`);
39
+ console.log(` šŸ”“ SSL disabled`);
37
40
  }
38
- // Use DATABASE_URL if provided, otherwise use individual components
41
+ // Build Sequelize configuration
39
42
  let sequelizeConfig = {
40
43
  dialect: "postgres",
41
44
  logging: false,
@@ -47,25 +50,97 @@ async function initializeDatabase(config) {
47
50
  },
48
51
  dialectOptions,
49
52
  };
53
+ // Prefer DATABASE_URL over individual parameters
50
54
  if (config.DATABASE_URL) {
51
- sequelizeConfig.url = 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`);
52
87
  }
53
88
  else {
89
+ // Use individual parameters
54
90
  sequelizeConfig.host = config.DB_HOST;
55
91
  sequelizeConfig.port = config.DB_PORT;
56
92
  sequelizeConfig.username = config.DB_USER;
57
- sequelizeConfig.password = config.DB_PASSWORD;
93
+ // CRITICAL FIX: Ensure password is a string
94
+ sequelizeConfig.password = config.DB_PASSWORD
95
+ ? String(config.DB_PASSWORD)
96
+ : "";
58
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}`);
59
103
  }
60
- _sequelize = new sequelize_1.Sequelize(sequelizeConfig);
104
+ // Final debug before connection
105
+ console.log(`\n šŸš€ Attempting database connection...`);
61
106
  try {
107
+ _sequelize = new sequelize_1.Sequelize(sequelizeConfig);
62
108
  await _sequelize.authenticate();
63
109
  _isInitialized = true;
64
- console.log(`āœ… Database connected successfully (SSL: ${enableSSL ? "ON" : "OFF"})`);
110
+ console.log(` āœ… Database connected successfully!\n`);
65
111
  return _sequelize;
66
112
  }
67
113
  catch (error) {
68
- console.error("āŒ Database authentication failed:", 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
+ }
69
144
  throw error;
70
145
  }
71
146
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vr-migrations",
3
- "version": "1.0.36",
3
+ "version": "1.0.37",
4
4
  "description": "Database migration and seeding package for VR applications",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",