-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a84c634
commit 1122e95
Showing
17 changed files
with
221 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,9 @@ just run | |
``` | ||
just release | ||
``` | ||
|
||
4. Reset local DB (for init, schema generation, etc.) | ||
|
||
``` | ||
just reset-db | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# For documentation on how to configure this file, | ||
# see https://diesel.rs/guides/configuring-diesel-cli | ||
|
||
[print_schema] | ||
file = "src/db_models/schema.rs" | ||
custom_type_derives = ["diesel::query_builder::QueryId"] | ||
|
||
[migrations_directory] | ||
dir = "migrations" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE profile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
CREATE TABLE profile ( | ||
id CHAR(36) PRIMARY KEY NOT NULL, | ||
seed_words CHAR(255) NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use crate::db_models::{NewProfile, Profile}; | ||
use diesel::{ | ||
connection::SimpleConnection, | ||
r2d2::{ConnectionManager, Pool}, | ||
SqliteConnection, | ||
}; | ||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; | ||
use std::{sync::Arc, time::Duration}; | ||
|
||
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!(); | ||
|
||
pub(crate) fn setup_db(url: &str, password: String) -> Arc<dyn DBConnection + Send + Sync> { | ||
let manager = ConnectionManager::<SqliteConnection>::new(url); | ||
let pool = Pool::builder() | ||
.max_size(50) | ||
.connection_customizer(Box::new(ConnectionOptions { | ||
key: password, | ||
enable_wal: true, | ||
enable_foreign_keys: true, | ||
busy_timeout: Some(Duration::from_secs(15)), | ||
})) | ||
.test_on_check_out(true) | ||
.build(manager) | ||
.expect("Unable to build DB connection pool"); | ||
Arc::new(SQLConnection { db: pool }) | ||
} | ||
|
||
pub trait DBConnection { | ||
// Gets a seed from the first profile in the DB or returns None | ||
fn get_seed(&self) -> anyhow::Result<Option<String>>; | ||
|
||
// Inserts a new profile into the DB | ||
fn insert_new_profile(&self, new_profile: NewProfile) -> anyhow::Result<Profile>; | ||
} | ||
|
||
pub(crate) struct SQLConnection { | ||
db: Pool<ConnectionManager<SqliteConnection>>, | ||
} | ||
|
||
impl DBConnection for SQLConnection { | ||
fn get_seed(&self) -> anyhow::Result<Option<String>> { | ||
let conn = &mut self.db.get()?; | ||
match Profile::get_first(conn)? { | ||
Some(p) => Ok(Some(p.seed_words)), | ||
None => Ok(None), | ||
} | ||
} | ||
|
||
fn insert_new_profile(&self, new_profile: NewProfile) -> anyhow::Result<Profile> { | ||
let conn = &mut self.db.get()?; | ||
new_profile.insert(conn) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct ConnectionOptions { | ||
pub key: String, | ||
pub enable_wal: bool, | ||
pub enable_foreign_keys: bool, | ||
pub busy_timeout: Option<Duration>, | ||
} | ||
|
||
impl diesel::r2d2::CustomizeConnection<SqliteConnection, diesel::r2d2::Error> | ||
for ConnectionOptions | ||
{ | ||
fn on_acquire(&self, conn: &mut SqliteConnection) -> Result<(), diesel::r2d2::Error> { | ||
(|| { | ||
// FIXME: Special characters might fuck up | ||
conn.batch_execute(&format!("PRAGMA key={}", self.key))?; | ||
if self.enable_wal { | ||
conn.batch_execute("PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL;")?; | ||
} | ||
if self.enable_foreign_keys { | ||
conn.batch_execute("PRAGMA foreign_keys = ON;")?; | ||
} | ||
if let Some(d) = self.busy_timeout { | ||
conn.batch_execute(&format!("PRAGMA busy_timeout = {};", d.as_millis()))?; | ||
} | ||
|
||
conn.run_pending_migrations(MIGRATIONS) | ||
.expect("Migration has to run successfully"); | ||
|
||
Ok(()) | ||
})() | ||
.map_err(diesel::r2d2::Error::QueryError) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod profile; | ||
pub use profile::*; | ||
|
||
pub mod schema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use crate::db_models::schema::profile; | ||
use diesel::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive( | ||
QueryableByName, Queryable, AsChangeset, Serialize, Deserialize, Debug, Clone, PartialEq, | ||
)] | ||
#[diesel(table_name = profile)] | ||
pub struct Profile { | ||
pub id: String, | ||
pub seed_words: String, | ||
} | ||
|
||
impl Profile { | ||
pub fn get_first(conn: &mut SqliteConnection) -> anyhow::Result<Option<Profile>> { | ||
Ok(profile::table.first::<Profile>(conn).optional()?) | ||
} | ||
} | ||
|
||
#[derive(Insertable)] | ||
#[diesel(table_name = profile)] | ||
pub struct NewProfile { | ||
pub id: String, | ||
pub seed_words: String, | ||
} | ||
|
||
impl From<&NewProfile> for Profile { | ||
fn from(new_profile: &NewProfile) -> Self { | ||
Profile { | ||
id: new_profile.id.clone(), | ||
seed_words: new_profile.seed_words.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl NewProfile { | ||
pub fn insert(&self, conn: &mut SqliteConnection) -> anyhow::Result<Profile> { | ||
let _ = diesel::insert_into(profile::table) | ||
.values(self) | ||
.execute(conn)?; | ||
|
||
Ok(self.into()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// @generated automatically by Diesel CLI. | ||
|
||
diesel::table! { | ||
profile (id) { | ||
id -> Text, | ||
seed_words -> Text, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters