yang-base 0.6.0

个人使用构建后端服务器
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! schema 同步的数据库驱动分派。
//!
//! [`SchemaDriver`] 按 [`yang_db::BackendKind`] 选择方言与连接类型;DDL 事务性与
//! advisory lock 实现也在此分派。MySQL 行为与历史实现一致,PostgreSQL 使用
//! `pg_advisory_lock(bigint)` 与单事务 DDL。

use super::dialect::SchemaDialect;
use super::model::{ExistingTableSchema, TableSyncPlan};
use super::preflight::preflight_locked;
use super::SchemaDataViolation;
use crate::error::BaseError;
use crate::table::TableDefinition;
use yang_db::BackendKind;

/// schema 同步锁等待上限(秒)。
const LOCK_TIMEOUT_SECONDS: u64 = 30;

/// DDL 等待表锁的上限(秒);超时返回错误而不是无限阻塞在线业务。
#[cfg(feature = "postgres")]
const DDL_LOCK_TIMEOUT_SECONDS: u64 = 30;

/// schema 同步驱动。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SchemaDriver {
    /// MySQL。
    #[cfg(feature = "mysql")]
    MySql,
    /// PostgreSQL。
    #[cfg(feature = "postgres")]
    PostgreSql,
}

/// 借出的 schema 同步连接。
pub(crate) enum SchemaConnection<'a> {
    #[cfg(feature = "mysql")]
    MySql(&'a mut sqlx::MySqlConnection),
    #[cfg(feature = "postgres")]
    PostgreSql(&'a mut sqlx::PgConnection),
}

/// 从连接池借出的连接;保持生命周期,供同步入口持有。
pub(crate) enum OwnedSchemaConnection {
    #[cfg(feature = "mysql")]
    MySql(sqlx::pool::PoolConnection<sqlx::MySql>),
    #[cfg(feature = "postgres")]
    PostgreSql(sqlx::pool::PoolConnection<sqlx::Postgres>),
}

impl OwnedSchemaConnection {
    /// 借出底层 sqlx 连接。
    pub(crate) fn as_schema(&mut self) -> SchemaConnection<'_> {
        match self {
            #[cfg(feature = "mysql")]
            Self::MySql(connection) => SchemaConnection::MySql(connection),
            #[cfg(feature = "postgres")]
            Self::PostgreSql(connection) => SchemaConnection::PostgreSql(connection),
        }
    }
}

/// advisory lock 句柄。
#[derive(Debug)]
pub(crate) enum SchemaLock {
    #[cfg(feature = "mysql")]
    MySql(String),
    #[cfg(feature = "postgres")]
    PostgreSql(i64),
}

impl SchemaConnection<'_> {
    /// 执行一条 DDL/DML。
    pub(crate) async fn execute(&mut self, statement: &str) -> Result<(), BaseError> {
        match self {
            #[cfg(feature = "mysql")]
            Self::MySql(connection) => sqlx::query(statement)
                .execute(&mut **connection)
                .await
                .map(|_| ())
                .map_err(|error| BaseError::DatabaseExecuteFailed(error.into())),
            #[cfg(feature = "postgres")]
            Self::PostgreSql(connection) => sqlx::query(statement)
                .execute(&mut **connection)
                .await
                .map(|_| ())
                .map_err(|error| BaseError::DatabaseExecuteFailed(error.into())),
        }
    }

    /// 读取单列文本结果(预检 SQL 使用)。
    pub(crate) async fn fetch_text_column(
        &mut self,
        statement: &str,
    ) -> Result<Vec<String>, BaseError> {
        match self {
            #[cfg(feature = "mysql")]
            Self::MySql(connection) => sqlx::query_scalar::<_, String>(statement)
                .fetch_all(&mut **connection)
                .await
                .map_err(|error| BaseError::DatabaseQueryFailed(error.into())),
            #[cfg(feature = "postgres")]
            Self::PostgreSql(connection) => sqlx::query_scalar::<_, String>(statement)
                .fetch_all(&mut **connection)
                .await
                .map_err(|error| BaseError::DatabaseQueryFailed(error.into())),
        }
    }

    /// 开启事务(仅 PostgreSQL DDL 使用)。
    pub(crate) async fn begin(&mut self) -> Result<(), BaseError> {
        self.execute("BEGIN").await
    }

    /// 提交事务。
    pub(crate) async fn commit(&mut self) -> Result<(), BaseError> {
        self.execute("COMMIT").await
    }

    /// 回滚事务;失败不覆盖原始错误。
    pub(crate) async fn rollback(&mut self) -> Result<(), BaseError> {
        self.execute("ROLLBACK").await
    }
}

