-
Notifications
You must be signed in to change notification settings - Fork 3
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
6550127
commit 023090c
Showing
8 changed files
with
125 additions
and
25 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,6 +1,9 @@ | ||
RUST_LOG=trace,hyper=info,tracing=info,reqwest=info | ||
|
||
# The database that should be used | ||
# SQLite and PostgreSQL are supported: | ||
# - sqlite://./db.sqlite | ||
# - postgresql://boltz:[email protected]:5432/covclaim | ||
DATABASE_URL=sqlite://./db.sqlite | ||
|
||
# When finding a lockup transaction, how many seconds to wait before broadcasting the covenant claim (0 for instantly) | ||
|
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DROP TABLE parameters; | ||
DROP TABLE pending_covenants; |
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,19 @@ | ||
CREATE TABLE parameters ( | ||
name VARCHAR PRIMARY KEY NOT NULL, | ||
value VARCHAR NOT NULL | ||
); | ||
|
||
CREATE TABLE pending_covenants ( | ||
output_script BYTEA PRIMARY KEY NOT NULL, | ||
status INT NOT NULL, | ||
internal_key BYTEA NOT NULL, | ||
preimage BYTEA NOT NULL, | ||
swap_tree VARCHAR NOT NULL, | ||
address BYTEA NOT NULL, | ||
blinding_key BYTEA, | ||
tx_id BYTEA, | ||
tx_time TIMESTAMP, | ||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
); | ||
|
||
CREATE INDEX pending_covenants_status_idx ON pending_covenants (status); |
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 |
---|---|---|
@@ -1,31 +1,58 @@ | ||
use std::error::Error; | ||
|
||
use diesel::prelude::*; | ||
use diesel::r2d2::ConnectionManager; | ||
use diesel::sqlite::Sqlite; | ||
use diesel::{Connection, SqliteConnection}; | ||
use diesel::Connection; | ||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; | ||
use std::error::Error; | ||
use log::info; | ||
|
||
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations"); | ||
pub const MIGRATIONS_POSTGRES: EmbeddedMigrations = embed_migrations!("./migrations_postgres"); | ||
|
||
pub mod helpers; | ||
|
||
pub mod models; | ||
mod schema; | ||
|
||
pub type DatabaseConnection = SqliteConnection; | ||
pub type Pool = r2d2::Pool<ConnectionManager<DatabaseConnection>>; | ||
#[derive(diesel::MultiConnection)] | ||
pub enum AnyConnection { | ||
Postgresql(PgConnection), | ||
Sqlite(SqliteConnection), | ||
} | ||
|
||
pub fn establish_connection(url: &str) -> Result<Pool, Box<dyn Error + Send + Sync>> { | ||
run_migrations(&mut SqliteConnection::establish(url)?)?; | ||
pub type Pool = r2d2::Pool<ConnectionManager<AnyConnection>>; | ||
|
||
let manager = ConnectionManager::<DatabaseConnection>::new(url); | ||
pub fn establish_connection(url: &str) -> Result<Pool, Box<dyn Error + Send + Sync>> { | ||
info!( | ||
"Using {} database", | ||
if is_postgres_connection_url(url) { | ||
"PostgreSQL" | ||
} else { | ||
"SQLite" | ||
} | ||
); | ||
|
||
let manager = ConnectionManager::new(url); | ||
let pool = Pool::builder().build(manager)?; | ||
|
||
run_migrations(is_postgres_connection_url(url), &pool)?; | ||
|
||
Ok(pool) | ||
} | ||
|
||
fn run_migrations( | ||
connection: &mut impl MigrationHarness<Sqlite>, | ||
is_postgres: bool, | ||
pool: &Pool, | ||
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> { | ||
connection.run_pending_migrations(MIGRATIONS)?; | ||
let mut con = pool.get()?; | ||
con.run_pending_migrations(if is_postgres { | ||
MIGRATIONS_POSTGRES | ||
} else { | ||
MIGRATIONS | ||
})?; | ||
Ok(()) | ||
} | ||
|
||
fn is_postgres_connection_url(url: &str) -> bool { | ||
url.starts_with("postgresql") | ||
} |
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