This commit is contained in:
Bob Farrell 2024-11-05 21:35:16 +00:00
parent 320c2706ae
commit 6c53ccf8fc
5 changed files with 18 additions and 16 deletions

View File

@ -330,7 +330,6 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
exe_database.root_module.addImport("jetquery_migrate", jetquery_migrate_module);
exe_database.root_module.addImport("jetquery_reflect", jetquery_reflect_module);
exe_database.root_module.addImport("Schema", schema_module);
// exe_database.root_module.addOptions("build_options", build_options);
registerDatabaseSteps(b, exe_database);
@ -347,7 +346,7 @@ fn registerDatabaseSteps(b: *std.Build, exe_database: *std.Build.Step.Compile) v
.{ "rollback", "Roll back a migration in your Jetzig app's database." },
.{ "create", "Create a database for your Jetzig app." },
.{ "drop", "Drop your Jetzig app's database." },
.{ "schema", "Read your app's database and generate a JetQuery schema." },
.{ "dump", "Read your app's database and generate a JetQuery schema." },
};
inline for (commands) |command| {

View File

@ -10,13 +10,13 @@ const migrate = @import("database/migrate.zig");
const rollback = @import("database/rollback.zig");
const create = @import("database/create.zig");
const drop = @import("database/drop.zig");
const schema = @import("database/schema.zig");
const dump = @import("database/dump.zig");
pub const confirm_drop_env = "JETZIG_DROP_PRODUCTION_DATABASE";
/// Command line options for the `database` command.
pub const Options = struct {
pub const meta = .{
.usage_summary = "[migrate|rollback|create|drop|schema]",
.usage_summary = "[migrate|rollback|create|drop|dump]",
.full_text =
\\Manage the application's database.
\\
@ -40,13 +40,13 @@ pub fn run(
defer arena.deinit();
const alloc = arena.allocator();
const Action = enum { migrate, rollback, create, drop, schema };
const Action = enum { migrate, rollback, create, drop, dump };
const map = std.StaticStringMap(Action).initComptime(.{
.{ "migrate", .migrate },
.{ "rollback", .rollback },
.{ "create", .create },
.{ "drop", .drop },
.{ "schema", .schema },
.{ "dump", .dump },
});
const action = if (main_options.positionals.len > 0)
@ -74,7 +74,7 @@ pub fn run(
.rollback => rollback.run(alloc, cwd, sub_args, options, T, main_options),
.create => create.run(alloc, cwd, sub_args, options, T, main_options),
.drop => drop.run(alloc, cwd, sub_args, options, T, main_options),
.schema => schema.run(alloc, cwd, sub_args, options, T, main_options),
.dump => dump.run(alloc, cwd, sub_args, options, T, main_options),
};
};
}

View File

@ -19,7 +19,7 @@ pub fn run(
\\
\\Example:
\\
\\ jetzig database schema
\\ jetzig database dump
\\
, .{});
@ -30,6 +30,6 @@ pub fn run(
"zig",
"build",
util.environmentBuildOption(main_options.options.environment),
"jetzig:database:schema",
"jetzig:database:dump",
});
}

View File

@ -14,7 +14,7 @@ const production_drop_failure_message = "To drop a production database, " ++
const environment = jetzig.build_options.environment;
const config = @field(jetquery.config.database, @tagName(environment));
const Action = enum { migrate, rollback, create, drop, schema };
const Action = enum { migrate, rollback, create, drop, dump };
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
@ -35,7 +35,7 @@ pub fn main() !void {
.{ "rollback", .rollback },
.{ "create", .create },
.{ "drop", .drop },
.{ "schema", .schema },
.{ "dump", .dump },
});
const action = map.get(args[1]) orelse return error.JetzigUnrecognizedDatabaseArgument;
@ -80,7 +80,7 @@ pub fn main() !void {
try repo.dropDatabase(config.database, .{});
}
},
.schema => {
.dump => {
var cwd = try jetzig.util.detectJetzigProjectDir();
defer cwd.close();
@ -99,13 +99,13 @@ pub fn main() !void {
,
},
);
const schema = try reflect.generateSchema();
const dump = try reflect.generateSchema();
const path = try cwd.realpathAlloc(
allocator,
try std.fs.path.join(allocator, &.{ "src", "app", "database", "Schema.zig" }),
);
try jetzig.util.createFile(path, schema);
std.log.info("Database schema written to `{s}`.", .{path});
try jetzig.util.createFile(path, dump);
std.log.info("Database dump written to `{s}`.", .{path});
},
}
}
@ -119,7 +119,7 @@ fn migrationsRepo(action: Action, allocator: std.mem.Allocator) !MigrationsRepo
.admin = switch (action) {
.migrate, .rollback => false,
.create, .drop => true,
.schema => undefined, // We use a separate repo for schema reflection.
.dump => unreachable, // We use a separate repo for schema reflection.
},
.context = .migration,
},

View File

@ -136,10 +136,12 @@ const sql_tokens = .{
"UPDATE",
"DELETE",
"WHERE",
"SET",
"ANY",
"FROM",
"INTO",
"IN",
"ON",
"IS",
"NOT",
"NULL",
@ -155,6 +157,7 @@ const sql_tokens = .{
"MIN",
"COUNT",
"SUM",
"VALUES",
};
fn printSql(self: *const DevelopmentLogger, sql: []const u8) !void {