impl SchemaDriver {
    /// 根据运行时后端选择驱动。
    pub(crate) fn for_backend(backend: BackendKind) -> Result<Self, BaseError> {
        match backend {
            #[cfg(feature = "mysql")]
            BackendKind::MySql => Ok(Self::MySql),
            #[cfg(feature = "postgres")]
            BackendKind::PostgreSql => Ok(Self::PostgreSql),
            #[allow(unreachable_patterns)]
            _ => Err(BaseError::DatabaseInitFailed(format!(
                "schema 同步不支持数据库后端: {backend:?}"
            ))),
        }
    }

    /// 对应方言。
    pub(crate) fn dialect(self) -> SchemaDialect {
        match self {
            #[cfg(feature = "mysql")]
            Self::MySql => SchemaDialect::MySql,
            #[cfg(feature = "postgres")]
            Self::PostgreSql => SchemaDialect::PostgreSql,
        }
    }

    /// DDL 是否在单事务中执行;MySQL 的 DDL 隐式提交,故为 `false`。
    pub(crate) fn transactional_ddl(self) -> bool {
        match self {
            #[cfg(feature = "mysql")]
            Self::MySql => false,
            #[cfg(feature = "postgres")]
            Self::PostgreSql => true,
        }
    }

    /// 应用 DDL 会话参数(PostgreSQL `lock_timeout`)。
    ///
    /// 在线 DDL 必须避免无限等待业务表锁:超时后同步失败并整体回滚/留下可重试状态。
    pub(crate) async fn configure_session(
        self,
        connection: &mut SchemaConnection<'_>,
    ) -> Result<(), BaseError> {
        match self {
            #[cfg(feature = "mysql")]
            Self::MySql => {
                let _ = connection;
                Ok(())
            }
            #[cfg(feature = "postgres")]
            Self::PostgreSql => {
                connection
                    .execute(&format!("SET lock_timeout = '{DDL_LOCK_TIMEOUT_SECONDS}s'"))
                    .await
            }
        }
    }

    /// 复位 [`Self::configure_session`] 下发的会话参数。
    ///
    /// 连接归还池后会被业务查询复用;`lock_timeout` 必须恢复服务端默认值,
    /// 否则业务查询会继承 30s 锁超时。MySQL 无会话级设置,空操作。
    pub(crate) async fn reset_session(
        self,
        connection: &mut SchemaConnection<'_>,
    ) -> Result<(), BaseError> {
        match self {
            #[cfg(feature = "mysql")]
            Self::MySql => {
                let _ = connection;
                Ok(())
            }
            #[cfg(feature = "postgres")]
            Self::PostgreSql => connection.execute("RESET lock_timeout").await,
        }
    }

    /// 读取现有表结构。
    pub(crate) async fn introspect(
        self,
        connection: &mut SchemaConnection<'_>,
        definition: &TableDefinition,
    ) -> Result<ExistingTableSchema, BaseError> {
        match (self, connection) {
            #[cfg(feature = "mysql")]
            (Self::MySql, SchemaConnection::MySql(connection)) => {
                super::inspect::load_existing_schema(connection, definition).await
            }
            #[cfg(feature = "postgres")]
            (Self::PostgreSql, SchemaConnection::PostgreSql(connection)) => {
                super::pg::load_existing_schema(connection, definition).await
            }
            #[allow(unreachable_patterns)]
            _ => Err(BaseError::DatabaseInitFailed(
                "schema driver 与数据库连接后端不一致".to_string(),
            )),
        }
    }

