fix: make sure tables are initialized

This commit is contained in:
mokurin000
2025-08-03 19:30:03 +08:00
parent a1b3a8ef0e
commit ca81c6495a
6 changed files with 57 additions and 34 deletions

View File

@@ -11,11 +11,6 @@ static DATABASE: LazyLock<redb::Database> = LazyLock::new(|| {
db
});
pub const PLAYERS: TableDefinition<'_, u32, Vec<u8>> = redb::TableDefinition::new("players");
pub const PLAYER_B50: TableDefinition<'_, u32, Vec<u8>> = redb::TableDefinition::new("b50");
pub const PLAYER_RECORD: TableDefinition<'_, u32, Vec<u8>> = redb::TableDefinition::new("records");
pub const PLAYER_REGION: TableDefinition<'_, u32, Vec<u8>> = redb::TableDefinition::new("regions");
pub fn write_txn() -> Result<WriteTransaction, redb::Error> {
Ok(DATABASE.begin_write()?)
}
@@ -38,11 +33,33 @@ pub fn open_table_ro(
Ok(read.open_table(definition)?)
}
pub fn init_db() -> Result<(), redb::Error> {
let write_txn = DATABASE.begin_write()?;
write_txn.open_table(PLAYERS)?;
write_txn.open_table(PLAYER_B50)?;
write_txn.open_table(PLAYER_RECORD)?;
write_txn.commit()?;
Ok(())
#[crabtime::function]
fn table_definitions_impl(tables: Vec<String>) {
let mut defs: Vec<String> = Vec::new();
for table in tables {
let definition = table.to_uppercase();
let table_name = format!("\"{table}\"");
crabtime::output!(
pub const {{definition}}: TableDefinition<'_, u32, Vec<u8>> = redb::TableDefinition::new({{table_name}});
);
defs.push(format!("write_txn.open_table({definition})?;"));
}
let init_statements = defs.join("\n");
crabtime::output!(
pub fn init_db() -> Result<(), redb::Error> {
let write_txn = DATABASE.begin_write()?;
{
{ init_statements }
}
write_txn.commit()?;
Ok(())
}
);
}
table_definitions_impl!(["players", "b50", "records", "regions"]);