Skip to main content

DatabaseInitializer

Struct DatabaseInitializer 

Source
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

Source

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);
Source

pub async fn plan_migrations( &self, plugin: &dyn Plugin, ) -> Result<MigrationPlan, BaseError>

只读生成单个插件的迁移计划,不创建迁移表、不执行 SQL、不写迁移记录。

Source

pub async fn plan_all( &self, plugin_manager: &PluginManager, ) -> Result<MigrationPlan, BaseError>

只读生成全部插件的迁移计划。

Source

pub async fn validate_table_definition( &self, table: &TableDefinition, ) -> Result<SchemaValidationReport, BaseError>

从 MySQL information_schema 读取当前列并验证表定义的运行期字段契约。

本方法只读,不生成或执行 ALTER;数据库额外列不视为问题。

Source

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?;
Source

pub async fn create_migration_table(&self) -> Result<(), BaseError>

创建迁移记录表

§返回
  • Ok(()): 创建成功
  • Err(BaseError): 创建失败
§说明

使用 yang-db::Database::execute 执行 SQL。

Source

pub async fn run_migrations(&self, plugin: &dyn Plugin) -> Result<(), BaseError>

执行迁移(非事务模式)

§参数
  • plugin: 插件实例
§返回
  • Ok(()): 迁移成功
  • Err(BaseError): 迁移失败
§说明

使用 yang-db::Database::execute 执行 SQL。

Source

pub async fn run_migrations_in_tx( &self, tx: &mut Transaction, plugin: &dyn Plugin, ) -> Result<(), BaseError>

执行迁移(事务模式)

§参数
  • tx: yang-db 事务对象
  • plugin: 插件实例
§返回
  • Ok(()): 迁移成功
  • Err(BaseError): 迁移失败
§说明

使用 yang-db::Transaction::execute 执行 SQL。

Source

pub async fn is_migration_executed( &self, module_name: &str, version: &str, ) -> Result<bool, BaseError>

检查迁移是否已执行

§参数
  • module_name: 模块名称
  • version: 迁移版本
§返回
  • Ok(bool): true 表示已执行,false 表示未执行
  • Err(BaseError): 查询失败
§说明

使用参数化查询,防止 SQL 注入攻击。

Source

pub async fn record_migration_with_checksum( &self, module_name: &str, version: &str, checksum: &str, status: &str, ) -> Result<(), BaseError>

记录迁移

§参数
  • module_name: 模块名称
  • version: 迁移版本
§返回
  • Ok(()): 记录成功
  • Err(BaseError): 记录失败
§说明

使用参数化查询,防止 SQL 注入攻击。

Source§

impl DatabaseInitializer

Source

pub async fn plan_table_definitions( &self, definitions: &[&TableDefinition], ) -> Result<SchemaSyncReport, BaseError>

只读取当前数据库 schema 并计算待执行变更,不获取同步锁、不执行 DDL。

适用于生产启动门禁:返回非空 changes 表示数据库尚未与定义对齐;不兼容 变更仍返回错误。调用方若要实际应用变更,应使用 Self::sync_table_definitions。

Source

pub async fn sync_table_definitions( &self, definitions: &[&TableDefinition], ) -> Result<SchemaSyncReport, BaseError>

同步一组不可变表定义,使用单个数据库级 advisory lock 串行化多实例启动。

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more