    /// 渲染单条 `CREATE TABLE`。
    #[allow(dead_code)]
    pub(crate) fn render_create_table(
        self,
        table: &crate::table::TableConfig,
        include_foreign_keys: bool,
    ) -> Result<String, BaseError> {
        self.dialect()
            .render_create_table(table, include_foreign_keys)
    }

    /// 计算变更计划。
    pub(crate) fn plan_changes(
        self,
        definition: &TableDefinition,
        existing: &ExistingTableSchema,
    ) -> Result<TableSyncPlan, BaseError> {
        super::plan::plan_table_sync_with(self.dialect(), definition, existing)
    }

    /// 只读预检。
    pub(crate) async fn preflight(
        self,
        connection: &mut SchemaConnection<'_>,
        definitions: &[&TableDefinition],
        plans: &[TableSyncPlan],
    ) -> Result<Vec<SchemaDataViolation>, BaseError> {
        preflight_locked(self.dialect(), connection, definitions, plans).await
    }

    /// 获取数据库级 advisory lock。
    pub(crate) async fn lock(
        self,
        connection: &mut SchemaConnection<'_>,
    ) -> Result<SchemaLock, BaseError> {
        match (self, connection) {
            #[cfg(feature = "mysql")]
            (Self::MySql, SchemaConnection::MySql(connection)) => {
                let database_name: Option<String> = sqlx::query_scalar("SELECT DATABASE()")
                    .fetch_one(&mut **connection)
                    .await
                    .map_err(|error| BaseError::DatabaseQueryFailed(error.into()))?;
                let database_name = database_name.ok_or_else(|| {
                    BaseError::DatabaseInitFailed("当前 MySQL 连接没有选定数据库".to_string())
                })?;
                let lock_name = mysql_lock_name(&database_name);
                let acquired: Option<i64> = sqlx::query_scalar("SELECT GET_LOCK(?, ?)")
                    .bind(&lock_name)
                    .bind(LOCK_TIMEOUT_SECONDS as i64)
                    .fetch_one(&mut **connection)
                    .await
                    .map_err(|error| BaseError::DatabaseQueryFailed(error.into()))?;
                if acquired != Some(1) {
                    return Err(BaseError::DatabaseInitFailed(format!(
                        "等待 schema 同步锁超时: {lock_name}"
                    )));
                }
                Ok(SchemaLock::MySql(lock_name))
            }
            #[cfg(feature = "postgres")]
            (Self::PostgreSql, SchemaConnection::PostgreSql(connection)) => {
                let database_name: String = sqlx::query_scalar("SELECT current_database()")
                    .fetch_one(&mut **connection)
                    .await
                    .map_err(|error| BaseError::DatabaseQueryFailed(error.into()))?;
                let key = postgres_lock_key(&database_name);
                let deadline = std::time::Instant::now()
                    + std::time::Duration::from_secs(LOCK_TIMEOUT_SECONDS);
                loop {
                    let acquired: bool = sqlx::query_scalar("SELECT pg_try_advisory_lock($1)")
                        .bind(key)
                        .fetch_one(&mut **connection)
                        .await
                        .map_err(|error| BaseError::DatabaseQueryFailed(error.into()))?;
                    if acquired {
                        return Ok(SchemaLock::PostgreSql(key));
                    }
                    if std::time::Instant::now() >= deadline {
                        return Err(BaseError::DatabaseInitFailed(format!(
                            "等待 schema 同步锁超时: pg_advisory_lock({key})"
                        )));
                    }
                    tokio::time::sleep(std::time::Duration::from_millis(50)).await;
                }
            }
            #[allow(unreachable_patterns)]
            _ => Err(BaseError::DatabaseInitFailed(
                "schema driver 与数据库连接后端不一致".to_string(),
            )),
        }
    }

    /// 释放 advisory lock。
    pub(crate) async fn unlock(
        self,
        connection: &mut SchemaConnection<'_>,
        lock: &SchemaLock,
    ) -> Result<(), BaseError> {
        match (self, connection, lock) {
            #[cfg(feature = "mysql")]
            (Self::MySql, SchemaConnection::MySql(connection), SchemaLock::MySql(lock_name)) => {
                let released: Option<i64> = sqlx::query_scalar("SELECT RELEASE_LOCK(?)")
                    .bind(lock_name)
                    .fetch_one(&mut **connection)
                    .await
                    .map_err(|error| BaseError::DatabaseQueryFailed(error.into()))?;
                if released != Some(1) {
                    return Err(BaseError::DatabaseInitFailed(format!(
                        "schema 同步锁释放失败: {lock_name}"
                    )));
                }
                Ok(())
            }
            #[cfg(feature = "postgres")]
            (
                Self::PostgreSql,
                SchemaConnection::PostgreSql(connection),
                SchemaLock::PostgreSql(key),
            ) => {
                let released: bool = sqlx::query_scalar("SELECT pg_advisory_unlock($1)")
                    .bind(key)
                    .fetch_one(&mut **connection)
                    .await
                    .map_err(|error| BaseError::DatabaseQueryFailed(error.into()))?;
                if !released {
                    return Err(BaseError::DatabaseInitFailed(format!(
                        "schema 同步锁释放失败: pg_advisory_lock({key})"
                    )));
                }
                Ok(())
            }
            #[allow(unreachable_patterns)]
            _ => Err(BaseError::DatabaseInitFailed(
                "schema driver 与数据库连接后端不一致".to_string(),
            )),
        }
    }
}

/// 由数据库名生成 MySQL advisory lock 名称。
#[cfg(feature = "mysql")]
fn mysql_lock_name(database_name: &str) -> String {
    format!(
        "yang_base_schema_{:016x}",
        fnv1a_64(database_name.as_bytes())
    )
}

/// 由数据库名生成 PostgreSQL bigint advisory lock key。
#[cfg(feature = "postgres")]
fn postgres_lock_key(database_name: &str) -> i64 {
    fnv1a_64(database_name.as_bytes()) as i64
}

/// FNV-1a 64 位哈希;仅用于锁名,不追求密码学强度。
#[cfg(any(feature = "mysql", feature = "postgres"))]
fn fnv1a_64(bytes: &[u8]) -> u64 {
    let mut hash: u64 = 0xcbf29ce484222325;
    for byte in bytes {
        hash ^= u64::from(*byte);
        hash = hash.wrapping_mul(0x100000001b3);
    }
    hash
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn driver_dispatch_follows_backend_and_ddl_transactionality() {
        #[cfg(feature = "mysql")]
        {
            let driver = SchemaDriver::for_backend(BackendKind::MySql).expect("MySQL 驱动应可用");
            assert_eq!(driver.dialect(), SchemaDialect::MySql);
            assert!(!driver.transactional_ddl());
            assert_eq!(mysql_lock_name("test"), mysql_lock_name("test"));
        }
        #[cfg(feature = "postgres")]
        {
            let driver =
                SchemaDriver::for_backend(BackendKind::PostgreSql).expect("PostgreSQL 驱动应可用");
            assert_eq!(driver.dialect(), SchemaDialect::PostgreSql);
            assert!(driver.transactional_ddl());
            assert_eq!(postgres_lock_key("test"), postgres_lock_key("test"));
        }
        // Redis 没有 schema 同步驱动,必须 fail-closed。
        assert!(SchemaDriver::for_backend(BackendKind::Redis).is_err());
    }
}