Skip to content
Clone

feat(models): schema-sync registry, drop dead repo modules, add app/migrate binary

ZhenYi committed
parent 43c41af commit 9957b37 Browse files

8 files changed

+91 −8

File tree 8
Added app/migrate/src/main.rs View app/migrate/src/main.rs
@@ -0,0 +1,35 @@
1use anyhow::Context;
2use config::AppConfig;
3use models::migrate::sync_schema;
4use sea_orm::Database;
5use tracing::info;
6
7const DEFAULT_LOG_FILTER: &str = "info,migrate=debug";
8
9#[tokio::main]
10async fn main() -> anyhow::Result<()> {
11 tracing_subscriber::fmt()
12 .with_env_filter(
13 tracing_subscriber::EnvFilter::try_from_default_env()
14 .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(DEFAULT_LOG_FILTER)),
15 )
16 .init();
17
18 let cfg = AppConfig::load().context("loading configuration")?;
19 cfg.validate()
20 .map_err(|e| anyhow::anyhow!(e.to_string()))?;
21
22 let opts = cfg
23 .database
24 .primary_connect_options()
25 .map_err(|e| anyhow::anyhow!(e))?;
26 let db = Database::connect(opts)
27 .await
28 .context("connecting to postgres")?;
29
30 info!("running schema migration");
31 sync_schema(&db).await.context("running schema sync")?;
32 info!("schema migration completed");
33
34 Ok(())
35}
No newline at end of file
Added app/migrate/Cargo.toml View app/migrate/Cargo.toml
@@ -0,0 +1,13 @@
1[package]
2name = "migrate"
3version.workspace = true
4edition.workspace = true
5
6[dependencies]
7anyhow = { workspace = true }
8config = { workspace = true }
9models = { workspace = true }
10sea-orm = { workspace = true }
11tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
12tracing = { workspace = true }
13tracing-subscriber = { workspace = true }
No newline at end of file
Added lib/models/src/migrate/mod.rs View lib/models/src/migrate/mod.rs
@@ -0,0 +1,10 @@
1use sea_orm::{ConnectionTrait, DatabaseBackend, DatabaseConnection};
2
3pub async fn sync_schema(db: &DatabaseConnection) -> Result<(), sea_orm::DbErr> {
4 db.get_schema_registry("models::").sync(db).await?;
5 if db.get_database_backend() == DatabaseBackend::Postgres {
6 db.execute_unprepared(include_str!("../../schema_extras.sql"))
7 .await?;
8 }
9 Ok(())
10}
No newline at end of file
Modified lib/models/src/repo/mod.rs View lib/models/src/repo/mod.rs
@@ -1,10 +1,8 @@
11pub mod repo_branch_protection_policy;
22pub mod repo_branch_protection_whitelists;
33pub mod repo_collaborations;
4pub mod repo_deploy_keys;
54pub mod repo_license;
65pub mod repo_relation_topic;
7pub mod repo_release;
86pub mod repo_star;
97pub mod repo_tag_protection_policy;
108pub mod repo_tag_protection_whitelists;
Modified lib/models/src/repo/repo_branch_protection_whitelists.rs View lib/models/src/repo/repo_branch_protection_whitelists.rs
@@ -4,6 +4,7 @@
44
55#[derive(
66 Debug, Clone, Copy, PartialEq, Eq, Default, EnumIter, DeriveActiveEnum, Serialize, Deserialize,
7 utoipa::ToSchema,
78)]
89#[sea_orm(rs_type = "i16", db_type = "SmallInteger")]
910pub enum WhitelistKind {
Modified lib/models/src/lib.rs View lib/models/src/lib.rs
@@ -1,8 +1,3 @@
1//! 全库 ORM 实体(entity-first 策略)。
2//!
3//! 实体属性无法表达的 DDL 约束(partial unique 索引、XOR CHECK、组合索引)
4//! 见同目录下的 `schema_extras.sql`,由 db 层的 schema sync 在每次迁移后执行。
5
61pub mod common;
72pub mod dir;
83pub mod git;
@@ -14,3 +9,4 @@
149pub mod system;
1510pub mod users;
1611pub mod webhook;
12pub mod migrate;
No newline at end of file
Modified lib/models/Cargo.toml View lib/models/Cargo.toml
@@ -4,7 +4,7 @@
44edition.workspace = true
55
66[dependencies]
7sea-orm = { workspace = true, features = ["with-time", "with-uuid", "with-json"] }
7sea-orm = { workspace = true, features = ["with-time", "with-uuid", "with-json", "schema-sync", "entity-registry"] }
88time = { workspace = true }
99uuid = { workspace = true }
1010serde = { workspace = true, features = ["derive"] }
Modified Cargo.lock View Cargo.lock
@@ -588,6 +588,7 @@
588588dependencies = [
589589 "aes-gcm 0.10.3",
590590 "argon2 0.5.3",
591 "async-trait",
591592 "axum",
592593 "base64 0.23.1",
593594 "config",
@@ -4482,6 +4483,15 @@
44824483]
44834484
44844485[[package]]
4486name = "inventory"
4487version = "0.3.24"
4488source = "registry+https://github.com/rust-lang/crates.io-index"
4489checksum = "a4f0c30c76f2f4ccee3fe55a2435f691ca00c0e4bd87abe4f4a851b1d4dac39b"
4490dependencies = [
4491 "rustversion",
4492]
4493
4494[[package]]
44854495name = "io-close"
44864496version = "0.3.7"
44874497source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -4506,6 +4516,7 @@
45064516name = "issues"
45074517version = "1.0.0"
45084518dependencies = [
4519 "async-trait",
45094520 "axum",
45104521 "config",
45114522 "email",
@@ -4956,6 +4967,19 @@
49564967]
49574968
49584969[[package]]
4970name = "migrate"
4971version = "1.0.0"
4972dependencies = [
4973 "anyhow",
4974 "config",
4975 "models",
4976 "sea-orm",
4977 "tokio",
4978 "tracing",
4979 "tracing-subscriber",
4980]
4981
4982[[package]]
49594983name = "mime"
49604984version = "0.2.6"
49614985source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -6746,6 +6770,7 @@
67466770 "derive-where",
67476771 "derive_more",
67486772 "futures-util",
6773 "inventory",
67496774 "itertools 0.14.0",
67506775 "log 0.4.33",
67516776 "mac_address",
@@ -7025,13 +7050,16 @@
70257050version = "1.0.0"
70267051dependencies = [
70277052 "async-trait",
7053 "axum",
70287054 "axum_session",
70297055 "base64 0.23.1",
70307056 "chrono",
70317057 "config",
70327058 "export",
70337059 "redis",
7060 "sea-orm",
70347061 "serde",
7062 "time 0.3.55",
70357063 "tracing",
70367064 "uuid",
70377065]
@@ -7246,6 +7274,7 @@
72467274name = "space"
72477275version = "1.0.0"
72487276dependencies = [
7277 "async-trait",
72497278 "axum",
72507279 "base64 0.23.1",
72517280 "config",
@@ -8310,6 +8339,7 @@
83108339name = "user"
83118340version = "1.0.0"
83128341dependencies = [
8342 "async-trait",
83138343 "axum",
83148344 "base64 0.23.1",
83158345 "config",