wip: Migration works now, still writing mirgations

This commit is contained in:
2026-03-26 00:13:43 +00:00
parent ba4a702574
commit 27083de136
7 changed files with 57 additions and 3 deletions
+1
View File
@@ -1,3 +1,4 @@
/target /target
/.direnv /.direnv
/.env /.env
/db.sqlite
+1 -1
View File
@@ -25,7 +25,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Login with a bot token from the environment // Login with a bot token from the environment
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment"); let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let dbc: DatabaseConnection = Database::connect("sqlite::memory:").await?; let dbc: DatabaseConnection = Database::connect("sqlite://./db.sqlite?mode=rwc").await?;
Migrator::up(&dbc, None).await?; Migrator::up(&dbc, None).await?;
// Set gateway intents, which decides what events the bot will be notified about // Set gateway intents, which decides what events the bot will be notified about
+2
View File
@@ -3,6 +3,7 @@ pub use sea_orm_migration::prelude::*;
mod m20260325_145211_servers; mod m20260325_145211_servers;
mod m20260325_145717_members; mod m20260325_145717_members;
mod m20260325_162920_messages; mod m20260325_162920_messages;
mod m20260325_233947_content;
pub struct Migrator; pub struct Migrator;
@@ -13,6 +14,7 @@ impl MigratorTrait for Migrator {
Box::new(m20260325_145211_servers::Migration), Box::new(m20260325_145211_servers::Migration),
Box::new(m20260325_145717_members::Migration), Box::new(m20260325_145717_members::Migration),
Box::new(m20260325_162920_messages::Migration), Box::new(m20260325_162920_messages::Migration),
Box::new(m20260325_233947_content::Migration),
] ]
} }
} }
@@ -1,5 +1,7 @@
use sea_orm_migration::{prelude::*, schema::*}; use sea_orm_migration::{prelude::*, schema::*};
use crate::m20260325_233947_content::Content;
#[derive(DeriveMigrationName)] #[derive(DeriveMigrationName)]
pub struct Migration; pub struct Migration;
@@ -14,7 +16,12 @@ impl MigrationTrait for Migration {
.col(pk_auto(Messages::Id)) .col(pk_auto(Messages::Id))
.col(integer(Messages::IdDiscord)) .col(integer(Messages::IdDiscord))
.col(integer(Messages::IdSender)) .col(integer(Messages::IdSender))
.col(integer(Messages::IdContent)) // Change to foreign key .col(integer(Messages::IdContent))
.foreign_key(
ForeignKey::create()
.from(Messages::Table, Messages::IdContent)
.to(Content::Table, Content::Id),
)
.col(integer(Messages::IdServer)) .col(integer(Messages::IdServer))
.to_owned(), .to_owned(),
) )
@@ -0,0 +1,35 @@
use sea_orm_migration::{prelude::*, schema::*};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Content::Table)
.if_not_exists()
.col(pk_auto(Content::Id))
.col(string(Content::Content))
.col(string(Content::Type))
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Content::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
pub enum Content {
Table,
Id,
Content,
Type,
}
+9 -1
View File
@@ -1,6 +1,14 @@
use migration::sea_orm::{ConnectOptions, Database, DatabaseConnection};
use sea_orm_migration::prelude::*; use sea_orm_migration::prelude::*;
#[async_std::main] #[async_std::main]
async fn main() { async fn main() {
cli::run_cli(migration::Migrator).await; let db = Database::connect("sqlite://./db.sqlite?mode=rwc")
.await
.unwrap();
db.execute_unprepared("PRAGMA foreign_keys = ON;")
.await
.unwrap();
let db_fn = async move |_| Ok((db));
cli::run_cli_with_connection(migration::Migrator, db_fn).await;
} }
+1
View File
@@ -189,6 +189,7 @@
checks = self.checks.${system}; checks = self.checks.${system};
DATABASE_URL = "sqlite://./db.sqlite?mode=rwc"; DATABASE_URL = "sqlite://./db.sqlite?mode=rwc";
MIGRATION_DIR = "./crates/migration";
packages = [ packages = [
pkgs.cargo-hakari pkgs.cargo-hakari
pkgs.sea-orm-cli pkgs.sea-orm-cli