mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-25 12:34:19 +00:00
Embed database migrations in the binary
This commit is contained in:
parent
19ec4cd947
commit
0544304e8c
6 changed files with 187 additions and 3 deletions
85
Cargo.lock
generated
85
Cargo.lock
generated
|
@ -305,6 +305,17 @@ dependencies = [
|
||||||
"syn 2.0.71",
|
"syn 2.0.71",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "diesel_migrations"
|
||||||
|
version = "2.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8a73ce704bad4231f001bff3314d91dce4aba0770cee8b233991859abc15c1f6"
|
||||||
|
dependencies = [
|
||||||
|
"diesel",
|
||||||
|
"migrations_internals",
|
||||||
|
"migrations_macros",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "diesel_table_macro_syntax"
|
name = "diesel_table_macro_syntax"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
|
@ -977,6 +988,27 @@ dependencies = [
|
||||||
"syn 2.0.71",
|
"syn 2.0.71",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "migrations_internals"
|
||||||
|
version = "2.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fd01039851e82f8799046eabbb354056283fb265c8ec0996af940f4e85a380ff"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
"toml",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "migrations_macros"
|
||||||
|
version = "2.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ffb161cc72176cb37aa47f1fc520d3ef02263d67d661f44f05d05a079e1237fd"
|
||||||
|
dependencies = [
|
||||||
|
"migrations_internals",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mime"
|
name = "mime"
|
||||||
version = "0.3.17"
|
version = "0.3.17"
|
||||||
|
@ -1428,6 +1460,7 @@ dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"chrono",
|
"chrono",
|
||||||
"diesel",
|
"diesel",
|
||||||
|
"diesel_migrations",
|
||||||
"graphql_client",
|
"graphql_client",
|
||||||
"log",
|
"log",
|
||||||
"micronfig",
|
"micronfig",
|
||||||
|
@ -1601,6 +1634,15 @@ dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_spanned"
|
||||||
|
version = "0.6.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "eb5b1b31579f3811bf615c144393417496f152e12ac8b7663bf664f4a815306d"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_urlencoded"
|
name = "serde_urlencoded"
|
||||||
version = "0.7.1"
|
version = "0.7.1"
|
||||||
|
@ -1939,6 +1981,40 @@ dependencies = [
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "toml"
|
||||||
|
version = "0.8.19"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a1ed1f98e3fdc28d6d910e6737ae6ab1a93bf1985935a1193e68f93eeb68d24e"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
"serde_spanned",
|
||||||
|
"toml_datetime",
|
||||||
|
"toml_edit",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "toml_datetime"
|
||||||
|
version = "0.6.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "toml_edit"
|
||||||
|
version = "0.22.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "583c44c02ad26b0c3f3066fe629275e50627026c51ac2e595cca4c230ce1ce1d"
|
||||||
|
dependencies = [
|
||||||
|
"indexmap",
|
||||||
|
"serde",
|
||||||
|
"serde_spanned",
|
||||||
|
"toml_datetime",
|
||||||
|
"winnow",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tower"
|
name = "tower"
|
||||||
version = "0.4.13"
|
version = "0.4.13"
|
||||||
|
@ -2336,6 +2412,15 @@ version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winnow"
|
||||||
|
version = "0.6.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "68a9bda4691f099d435ad181000724da8e5899daa10713c2d432552b9ccd3a6f"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winreg"
|
name = "winreg"
|
||||||
version = "0.50.0"
|
version = "0.50.0"
|
||||||
|
|
|
@ -69,6 +69,10 @@ version = "2.2.1"
|
||||||
features = ["postgres"]
|
features = ["postgres"]
|
||||||
optional = true
|
optional = true
|
||||||
|
|
||||||
|
[dependencies.diesel_migrations]
|
||||||
|
version = "2.2.0"
|
||||||
|
optional = true
|
||||||
|
|
||||||
[dependencies.teloxide]
|
[dependencies.teloxide]
|
||||||
version = "0.12.2"
|
version = "0.12.2"
|
||||||
default-features = false
|
default-features = false
|
||||||
|
@ -102,7 +106,8 @@ default = [
|
||||||
"service_telegram",
|
"service_telegram",
|
||||||
]
|
]
|
||||||
interface_database = [
|
interface_database = [
|
||||||
"diesel"
|
"diesel",
|
||||||
|
"diesel_migrations",
|
||||||
]
|
]
|
||||||
interface_stratz = [
|
interface_stratz = [
|
||||||
"graphql_client"
|
"graphql_client"
|
||||||
|
|
47
src/database/schema.rs
Normal file
47
src/database/schema.rs
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
// @generated automatically by Diesel CLI.
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
brooch_match (id) {
|
||||||
|
id -> Int8,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
discord (discord_id) {
|
||||||
|
user_id -> Int4,
|
||||||
|
discord_id -> Int8,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
steam (steam_id) {
|
||||||
|
user_id -> Int4,
|
||||||
|
steam_id -> Int8,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
telegram (telegram_id) {
|
||||||
|
user_id -> Int4,
|
||||||
|
telegram_id -> Int8,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
users (id) {
|
||||||
|
id -> Int4,
|
||||||
|
username -> Varchar,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
diesel::joinable!(discord -> users (user_id));
|
||||||
|
diesel::joinable!(steam -> users (user_id));
|
||||||
|
diesel::joinable!(telegram -> users (user_id));
|
||||||
|
|
||||||
|
diesel::allow_tables_to_appear_in_same_query!(
|
||||||
|
brooch_match,
|
||||||
|
discord,
|
||||||
|
steam,
|
||||||
|
telegram,
|
||||||
|
users,
|
||||||
|
);
|
|
@ -1,6 +1,17 @@
|
||||||
#![allow(unused_attributes, unused_qualifications, clippy::needless_pub_self)]
|
#![allow(unused_attributes, unused_qualifications, clippy::needless_pub_self)]
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(feature = "interface_database")]
|
||||||
|
pub mod interface_database {
|
||||||
|
use micronfig::config;
|
||||||
|
|
||||||
|
config! {
|
||||||
|
DATABASE_AUTOMIGRATE: String > bool,
|
||||||
|
DATABASE_URL: String,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(feature = "service_telegram")]
|
#[cfg(feature = "service_telegram")]
|
||||||
pub mod service_telegram {
|
pub mod service_telegram {
|
||||||
use micronfig::config;
|
use micronfig::config;
|
||||||
|
|
|
@ -23,6 +23,8 @@ impl RoyalnetInstance {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run(mut self) {
|
pub async fn run(mut self) {
|
||||||
|
Self::run_pending_migrations();
|
||||||
|
|
||||||
let future_telegram = async move {
|
let future_telegram = async move {
|
||||||
Self::get_telegram_future(&mut self.service_telegram).await;
|
Self::get_telegram_future(&mut self.service_telegram).await;
|
||||||
};
|
};
|
||||||
|
@ -39,6 +41,29 @@ impl RoyalnetInstance {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "interface_database")]
|
||||||
|
fn run_pending_migrations() {
|
||||||
|
if !config::interface_database::DATABASE_AUTOMIGRATE() {
|
||||||
|
log::warn!("Database automigration is disabled.");
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log::debug!("Automatically applying database migrations...");
|
||||||
|
|
||||||
|
let mut db = crate::interfaces::database::connect(
|
||||||
|
config::interface_database::DATABASE_URL()
|
||||||
|
).expect("Unable to connect to the database to apply migrations.");
|
||||||
|
|
||||||
|
crate::interfaces::database::migrate(&mut db)
|
||||||
|
.expect("Failed to automatically apply migrations to the database.");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "interface_database"))]
|
||||||
|
fn run_pending_migrations() {
|
||||||
|
log::warn!("Database automigration is not compiled in.");
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "service_telegram")]
|
#[cfg(feature = "service_telegram")]
|
||||||
async fn setup_telegram_service() -> crate::services::telegram::TelegramService {
|
async fn setup_telegram_service() -> crate::services::telegram::TelegramService {
|
||||||
log::debug!("Setting up Telegram service...");
|
log::debug!("Setting up Telegram service...");
|
||||||
|
@ -52,7 +77,7 @@ impl RoyalnetInstance {
|
||||||
|
|
||||||
#[cfg(not(feature = "service_telegram"))]
|
#[cfg(not(feature = "service_telegram"))]
|
||||||
async fn setup_telegram_service() -> () {
|
async fn setup_telegram_service() -> () {
|
||||||
log::warn!("Telegram service is disabled.");
|
log::warn!("Telegram service is not compiled in.");
|
||||||
|
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
|
@ -85,7 +110,7 @@ impl RoyalnetInstance {
|
||||||
|
|
||||||
#[cfg(not(feature = "service_brooch"))]
|
#[cfg(not(feature = "service_brooch"))]
|
||||||
fn setup_brooch_service() -> () {
|
fn setup_brooch_service() -> () {
|
||||||
log::warn!("Brooch service is disabled.");
|
log::warn!("Brooch service is not compiled in.");
|
||||||
|
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
|
use anyhow::anyhow;
|
||||||
use diesel::{Connection, ConnectionResult, PgConnection};
|
use diesel::{Connection, ConnectionResult, PgConnection};
|
||||||
|
use diesel::migration::MigrationVersion;
|
||||||
|
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
||||||
|
use crate::utils::result::AnyResult;
|
||||||
|
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
pub mod models;
|
pub mod models;
|
||||||
|
@ -6,3 +10,10 @@ pub mod models;
|
||||||
pub fn connect(database_url: &str) -> ConnectionResult<PgConnection> {
|
pub fn connect(database_url: &str) -> ConnectionResult<PgConnection> {
|
||||||
PgConnection::establish(database_url)
|
PgConnection::establish(database_url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
|
||||||
|
|
||||||
|
pub fn migrate(database: &mut PgConnection) -> AnyResult<Vec<MigrationVersion>> {
|
||||||
|
database.run_pending_migrations(MIGRATIONS)
|
||||||
|
.map_err(|e| anyhow!("Failed to run pending migrations: {e:?}"))
|
||||||
|
}
|
Loading…
Reference in a new issue