auto migrate

This commit is contained in:
DanceMore 2024-01-18 11:36:41 -08:00
commit 2d1bbc8d2a
3 changed files with 118 additions and 27 deletions

109
Cargo.lock generated
View file

@ -321,6 +321,17 @@ dependencies = [
"syn 2.0.43",
]
[[package]]
name = "diesel_migrations"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6036b3f0120c5961381b570ee20a02432d7e2d27ea60de9578799cf9156914ac"
dependencies = [
"diesel",
"migrations_internals",
"migrations_macros",
]
[[package]]
name = "diesel_table_macro_syntax"
version = "0.1.0"
@ -340,6 +351,19 @@ dependencies = [
"crypto-common",
]
[[package]]
name = "discord_bridgebot"
version = "0.1.0"
dependencies = [
"diesel",
"diesel_migrations",
"dotenv",
"env_logger",
"log",
"serenity",
"tokio",
]
[[package]]
name = "dotenv"
version = "0.15.0"
@ -767,6 +791,27 @@ version = "2.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
[[package]]
name = "migrations_internals"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f23f71580015254b020e856feac3df5878c2c7a8812297edd6c0a485ac9dada"
dependencies = [
"serde",
"toml",
]
[[package]]
name = "migrations_macros"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cce3325ac70e67bbab5bd837a31cae01f1a6db64e0e744a33cb03a543469ef08"
dependencies = [
"migrations_internals",
"proc-macro2",
"quote",
]
[[package]]
name = "mime"
version = "0.3.17"
@ -1068,18 +1113,6 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "rust_bridgebot"
version = "0.1.0"
dependencies = [
"diesel",
"dotenv",
"env_logger",
"log",
"serenity",
"tokio",
]
[[package]]
name = "rustc-demangle"
version = "0.1.23"
@ -1211,6 +1244,15 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_spanned"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1"
dependencies = [
"serde",
]
[[package]]
name = "serde_urlencoded"
version = "0.7.1"
@ -1524,6 +1566,40 @@ dependencies = [
"tracing",
]
[[package]]
name = "toml"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd79e69d3b627db300ff956027cc6c3798cef26d22526befdfcd12feeb6d2257"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.19.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421"
dependencies = [
"indexmap",
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
]
[[package]]
name = "tower-service"
version = "0.3.2"
@ -1999,6 +2075,15 @@ version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"
[[package]]
name = "winnow"
version = "0.5.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7cf47b659b318dccbd69cc4797a39ae128f533dce7902a1096044d1967b9c16"
dependencies = [
"memchr",
]
[[package]]
name = "winreg"
version = "0.50.0"

View file

@ -10,3 +10,4 @@ tokio = { version = "1.34.0", features = ["macros", "rt-multi-thread"] }
diesel = { version = "2.1.4", features = ["postgres"] }
log = "0.4"
env_logger = "0.10"
diesel_migrations = "2.1.0"

View file

@ -20,8 +20,17 @@ use diesel::ExpressionMethods;
use diesel::QueryDsl;
use diesel::RunQueryDsl;
use rust_bridgebot::establish_connection;
use rust_bridgebot::models::*;
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
fn run_migrations() {
let mut conn = establish_connection();
conn.run_pending_migrations(MIGRATIONS).unwrap();
}
use discord_bridgebot::establish_connection;
use discord_bridgebot::models::*;
extern crate env_logger;
#[macro_use]
@ -99,7 +108,7 @@ fn get_channel_pairs(channel_id: i64) -> Result<Vec<(i64, i64)>, diesel::result:
let connection = &mut establish_connection();
debug!("[-] db query for channel id {}", channel_id);
use rust_bridgebot::schema::channel_pairs::dsl::*;
use discord_bridgebot::schema::channel_pairs::dsl::*;
// this works
let results = channel_pairs
@ -128,17 +137,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok();
debug!("[+] config loaded!");
// we don't actually pass this around ..?
//let connection = &mut establish_connection();
//println!("{:?}", connection);
// perform migrations
run_migrations();
let framework = StandardFramework::new()
.group(&GENERAL_GROUP);
let framework = StandardFramework::new().group(&GENERAL_GROUP);
framework.configure(
Configuration::new().with_whitespace(true)
.prefix("!")
);
framework.configure(Configuration::new().with_whitespace(true).prefix("!"));
// Login with a bot token from the environment
let token = env::var("DISCORD_TOKEN").expect("token");
@ -186,7 +190,7 @@ async fn getcurrentid(ctx: &Context, msg: &Message) -> CommandResult {
async fn register(ctx: &Context, msg: &Message) -> CommandResult {
// it seems like limiting the scope of the imports is important?
// I can definitely break code with name-conflicts by putting this at the top....
use rust_bridgebot::schema::channel_pairs;
use discord_bridgebot::schema::channel_pairs;
let connection = &mut establish_connection();
// Extract the text content of the message
@ -217,7 +221,8 @@ async fn register(ctx: &Context, msg: &Message) -> CommandResult {
// Get the ID of the channel where the message was sent
let channel1 = msg.channel_id;
let channel2_o = serenity::model::id::ChannelId::from( NonZeroU64::new(channel2 as u64).unwrap() );
let channel2_o =
serenity::model::id::ChannelId::from(NonZeroU64::new(channel2 as u64).unwrap());
// make sure we can see the channel target...
match ctx.http.get_channel(channel2_o).await {
@ -260,7 +265,7 @@ async fn mirror_message(
custom_username: &str,
message_content: &str,
) {
let channel = serenity::model::id::ChannelId::from( NonZeroU64::new(channel_id as u64).unwrap() );
let channel = serenity::model::id::ChannelId::from(NonZeroU64::new(channel_id as u64).unwrap());
let message = format!("🔊 {}: {}", custom_username, message_content);