pub struct DatabaseInitializer { /* private fields */ }Expand description
持有数据库连接并执行 crate::table::TableDefinition 声明的结构同步。
Implementations§
Source§impl DatabaseInitializer
impl DatabaseInitializer
Sourcepub fn new(db: Database) -> Self
pub fn new(db: Database) -> Self
用 MySQL 连接构造同步器(保持既有调用方式)。
逻辑源固定为 default;具名数据源必须使用
Self::from_any 并传入 tools.database_named(id)? 的容器。
Sourcepub fn new_postgres(db: Database) -> Self
pub fn new_postgres(db: Database) -> Self
用 PostgreSQL 连接构造同步器。
逻辑源固定为 default;接入具名源时同样应使用 Self::from_any。
Sourcepub fn from_any(db: AnyDatabase) -> Self
pub fn from_any(db: AnyDatabase) -> Self
用运行时数据库容器构造同步器。
Source§impl DatabaseInitializer
impl DatabaseInitializer
Sourcepub async fn validate_table_definition(
&self,
table: &TableDefinition,
) -> Result<SchemaValidationReport, BaseError>
pub async fn validate_table_definition( &self, table: &TableDefinition, ) -> Result<SchemaValidationReport, BaseError>
从数据库读取当前列并验证表定义的运行期字段契约。
本方法只读,不生成或执行 ALTER;数据库额外列不视为问题。
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 preflight_table_definitions(
&self,
definitions: &[&TableDefinition],
) -> Result<SchemaPreflightReport, BaseError>
pub async fn preflight_table_definitions( &self, definitions: &[&TableDefinition], ) -> Result<SchemaPreflightReport, BaseError>
只读预检结构计划及会阻止新约束生效的旧数据。
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 串行化:MySQL 用 GET_LOCK,
PostgreSQL 用 pg_advisory_lock(bigint)(session 级)。PostgreSQL 按阶段
执行:地基 DDL(建表/加列)→ 同事务预检 → 其余事务步骤 → 提交 →
并发索引与非外键 VALIDATE CONSTRAINT → 外键事务(ADD ... NOT VALID)→
提交后执行外键 VALIDATE CONSTRAINT。
Foundation 与 Main 阶段共享同一事务,整体提交或回滚;Concurrent 与外键阶段
在提交后以 autocommit 执行,失败不会回滚已提交的 DDL,只会留下可重试状态
(INVALID 索引 / NOT VALID 约束),下次同步会自动自愈。
Auto Trait Implementations§
impl !Freeze for DatabaseInitializer
impl !RefUnwindSafe for DatabaseInitializer
impl !UnwindSafe for DatabaseInitializer
impl Forget for DatabaseInitializer
impl Move 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
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> ⓘ
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> ⓘ
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