pub struct DatabaseInitializer { /* private fields */ }Expand description
数据库初始化器
负责执行插件的数据库初始化脚本和迁移。 所有数据库操作都通过 yang-db::Database 提供的方法实现。
§字段
db: 调用方显式传入并交由初始化器持有的数据库实例use_transaction: 是否启用事务模式
§示例
ⓘ
use yang_base::database::DatabaseInitializer;
use yang_db::Database;
let db = Database::connect("mysql://root:password@localhost/test").await?;
let initializer = DatabaseInitializer::new(db, true);Implementations§
Source§impl DatabaseInitializer
impl DatabaseInitializer
Sourcepub fn new(db: Database, use_transaction: bool) -> Self
pub fn new(db: Database, use_transaction: bool) -> Self
创建新的数据库初始化器
§参数
db: yang-db::Database 实例use_transaction: 是否启用事务模式
§返回
- DatabaseInitializer 实例
§示例
ⓘ
use yang_base::database::DatabaseInitializer;
use yang_db::Database;
let db = Database::connect("mysql://root:password@localhost/test").await?;
// 创建事务模式的初始化器
let initializer = DatabaseInitializer::new(db, true);
// 创建非事务模式的初始化器
let db2 = Database::connect("mysql://root:password@localhost/test").await?;
let initializer = DatabaseInitializer::new(db2, false);Sourcepub async fn plan_migrations(
&self,
plugin: &dyn Plugin,
) -> Result<MigrationPlan, BaseError>
pub async fn plan_migrations( &self, plugin: &dyn Plugin, ) -> Result<MigrationPlan, BaseError>
只读生成单个插件的迁移计划,不创建迁移表、不执行 SQL、不写迁移记录。
Sourcepub async fn plan_all(
&self,
plugin_manager: &PluginManager,
) -> Result<MigrationPlan, BaseError>
pub async fn plan_all( &self, plugin_manager: &PluginManager, ) -> Result<MigrationPlan, BaseError>
只读生成全部插件的迁移计划。
Sourcepub async fn validate_table_definition(
&self,
table: &TableDefinition,
) -> Result<SchemaValidationReport, BaseError>
pub async fn validate_table_definition( &self, table: &TableDefinition, ) -> Result<SchemaValidationReport, BaseError>
从 MySQL information_schema 读取当前列并验证表定义的运行期字段契约。
本方法只读,不生成或执行 ALTER;数据库额外列不视为问题。
Sourcepub async fn initialize_all(
&self,
plugin_manager: &PluginManager,
) -> Result<(), BaseError>
pub async fn initialize_all( &self, plugin_manager: &PluginManager, ) -> Result<(), BaseError>
初始化所有插件的数据库
§参数
plugin_manager: 插件管理器
§返回
Ok(()): 初始化成功Err(BaseError): 初始化失败
§说明
使用 yang-db::Database::execute 和 yang-db::Database::transaction 方法。 根据 use_transaction 标志选择事务或非事务模式。
§示例
ⓘ
use yang_base::database::DatabaseInitializer;
use yang_base::plugin::PluginManager;
use yang_db::Database;
let db = Database::connect("mysql://root:password@localhost/test").await?;
let manager = PluginManager::new();
manager.register(MyPlugin).await?;
let initializer = DatabaseInitializer::new(db, true);
initializer.initialize_all(&manager).await?;Sourcepub async fn create_migration_table(&self) -> Result<(), BaseError>
pub async fn create_migration_table(&self) -> Result<(), BaseError>
Sourcepub async fn run_migrations_in_tx(
&self,
tx: &mut Transaction,
plugin: &dyn Plugin,
) -> Result<(), BaseError>
pub async fn run_migrations_in_tx( &self, tx: &mut Transaction, plugin: &dyn Plugin, ) -> Result<(), BaseError>
Source§impl DatabaseInitializer
impl DatabaseInitializer
Sourcepub async fn plan_table_definitions(
&self,
definitions: &[&TableDefinition],
) -> Result<SchemaSyncReport, BaseError>
pub async fn plan_table_definitions( &self, definitions: &[&TableDefinition], ) -> Result<SchemaSyncReport, BaseError>
只读取当前数据库 schema 并计算待执行变更,不获取同步锁、不执行 DDL。
适用于生产启动门禁:返回非空 changes 表示数据库尚未与定义对齐;不兼容
变更仍返回错误。调用方若要实际应用变更,应使用 Self::sync_table_definitions。
Sourcepub async fn sync_table_definitions(
&self,
definitions: &[&TableDefinition],
) -> Result<SchemaSyncReport, BaseError>
pub async fn sync_table_definitions( &self, definitions: &[&TableDefinition], ) -> Result<SchemaSyncReport, BaseError>
同步一组不可变表定义,使用单个数据库级 advisory lock 串行化多实例启动。
Auto Trait Implementations§
impl !RefUnwindSafe for DatabaseInitializer
impl !UnwindSafe for DatabaseInitializer
impl Freeze for DatabaseInitializer
impl Send for DatabaseInitializer
impl Sync for DatabaseInitializer
impl Unpin for DatabaseInitializer
impl UnsafeUnpin for DatabaseInitializer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more