Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast commit alter when non-destructive change is made #445

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions core/rs/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@ use sqlite_nostd as sqlite;
use sqlite_nostd::{Connection, Context, Value};
use tableinfo::{crsql_ensure_table_infos_are_up_to_date, is_table_compatible, pull_table_info};
use teardown::*;
use triggers::recreate_update_triggers;
use triggers::create_triggers;

pub extern "C" fn crsql_as_table(
ctx: *mut sqlite::context,
@@ -667,7 +667,7 @@ unsafe extern "C" fn x_crsql_commit_alter(
let rc = if non_destructive {
match pull_table_info(db, table_name, &mut err_msg as *mut _) {
Ok(table_info) => {
match recreate_update_triggers(db, &table_info, &mut err_msg as *mut _) {
match create_triggers(db, &table_info, &mut err_msg) {
Ok(ResultCode::OK) => {
// need to ensure the right table infos in ext data
crsql_ensure_table_infos_are_up_to_date(
27 changes: 0 additions & 27 deletions core/rs/core/src/triggers.rs
Original file line number Diff line number Diff line change
@@ -9,33 +9,6 @@ use sqlite_nostd as sqlite;

use crate::tableinfo::TableInfo;

pub fn recreate_update_triggers(
db: *mut sqlite3,
table_info: &TableInfo,
err: *mut *mut c_char,
) -> Result<ResultCode, ResultCode> {
db.exec_safe(&format!(
"DROP TRIGGER IF EXISTS \"{table}__crsql_utrig\"",
table = crate::util::escape_ident(&table_info.tbl_name)
))?;

// get all columns of table
// iterate pk cols
// drop triggers against those pk cols
let stmt = db.prepare_v2("SELECT name FROM pragma_table_info(?) WHERE pk > 0")?;
stmt.bind_text(1, &table_info.tbl_name, sqlite::Destructor::STATIC)?;
while stmt.step()? == ResultCode::ROW {
let col_name = stmt.column_text(0)?;
db.exec_safe(&format!(
"DROP TRIGGER IF EXISTS \"{tbl_name}_{col_name}__crsql_utrig\"",
tbl_name = crate::util::escape_ident(&table_info.tbl_name),
col_name = crate::util::escape_ident(col_name),
))?;
}

create_update_trigger(db, table_info, err)
}

pub fn create_triggers(
db: *mut sqlite3,
table_info: &TableInfo,
Loading