Fix database CLI environment variables config

Use the same environment setup as the main Jetzig app when running
database CLI commands.
This commit is contained in:
Bob Farrell 2024-11-16 17:16:29 +00:00
parent 3063615e26
commit e12b69985b
3 changed files with 30 additions and 17 deletions

View File

@ -15,8 +15,8 @@
.hash = "12201d75d73aad5e1c996de4d5ae87a00e58479c8d469bc2eeb5fdeeac8857bc09af", .hash = "12201d75d73aad5e1c996de4d5ae87a00e58479c8d469bc2eeb5fdeeac8857bc09af",
}, },
.jetquery = .{ .jetquery = .{
.url = "https://github.com/jetzig-framework/jetquery/archive/cc3aca60511079636f38701c790c2751582666b4.tar.gz", .url = "https://github.com/jetzig-framework/jetquery/archive/4069610e0aea2dcdf9f73ca55f3aeb96f01fca5e.tar.gz",
.hash = "122042fe961bce870e1b072d346fe21863d4baa3f78906eafd35e3aaae8dab020c78", .hash = "122043f7b1614c94d311df1153c7702492b30fb56210fafbb80b52ab39de08a25092",
}, },
.jetcommon = .{ .jetcommon = .{
.url = "https://github.com/jetzig-framework/jetcommon/archive/a248776ba56d6cc2b160d593ac3305756adcd26e.tar.gz", .url = "https://github.com/jetzig-framework/jetcommon/archive/a248776ba56d6cc2b160d593ac3305756adcd26e.tar.gz",

View File

@ -9,8 +9,8 @@
.hash = "1220411a8c46d95bbf3b6e2059854bcb3c5159d428814099df5294232b9980517e9c", .hash = "1220411a8c46d95bbf3b6e2059854bcb3c5159d428814099df5294232b9980517e9c",
}, },
.jetquery = .{ .jetquery = .{
.url = "https://github.com/jetzig-framework/jetquery/archive/cc3aca60511079636f38701c790c2751582666b4.tar.gz", .url = "https://github.com/jetzig-framework/jetquery/archive/4069610e0aea2dcdf9f73ca55f3aeb96f01fca5e.tar.gz",
.hash = "122042fe961bce870e1b072d346fe21863d4baa3f78906eafd35e3aaae8dab020c78", .hash = "122043f7b1614c94d311df1153c7702492b30fb56210fafbb80b52ab39de08a25092",
}, },
}, },
.paths = .{ .paths = .{

View File

@ -39,45 +39,57 @@ pub fn main() !void {
}); });
const action = map.get(args[1]) orelse return error.JetzigUnrecognizedArgument; const action = map.get(args[1]) orelse return error.JetzigUnrecognizedArgument;
const env = try jetzig.Environment.init(allocator, .{ .silent = true });
const repo_env = try jetzig.database.repoEnv(env);
const maybe_database = repo_env.database orelse
if (comptime @hasField(@TypeOf(config), "database")) config.database else null;
const database = maybe_database orelse {
std.debug.print("Missing `database` option in `config/database.zig` " ++
"for current environment or `JETQUERY_DATABASE` environment variable.\n", .{});
std.process.exit(1);
return;
};
switch (action) { switch (action) {
.migrate => { .migrate => {
var repo = try migrationsRepo(action, allocator); var repo = try migrationsRepo(action, allocator, repo_env);
defer repo.deinit(); defer repo.deinit();
try Migrate(config.adapter).init(&repo).migrate(); try Migrate(config.adapter).init(&repo).migrate();
}, },
.rollback => { .rollback => {
var repo = try migrationsRepo(action, allocator); var repo = try migrationsRepo(action, allocator, repo_env);
defer repo.deinit(); defer repo.deinit();
try Migrate(config.adapter).init(&repo).rollback(); try Migrate(config.adapter).init(&repo).rollback();
}, },
.create => { .create => {
var repo = try migrationsRepo(action, allocator); var repo = try migrationsRepo(action, allocator, repo_env);
defer repo.deinit(); defer repo.deinit();
try repo.createDatabase(config.database, .{}); try repo.createDatabase(database, .{});
}, },
.drop => { .drop => {
if (environment == .production) { if (environment == .production) {
const confirm = std.process.getEnvVarOwned(allocator, confirm_drop_env) catch |err| { const confirm = std.process.getEnvVarOwned(allocator, confirm_drop_env) catch |err| {
switch (err) { switch (err) {
error.EnvironmentVariableNotFound => { error.EnvironmentVariableNotFound => {
std.log.err(production_drop_failure_message, .{config.database}); std.log.err(production_drop_failure_message, .{database});
std.process.exit(1); std.process.exit(1);
}, },
else => return err, else => return err,
} }
}; };
if (std.mem.eql(u8, confirm, config.database)) { if (std.mem.eql(u8, confirm, database)) {
var repo = try migrationsRepo(action, allocator); var repo = try migrationsRepo(action, allocator, repo_env);
defer repo.deinit(); defer repo.deinit();
try repo.dropDatabase(config.database, .{}); try repo.dropDatabase(database, .{});
} else { } else {
std.log.err(production_drop_failure_message, .{config.database}); std.log.err(production_drop_failure_message, .{database});
std.process.exit(1); std.process.exit(1);
} }
} else { } else {
var repo = try migrationsRepo(action, allocator); var repo = try migrationsRepo(action, allocator, repo_env);
defer repo.deinit(); defer repo.deinit();
try repo.dropDatabase(config.database, .{}); try repo.dropDatabase(database, .{});
} }
}, },
.reflect => { .reflect => {
@ -88,7 +100,7 @@ pub fn main() !void {
var repo = try Repo.loadConfig( var repo = try Repo.loadConfig(
allocator, allocator,
std.enums.nameCast(jetquery.Environment, environment), std.enums.nameCast(jetquery.Environment, environment),
.{ .context = .migration }, .{ .context = .migration, .env = repo_env },
); );
const reflect = @import("jetquery_reflect").Reflect(config.adapter, Schema).init( const reflect = @import("jetquery_reflect").Reflect(config.adapter, Schema).init(
allocator, allocator,
@ -113,7 +125,7 @@ pub fn main() !void {
} }
const MigrationsRepo = jetquery.Repo(config.adapter, MigrateSchema); const MigrationsRepo = jetquery.Repo(config.adapter, MigrateSchema);
fn migrationsRepo(action: Action, allocator: std.mem.Allocator) !MigrationsRepo { fn migrationsRepo(action: Action, allocator: std.mem.Allocator, repo_env: anytype) !MigrationsRepo {
return try MigrationsRepo.loadConfig( return try MigrationsRepo.loadConfig(
allocator, allocator,
std.enums.nameCast(jetquery.Environment, environment), std.enums.nameCast(jetquery.Environment, environment),
@ -124,6 +136,7 @@ fn migrationsRepo(action: Action, allocator: std.mem.Allocator) !MigrationsRepo
.reflect => unreachable, // We use a separate repo for schema reflection. .reflect => unreachable, // We use a separate repo for schema reflection.
}, },
.context = .migration, .context = .migration,
.env = repo_env,
}, },
); );
} }