feat(db): 添加 pg_trgm 扩展 + plugin_entity_columns 元数据表
- 启用 pg_trgm 扩展加速 ILIKE '%keyword%' 模糊搜索 - 新增 plugin_entity_columns 表,记录插件动态表中哪些字段被提取为 Generated Column - 添加 plugin_entity_id 外键关联 plugin_entities 表 - down 方法仅删表不卸载 pg_trgm(其他功能可能依赖)
This commit is contained in:
@@ -34,6 +34,7 @@ mod m20260415_000030_add_version_to_message_tables;
|
||||
mod m20260416_000031_create_domain_events;
|
||||
mod m20260417_000033_create_plugins;
|
||||
mod m20260417_000034_seed_plugin_permissions;
|
||||
mod m20260418_000035_pg_trgm_and_entity_columns;
|
||||
|
||||
pub struct Migrator;
|
||||
|
||||
@@ -75,6 +76,7 @@ impl MigratorTrait for Migrator {
|
||||
Box::new(m20260414_000032_fix_settings_unique_index_null::Migration),
|
||||
Box::new(m20260417_000033_create_plugins::Migration),
|
||||
Box::new(m20260417_000034_seed_plugin_permissions::Migration),
|
||||
Box::new(m20260418_000035_pg_trgm_and_entity_columns::Migration),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
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> {
|
||||
// 启用 pg_trgm 扩展(加速 ILIKE '%keyword%' 搜索)
|
||||
manager
|
||||
.get_connection()
|
||||
.execute_unprepared("CREATE EXTENSION IF NOT EXISTS pg_trgm")
|
||||
.await?;
|
||||
|
||||
// 插件实体列元数据表 — 记录哪些字段被提取为 Generated Column
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(Alias::new("plugin_entity_columns"))
|
||||
.if_not_exists()
|
||||
.col(
|
||||
ColumnDef::new(Alias::new("id"))
|
||||
.uuid()
|
||||
.not_null()
|
||||
.default(Expr::cust("gen_random_uuid()"))
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(Alias::new("tenant_id")).uuid().not_null())
|
||||
.col(ColumnDef::new(Alias::new("plugin_entity_id")).uuid().not_null())
|
||||
.col(ColumnDef::new(Alias::new("field_name")).string().not_null())
|
||||
.col(ColumnDef::new(Alias::new("column_name")).string().not_null())
|
||||
.col(ColumnDef::new(Alias::new("sql_type")).string().not_null())
|
||||
.col(
|
||||
ColumnDef::new(Alias::new("is_generated"))
|
||||
.boolean()
|
||||
.not_null()
|
||||
.default(true),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(Alias::new("created_at"))
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::cust("NOW()")),
|
||||
)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// plugin_entity_id 外键
|
||||
manager
|
||||
.create_foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_plugin_entity_columns_entity")
|
||||
.from(
|
||||
Alias::new("plugin_entity_columns"),
|
||||
Alias::new("plugin_entity_id"),
|
||||
)
|
||||
.to(Alias::new("plugin_entities"), Alias::new("id"))
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(
|
||||
Table::drop()
|
||||
.table(Alias::new("plugin_entity_columns"))
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
// pg_trgm 不卸载(其他功能可能依赖)
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user