start on db migrations
This commit is contained in:
parent
b8140d49d5
commit
a4ffe5d155
3 changed files with 13 additions and 6 deletions
0
migrations/001/init.up.sql
Normal file
0
migrations/001/init.up.sql
Normal file
15
src/db.rs
15
src/db.rs
|
|
@ -1,7 +1,7 @@
|
||||||
use std::{path::PathBuf, sync::LazyLock};
|
use std::{path::PathBuf, sync::LazyLock};
|
||||||
|
|
||||||
use include_dir::{include_dir, Dir};
|
use include_dir::{include_dir, Dir};
|
||||||
use rusqlite::{params, Connection};
|
use rusqlite::{config::DbConfig, params, Connection};
|
||||||
use rusqlite_migration::Migrations;
|
use rusqlite_migration::Migrations;
|
||||||
|
|
||||||
static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations");
|
static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations");
|
||||||
|
|
@ -10,15 +10,20 @@ static MIGRATIONS: LazyLock<Migrations<'static>> =
|
||||||
LazyLock::new(|| Migrations::from_directory(&MIGRATIONS_DIR).unwrap());
|
LazyLock::new(|| Migrations::from_directory(&MIGRATIONS_DIR).unwrap());
|
||||||
|
|
||||||
static ASSETS_DB: LazyLock<PathBuf> = LazyLock::new(|| {
|
static ASSETS_DB: LazyLock<PathBuf> = LazyLock::new(|| {
|
||||||
dirs::data_dir()
|
let d = dirs::data_dir().unwrap_or(".".into()).join("autobarts");
|
||||||
.unwrap_or(".".into())
|
std::fs::create_dir_all(&d)
|
||||||
.join("autobarts")
|
.unwrap_or_else(|e| panic!("could not create autobarts data dir {d:?}: {e}"));
|
||||||
.join("assets.db")
|
d.join("assets.db")
|
||||||
});
|
});
|
||||||
|
|
||||||
pub fn init_db() -> Result<Connection, String> {
|
pub fn init_db() -> Result<Connection, String> {
|
||||||
let mut conn = Connection::open(&*ASSETS_DB).map_err(|e| format!("oh shit: {e}"))?;
|
let mut conn = Connection::open(&*ASSETS_DB).map_err(|e| format!("oh shit: {e}"))?;
|
||||||
|
|
||||||
|
conn.set_db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_FKEY, true)
|
||||||
|
.map_err(|e| format!("oh shit: {e}"))?;
|
||||||
|
|
||||||
|
let _ = conn.pragma_update(None, "journal_mode", "WAL");
|
||||||
|
|
||||||
MIGRATIONS
|
MIGRATIONS
|
||||||
.to_latest(&mut conn)
|
.to_latest(&mut conn)
|
||||||
.map_err(|e| format!("oh shit: {e}"))?;
|
.map_err(|e| format!("oh shit: {e}"))?;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
//! Renders a 2D scene containing a single, moving sprite.
|
//! Renders a 2D scene containing a single, moving sprite.
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
use db::init_db;
|
||||||
|
|
||||||
mod db;
|
mod db;
|
||||||
|
|
||||||
|
|
@ -18,6 +19,7 @@ enum Direction {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
|
let conn = init_db().unwrap();
|
||||||
commands.spawn(Camera2d);
|
commands.spawn(Camera2d);
|
||||||
|
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
|
|
@ -30,7 +32,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
image_mode: SpriteImageMode::Tiled {
|
image_mode: SpriteImageMode::Tiled {
|
||||||
tile_x: true,
|
tile_x: true,
|
||||||
tile_y: true,
|
tile_y: true,
|
||||||
stretch_value: 0.5, // The image will tile every 128px
|
stretch_value: 1.0, // The image will tile every 128px
|
||||||
},
|
},
|
||||||
custom_size: Some(Vec2::splat(2048.0)),
|
custom_size: Some(Vec2::splat(2048.0)),
|
||||||
..default()
|
..default()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue