wip: Migration works now, still writing mirgations
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
/target
|
||||
/.direnv
|
||||
/.env
|
||||
/db.sqlite
|
||||
|
||||
@@ -25,7 +25,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
// Login with a bot token from 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?;
|
||||
|
||||
// Set gateway intents, which decides what events the bot will be notified about
|
||||
|
||||
@@ -3,6 +3,7 @@ pub use sea_orm_migration::prelude::*;
|
||||
mod m20260325_145211_servers;
|
||||
mod m20260325_145717_members;
|
||||
mod m20260325_162920_messages;
|
||||
mod m20260325_233947_content;
|
||||
|
||||
pub struct Migrator;
|
||||
|
||||
@@ -13,6 +14,7 @@ impl MigratorTrait for Migrator {
|
||||
Box::new(m20260325_145211_servers::Migration),
|
||||
Box::new(m20260325_145717_members::Migration),
|
||||
Box::new(m20260325_162920_messages::Migration),
|
||||
Box::new(m20260325_233947_content::Migration),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use sea_orm_migration::{prelude::*, schema::*};
|
||||
|
||||
use crate::m20260325_233947_content::Content;
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
pub struct Migration;
|
||||
|
||||
@@ -14,7 +16,12 @@ impl MigrationTrait for Migration {
|
||||
.col(pk_auto(Messages::Id))
|
||||
.col(integer(Messages::IdDiscord))
|
||||
.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))
|
||||
.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,
|
||||
}
|
||||
@@ -1,6 +1,14 @@
|
||||
use migration::sea_orm::{ConnectOptions, Database, DatabaseConnection};
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[async_std::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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user