Added SQL migrations and bootstrapping
ci/woodpecker/push/ociImagePush Pipeline was successful Details

This commit is contained in:
Natty 2023-07-29 05:20:00 +02:00
parent c16593b8c1
commit fd26fdef93
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
13 changed files with 3616 additions and 3 deletions

113
Cargo.lock generated
View File

@ -383,6 +383,42 @@ dependencies = [
"serde",
]
[[package]]
name = "clap"
version = "3.2.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123"
dependencies = [
"bitflags",
"clap_derive",
"clap_lex",
"indexmap",
"once_cell",
"textwrap",
]
[[package]]
name = "clap_derive"
version = "3.2.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae6371b8bdc8b7d3959e9cf7b22d4435ef3e79e138688421ec654acf8c81b008"
dependencies = [
"heck 0.4.1",
"proc-macro-error",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "clap_lex"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.4"
@ -482,6 +518,14 @@ version = "2.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0"
[[package]]
name = "ext_calckey_model_migration"
version = "0.2.0"
dependencies = [
"sea-orm-migration",
"tokio",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -996,6 +1040,7 @@ dependencies = [
"chrono",
"ck",
"dotenvy",
"ext_calckey_model_migration",
"magnetar_common",
"sea-orm",
"serde",
@ -1194,6 +1239,12 @@ version = "1.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
[[package]]
name = "os_str_bytes"
version = "6.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d5d9eb14b174ee9aa2ef96dc2b94637a2d4b6e7cb873c7e171f0c20c6cf3eac"
[[package]]
name = "ouroboros"
version = "0.15.6"
@ -1728,6 +1779,22 @@ dependencies = [
"uuid",
]
[[package]]
name = "sea-orm-cli"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efbf34a2caf70c2e3be9bb1e674e9540f6dfd7c8f40f6f05daf3b9740e476005"
dependencies = [
"chrono",
"clap",
"dotenvy",
"regex",
"sea-schema",
"tracing",
"tracing-subscriber",
"url",
]
[[package]]
name = "sea-orm-macros"
version = "0.11.3"
@ -1741,6 +1808,23 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "sea-orm-migration"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "278d3adfd0832b6ffc17d3cfbc574d3695a5c1b38814e0bc8ac238d33f3d87cf"
dependencies = [
"async-trait",
"clap",
"dotenvy",
"futures",
"sea-orm",
"sea-orm-cli",
"sea-schema",
"tracing",
"tracing-subscriber",
]
[[package]]
name = "sea-query"
version = "0.28.5"
@ -1785,6 +1869,29 @@ dependencies = [
"thiserror",
]
[[package]]
name = "sea-schema"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eeb2940bb5a10bc6cd05b450ce6cd3993e27fddd7eface2becb97fc5af3a040e"
dependencies = [
"futures",
"sea-query",
"sea-schema-derive",
]
[[package]]
name = "sea-schema-derive"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56821b7076f5096b8f726e2791ad255a99c82498e08ec477a65a96c461ff1927"
dependencies = [
"heck 0.3.3",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "sea-strum"
version = "0.23.0"
@ -2147,6 +2254,12 @@ dependencies = [
"unic-segment",
]
[[package]]
name = "textwrap"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d"
[[package]]
name = "thiserror"
version = "1.0.40"

View File

@ -29,6 +29,7 @@ log = "0.4"
miette = "5.9"
percent-encoding = "2.2"
sea-orm = "0.11"
sea-orm-migration = "0.11"
serde = "1"
serde_json = "1"
tera = { version = "1", default-features = false }

View File

@ -4,9 +4,6 @@
A social networking server anyone can self-host
**WARNING: Currently the project cannot be bootstrapped from scratch as
it requires a Calckey 14.0RC3 database model.**
**This project is in early development.**
## Quick start

View File

@ -8,6 +8,8 @@ crate-type = ["rlib"]
[dependencies]
ck = { path = "./entity_ck" }
ext_calckey_model_migration = { path = "./migration" }
magnetar_common = { path = "../magnetar_common" }
dotenvy = { workspace = true}

View File

@ -0,0 +1,16 @@
[package]
name = "ext_calckey_model_migration"
version.workspace = true
edition.workspace = true
[[bin]]
name = "migrate"
path = "src/main.rs"
[lib]
name = "ext_calckey_model_migration"
path = "src/lib.rs"
[dependencies]
tokio = { workspace = true, features = ["full"] }
sea-orm-migration = { workspace = true, features = ["runtime-tokio-rustls", "sqlx-postgres"] }

View File

@ -0,0 +1,41 @@
# Running Migrator CLI
- Generate a new migration file
```sh
cargo run -- migrate generate MIGRATION_NAME
```
- Apply all pending migrations
```sh
cargo run
```
```sh
cargo run -- up
```
- Apply first 10 pending migrations
```sh
cargo run -- up -n 10
```
- Rollback last applied migrations
```sh
cargo run -- down
```
- Rollback last 10 applied migrations
```sh
cargo run -- down -n 10
```
- Drop all tables from the database, then reapply all migrations
```sh
cargo run -- fresh
```
- Rollback all applied migrations, then reapply all migrations
```sh
cargo run -- refresh
```
- Rollback all applied migrations
```sh
cargo run -- reset
```
- Check the status of all migrations
```sh
cargo run -- status
```

View File

@ -0,0 +1,223 @@
alter table drive_folder
drop constraint "FK_f4fc06e49c0171c85f1c48060d2";
alter table drive_file
drop constraint "FK_860fa6f6c7df5bb887249fba22e";
drop table poll_vote;
drop table note_reaction;
drop table note_watching;
drop table note_unread;
drop table notification;
drop type notification_type_enum;
drop table access_token;
drop table meta;
drop type meta_sensitivemediadetection_enum;
drop type meta_sensitivemediadetectionsensitivity_enum;
drop table following;
drop table instance;
drop table muting;
drop table sw_subscription;
drop table blocking;
drop table user_list_joining;
drop table hashtag;
drop table note_favorite;
drop table abuse_user_report;
drop table registration_ticket;
drop table messaging_message;
drop table signin;
drop table auth_session;
drop table app;
drop table follow_request;
drop table emoji;
drop table user_note_pining;
drop table poll;
drop type poll_notevisibility_enum;
drop table user_keypair;
drop table user_publickey;
drop table user_profile;
drop type user_profile_ffvisibility_enum;
drop table __chart__active_users;
drop table __chart__drive;
drop table __chart__federation;
drop table __chart__hashtag;
drop table __chart__instance;
drop table __chart__network;
drop table __chart__notes;
drop table __chart__per_user_drive;
drop table __chart__per_user_following;
drop table __chart__per_user_notes;
drop table __chart__per_user_reaction;
drop table __chart__test_grouped;
drop table __chart__test_unique;
drop table __chart__test;
drop table __chart__users;
drop table page_like;
drop table page;
drop type page_visibility_enum;
drop table user_group_invite;
drop table attestation_challenge;
drop table user_security_key;
drop table moderation_log;
drop table used_username;
drop table announcement_read;
drop table announcement;
drop table clip_note;
drop table clip;
drop table antenna_note;
drop table antenna;
drop type antenna_src_enum;
drop table user_list;
drop table user_group_joining;
drop table user_group_invitation;
drop table user_group;
drop table promo_note;
drop table promo_read;
drop table relay;
drop type relay_status_enum;
drop table muted_note;
drop type muted_note_reason_enum;
drop table channel_following;
drop table channel_note_pining;
drop table registry_item;
drop table gallery_like;
drop table gallery_post;
drop table password_reset_request;
drop table ad;
drop table user_pending;
drop table note_thread_muting;
drop table __chart_day__federation;
drop table __chart_day__notes;
drop table __chart_day__users;
drop table __chart_day__network;
drop table __chart_day__active_users;
drop table __chart_day__instance;
drop table __chart_day__per_user_notes;
drop table __chart_day__drive;
drop table __chart_day__per_user_reaction;
drop table __chart_day__hashtag;
drop table __chart_day__per_user_following;
drop table __chart_day__per_user_drive;
drop table __chart__ap_request;
drop table __chart_day__ap_request;
drop table webhook;
drop table user_ip;
drop table renote_muting;
drop table note_edit;
drop table note;
drop type note_visibility_enum;
drop table channel;
drop table "user";
drop table drive_file;
drop table drive_folder;
drop function note_replies(varchar, integer, integer);
drop type log_level_enum;
drop type user_profile_mutingnotificationtypes_enum;

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,12 @@
pub use sea_orm_migration::prelude::*;
mod m20220101_000001_bootstrap;
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20220101_000001_bootstrap::Migration)]
}
}

