feat(sqlite): support attached database schema changes
This commit is contained in:
41 files changed
+1219
-129
No files matched your search
@@ -1167,6 +1167,12 @@ impl AppState {
|
||||
PoolKind::Postgres(pg_pool)
|
||||
}
|
||||
DatabaseType::Sqlite => {
|
||||
let sqlite_path = expand_tilde(&db_config.host);
|
||||
db::sqlite::validate_persistent_attachments(
|
||||
&sqlite_path,
|
||||
&db_config.password,
|
||||
!db_config.attached_databases.is_empty(),
|
||||
)?;
|
||||
let extensions = db::sqlite::sqlite_extension_specs_from_url_params(db_config.url_params.as_deref())
|
||||
.into_iter()
|
||||
.map(|mut extension| {
|
||||
@@ -1174,14 +1180,16 @@ impl AppState {
|
||||
extension
|
||||
})
|
||||
.collect();
|
||||
PoolKind::Sqlite(
|
||||
db::sqlite::connect_path_with_cipher_key_and_extensions(
|
||||
&expand_tilde(&db_config.host),
|
||||
&db_config.password,
|
||||
extensions,
|
||||
)
|
||||
.await?,
|
||||
let pool = db::sqlite::connect_path_with_cipher_key_and_extensions(
|
||||
&sqlite_path,
|
||||
&db_config.password,
|
||||
extensions,
|
||||
)
|
||||
.await?;
|
||||
for attached in &db_config.attached_databases {
|
||||
db::sqlite::attach_database(&pool, &attached.name, &expand_tilde(&attached.path))?;
|
||||
}
|
||||
PoolKind::Sqlite(pool)
|
||||
}
|
||||
DatabaseType::Rqlite => {
|
||||
let client = db::rqlite_driver::RqliteClient::new(
|
||||
@@ -4668,6 +4676,41 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn sqlite_connection_restores_attached_databases() {
|
||||
let (state, dir) = test_app_state().await;
|
||||
let main_path = dir.join("main.sqlite");
|
||||
let attached_path = dir.join("analytics.sqlite");
|
||||
drop(db::sqlite::connect_path_create_if_missing(main_path.to_str().unwrap()).await.unwrap());
|
||||
let attached = db::sqlite::connect_path_create_if_missing(attached_path.to_str().unwrap()).await.unwrap();
|
||||
db::sqlite::execute_query(&attached, "CREATE TABLE events(id INTEGER PRIMARY KEY);").await.unwrap();
|
||||
drop(attached);
|
||||
|
||||
let mut config = mysql_config(None);
|
||||
config.id = "sqlite-conn".to_string();
|
||||
config.name = "SQLite".to_string();
|
||||
config.db_type = DatabaseType::Sqlite;
|
||||
config.host = main_path.to_string_lossy().to_string();
|
||||
config.port = 0;
|
||||
config.password.clear();
|
||||
config.attached_databases.push(AttachedDatabaseConfig {
|
||||
name: "analytics".to_string(),
|
||||
path: attached_path.to_string_lossy().to_string(),
|
||||
});
|
||||
state.configs.write().await.insert(config.id.clone(), config);
|
||||
|
||||
state.get_or_create_pool("sqlite-conn", None).await.unwrap();
|
||||
let databases = schema::list_databases_core(&state, "sqlite-conn").await.unwrap();
|
||||
assert!(databases.iter().any(|database| database.name == "analytics"));
|
||||
let tables = schema::list_tables_core(&state, "sqlite-conn", "analytics", "analytics", None, None, None, None)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(tables.iter().any(|table| table.name == "events"));
|
||||
|
||||
state.connections.write().await.clear();
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[cfg(feature = "duckdb-bundled")]
|
||||
#[tokio::test]
|
||||
async fn duckdb_connection_test_supports_memory_bootstrap() {
|
||||
|
||||
@@ -39,6 +39,43 @@ impl SqliteHandle {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn attach_database(pool: &SqliteHandle, name: &str, path: &str) -> Result<(), String> {
|
||||
let name = name.trim();
|
||||
if name.is_empty() {
|
||||
return Err("SQLite attached database name cannot be empty".to_string());
|
||||
}
|
||||
if name.eq_ignore_ascii_case("main") || name.eq_ignore_ascii_case("temp") {
|
||||
return Err(format!("SQLite database name \"{name}\" is reserved"));
|
||||
}
|
||||
if !is_memory_database_path(path) {
|
||||
validate_file_path(path, is_network_path)?;
|
||||
}
|
||||
|
||||
let sql = format!("ATTACH DATABASE ?1 AS {}", sqlite_quote_ident(name));
|
||||
pool.with_connection(|conn| {
|
||||
conn.execute(&sql, [path]).map(|_| ()).map_err(|e| format!("Failed to attach SQLite database \"{name}\": {e}"))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn validate_persistent_attachments(main_path: &str, cipher_key: &str, has_attachments: bool) -> Result<(), String> {
|
||||
if !has_attachments {
|
||||
return Ok(());
|
||||
}
|
||||
if is_memory_database_path(main_path) {
|
||||
return Err(
|
||||
"Persistent SQLite attachments are unavailable for an in-memory main database because reconnecting would discard its data."
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
if !cipher_key.is_empty() {
|
||||
return Err(
|
||||
"Persistent SQLite attachments are unavailable for SQLCipher connections because each attached database requires an explicit key."
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn connect_path(path: &str) -> Result<SqliteHandle, String> {
|
||||
connect_path_with_options(path, false, None, Vec::new()).await
|
||||
}
|
||||
@@ -498,6 +535,18 @@ pub fn is_memory_database_path(path: &str) -> bool {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn persistent_attachments_require_a_file_backed_plaintext_main_database() {
|
||||
assert!(validate_persistent_attachments("/tmp/main.sqlite", "", false).is_ok());
|
||||
assert!(validate_persistent_attachments("/tmp/main.sqlite", "", true).is_ok());
|
||||
|
||||
let memory_error = validate_persistent_attachments(":memory:", "", true).unwrap_err();
|
||||
assert!(memory_error.contains("in-memory main database"), "{memory_error}");
|
||||
|
||||
let cipher_error = validate_persistent_attachments("/tmp/main.sqlite", "secret", true).unwrap_err();
|
||||
assert!(cipher_error.contains("SQLCipher"), "{cipher_error}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn connect_path_supports_memory_database_across_statements() {
|
||||
let pool = connect_path(":memory:").await.expect("connect in-memory SQLite");
|
||||
@@ -1078,22 +1127,113 @@ mod tests {
|
||||
assert_eq!(columns.candidates[0].name, "display_name");
|
||||
assert_eq!(columns.candidates[0].data_type.as_deref(), Some("TEXT"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn attached_database_metadata_uses_attached_schema() {
|
||||
let path = std::env::temp_dir().join(format!("dbx-sqlite-attach-{}.sqlite", uuid::Uuid::new_v4()));
|
||||
{
|
||||
let conn = Connection::open(&path).expect("create attached database");
|
||||
conn.execute_batch(
|
||||
"CREATE TABLE parent(id INTEGER PRIMARY KEY); \
|
||||
CREATE TABLE child(id INTEGER PRIMARY KEY AUTOINCREMENT, parent_id INTEGER REFERENCES parent(id), source TEXT NOT NULL); \
|
||||
INSERT INTO child(parent_id, source) VALUES(NULL, 'attached'); \
|
||||
CREATE INDEX child_parent_idx ON child(parent_id); \
|
||||
CREATE TRIGGER child_touch AFTER INSERT ON child BEGIN UPDATE child SET parent_id = parent_id WHERE id = new.id; END;",
|
||||
)
|
||||
.expect("create attached schema");
|
||||
}
|
||||
|
||||
let pool = connect_path(":memory:").await.expect("connect primary database");
|
||||
attach_database(&pool, "analytics", path.to_str().unwrap()).expect("attach database");
|
||||
execute_query(&pool, "CREATE TABLE child(source TEXT NOT NULL); INSERT INTO child(source) VALUES('main');")
|
||||
.await
|
||||
.expect("create same-named main table");
|
||||
|
||||
let qualified_child = crate::sql_dialect::qualified_table_name(
|
||||
Some(crate::models::connection::DatabaseType::Sqlite),
|
||||
Some("analytics"),
|
||||
"child",
|
||||
);
|
||||
pool.with_connection(|conn| {
|
||||
let attached_source: String = conn
|
||||
.query_row(&format!("SELECT source FROM {qualified_child}"), [], |row| row.get(0))
|
||||
.map_err(|error| error.to_string())?;
|
||||
assert_eq!(attached_source, "attached");
|
||||
|
||||
conn.execute(&format!("UPDATE {qualified_child} SET source = 'updated'"), [])
|
||||
.map_err(|error| error.to_string())?;
|
||||
let main_source: String = conn
|
||||
.query_row("SELECT source FROM main.child", [], |row| row.get(0))
|
||||
.map_err(|error| error.to_string())?;
|
||||
let updated_attached_source: String = conn
|
||||
.query_row("SELECT source FROM analytics.child", [], |row| row.get(0))
|
||||
.map_err(|error| error.to_string())?;
|
||||
assert_eq!(main_source, "main");
|
||||
assert_eq!(updated_attached_source, "updated");
|
||||
Ok(())
|
||||
})
|
||||
.expect("query attached table by qualified name");
|
||||
|
||||
let databases = list_databases(&pool).await.expect("list databases");
|
||||
assert_eq!(databases.into_iter().map(|database| database.name).collect::<Vec<_>>(), vec!["main", "analytics"]);
|
||||
assert!(list_tables(&pool, "analytics")
|
||||
.await
|
||||
.expect("list attached tables")
|
||||
.iter()
|
||||
.any(|table| table.name == "child"));
|
||||
|
||||
let columns = get_columns(&pool, "analytics", "child").await.expect("list attached columns");
|
||||
assert_eq!(
|
||||
columns.iter().find(|column| column.name == "id").and_then(|column| column.extra.as_deref()),
|
||||
Some("autoincrement")
|
||||
);
|
||||
assert!(list_indexes(&pool, "analytics", "child")
|
||||
.await
|
||||
.expect("list attached indexes")
|
||||
.iter()
|
||||
.any(|index| index.name == "child_parent_idx"));
|
||||
assert_eq!(list_foreign_keys(&pool, "analytics", "child").await.expect("list attached foreign keys").len(), 1);
|
||||
assert!(list_triggers(&pool, "analytics", "child")
|
||||
.await
|
||||
.expect("list attached triggers")
|
||||
.iter()
|
||||
.any(|trigger| trigger.name == "child_touch"));
|
||||
|
||||
drop(pool);
|
||||
let _ = std::fs::remove_file(path);
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn list_databases(_pool: &SqliteHandle) -> Result<Vec<DatabaseInfo>, String> {
|
||||
Ok(vec![DatabaseInfo { name: "main".to_string() }])
|
||||
}
|
||||
|
||||
pub async fn list_tables(pool: &SqliteHandle, _schema: &str) -> Result<Vec<TableInfo>, String> {
|
||||
pub async fn list_databases(pool: &SqliteHandle) -> Result<Vec<DatabaseInfo>, String> {
|
||||
let pool = pool.clone();
|
||||
tokio::task::spawn_blocking(move || {
|
||||
pool.with_connection(|conn| {
|
||||
let mut stmt = conn
|
||||
.prepare(
|
||||
"SELECT name, type FROM sqlite_master \
|
||||
WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%' ORDER BY name",
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let mut stmt = conn.prepare("PRAGMA database_list").map_err(|e| e.to_string())?;
|
||||
let rows = stmt.query_map([], |row| row.get::<_, String>("name")).map_err(|e| e.to_string())?;
|
||||
rows.filter_map(|row| match row {
|
||||
Ok(name) if !name.eq_ignore_ascii_case("temp") => Some(Ok(DatabaseInfo { name })),
|
||||
Ok(_) => None,
|
||||
Err(error) => Some(Err(error)),
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.map_err(|e| e.to_string())
|
||||
})
|
||||
})
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
|
||||
pub async fn list_tables(pool: &SqliteHandle, schema: &str) -> Result<Vec<TableInfo>, String> {
|
||||
let pool = pool.clone();
|
||||
let schema = sqlite_schema_name(schema).to_string();
|
||||
tokio::task::spawn_blocking(move || {
|
||||
pool.with_connection(|conn| {
|
||||
let sql = format!(
|
||||
"SELECT name, type FROM {}.sqlite_master \
|
||||
WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%' ORDER BY name",
|
||||
sqlite_quote_ident(&schema)
|
||||
);
|
||||
let mut stmt = conn.prepare(&sql).map_err(|e| e.to_string())?;
|
||||
let rows = stmt
|
||||
.query_map([], |row| {
|
||||
let table_type: String = row.get(1)?;
|
||||
@@ -1113,13 +1253,14 @@ pub async fn list_tables(pool: &SqliteHandle, _schema: &str) -> Result<Vec<Table
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
|
||||
pub async fn get_columns(pool: &SqliteHandle, _schema: &str, table: &str) -> Result<Vec<ColumnInfo>, String> {
|
||||
pub async fn get_columns(pool: &SqliteHandle, schema: &str, table: &str) -> Result<Vec<ColumnInfo>, String> {
|
||||
let pool = pool.clone();
|
||||
let schema = sqlite_schema_name(schema).to_string();
|
||||
let table = table.to_string();
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let sql = format!("PRAGMA table_info(\"{}\")", table.replace('"', "\"\""));
|
||||
let sql = format!("PRAGMA {}.table_info({})", sqlite_quote_ident(&schema), sqlite_quote_string(&table));
|
||||
pool.with_connection(|conn| {
|
||||
let autoincrement_columns = sqlite_autoincrement_pk_columns(conn, &table).unwrap_or_default();
|
||||
let autoincrement_columns = sqlite_autoincrement_pk_columns(conn, &schema, &table).unwrap_or_default();
|
||||
let mut stmt = conn.prepare(&sql).map_err(|e| e.to_string())?;
|
||||
let rows = stmt
|
||||
.query_map([], |row| {
|
||||
@@ -1379,10 +1520,23 @@ fn sqlite_completion_like_pattern(request: &CompletionAssistantRequest) -> Strin
|
||||
}
|
||||
}
|
||||
|
||||
fn sqlite_quote_ident(value: &str) -> String {
|
||||
pub(crate) fn sqlite_quote_ident(value: &str) -> String {
|
||||
format!("\"{}\"", value.replace('"', "\"\""))
|
||||
}
|
||||
|
||||
pub(crate) fn sqlite_quote_schema_ident(value: &str) -> String {
|
||||
sqlite_quote_ident(sqlite_schema_name(value))
|
||||
}
|
||||
|
||||
fn sqlite_schema_name(value: &str) -> &str {
|
||||
let value = value.trim();
|
||||
if value.is_empty() {
|
||||
"main"
|
||||
} else {
|
||||
value
|
||||
}
|
||||
}
|
||||
|
||||
fn sqlite_quote_string(value: &str) -> String {
|
||||
format!("'{}'", value.replace('\'', "''"))
|
||||
}
|
||||
@@ -1391,11 +1545,10 @@ fn sqlite_quote_string(value: &str) -> String {
|
||||
/// are rowid-alias autoincrement primary keys (i.e. SQLite will assign a value when
|
||||
/// the column is omitted from an INSERT). Returns `None` only on connection / query
|
||||
/// errors; an unparseable build statement yields `Some(empty)`.
|
||||
fn sqlite_autoincrement_pk_columns(conn: &Connection, table: &str) -> Option<HashSet<String>> {
|
||||
let create_sql: Option<String> = conn
|
||||
.query_row("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?1", [table], |row| row.get(0))
|
||||
.ok()
|
||||
.flatten();
|
||||
fn sqlite_autoincrement_pk_columns(conn: &Connection, schema: &str, table: &str) -> Option<HashSet<String>> {
|
||||
let sql =
|
||||
format!("SELECT sql FROM {}.sqlite_master WHERE type = 'table' AND name = ?1", sqlite_quote_ident(schema));
|
||||
let create_sql: Option<String> = conn.query_row(&sql, [table], |row| row.get(0)).ok().flatten();
|
||||
Some(parse_sqlite_autoincrement_pk_columns(create_sql.as_deref()?))
|
||||
}
|
||||
|
||||
@@ -1946,13 +2099,15 @@ fn is_sql_keyword(value: &str) -> bool {
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn list_indexes(pool: &SqliteHandle, _schema: &str, table: &str) -> Result<Vec<IndexInfo>, String> {
|
||||
pub async fn list_indexes(pool: &SqliteHandle, schema: &str, table: &str) -> Result<Vec<IndexInfo>, String> {
|
||||
let pool = pool.clone();
|
||||
let schema = sqlite_schema_name(schema).to_string();
|
||||
let table = table.to_string();
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let safe_table = table.replace('"', "\"\"");
|
||||
pool.with_connection(|conn| {
|
||||
let mut stmt = conn.prepare(&format!("PRAGMA index_list(\"{safe_table}\")")).map_err(|e| e.to_string())?;
|
||||
let mut stmt = conn
|
||||
.prepare(&format!("PRAGMA {}.index_list({})", sqlite_quote_ident(&schema), sqlite_quote_string(&table)))
|
||||
.map_err(|e| e.to_string())?;
|
||||
let idx_rows = stmt
|
||||
.query_map([], |row| {
|
||||
Ok((
|
||||
@@ -1967,9 +2122,13 @@ pub async fn list_indexes(pool: &SqliteHandle, _schema: &str, table: &str) -> Re
|
||||
|
||||
let mut indexes = Vec::new();
|
||||
for (name, is_unique, origin) in idx_rows {
|
||||
let safe_name = name.replace('"', "\"\"");
|
||||
let mut col_stmt =
|
||||
conn.prepare(&format!("PRAGMA index_info(\"{safe_name}\")")).map_err(|e| e.to_string())?;
|
||||
let mut col_stmt = conn
|
||||
.prepare(&format!(
|
||||
"PRAGMA {}.index_info({})",
|
||||
sqlite_quote_ident(&schema),
|
||||
sqlite_quote_string(&name)
|
||||
))
|
||||
.map_err(|e| e.to_string())?;
|
||||
let columns = col_stmt
|
||||
.query_map([], |row| row.get::<_, String>("name"))
|
||||
.map_err(|e| e.to_string())?
|
||||
@@ -1994,11 +2153,12 @@ pub async fn list_indexes(pool: &SqliteHandle, _schema: &str, table: &str) -> Re
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
|
||||
pub async fn list_foreign_keys(pool: &SqliteHandle, _schema: &str, table: &str) -> Result<Vec<ForeignKeyInfo>, String> {
|
||||
pub async fn list_foreign_keys(pool: &SqliteHandle, schema: &str, table: &str) -> Result<Vec<ForeignKeyInfo>, String> {
|
||||
let pool = pool.clone();
|
||||
let schema = sqlite_schema_name(schema).to_string();
|
||||
let table = table.to_string();
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let sql = format!("PRAGMA foreign_key_list(\"{}\")", table.replace('"', "\"\""));
|
||||
let sql = format!("PRAGMA {}.foreign_key_list({})", sqlite_quote_ident(&schema), sqlite_quote_string(&table));
|
||||
pool.with_connection(|conn| {
|
||||
let mut stmt = conn.prepare(&sql).map_err(|e| e.to_string())?;
|
||||
let rows = stmt
|
||||
@@ -2021,14 +2181,17 @@ pub async fn list_foreign_keys(pool: &SqliteHandle, _schema: &str, table: &str)
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
|
||||
pub async fn list_triggers(pool: &SqliteHandle, _schema: &str, table: &str) -> Result<Vec<TriggerInfo>, String> {
|
||||
pub async fn list_triggers(pool: &SqliteHandle, schema: &str, table: &str) -> Result<Vec<TriggerInfo>, String> {
|
||||
let pool = pool.clone();
|
||||
let schema = sqlite_schema_name(schema).to_string();
|
||||
let table = table.to_string();
|
||||
tokio::task::spawn_blocking(move || {
|
||||
pool.with_connection(|conn| {
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT name, sql FROM sqlite_master WHERE type = 'trigger' AND tbl_name = ? ORDER BY name")
|
||||
.map_err(|e| e.to_string())?;
|
||||
let sql = format!(
|
||||
"SELECT name, sql FROM {}.sqlite_master WHERE type = 'trigger' AND tbl_name = ? ORDER BY name",
|
||||
sqlite_quote_ident(&schema)
|
||||
);
|
||||
let mut stmt = conn.prepare(&sql).map_err(|e| e.to_string())?;
|
||||
let rows = stmt
|
||||
.query_map([table], |row| {
|
||||
let sql_text: Option<String> = row.get("sql")?;
|
||||
|
||||
@@ -69,6 +69,13 @@ pub struct DuckDbAttachDatabaseSqlOptions {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SqliteAttachDatabaseSqlOptions {
|
||||
pub path: String,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DropObjectSqlOptions {
|
||||
@@ -303,6 +310,14 @@ pub fn build_duckdb_attach_database_sql(options: DuckDbAttachDatabaseSqlOptions)
|
||||
)
|
||||
}
|
||||
|
||||
pub fn build_sqlite_attach_database_sql(options: SqliteAttachDatabaseSqlOptions) -> String {
|
||||
format!(
|
||||
"ATTACH DATABASE {} AS {};",
|
||||
quote_sql_string(&options.path),
|
||||
quote_table_identifier(Some(DatabaseType::Sqlite), &options.name)
|
||||
)
|
||||
}
|
||||
|
||||
pub fn build_create_user_sql(username: &str, password: &str, tablespace: &str) -> String {
|
||||
format!(
|
||||
"CREATE USER {} IDENTIFIED BY {} DEFAULT TABLESPACE {};",
|
||||
@@ -388,6 +403,7 @@ pub fn build_drop_table_child_object_sql(options: DropTableChildObjectSqlOptions
|
||||
| DatabaseType::Dameng
|
||||
| DatabaseType::OceanbaseOracle
|
||||
| DatabaseType::Iris
|
||||
| DatabaseType::Sqlite
|
||||
)
|
||||
) && options.schema.as_deref().is_some_and(|schema| !schema.is_empty())
|
||||
{
|
||||
@@ -1060,6 +1076,17 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builds_sqlite_attach_sql() {
|
||||
assert_eq!(
|
||||
build_sqlite_attach_database_sql(SqliteAttachDatabaseSqlOptions {
|
||||
path: "/Users/me/O'Reilly data.sqlite".to_string(),
|
||||
name: "report db".to_string(),
|
||||
}),
|
||||
"ATTACH DATABASE '/Users/me/O''Reilly data.sqlite' AS \"report db\";"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builds_dameng_create_user_sql_with_escaped_values() {
|
||||
assert_eq!(
|
||||
@@ -1319,6 +1346,17 @@ mod tests {
|
||||
.unwrap(),
|
||||
"DROP INDEX \"public\".\"idx_orders_status\";"
|
||||
);
|
||||
assert_eq!(
|
||||
build_drop_table_child_object_sql(DropTableChildObjectSqlOptions {
|
||||
database_type: Some(DatabaseType::Sqlite),
|
||||
object_type: TableChildObjectType::Index,
|
||||
schema: Some("analytics".to_string()),
|
||||
table_name: "orders".to_string(),
|
||||
name: "idx_orders_status".to_string(),
|
||||
})
|
||||
.unwrap(),
|
||||
"DROP INDEX \"analytics\".\"idx_orders_status\";"
|
||||
);
|
||||
assert_eq!(
|
||||
build_drop_table_child_object_sql(DropTableChildObjectSqlOptions {
|
||||
database_type: Some(DatabaseType::Mysql),
|
||||
|
||||
@@ -4938,7 +4938,7 @@ pub async fn get_table_ddl_core(
|
||||
}
|
||||
}
|
||||
PoolKind::Postgres(p) => pg_ddl(p, schema, table).await,
|
||||
PoolKind::Sqlite(p) => sqlite_ddl(p, table).await,
|
||||
PoolKind::Sqlite(p) => sqlite_ddl(p, schema, table).await,
|
||||
PoolKind::Rqlite(client) => db::rqlite_driver::table_ddl(client, table).await,
|
||||
PoolKind::CloudflareD1(client) => db::cloudflare_d1_driver::table_ddl(client, table).await,
|
||||
_ => Err("DDL not supported for this database type".to_string()),
|
||||
@@ -5220,9 +5220,10 @@ pub fn oracle_object_source_sql(schema: &str, name: &str, kind: &db::ObjectSourc
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sqlite_object_source_sql(name: &str, kind: &db::ObjectSourceKind) -> String {
|
||||
pub fn sqlite_object_source_sql(schema: &str, name: &str, kind: &db::ObjectSourceKind) -> String {
|
||||
format!(
|
||||
"SELECT sql FROM sqlite_master WHERE type = {} AND name = {}",
|
||||
"SELECT sql FROM {}.sqlite_master WHERE type = {} AND name = {}",
|
||||
db::sqlite::sqlite_quote_schema_ident(schema),
|
||||
sql_string(sqlite_object_type(kind)),
|
||||
sql_string(name)
|
||||
)
|
||||
@@ -5381,7 +5382,7 @@ async fn get_object_source_once(
|
||||
}
|
||||
PoolKind::Postgres(pool) => postgres_object_source(pool, schema, name, &object_type, signature).await?,
|
||||
PoolKind::Sqlite(pool) => first_string_cell(
|
||||
db::sqlite::execute_query(pool, &sqlite_object_source_sql(name, &object_type)).await?,
|
||||
db::sqlite::execute_query(pool, &sqlite_object_source_sql(schema, name, &object_type)).await?,
|
||||
)?,
|
||||
#[cfg(feature = "duckdb-bundled")]
|
||||
PoolKind::DuckDb(con) => {
|
||||
@@ -6209,15 +6210,14 @@ fn ensure_display_ddl_terminated(sql: String) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn sqlite_ddl(pool: &db::sqlite::SqliteHandle, table: &str) -> Result<String, String> {
|
||||
pub async fn sqlite_ddl(pool: &db::sqlite::SqliteHandle, schema: &str, table: &str) -> Result<String, String> {
|
||||
let pool = pool.clone();
|
||||
let schema = db::sqlite::sqlite_quote_schema_ident(schema);
|
||||
let table = table.to_string();
|
||||
tokio::task::spawn_blocking(move || {
|
||||
pool.with_connection(|conn| {
|
||||
conn.query_row("SELECT sql FROM sqlite_master WHERE type='table' AND name=?1", [table], |row| {
|
||||
row.get::<_, String>(0)
|
||||
})
|
||||
.map_err(|e| e.to_string())
|
||||
let sql = format!("SELECT sql FROM {}.sqlite_master WHERE type='table' AND name=?1", schema);
|
||||
conn.query_row(&sql, [table], |row| row.get::<_, String>(0)).map_err(|e| e.to_string())
|
||||
})
|
||||
})
|
||||
.await
|
||||
|
||||
@@ -234,7 +234,7 @@ pub(in crate::schema) async fn table_ddl(
|
||||
}
|
||||
}
|
||||
PoolKind::Postgres(p) => super::super::pg_ddl(p, schema, table).await,
|
||||
PoolKind::Sqlite(p) => super::super::sqlite_ddl(p, table).await,
|
||||
PoolKind::Sqlite(p) => super::super::sqlite_ddl(p, schema, table).await,
|
||||
PoolKind::Rqlite(client) => db::rqlite_driver::table_ddl(client, table).await,
|
||||
PoolKind::Turso(client) => db::turso_driver::table_ddl(client, table).await,
|
||||
_ => Err("DDL not supported for this database type".to_string()),
|
||||
@@ -262,7 +262,11 @@ pub(in crate::schema) async fn object_source(
|
||||
}
|
||||
PoolKind::Sqlite(pool) => {
|
||||
let source = super::super::first_string_cell(
|
||||
db::sqlite::execute_query(pool, &super::super::sqlite_object_source_sql(name, object_type)).await?,
|
||||
db::sqlite::execute_query(
|
||||
pool,
|
||||
&super::super::sqlite_object_source_sql(schema, name, object_type),
|
||||
)
|
||||
.await?,
|
||||
)?;
|
||||
Ok(Some(source))
|
||||
}
|
||||
|
||||
@@ -56,6 +56,7 @@ pub fn is_schema_aware(database_type: DatabaseType) -> bool {
|
||||
| DatabaseType::Db2
|
||||
| DatabaseType::Tdengine
|
||||
| DatabaseType::Xugu
|
||||
| DatabaseType::Sqlite
|
||||
| DatabaseType::DuckDb
|
||||
| DatabaseType::Iris
|
||||
)
|
||||
|
||||
@@ -50,6 +50,7 @@ fn qualifies_schema_only_for_schema_aware_databases() {
|
||||
"\"DBX_TEST\".\"PRODUCTS\""
|
||||
);
|
||||
assert_eq!(qualified_table_name(Some(DatabaseType::Oscar), Some("SYSDBA"), "EMPLOYEE"), "\"SYSDBA\".\"EMPLOYEE\"");
|
||||
assert_eq!(qualified_table_name(Some(DatabaseType::Sqlite), Some("analytics"), "users"), "\"analytics\".\"users\"");
|
||||
assert_eq!(qualified_table_name(Some(DatabaseType::Jdbc), Some("cbsdw_dwd"), "dwd_test_df"), "dwd_test_df");
|
||||
assert_eq!(qualified_table_name(Some(DatabaseType::Iotdb), Some("root.test"), "device2"), "root.test.device2");
|
||||
assert_eq!(
|
||||
|
||||
@@ -131,7 +131,11 @@ pub(super) fn build_drop_index_sql(
|
||||
}
|
||||
if matches!(
|
||||
dialect,
|
||||
StructureDialect::Postgres | StructureDialect::Oracle | StructureDialect::Dameng | StructureDialect::Informix
|
||||
StructureDialect::Postgres
|
||||
| StructureDialect::Oracle
|
||||
| StructureDialect::Dameng
|
||||
| StructureDialect::Informix
|
||||
| StructureDialect::Sqlite
|
||||
) && schema.is_some_and(|schema| !schema.trim().is_empty())
|
||||
{
|
||||
return format!("DROP INDEX {}.{};", quote_ident(dialect, schema.unwrap()), quote_ident(dialect, index_name));
|
||||
@@ -200,15 +204,22 @@ pub(super) fn build_create_index_statements(
|
||||
let supports_where = capabilities.index_filter
|
||||
&& matches!(dialect, StructureDialect::Postgres | StructureDialect::SqlServer | StructureDialect::Sqlite);
|
||||
let where_clause = if !filter.is_empty() && supports_where { format!(" WHERE {filter}") } else { String::new() };
|
||||
let index_name = if dialect == StructureDialect::Sqlite && schema.is_some_and(|schema| !schema.trim().is_empty()) {
|
||||
format!("{}.{}", quote_ident(dialect, schema.unwrap()), quote_ident(dialect, &name))
|
||||
} else {
|
||||
quote_ident(dialect, &name)
|
||||
};
|
||||
// SQLite assigns an index to the database named by the qualified index.
|
||||
// Its CREATE INDEX grammar does not allow a qualified table name.
|
||||
let create_table =
|
||||
if dialect == StructureDialect::Sqlite { quote_ident(dialect, table_name) } else { table.to_string() };
|
||||
let create_sql = if dialect == StructureDialect::Postgres {
|
||||
format!(
|
||||
"CREATE {unique}{type_prefix}INDEX {} ON {table}{using_clause} ({cols}){include_clause}{where_clause};",
|
||||
quote_ident(dialect, &name)
|
||||
"CREATE {unique}{type_prefix}INDEX {index_name} ON {create_table}{using_clause} ({cols}){include_clause}{where_clause};"
|
||||
)
|
||||
} else {
|
||||
format!(
|
||||
"CREATE {unique}{type_prefix}INDEX {}{using_clause} ON {table} ({cols}){include_clause}{where_clause}{comment_clause};",
|
||||
quote_ident(dialect, &name)
|
||||
"CREATE {unique}{type_prefix}INDEX {index_name}{using_clause} ON {create_table} ({cols}){include_clause}{where_clause}{comment_clause};"
|
||||
)
|
||||
};
|
||||
let mut statements = vec![create_sql];
|
||||
|
||||
@@ -129,9 +129,6 @@ fn build_change_plan(conn: &mut Connection, options: &TableStructureSqlOptions)
|
||||
return Err("SQLite table rebuild requires databaseType=sqlite.".to_string());
|
||||
}
|
||||
let schema = options.schema.as_deref().map(str::trim).filter(|schema| !schema.is_empty()).unwrap_or("main");
|
||||
if !schema.eq_ignore_ascii_case("main") {
|
||||
return Err("SQLite table rebuild currently supports the native main schema only.".to_string());
|
||||
}
|
||||
if options.table_name.trim().is_empty() {
|
||||
return Err("SQLite table name cannot be empty.".to_string());
|
||||
}
|
||||
@@ -216,7 +213,7 @@ fn build_change_plan(conn: &mut Connection, options: &TableStructureSqlOptions)
|
||||
|
||||
let mut generic_options = options.clone();
|
||||
generic_options.database_type = Some(DatabaseType::Sqlite);
|
||||
generic_options.schema = None;
|
||||
generic_options.schema = (!schema.eq_ignore_ascii_case("main")).then(|| schema.to_string());
|
||||
generic_options.triggers.clear();
|
||||
for column in &mut generic_options.columns {
|
||||
if let Some(original) = &column.original {
|
||||
@@ -395,7 +392,7 @@ fn load_schema_snapshot(conn: &Connection, schema: &str, table_name: &str) -> Re
|
||||
drop(dependency_stmt);
|
||||
|
||||
let schema_version: i64 = conn
|
||||
.pragma_query_value(Some(rusqlite::DatabaseName::Main), "schema_version", |row| row.get(0))
|
||||
.query_row(&format!("PRAGMA {schema_ident}.schema_version"), [], |row| row.get(0))
|
||||
.map_err(|error| format!("Failed to read SQLite schema version: {error}"))?;
|
||||
let autoincrement_sequence = if contains_sql_keyword(&table_sql, "autoincrement") {
|
||||
conn.query_row(
|
||||
@@ -481,8 +478,16 @@ fn build_rebuild_statements(
|
||||
replaced_index_names: &HashSet<String>,
|
||||
) -> Result<Vec<String>, String> {
|
||||
let mut create_replacement = rewrite_declared_column_types(&snapshot.table_sql, type_changes)?;
|
||||
let (_, body_end) = find_create_table_body_span(&create_replacement)
|
||||
let (body_start, body_end) = find_create_table_body_span(&create_replacement)
|
||||
.ok_or_else(|| "SQLite CREATE TABLE statement could not be parsed safely.".to_string())?;
|
||||
let without_rowid = has_without_rowid_clause(&create_replacement[body_end + 1..]);
|
||||
if !snapshot.schema.eq_ignore_ascii_case("main") {
|
||||
create_replacement = format!(
|
||||
"CREATE TABLE {}{}",
|
||||
qualified_name(&snapshot.schema, &snapshot.table_name),
|
||||
&create_replacement[body_start..]
|
||||
);
|
||||
}
|
||||
ensure_statement_terminated(&mut create_replacement);
|
||||
let backup_seed = u64::from_str_radix(&snapshot.revision[..15], 16)
|
||||
.map_err(|error| format!("Failed to derive SQLite backup table suffix: {error}"))?;
|
||||
@@ -508,7 +513,6 @@ fn build_rebuild_statements(
|
||||
let qualified_backup = qualified_name(&snapshot.schema, &backup_name);
|
||||
let qualified_source = qualified_name(&snapshot.schema, &source_name);
|
||||
|
||||
let without_rowid = has_without_rowid_clause(&create_replacement[body_end + 1..]);
|
||||
let visible_columns = snapshot.columns.iter().filter(|column| column.hidden == 0).collect::<Vec<_>>();
|
||||
let live_names = snapshot.columns.iter().map(|column| column.name.to_ascii_lowercase()).collect::<HashSet<_>>();
|
||||
let rowid_alias = if without_rowid {
|
||||
@@ -581,19 +585,23 @@ fn build_rebuild_statements(
|
||||
));
|
||||
}
|
||||
statements.push(format!("DROP TABLE {qualified_source};"));
|
||||
statements.extend(
|
||||
snapshot
|
||||
.dependencies
|
||||
.iter()
|
||||
.filter(|dependency| {
|
||||
dependency.kind != "index" || !replaced_index_names.contains(&dependency.name.to_ascii_lowercase())
|
||||
})
|
||||
.map(|dependency| {
|
||||
let mut sql = dependency.sql.clone();
|
||||
ensure_statement_terminated(&mut sql);
|
||||
sql
|
||||
}),
|
||||
);
|
||||
let dependency_statements = snapshot
|
||||
.dependencies
|
||||
.iter()
|
||||
.filter(|dependency| {
|
||||
dependency.kind != "index" || !replaced_index_names.contains(&dependency.name.to_ascii_lowercase())
|
||||
})
|
||||
.map(|dependency| -> Result<String, String> {
|
||||
let mut sql = if snapshot.schema.eq_ignore_ascii_case("main") {
|
||||
dependency.sql.clone()
|
||||
} else {
|
||||
qualify_sqlite_create_object_sql(&dependency.sql, &dependency.kind, &snapshot.schema, &dependency.name)?
|
||||
};
|
||||
ensure_statement_terminated(&mut sql);
|
||||
Ok(sql)
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
statements.extend(dependency_statements);
|
||||
Ok(statements)
|
||||
}
|
||||
|
||||
@@ -963,7 +971,7 @@ fn parse_column_type_span(entry: &str) -> Option<(String, usize, usize)> {
|
||||
fn parse_identifier(value: &str, start: usize) -> Option<(String, usize, bool)> {
|
||||
let bytes = value.as_bytes();
|
||||
let first = *bytes.get(start)?;
|
||||
if matches!(first, b'"' | b'`' | b'[') {
|
||||
if matches!(first, b'\'' | b'"' | b'`' | b'[') {
|
||||
let close = if first == b'[' { b']' } else { first };
|
||||
let mut decoded = String::new();
|
||||
let mut index = start + 1;
|
||||
@@ -1276,6 +1284,153 @@ fn sql_words_outside_comments_and_quotes(sql: &str) -> Vec<String> {
|
||||
words
|
||||
}
|
||||
|
||||
fn qualify_sqlite_create_object_sql(
|
||||
sql: &str,
|
||||
kind: &str,
|
||||
schema: &str,
|
||||
expected_name: &str,
|
||||
) -> Result<String, String> {
|
||||
let keyword = match kind.to_ascii_lowercase().as_str() {
|
||||
"index" => "index",
|
||||
"trigger" => "trigger",
|
||||
_ => return Err(format!("Unsupported SQLite dependency kind \"{kind}\".")),
|
||||
};
|
||||
let keyword_end = find_sql_keyword_end(sql, keyword)
|
||||
.ok_or_else(|| format!("SQLite {kind} definition does not contain a {keyword} keyword."))?;
|
||||
let mut object_start = skip_space_and_comments(sql, keyword_end)
|
||||
.ok_or_else(|| format!("SQLite {kind} definition ended before the object name."))?;
|
||||
|
||||
if let Some((word, word_end, quoted)) = parse_identifier(sql, object_start) {
|
||||
if !quoted && word.eq_ignore_ascii_case("if") {
|
||||
let not_start = skip_space_and_comments(sql, word_end)
|
||||
.ok_or_else(|| format!("SQLite {kind} IF clause is incomplete."))?;
|
||||
let (not_word, not_end, not_quoted) =
|
||||
parse_identifier(sql, not_start).ok_or_else(|| format!("SQLite {kind} IF clause is incomplete."))?;
|
||||
let exists_start = skip_space_and_comments(sql, not_end)
|
||||
.ok_or_else(|| format!("SQLite {kind} IF NOT clause is incomplete."))?;
|
||||
let (exists_word, exists_end, exists_quoted) = parse_identifier(sql, exists_start)
|
||||
.ok_or_else(|| format!("SQLite {kind} IF NOT clause is incomplete."))?;
|
||||
if not_quoted
|
||||
|| exists_quoted
|
||||
|| !not_word.eq_ignore_ascii_case("not")
|
||||
|| !exists_word.eq_ignore_ascii_case("exists")
|
||||
{
|
||||
return Err(format!("SQLite {kind} definition has an invalid IF NOT EXISTS clause."));
|
||||
}
|
||||
object_start = skip_space_and_comments(sql, exists_end)
|
||||
.ok_or_else(|| format!("SQLite {kind} definition ended before the object name."))?;
|
||||
}
|
||||
}
|
||||
|
||||
let (first_name, first_end, first_quoted) = parse_identifier(sql, object_start)
|
||||
.ok_or_else(|| format!("SQLite {kind} definition has an invalid object name."))?;
|
||||
let mut actual_name = first_name.clone();
|
||||
let mut object_end = first_end;
|
||||
let after_first = skip_space_and_comments(sql, first_end)
|
||||
.ok_or_else(|| format!("SQLite {kind} definition has an invalid object name."))?;
|
||||
if sql.as_bytes().get(after_first) == Some(&b'.') {
|
||||
let second_start = skip_space_and_comments(sql, after_first + 1)
|
||||
.ok_or_else(|| format!("SQLite {kind} definition has an incomplete qualified name."))?;
|
||||
let (second_name, second_end, _) = parse_identifier(sql, second_start)
|
||||
.ok_or_else(|| format!("SQLite {kind} definition has an incomplete qualified name."))?;
|
||||
actual_name = second_name;
|
||||
object_end = second_end;
|
||||
} else if !first_quoted {
|
||||
actual_name = first_name.rsplit_once('.').map_or(first_name.as_str(), |(_, name)| name).to_string();
|
||||
}
|
||||
if !actual_name.eq_ignore_ascii_case(expected_name) {
|
||||
return Err(format!(
|
||||
"SQLite {kind} definition name \"{actual_name}\" does not match metadata name \"{expected_name}\"."
|
||||
));
|
||||
}
|
||||
|
||||
let mut qualified = sql.to_string();
|
||||
qualified.replace_range(object_start..object_end, &qualified_name(schema, expected_name));
|
||||
Ok(qualified)
|
||||
}
|
||||
|
||||
fn find_sql_keyword_end(sql: &str, keyword: &str) -> Option<usize> {
|
||||
let bytes = sql.as_bytes();
|
||||
let mut mode = ScanMode::Normal;
|
||||
let mut index = 0_usize;
|
||||
while index < bytes.len() {
|
||||
let byte = bytes[index];
|
||||
match mode {
|
||||
ScanMode::Normal => match byte {
|
||||
b'\'' => mode = ScanMode::SingleQuote,
|
||||
b'"' => mode = ScanMode::DoubleQuote,
|
||||
b'`' => mode = ScanMode::Backtick,
|
||||
b'[' => mode = ScanMode::Bracket,
|
||||
b'-' if bytes.get(index + 1) == Some(&b'-') => {
|
||||
mode = ScanMode::LineComment;
|
||||
index += 1;
|
||||
}
|
||||
b'/' if bytes.get(index + 1) == Some(&b'*') => {
|
||||
mode = ScanMode::BlockComment;
|
||||
index += 1;
|
||||
}
|
||||
_ if byte.is_ascii_alphabetic() || byte == b'_' => {
|
||||
let start = index;
|
||||
index += 1;
|
||||
while index < bytes.len() && (bytes[index].is_ascii_alphanumeric() || bytes[index] == b'_') {
|
||||
index += 1;
|
||||
}
|
||||
if sql[start..index].eq_ignore_ascii_case(keyword) {
|
||||
return Some(index);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
ScanMode::SingleQuote => {
|
||||
if byte == b'\'' {
|
||||
if bytes.get(index + 1) == Some(&b'\'') {
|
||||
index += 1;
|
||||
} else {
|
||||
mode = ScanMode::Normal;
|
||||
}
|
||||
}
|
||||
}
|
||||
ScanMode::DoubleQuote => {
|
||||
if byte == b'"' {
|
||||
if bytes.get(index + 1) == Some(&b'"') {
|
||||
index += 1;
|
||||
} else {
|
||||
mode = ScanMode::Normal;
|
||||
}
|
||||
}
|
||||
}
|
||||
ScanMode::Backtick => {
|
||||
if byte == b'`' {
|
||||
if bytes.get(index + 1) == Some(&b'`') {
|
||||
index += 1;
|
||||
} else {
|
||||
mode = ScanMode::Normal;
|
||||
}
|
||||
}
|
||||
}
|
||||
ScanMode::Bracket => {
|
||||
if byte == b']' {
|
||||
mode = ScanMode::Normal;
|
||||
}
|
||||
}
|
||||
ScanMode::LineComment => {
|
||||
if byte == b'\n' {
|
||||
mode = ScanMode::Normal;
|
||||
}
|
||||
}
|
||||
ScanMode::BlockComment => {
|
||||
if byte == b'*' && bytes.get(index + 1) == Some(&b'/') {
|
||||
mode = ScanMode::Normal;
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn quote_ident(value: &str) -> String {
|
||||
format!("\"{}\"", value.replace('"', "\"\""))
|
||||
}
|
||||
@@ -1479,6 +1634,221 @@ mod tests {
|
||||
assert_eq!(backup_data.rows[0], serde_json::json!(["text", "42"]).as_array().unwrap().clone());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rebuilds_attached_schema_without_touching_same_named_main_objects() {
|
||||
let pool = db::sqlite::connect_path(":memory:").await.unwrap();
|
||||
pool.with_connection(|conn| {
|
||||
conn.execute_batch(
|
||||
"PRAGMA foreign_keys=ON;
|
||||
ATTACH DATABASE ':memory:' AS analytics;
|
||||
CREATE TABLE main.audit(message TEXT);
|
||||
CREATE TABLE main.items(id INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT NOT NULL);
|
||||
CREATE INDEX main.idx_items_value ON items(value);
|
||||
CREATE TRIGGER main.trg_items_ai AFTER INSERT ON items
|
||||
BEGIN INSERT INTO audit VALUES ('main:' || NEW.value); END;
|
||||
INSERT INTO main.items(value) VALUES ('11');
|
||||
DELETE FROM main.audit;
|
||||
CREATE TABLE analytics.audit(message TEXT);
|
||||
CREATE TABLE analytics.items(id INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT NOT NULL);
|
||||
CREATE INDEX analytics.idx_items_value ON items(value);
|
||||
CREATE TRIGGER analytics.trg_items_ai AFTER INSERT ON items
|
||||
BEGIN INSERT INTO audit VALUES ('analytics:' || NEW.value); END;
|
||||
INSERT INTO analytics.items(id, value) VALUES (100, '42');
|
||||
DELETE FROM analytics.audit;",
|
||||
)
|
||||
.map_err(|error| error.to_string())
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let mut options = type_change_options("items", "value", "TEXT", "INTEGER");
|
||||
options.schema = Some("analytics".to_string());
|
||||
options.columns.push(EditableStructureColumn {
|
||||
id: "note".to_string(),
|
||||
name: "note".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
default_value: String::new(),
|
||||
comment: String::new(),
|
||||
is_primary_key: false,
|
||||
extra: None,
|
||||
original: None,
|
||||
original_position: None,
|
||||
marked_for_drop: false,
|
||||
character_set: String::new(),
|
||||
collation: String::new(),
|
||||
});
|
||||
options.indexes.push(EditableStructureIndex {
|
||||
id: "idx_items_note".to_string(),
|
||||
name: "idx_items_note".to_string(),
|
||||
columns: vec!["note".to_string()],
|
||||
is_unique: false,
|
||||
is_primary: false,
|
||||
filter: String::new(),
|
||||
index_type: String::new(),
|
||||
included_columns: Vec::new(),
|
||||
comment: String::new(),
|
||||
original: None,
|
||||
marked_for_drop: false,
|
||||
});
|
||||
let preview = preview_sqlite_table_structure_change_with_pool(pool.clone(), options.clone()).await.unwrap();
|
||||
assert!(preview.warnings.is_empty(), "{:?}", preview.warnings);
|
||||
assert!(preview.statements.iter().any(|sql| sql.starts_with("CREATE TABLE \"analytics\".\"items\"")));
|
||||
assert!(preview.statements.iter().any(|sql| sql.starts_with("CREATE INDEX \"analytics\".\"idx_items_value\"")));
|
||||
assert!(preview.statements.iter().any(|sql| sql.starts_with("CREATE TRIGGER \"analytics\".\"trg_items_ai\"")));
|
||||
|
||||
// A main-schema change after preview must not invalidate an attached-schema revision.
|
||||
db::sqlite::execute_query(&pool, "CREATE TABLE main.unrelated(id INTEGER);").await.unwrap();
|
||||
apply_sqlite_table_structure_change_with_pool(pool.clone(), options, &preview.schema_revision).await.unwrap();
|
||||
|
||||
let main_value =
|
||||
db::sqlite::execute_query(&pool, "SELECT typeof(value), value FROM main.items;").await.unwrap();
|
||||
assert_eq!(main_value.rows[0], serde_json::json!(["text", "11"]).as_array().unwrap().clone());
|
||||
let attached_value =
|
||||
db::sqlite::execute_query(&pool, "SELECT typeof(value), value FROM analytics.items;").await.unwrap();
|
||||
assert_eq!(attached_value.rows[0], serde_json::json!(["integer", 42]).as_array().unwrap().clone());
|
||||
let main_note = db::sqlite::execute_query(
|
||||
&pool,
|
||||
"SELECT count(*) FROM pragma_table_info('items', 'main') WHERE name='note';",
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(main_note.rows[0][0], serde_json::json!(0));
|
||||
let attached_note = db::sqlite::execute_query(
|
||||
&pool,
|
||||
"SELECT count(*) FROM pragma_table_info('items', 'analytics') WHERE name='note';",
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(attached_note.rows[0][0], serde_json::json!(1));
|
||||
let attached_note_index = db::sqlite::execute_query(
|
||||
&pool,
|
||||
"SELECT count(*) FROM analytics.sqlite_schema WHERE type='index' AND name='idx_items_note';",
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(attached_note_index.rows[0][0], serde_json::json!(1));
|
||||
let main_note_index = db::sqlite::execute_query(
|
||||
&pool,
|
||||
"SELECT count(*) FROM main.sqlite_schema WHERE type='index' AND name='idx_items_note';",
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(main_note_index.rows[0][0], serde_json::json!(0));
|
||||
|
||||
db::sqlite::execute_query(&pool, "INSERT INTO analytics.items(value) VALUES (7);").await.unwrap();
|
||||
let main_audit = db::sqlite::execute_query(&pool, "SELECT count(*) FROM main.audit;").await.unwrap();
|
||||
assert_eq!(main_audit.rows[0][0], serde_json::json!(0));
|
||||
let attached_audit = db::sqlite::execute_query(&pool, "SELECT message FROM analytics.audit;").await.unwrap();
|
||||
assert_eq!(attached_audit.rows[0][0], serde_json::json!("analytics:7"));
|
||||
let attached_max_id = db::sqlite::execute_query(&pool, "SELECT max(id) FROM analytics.items;").await.unwrap();
|
||||
assert_eq!(attached_max_id.rows[0][0], serde_json::json!(101));
|
||||
|
||||
for schema in ["main", "analytics"] {
|
||||
let dependencies = db::sqlite::execute_query(
|
||||
&pool,
|
||||
&format!(
|
||||
"SELECT type, name FROM {schema}.sqlite_schema \
|
||||
WHERE name IN ('idx_items_value', 'trg_items_ai') ORDER BY type;"
|
||||
),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(dependencies.rows.len(), 2);
|
||||
}
|
||||
let main_backups = db::sqlite::execute_query(
|
||||
&pool,
|
||||
"SELECT count(*) FROM main.sqlite_schema WHERE type='table' AND name GLOB 'items_[0-9]*';",
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(main_backups.rows[0][0], serde_json::json!(0));
|
||||
let attached_backups = db::sqlite::execute_query(
|
||||
&pool,
|
||||
"SELECT count(*) FROM analytics.sqlite_schema WHERE type='table' AND name GLOB 'items_[0-9]*';",
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(attached_backups.rows[0][0], serde_json::json!(1));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rebuilds_keyword_named_attached_schema_with_single_quoted_dependencies() {
|
||||
let pool = db::sqlite::connect_path(":memory:").await.unwrap();
|
||||
db::sqlite::execute_query(
|
||||
&pool,
|
||||
"ATTACH DATABASE ':memory:' AS \"select\";
|
||||
CREATE TABLE \"select\".audit(message TEXT);
|
||||
CREATE TABLE \"select\".items(value TEXT NOT NULL);
|
||||
CREATE INDEX \"select\".'idx weird' ON items(value);
|
||||
CREATE TRIGGER \"select\".'trg weird' AFTER INSERT ON items
|
||||
BEGIN INSERT INTO audit VALUES ('attached:' || NEW.value); END;
|
||||
INSERT INTO \"select\".items VALUES ('42');
|
||||
DELETE FROM \"select\".audit;",
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let definitions = db::sqlite::execute_query(
|
||||
&pool,
|
||||
"SELECT sql FROM \"select\".sqlite_schema
|
||||
WHERE name IN ('idx weird', 'trg weird') ORDER BY type;",
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(definitions.rows.len(), 2);
|
||||
assert!(definitions.rows.iter().all(|row| row[0].as_str().unwrap().contains("'")));
|
||||
|
||||
let mut options = type_change_options("items", "value", "TEXT", "INTEGER");
|
||||
options.schema = Some("select".to_string());
|
||||
let preview = preview_sqlite_table_structure_change_with_pool(pool.clone(), options.clone()).await.unwrap();
|
||||
assert!(preview.warnings.is_empty(), "{:?}", preview.warnings);
|
||||
assert!(preview.statements.iter().any(|sql| sql.starts_with("CREATE TABLE \"select\".\"items\"")));
|
||||
assert!(preview.statements.iter().any(|sql| sql.starts_with("CREATE INDEX \"select\".\"idx weird\"")));
|
||||
assert!(preview.statements.iter().any(|sql| sql.starts_with("CREATE TRIGGER \"select\".\"trg weird\"")));
|
||||
|
||||
apply_sqlite_table_structure_change_with_pool(pool.clone(), options, &preview.schema_revision).await.unwrap();
|
||||
|
||||
let value =
|
||||
db::sqlite::execute_query(&pool, "SELECT typeof(value), value FROM \"select\".items;").await.unwrap();
|
||||
assert_eq!(value.rows[0], serde_json::json!(["integer", 42]).as_array().unwrap().clone());
|
||||
db::sqlite::execute_query(&pool, "INSERT INTO \"select\".items VALUES (7);").await.unwrap();
|
||||
let audit = db::sqlite::execute_query(&pool, "SELECT message FROM \"select\".audit;").await.unwrap();
|
||||
assert_eq!(audit.rows[0][0], serde_json::json!("attached:7"));
|
||||
let dependencies = db::sqlite::execute_query(
|
||||
&pool,
|
||||
"SELECT type, name FROM \"select\".sqlite_schema
|
||||
WHERE name IN ('idx weird', 'trg weird') ORDER BY type;",
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(dependencies.rows.len(), 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn attached_schema_change_after_preview_is_rejected_before_mutation() {
|
||||
let pool = db::sqlite::connect_path(":memory:").await.unwrap();
|
||||
db::sqlite::execute_query(
|
||||
&pool,
|
||||
"ATTACH DATABASE ':memory:' AS analytics;
|
||||
CREATE TABLE analytics.items(value TEXT NOT NULL);
|
||||
INSERT INTO analytics.items VALUES ('42');",
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let mut options = type_change_options("items", "value", "TEXT", "INTEGER");
|
||||
options.schema = Some("analytics".to_string());
|
||||
let preview = preview_sqlite_table_structure_change_with_pool(pool.clone(), options.clone()).await.unwrap();
|
||||
|
||||
db::sqlite::execute_query(&pool, "CREATE TABLE analytics.unrelated(id INTEGER);").await.unwrap();
|
||||
let error = apply_sqlite_table_structure_change_with_pool(pool.clone(), options, &preview.schema_revision)
|
||||
.await
|
||||
.unwrap_err();
|
||||
|
||||
assert!(error.contains("schema changed"), "{error}");
|
||||
let value =
|
||||
db::sqlite::execute_query(&pool, "SELECT typeof(value), value FROM analytics.items;").await.unwrap();
|
||||
assert_eq!(value.rows[0], serde_json::json!(["text", "42"]).as_array().unwrap().clone());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn unrelated_existing_foreign_key_violation_does_not_block_rebuild() {
|
||||
let pool = db::sqlite::connect_path(":memory:").await.unwrap();
|
||||
|
||||
@@ -1237,6 +1237,82 @@ fn warns_for_sqlite_unsafe_column_changes() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qualifies_attached_sqlite_table_and_index_changes() {
|
||||
let mut email = column("email");
|
||||
email.data_type = "text".to_string();
|
||||
let mut old_index = index("idx_users_old", &["email"]);
|
||||
old_index.marked_for_drop = true;
|
||||
old_index.original = Some(IndexInfo {
|
||||
name: "idx_users_old".to_string(),
|
||||
columns: vec!["email".to_string()],
|
||||
is_unique: false,
|
||||
is_primary: false,
|
||||
filter: None,
|
||||
index_type: None,
|
||||
included_columns: None,
|
||||
comment: None,
|
||||
});
|
||||
let email_index = index("idx_users_email", &["email"]);
|
||||
|
||||
let result = build_table_structure_change_sql(TableStructureSqlOptions {
|
||||
database_type: Some(DatabaseType::Sqlite),
|
||||
schema: Some("analytics".to_string()),
|
||||
table_name: "users".to_string(),
|
||||
columns: vec![email],
|
||||
indexes: vec![old_index, email_index],
|
||||
foreign_keys: Vec::new(),
|
||||
triggers: Vec::new(),
|
||||
table_comment: None,
|
||||
original_table_comment: None,
|
||||
});
|
||||
|
||||
assert_eq!(result.warnings, Vec::<String>::new());
|
||||
assert_eq!(
|
||||
result.statements,
|
||||
vec![
|
||||
"ALTER TABLE \"analytics\".\"users\" ADD COLUMN \"email\" text;",
|
||||
"DROP INDEX \"analytics\".\"idx_users_old\";",
|
||||
"CREATE INDEX \"analytics\".\"idx_users_email\" ON \"users\" (\"email\");",
|
||||
]
|
||||
);
|
||||
|
||||
let connection = rusqlite::Connection::open_in_memory().unwrap();
|
||||
connection
|
||||
.execute_batch(
|
||||
"ATTACH DATABASE ':memory:' AS analytics;
|
||||
CREATE TABLE main.users(id INTEGER);
|
||||
CREATE TABLE analytics.users(id INTEGER);
|
||||
CREATE INDEX analytics.idx_users_old ON users(id);",
|
||||
)
|
||||
.unwrap();
|
||||
connection.execute_batch(&result.statements.join("\n")).unwrap();
|
||||
let main_columns = connection
|
||||
.prepare("PRAGMA main.table_info('users')")
|
||||
.unwrap()
|
||||
.query_map([], |row| row.get::<_, String>("name"))
|
||||
.unwrap()
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.unwrap();
|
||||
let attached_columns = connection
|
||||
.prepare("PRAGMA analytics.table_info('users')")
|
||||
.unwrap()
|
||||
.query_map([], |row| row.get::<_, String>("name"))
|
||||
.unwrap()
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.unwrap();
|
||||
let attached_indexes = connection
|
||||
.prepare("PRAGMA analytics.index_list('users')")
|
||||
.unwrap()
|
||||
.query_map([], |row| row.get::<_, String>("name"))
|
||||
.unwrap()
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.unwrap();
|
||||
assert_eq!(main_columns, vec!["id"]);
|
||||
assert_eq!(attached_columns, vec!["id", "email"]);
|
||||
assert_eq!(attached_indexes, vec!["idx_users_email"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builds_rqlite_changes_with_sqlite_dialect() {
|
||||
let mut email = column("email");
|
||||
|
||||
@@ -10,6 +10,7 @@ pub(super) fn qualified_table(dialect: StructureDialect, schema: Option<&str>, t
|
||||
| StructureDialect::SqlServer
|
||||
| StructureDialect::H2
|
||||
| StructureDialect::Informix
|
||||
| StructureDialect::Sqlite
|
||||
) && schema.is_some_and(|schema| !schema.trim().is_empty())
|
||||
{
|
||||
return format!("{}.{}", quote_ident(dialect, schema.unwrap()), quote_ident(dialect, table_name));
|
||||
|
||||
Reference in new issue
Block a user