vr-migrations 1.0.31 → 1.0.32
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/cli.js +61 -11
- package/dist/db.d.ts +0 -8
- package/dist/db.js +29 -51
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -14,27 +14,77 @@ const create_migration_1 = require("./create-migration");
|
|
|
14
14
|
const create_seeder_1 = require("./create-seeder");
|
|
15
15
|
const db_1 = require("./db");
|
|
16
16
|
const dotenv_1 = __importDefault(require("dotenv"));
|
|
17
|
-
|
|
17
|
+
const path_1 = __importDefault(require("path"));
|
|
18
|
+
const fs_1 = __importDefault(require("fs"));
|
|
19
|
+
// Load environment variables - try multiple sources
|
|
20
|
+
function loadEnv() {
|
|
21
|
+
const envFile = process.env.NODE_ENV === "test"
|
|
22
|
+
? ".env.test"
|
|
23
|
+
: `.env.${process.env.NODE_ENV || "development"}`;
|
|
24
|
+
const envPath = path_1.default.resolve(process.cwd(), envFile);
|
|
25
|
+
// Try environment-specific file first
|
|
26
|
+
if (fs_1.default.existsSync(envPath)) {
|
|
27
|
+
dotenv_1.default.config({ path: envPath });
|
|
28
|
+
console.log(`📄 Loaded env from: ${envFile}`);
|
|
29
|
+
}
|
|
30
|
+
// Also load default .env as fallback
|
|
31
|
+
dotenv_1.default.config({ path: path_1.default.resolve(process.cwd(), ".env") });
|
|
32
|
+
}
|
|
33
|
+
loadEnv();
|
|
18
34
|
exports.program = new commander_1.Command();
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
.
|
|
23
|
-
|
|
24
|
-
|
|
35
|
+
// Helper to get database configuration supporting both URL and individual params
|
|
36
|
+
function getDatabaseConfig() {
|
|
37
|
+
// Priority: DATABASE_URL > individual params
|
|
38
|
+
const databaseUrl = process.env.DATABASE_URL;
|
|
39
|
+
if (databaseUrl) {
|
|
40
|
+
// Parse DATABASE_URL to extract components
|
|
41
|
+
try {
|
|
42
|
+
const url = new URL(databaseUrl);
|
|
43
|
+
return {
|
|
44
|
+
DB_HOST: url.hostname,
|
|
45
|
+
DB_PORT: parseInt(url.port || "5432"),
|
|
46
|
+
DB_USER: decodeURIComponent(url.username),
|
|
47
|
+
DB_PASSWORD: decodeURIComponent(url.password),
|
|
48
|
+
DB_NAME: url.pathname.slice(1),
|
|
49
|
+
NODE_ENV: process.env.NODE_ENV,
|
|
50
|
+
DATABASE_URL: databaseUrl, // Pass through for SSL detection
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
console.error("❌ Failed to parse DATABASE_URL:", error);
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// Fallback to individual params
|
|
59
|
+
return {
|
|
25
60
|
DB_HOST: process.env.DB_HOST,
|
|
26
61
|
DB_PORT: parseInt(process.env.DB_PORT || "5432"),
|
|
27
62
|
DB_USER: process.env.DB_USER,
|
|
28
63
|
DB_PASSWORD: process.env.DB_PASSWORD,
|
|
29
64
|
DB_NAME: process.env.DB_NAME,
|
|
30
65
|
NODE_ENV: process.env.NODE_ENV,
|
|
31
|
-
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
exports.program
|
|
69
|
+
.name("vr-migrate")
|
|
70
|
+
.description("CENTRALIZED VR Migration Tool")
|
|
71
|
+
.version("1.0.0")
|
|
72
|
+
.hook("preAction", async () => {
|
|
73
|
+
const config = getDatabaseConfig();
|
|
74
|
+
// Validate required fields
|
|
75
|
+
if (!config.DB_HOST || !config.DB_USER || !config.DB_NAME) {
|
|
76
|
+
console.error("❌ Missing database configuration. Please set DATABASE_URL or DB_HOST, DB_USER, DB_NAME");
|
|
77
|
+
process.exit(1);
|
|
78
|
+
}
|
|
79
|
+
console.log(`🔌 Connecting to database: ${config.DB_HOST}:${config.DB_PORT}/${config.DB_NAME}`);
|
|
80
|
+
const sequelize = await (0, db_1.initializeDatabase)(config);
|
|
32
81
|
await sequelize.authenticate();
|
|
82
|
+
console.log("✅ Database connection established");
|
|
33
83
|
});
|
|
34
84
|
// Centralized Commands (No path overrides allowed)
|
|
35
85
|
exports.program
|
|
36
86
|
.command("migrate")
|
|
37
|
-
.description("Run CENTRALIZED migrations from vr-migrations package")
|
|
87
|
+
.description("Run CENTRALIZED migrations from vr-migrations package")
|
|
38
88
|
.action(async () => await (0, migrate_1.migrate)());
|
|
39
89
|
exports.program
|
|
40
90
|
.command("rollback")
|
|
@@ -51,10 +101,10 @@ exports.program
|
|
|
51
101
|
.action(async () => await (0, reset_1.reset)());
|
|
52
102
|
exports.program
|
|
53
103
|
.command("create:migration <name>")
|
|
54
|
-
.description("Create new migration in vr-migrations package")
|
|
104
|
+
.description("Create new migration in vr-migrations package")
|
|
55
105
|
.action((name) => (0, create_migration_1.createMigration)(name));
|
|
56
106
|
exports.program
|
|
57
107
|
.command("create:seeder <name>")
|
|
58
|
-
.description("Create new seeder in vr-migrations package")
|
|
108
|
+
.description("Create new seeder in vr-migrations package")
|
|
59
109
|
.action((name) => (0, create_seeder_1.createSeeder)(name));
|
|
60
110
|
exports.program.parse(process.argv);
|
package/dist/db.d.ts
CHANGED
|
@@ -7,14 +7,6 @@ export interface DatabaseConfig {
|
|
|
7
7
|
DB_NAME?: string;
|
|
8
8
|
DATABASE_URL?: string;
|
|
9
9
|
NODE_ENV?: string;
|
|
10
|
-
pool?: {
|
|
11
|
-
max?: number;
|
|
12
|
-
min?: number;
|
|
13
|
-
acquire?: number;
|
|
14
|
-
idle?: number;
|
|
15
|
-
};
|
|
16
|
-
logging?: boolean | ((sql: string, timing?: number) => void);
|
|
17
|
-
dialectOptions?: any;
|
|
18
10
|
}
|
|
19
11
|
export declare function getSequelize(): Sequelize;
|
|
20
12
|
export declare function initializeDatabase(config: DatabaseConfig): Promise<Sequelize>;
|
package/dist/db.js
CHANGED
|
@@ -4,7 +4,7 @@ exports.Op = exports.DataTypes = exports.Model = void 0;
|
|
|
4
4
|
exports.getSequelize = getSequelize;
|
|
5
5
|
exports.initializeDatabase = initializeDatabase;
|
|
6
6
|
exports.closeDatabase = closeDatabase;
|
|
7
|
-
//
|
|
7
|
+
// vr-migrations/src/db.ts
|
|
8
8
|
const sequelize_1 = require("sequelize");
|
|
9
9
|
let _sequelize = null;
|
|
10
10
|
let _isInitialized = false;
|
|
@@ -17,65 +17,43 @@ function getSequelize() {
|
|
|
17
17
|
async function initializeDatabase(config) {
|
|
18
18
|
if (_isInitialized)
|
|
19
19
|
return _sequelize;
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
min: 2,
|
|
28
|
-
acquire: 120000,
|
|
29
|
-
idle: 10000,
|
|
30
|
-
};
|
|
31
|
-
// Default dialect options
|
|
32
|
-
let dialectOptions = config.dialectOptions || {};
|
|
33
|
-
// Add SSL for production/staging/Neon
|
|
20
|
+
// Detect if we need SSL (Neon or production/staging)
|
|
21
|
+
const isNeon = config.DATABASE_URL?.includes("neon.tech") ||
|
|
22
|
+
config.DB_HOST?.includes("neon.tech");
|
|
23
|
+
const isProduction = config.NODE_ENV === "production" || config.NODE_ENV === "staging";
|
|
24
|
+
const shouldUseSSL = isNeon || isProduction;
|
|
25
|
+
// Configure dialect options for SSL
|
|
26
|
+
const dialectOptions = {};
|
|
34
27
|
if (shouldUseSSL) {
|
|
35
|
-
dialectOptions = {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
require: true,
|
|
39
|
-
rejectUnauthorized: false, // Required for Neon
|
|
40
|
-
},
|
|
28
|
+
dialectOptions.ssl = {
|
|
29
|
+
require: true,
|
|
30
|
+
rejectUnauthorized: false,
|
|
41
31
|
};
|
|
32
|
+
console.log("🔒 SSL enabled for database connection");
|
|
42
33
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
port: config.DB_PORT || 5432,
|
|
60
|
-
username: config.DB_USER,
|
|
61
|
-
password: config.DB_PASSWORD,
|
|
62
|
-
database: config.DB_NAME,
|
|
63
|
-
logging: config.logging !== undefined ? config.logging : false,
|
|
64
|
-
pool: poolConfig,
|
|
65
|
-
dialectOptions,
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
throw new Error("Invalid database configuration. Provide either DATABASE_URL or (DB_HOST, DB_USER, DB_NAME)");
|
|
70
|
-
}
|
|
34
|
+
_sequelize = new sequelize_1.Sequelize({
|
|
35
|
+
dialect: "postgres",
|
|
36
|
+
host: config.DB_HOST,
|
|
37
|
+
port: config.DB_PORT,
|
|
38
|
+
username: config.DB_USER,
|
|
39
|
+
password: config.DB_PASSWORD,
|
|
40
|
+
database: config.DB_NAME,
|
|
41
|
+
logging: false,
|
|
42
|
+
pool: {
|
|
43
|
+
max: 20,
|
|
44
|
+
min: 2,
|
|
45
|
+
acquire: 120000,
|
|
46
|
+
idle: 10000,
|
|
47
|
+
},
|
|
48
|
+
dialectOptions,
|
|
49
|
+
});
|
|
71
50
|
try {
|
|
72
51
|
await _sequelize.authenticate();
|
|
73
|
-
console.log(`✅ Database connection established successfully`);
|
|
74
52
|
_isInitialized = true;
|
|
75
53
|
return _sequelize;
|
|
76
54
|
}
|
|
77
55
|
catch (error) {
|
|
78
|
-
console.error("❌ Database
|
|
56
|
+
console.error("❌ Database authentication failed:", error);
|
|
79
57
|
throw error;
|
|
80
58
|
}
|
|
81
59
|
}
|