Skip to content

Commit a331d97

Browse files
committed
Migrate from rusqlite to seaorm
1 parent f8ff20f commit a331d97

File tree

14 files changed

+2305
-196
lines changed

14 files changed

+2305
-196
lines changed

Cargo.lock

Lines changed: 2039 additions & 69 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
[workspace]
22
resolver = "3"
3-
members = ["client", "proto-codegen", "server", "service", "stresstest"]
3+
members = [
4+
"client",
5+
"proto-codegen",
6+
"server",
7+
"service",
8+
"stresstest",
9+
"migration",
10+
]
411
default-members = ["server"]
512

613
[profile.dev]

migration/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "migration"
3+
version = "0.1.0"
4+
edition = "2024"
5+
publish = false
6+
7+
[dependencies]
8+
async-std = { version = "1", features = ["attributes", "tokio1"] }
9+
sea-orm-migration = { version = "1.1.0", features = [
10+
"sqlx-postgres",
11+
"runtime-tokio-rustls",
12+
] }

migration/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Running Migrator CLI
2+
3+
- Generate a new migration file
4+
```sh
5+
cargo run -- generate MIGRATION_NAME
6+
```
7+
- Apply all pending migrations
8+
```sh
9+
cargo run
10+
```
11+
```sh
12+
cargo run -- up
13+
```
14+
- Apply first 10 pending migrations
15+
```sh
16+
cargo run -- up -n 10
17+
```
18+
- Rollback last applied migrations
19+
```sh
20+
cargo run -- down
21+
```
22+
- Rollback last 10 applied migrations
23+
```sh
24+
cargo run -- down -n 10
25+
```
26+
- Drop all tables from the database, then reapply all migrations
27+
```sh
28+
cargo run -- fresh
29+
```
30+
- Rollback all applied migrations, then reapply all migrations
31+
```sh
32+
cargo run -- refresh
33+
```
34+
- Rollback all applied migrations
35+
```sh
36+
cargo run -- reset
37+
```
38+
- Check the status of all migrations
39+
```sh
40+
cargo run -- status
41+
```

migration/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
pub use sea_orm_migration::prelude::*;
2+
3+
mod m20220101_000001_create_table;
4+
5+
pub struct Migrator;
6+
7+
#[async_trait::async_trait]
8+
impl MigratorTrait for Migrator {
9+
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
10+
vec![Box::new(m20220101_000001_create_table::Migration)]
11+
}
12+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use sea_orm_migration::{prelude::*, schema::*};
2+
3+
#[derive(DeriveMigrationName)]
4+
pub struct Migration;
5+
6+
#[derive(DeriveIden)]
7+
enum Part {
8+
Table,
9+
Id,
10+
Compression,
11+
#[allow(clippy::enum_variant_names)]
12+
PartSize,
13+
CompressedSize,
14+
SegmentId,
15+
SegmentOffset,
16+
}
17+
18+
#[derive(DeriveIden)]
19+
enum Blob {
20+
Table,
21+
Id,
22+
Parts,
23+
}
24+
25+
#[async_trait::async_trait]
26+
impl MigrationTrait for Migration {
27+
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
28+
manager
29+
.create_table(
30+
Table::create()
31+
.table(Part::Table)
32+
.if_not_exists()
33+
.col(big_integer(Part::Id).auto_increment().primary_key())
34+
.col(unsigned(Part::PartSize))
35+
.col(small_unsigned(Part::Compression))
36+
.col(unsigned(Part::CompressedSize))
37+
.col(uuid(Part::SegmentId))
38+
.col(unsigned(Part::SegmentOffset))
39+
.to_owned(),
40+
)
41+
.await?;
42+
43+
manager
44+
.create_table(
45+
Table::create()
46+
.table(Blob::Table)
47+
.if_not_exists()
48+
.col(string(Blob::Id).primary_key())
49+
.col(array(Blob::Parts, ColumnType::BigUnsigned))
50+
.to_owned(),
51+
)
52+
.await
53+
}
54+
55+
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
56+
manager
57+
.drop_table(Table::drop().table(Part::Table).to_owned())
58+
.await?;
59+
60+
manager
61+
.drop_table(Table::drop().table(Blob::Table).to_owned())
62+
.await
63+
}
64+
}

migration/src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use sea_orm_migration::prelude::*;
2+
3+
#[async_std::main]
4+
async fn main() {
5+
cli::run_cli(migration::Migrator).await;
6+
}

service/Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7+
anyhow = "1.0.98"
78
proto-codegen = { path = "../proto-codegen" }
8-
rusqlite = "0.36.0"
9+
migration = { path = "../migration" }
10+
sea-orm = { version = "1.1.0", features = [
11+
"sqlx-postgres",
12+
"runtime-tokio-rustls",
13+
"macros",
14+
] }
915
uuid = { version = "1.17.0", features = ["v4"] }
16+
tokio = { version = "1.46.0", features = ["full"] }
1017

1118
[dev-dependencies]
1219
tempfile = "3.20.0"

service/src/datamodel.rs

Lines changed: 0 additions & 42 deletions
This file was deleted.

service/src/db/blob.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.13
2+
3+
use sea_orm::entity::prelude::*;
4+
5+
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
6+
#[sea_orm(table_name = "blob")]
7+
pub struct Model {
8+
#[sea_orm(primary_key, auto_increment = false)]
9+
pub id: String,
10+
pub parts: Vec<i64>,
11+
}
12+
13+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
14+
pub enum Relation {}
15+
16+
impl ActiveModelBehavior for ActiveModel {}

0 commit comments

Comments
 (0)