From d0d8139ff0ce8483913f0e6bb67a5478b1eb3cb5 Mon Sep 17 00:00:00 2001 From: Dull Bananas Date: Sat, 11 May 2024 20:45:27 +0000 Subject: [PATCH] migriation run command --- .woodpecker.yml | 23 ++++++++++++----------- crates/db_schema/src/lib.rs | 2 +- scripts/dump_schema.sh | 4 ++-- src/lib.rs | 30 +++++++++++++++++++++++++++++- 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/.woodpecker.yml b/.woodpecker.yml index 65dc8a8a6..f61da4ad8 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -132,17 +132,6 @@ steps: - diff config/defaults.hjson config/defaults_current.hjson when: *slow_check_paths - check_diesel_schema: - image: willsquire/diesel-cli - environment: - CARGO_HOME: .cargo_home - DATABASE_URL: postgres://lemmy:password@database:5432/lemmy - commands: - - diesel migration run - - diesel print-schema --config-file=diesel.toml > tmp.schema - - diff tmp.schema crates/db_schema/src/schema.rs - when: *slow_check_paths - check_db_perf_tool: image: *rust_image environment: @@ -173,6 +162,18 @@ steps: - mv target/debug/lemmy_server target/lemmy_server when: *slow_check_paths + check_diesel_schema: + image: *rust_image + environment: + LEMMY_DATABASE_URL: postgres://lemmy:password@database:5432/lemmy + RUST_BACKTRACE: "1" + CARGO_HOME: .cargo_home + commands: + - target/lemmy_server migration run + - diesel print-schema --config-file=diesel.toml > tmp.schema + - diff tmp.schema crates/db_schema/src/schema.rs + when: *slow_check_paths + cargo_test: image: *rust_image environment: diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index 37c44e263..df9c41bcb 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -44,7 +44,7 @@ pub mod traits; pub mod utils; #[cfg(feature = "full")] -mod schema_setup; +pub mod schema_setup; use serde::{Deserialize, Serialize}; use strum_macros::{Display, EnumString}; diff --git a/scripts/dump_schema.sh b/scripts/dump_schema.sh index f783be26b..312061dd1 100755 --- a/scripts/dump_schema.sh +++ b/scripts/dump_schema.sh @@ -9,8 +9,8 @@ cd $CWD/../ source scripts/start_dev_db.sh -diesel migration run -pg_dump --no-owner --no-privileges --no-table-access-method --schema-only --no-sync -f schema.sqldump +cargo run --package lemmy_server -- migration run +pg_dump --no-owner --no-privileges --no-table-access-method --schema-only --exclude-schema=r --no-sync -f schema.sqldump pg_ctl stop rm -rf $PGDATA diff --git a/src/lib.rs b/src/lib.rs index 61e8abd13..f3a192325 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ use actix_web::{ HttpServer, }; use actix_web_prom::PrometheusMetricsBuilder; -use clap::Parser; +use clap::{Parser, Subcommand}; use lemmy_api_common::{ context::LemmyContext, lemmy_db_views::structs::SiteView, @@ -70,6 +70,7 @@ use url::Url; about = "A link aggregator for the fediverse", long_about = "A link aggregator for the fediverse.\n\nThis is the Lemmy backend API server. This will connect to a PostgreSQL database, run any pending migrations and start accepting API requests." )] +#[command(args_conflicts_with_subcommands = true)] pub struct CmdArgs { /// Don't run scheduled tasks. /// @@ -103,6 +104,23 @@ pub struct CmdArgs { /// If set, make sure to set --federate-process-index differently for each. #[arg(long, default_value_t = 1)] federate_process_count: i32, + #[command(subcommand)] + subcommand: Option, +} + +#[derive(Subcommand, Debug)] +enum CmdSubcommand { + /// Do something with migrations, then exit. + Migration { + #[command(subcommand)] + subcommand: MigrationSubcommand, + }, +} + +#[derive(Subcommand, Debug)] +enum MigrationSubcommand { + /// Run all pending migrations. + Run, } /// Placing the main function in lib.rs allows other crates to import it and embed Lemmy @@ -110,6 +128,16 @@ pub async fn start_lemmy_server(args: CmdArgs) -> LemmyResult<()> { // Print version number to log println!("Lemmy v{VERSION}"); + if let Some(CmdSubcommand::Migration { subcommand }) = args.subcommand { + match subcommand { + MigrationSubcommand::Run => {} + } + + lemmy_db_schema::schema_setup::run(&SETTINGS.get_database_url())?; + + return Ok(()); + } + // return error 503 while running db migrations and startup tasks let mut startup_server_handle = None; if !args.disable_http_server {