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(Alias::new("api_clients")) .col( ColumnDef::new(Alias::new("id")) .uuid() .not_null() .default(Expr::cust("gen_random_uuid()")), ) .col(ColumnDef::new(Alias::new("tenant_id")).uuid().not_null()) .col( ColumnDef::new(Alias::new("client_id")) .string_len(128) .not_null(), ) .col( ColumnDef::new(Alias::new("client_secret_hash")) .string_len(256) .not_null(), ) .col( ColumnDef::new(Alias::new("client_name")) .string_len(200) .not_null(), ) .col(ColumnDef::new(Alias::new("scopes")).json().not_null()) .col(ColumnDef::new(Alias::new("allowed_patient_ids")).json().null()) .col( ColumnDef::new(Alias::new("rate_limit_per_minute")) .integer() .not_null() .default(60), ) .col( ColumnDef::new(Alias::new("is_active")) .boolean() .not_null() .default(true), ) .col( ColumnDef::new(Alias::new("token_lifetime_seconds")) .integer() .not_null() .default(3600), ) .col( ColumnDef::new(Alias::new("created_at")) .timestamp_with_time_zone() .not_null() .default(Expr::cust("NOW()")), ) .col( ColumnDef::new(Alias::new("updated_at")) .timestamp_with_time_zone() .not_null() .default(Expr::cust("NOW()")), ) .col(ColumnDef::new(Alias::new("created_by")).uuid().null()) .col(ColumnDef::new(Alias::new("updated_by")).uuid().null()) .col(ColumnDef::new(Alias::new("deleted_at")).timestamp_with_time_zone().null()) .col( ColumnDef::new(Alias::new("version")) .integer() .not_null() .default(1), ) .primary_key(Index::create().col(Alias::new("id"))) .to_owned(), ) .await?; manager .create_index( Index::create() .name("idx_api_clients_client_id_unique") .table(Alias::new("api_clients")) .col(Alias::new("client_id")) .unique() .to_owned(), ) .await?; manager .create_index( Index::create() .name("idx_api_clients_tenant_id") .table(Alias::new("api_clients")) .col(Alias::new("tenant_id")) .to_owned(), ) .await } async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager .drop_table(Table::drop().table(Alias::new("api_clients")).to_owned()) .await } }