Skip to content

Commit

Permalink
Merge pull request #3 from somtochiama/recreate-all-triggers
Browse files Browse the repository at this point in the history
Recreate all triggers when commit_alter is run for non-destructive changes
  • Loading branch information
jeromegn authored Dec 2, 2024
2 parents b4e278d + 9cd3b4c commit 0db37d3
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 29 deletions.
4 changes: 2 additions & 2 deletions core/rs/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
27 changes: 0 additions & 27 deletions core/rs/core/src/triggers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0db37d3

Please sign in to comment.