From 9640f03d69d24b96c077ed084fc9702ae3602f57 Mon Sep 17 00:00:00 2001 From: GW_MC <72297530+GWMCwing@users.noreply.github.com> Date: Sat, 21 Mar 2026 03:09:22 +0000 Subject: [PATCH] feat: Add migration scripts for agents and public key revocations --- crates/migration/Cargo.toml | 1 + crates/migration/src/lib.rs | 9 ++- .../src/m20260301_000001_create_agents.rs | 61 +++++++++++++++++++ ...01_000002_create_public_key_revokaction.rs | 59 ++++++++++++++++++ 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 crates/migration/src/m20260301_000001_create_agents.rs create mode 100644 crates/migration/src/m20260301_000002_create_public_key_revokaction.rs diff --git a/crates/migration/Cargo.toml b/crates/migration/Cargo.toml index 1a9ca86..14a2192 100644 --- a/crates/migration/Cargo.toml +++ b/crates/migration/Cargo.toml @@ -14,3 +14,4 @@ async-std = { version = "1", features = ["attributes", "tokio1"] } [dependencies.sea-orm-migration] version = "2.0.0-rc" features = ["runtime-tokio-rustls", "sqlx-postgres"] + diff --git a/crates/migration/src/lib.rs b/crates/migration/src/lib.rs index 467dfa9..fb7c7c5 100644 --- a/crates/migration/src/lib.rs +++ b/crates/migration/src/lib.rs @@ -1,9 +1,16 @@ pub use sea_orm_migration::prelude::*; + +mod m20260301_000001_create_agents; +mod m20260301_000002_create_public_key_revokaction; + pub struct Migrator; #[async_trait::async_trait] impl MigratorTrait for Migrator { fn migrations() -> Vec> { - vec![] + vec![ + Box::new(m20260301_000001_create_agents::Migration), + Box::new(m20260301_000002_create_public_key_revokaction::Migration), + ] } } diff --git a/crates/migration/src/m20260301_000001_create_agents.rs b/crates/migration/src/m20260301_000001_create_agents.rs new file mode 100644 index 0000000..1441394 --- /dev/null +++ b/crates/migration/src/m20260301_000001_create_agents.rs @@ -0,0 +1,61 @@ +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> { + manager + .create_table( + Table::create() + .table(Agents::Table) + .if_not_exists() + .col(ColumnDef::new(Agents::Id).uuid().not_null().primary_key()) + .col(ColumnDef::new(Agents::Name).string().not_null()) + .col(ColumnDef::new(Agents::IpAddress).string()) + .col(ColumnDef::new(Agents::Version).string()) + .col(ColumnDef::new(Agents::State).string().not_null()) + .col(ColumnDef::new(Agents::DeploymentMode).string()) + .col(ColumnDef::new(Agents::LastSeenAt).timestamp_with_time_zone()) + .col(ColumnDef::new(Agents::Capabilities).json()) + .col(ColumnDef::new(Agents::PublicKeyHash).string()) + .col(ColumnDef::new(Agents::Labels).json()) + .col( + ColumnDef::new(Agents::CreatedAt) + .timestamp_with_time_zone() + .not_null(), + ) + .col( + ColumnDef::new(Agents::UpdatedAt) + .timestamp_with_time_zone() + .not_null(), + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(Agents::Table).to_owned()) + .await + } +} + +#[derive(DeriveIden)] +enum Agents { + Table, + Id, + Name, + IpAddress, + Version, + State, + DeploymentMode, + LastSeenAt, + Capabilities, + PublicKeyHash, + Labels, + CreatedAt, + UpdatedAt, +} diff --git a/crates/migration/src/m20260301_000002_create_public_key_revokaction.rs b/crates/migration/src/m20260301_000002_create_public_key_revokaction.rs new file mode 100644 index 0000000..6da5eea --- /dev/null +++ b/crates/migration/src/m20260301_000002_create_public_key_revokaction.rs @@ -0,0 +1,59 @@ +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 table = manager + .create_table( + Table::create() + .table(PublicKeyRevocations::Table) + .if_not_exists() + .col( + ColumnDef::new(PublicKeyRevocations::Id) + .uuid() + .not_null() + .primary_key(), + ) + .col( + ColumnDef::new(PublicKeyRevocations::PublicKeyHash) + .string() + .not_null(), + ) + .col( + ColumnDef::new(PublicKeyRevocations::CreatedAt) + .timestamp_with_time_zone() + .not_null(), + ) + .to_owned(), + ) + .await?; + // add index on PublicKeyHash for faster lookups + manager + .create_index( + Index::create() + .name("idx_public_key_hash") + .table(PublicKeyRevocations::Table) + .col(PublicKeyRevocations::PublicKeyHash) + .to_owned(), + ) + .await?; + Ok(table) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(PublicKeyRevocations::Table).to_owned()) + .await + } +} + +#[derive(DeriveIden)] +enum PublicKeyRevocations { + Table, + Id, + PublicKeyHash, + CreatedAt, +}