View File

@ -0,0 +1,25 @@
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let db = manager.get_connection();
db.execute_unprepared(include_str!("bootstrap_up.sql"))
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let db = manager.get_connection();
db.execute_unprepared(include_str!("bootstrap_down.sql"))
.await?;
Ok(())
}
}

View File

@ -0,0 +1,6 @@
use sea_orm_migration::prelude::*;
#[tokio::main]
async fn main() {
cli::run_cli(ext_calckey_model_migration::Migrator).await;
}

View File

@ -1,4 +1,5 @@
use ck::user;
use ext_calckey_model_migration::{Migrator, MigratorTrait};
use sea_orm::{ColumnTrait, ConnectOptions, DatabaseConnection, EntityTrait, QueryFilter};
use thiserror::Error;
use tracing::log::LevelFilter;
@ -29,6 +30,12 @@ impl CalckeyModel {
Ok(CalckeyModel(sea_orm::Database::connect(opt).await?))
}
pub async fn migrate(&self) -> Result<(), CalckeyDbError> {
Migrator::up(&self.0, None).await?;
Ok(())
}
pub async fn get_user_by_tag(
&self,
name: &str,

View File

@ -37,6 +37,8 @@ async fn main() -> miette::Result<()> {
.await
.into_diagnostic()?;
db.migrate().await.into_diagnostic()?;
let well_known_router = Router::new()
.route(
"/webfinger",