From d71f9e500799a5a5647674de5c56a6a163ee7c04 Mon Sep 17 00:00:00 2001 From: fluo10 Date: Sun, 25 May 2025 12:56:56 +0900 Subject: [PATCH] Remove automerge and implement migration --- lazy-supplements-migration/Cargo.toml | 18 ++++ lazy-supplements-migration/README.md | 41 +++++++++ lazy-supplements-migration/src/lib.rs | 18 ++++ ...1_000001_create_lazy_supplements_tables.rs | 85 +++++++++++++++++++ lazy-supplements-migration/src/main.rs | 6 ++ lazy-supplements/Cargo.toml | 3 +- lazy-supplements/src/config/node.rs | 29 ------- lazy-supplements/src/config/server.rs | 1 - 8 files changed, 169 insertions(+), 32 deletions(-) create mode 100644 lazy-supplements-migration/Cargo.toml create mode 100644 lazy-supplements-migration/README.md create mode 100644 lazy-supplements-migration/src/lib.rs create mode 100644 lazy-supplements-migration/src/m20220101_000001_create_lazy_supplements_tables.rs create mode 100644 lazy-supplements-migration/src/main.rs diff --git a/lazy-supplements-migration/Cargo.toml b/lazy-supplements-migration/Cargo.toml new file mode 100644 index 0000000..1103839 --- /dev/null +++ b/lazy-supplements-migration/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "lazy-supplements-migration" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-std = { version = "1", features = ["attributes", "tokio1"] } + +[dependencies.sea-orm-migration] +version = "1.1.0" +features = [ + # Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI. + # View the list of supported features at https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime. + # e.g. + "runtime-tokio-rustls", # `ASYNC_RUNTIME` feature + "sqlx-postgres", # `DATABASE_DRIVER` feature +] diff --git a/lazy-supplements-migration/README.md b/lazy-supplements-migration/README.md new file mode 100644 index 0000000..3b438d8 --- /dev/null +++ b/lazy-supplements-migration/README.md @@ -0,0 +1,41 @@ +# Running Migrator CLI + +- Generate a new migration file + ```sh + cargo run -- 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 + ``` diff --git a/lazy-supplements-migration/src/lib.rs b/lazy-supplements-migration/src/lib.rs new file mode 100644 index 0000000..3794aba --- /dev/null +++ b/lazy-supplements-migration/src/lib.rs @@ -0,0 +1,18 @@ +use sea_orm_migration::{prelude::*, schema::*}; + +mod m20220101_000001_create_lazy_supplements_tables; + +pub struct Migrator; + +#[async_trait::async_trait] +pub trait TableMigration { + async fn up<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr> ; + async fn down<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr>; +} + +#[async_trait::async_trait] +impl MigratorTrait for Migrator { + fn migrations() -> Vec> { + vec![Box::new(m20220101_000001_create_lazy_supplements_tables::Migration)] + } +} diff --git a/lazy-supplements-migration/src/m20220101_000001_create_lazy_supplements_tables.rs b/lazy-supplements-migration/src/m20220101_000001_create_lazy_supplements_tables.rs new file mode 100644 index 0000000..0e00793 --- /dev/null +++ b/lazy-supplements-migration/src/m20220101_000001_create_lazy_supplements_tables.rs @@ -0,0 +1,85 @@ +use sea_orm_migration::{prelude::*, schema::*}; + +use crate::TableMigration; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + Device::up(manager).await?; + RecordDeletion::up(manager).await?; + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + Device::down(manager).await?; + RecordDeletion::down(manager).await?; + Ok(()) + } +} + +#[derive(DeriveIden)] +enum Device { + Table, + Id, + CreatedAt, + UpdatedAt, + SyncedAt, + Name, + Note, +} + +#[async_trait::async_trait] +impl TableMigration for Device { + async fn up<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr> { + manager.create_table( + Table::create() + .table(Self::Table) + .if_not_exists() + .col(pk_uuid(Self::Id)) + .col(timestamp_with_time_zone(Self::CreatedAt)) + .col(timestamp(Self::UpdatedAt)) + .col(timestamp_with_time_zone_null(Self::SyncedAt)) + .col(string(Self::Name)) + .col(text(Self::Note)) + .to_owned() + ).await?; + Ok(()) + + + } + async fn down<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr>{ + manager.drop_table(Table::drop().table(Self::Table).to_owned()).await + } +} + +#[derive(DeriveIden, DeriveMigrationName)] +enum RecordDeletion { + Table, + Id, + CreatedAt, + TableName, + RecordId, +} + +#[async_trait::async_trait] +impl TableMigration for RecordDeletion { + async fn up<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr> { + manager.create_table( + Table::create() + .table(Self::Table) + .if_not_exists() + .col(pk_uuid(Self::Id)) + .col(timestamp_with_time_zone(Self::CreatedAt)) + .col(string(Self::TableName)) + .col(uuid(Self::RecordId)) + .to_owned() + ).await?; + Ok(()) + } + async fn down<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr>{ + manager.drop_table(Table::drop().table(Self::Table).to_owned()).await + } +} \ No newline at end of file diff --git a/lazy-supplements-migration/src/main.rs b/lazy-supplements-migration/src/main.rs new file mode 100644 index 0000000..03983ba --- /dev/null +++ b/lazy-supplements-migration/src/main.rs @@ -0,0 +1,6 @@ +use sea_orm_migration::prelude::*; + +#[async_std::main] +async fn main() { + cli::run_cli(lazy_supplements_migration::Migrator).await; +} diff --git a/lazy-supplements/Cargo.toml b/lazy-supplements/Cargo.toml index a2e1adc..04cf1e1 100644 --- a/lazy-supplements/Cargo.toml +++ b/lazy-supplements/Cargo.toml @@ -7,11 +7,10 @@ license.workspace = true repository.workspace = true [dependencies] -automerge = "0.6.1" -autosurgeon = "0.8.7" base64 = "0.22.1" clap = { version = "4.5.38", features = ["derive"] } libp2p.workspace = true +sea-orm = "1.1.11" serde = { version = "1.0.219", features = ["derive"] } thiserror = "2.0.12" tokio = { version = "1.45.0", features = ["macros", "rt"] } diff --git a/lazy-supplements/src/config/node.rs b/lazy-supplements/src/config/node.rs index 0513554..2437f94 100644 --- a/lazy-supplements/src/config/node.rs +++ b/lazy-supplements/src/config/node.rs @@ -1,4 +1,3 @@ -use automerge::ActorId; use libp2p::identity::{self, Keypair}; use serde::{Deserialize, Serialize}; @@ -6,8 +5,6 @@ use serde::{Deserialize, Serialize}; pub struct NodeConfig { #[serde(with = "keypair")] secret: Keypair, - #[serde(with = "actor_id")] - actor_id: ActorId } mod keypair { @@ -31,31 +28,8 @@ mod keypair { } } -mod actor_id { - use automerge::ActorId; - use base64::{prelude::BASE64_STANDARD, Engine}; - use serde::{Deserialize, Deserializer, Serializer}; - - pub fn serialize(actor_id: &ActorId, serializer: S) -> Result - where S: Serializer - { - let bytes = actor_id.to_bytes(); - let base64 = BASE64_STANDARD.encode(bytes); - serializer.serialize_str(&base64) - } - - pub fn deserialize<'de, D>(deserializer: D) -> Result - where D: Deserializer<'de> - { - let base64 = String::deserialize(deserializer)?; - let vec = BASE64_STANDARD.decode(base64).unwrap(); - Ok(ActorId::from(vec)) - } -} - #[cfg(test)] mod tests { - use automerge::ActorId; use libp2p::identity; use super::*; @@ -63,15 +37,12 @@ mod tests { #[tokio::test] async fn serialize_deserialize() { let keypair = identity::Keypair::generate_ed25519(); - let actor_id = ActorId::random(); let config = NodeConfig { secret: keypair.clone(), - actor_id: actor_id.clone(), }; let string = toml::to_string(&config).unwrap(); println!("Parsed config: {}", &string); let parsed_config: NodeConfig = toml::from_str(&string).unwrap(); assert_eq!(keypair.public(), parsed_config.secret.public()); - assert_eq!(actor_id, parsed_config.actor_id); } } diff --git a/lazy-supplements/src/config/server.rs b/lazy-supplements/src/config/server.rs index 0deed9b..2805d57 100644 --- a/lazy-supplements/src/config/server.rs +++ b/lazy-supplements/src/config/server.rs @@ -1,6 +1,5 @@ use std::{collections::HashSet, net::{IpAddr, Ipv4Addr}, str::FromStr, sync::LazyLock}; -use automerge::hydrate::List; use clap::Args; use serde::{Deserialize, Serialize};