When running the exact same queries in the app, but outside a transaction, the trigger runs as expected. It's a "before update" trigger, which isn't deferrable even if we wanted to, so deferring isn't the problem. I'm so confused by this.
-
Show this thread
-
I figured it out. Update triggers don't fire for modifications to a row created in the current transaction. If you begin, insert a row, and modify that row, "before update" triggers won't fire. But if you insert a row, begin, and modify that row, the triggers will fire.
3 replies 0 retweets 9 likesShow this thread -
I am vexed.
1 reply 0 retweets 3 likesShow this thread -
I'm using this trigger to update the `updated_at` fields of tables whenever they change. A correct `updated_at` might theoretically be important, even inside a transaction where the row was created and then modified. So this seems like a big problem.
4 replies 0 retweets 2 likesShow this thread -
Admittedly, the probability of this mattering in production code is relatively small. My actual immediate problem is that all tests are run inside a transaction, and I want to write a test for this updated_at behavior, and I can't!
1 reply 0 retweets 4 likesShow this thread -
Gary Bernhardt Retweeted Miss Dada 🏳️⚧️
Apparently this was all wrong. My problem was that neither now() nor CURRENT_TIMESTAMP changes during a transaction. But there is clock_timestamp(), which does change. Thank you
@sgrif, as usual!https://twitter.com/sgrif/status/1111368222861746176 …Gary Bernhardt added,
6 replies 2 retweets 43 likesShow this thread -
Replying to @garybernhardt
FWIW I think using `CURRENT_TIMESTAMP` is what users would expect. Assuming you're testing the trigger in general, and not that it's created for specific tables, you can test by creating a new table in the test outside a transaction. https://github.com/diesel-rs/diesel/blob/50798e5dd52ae9d3c7c9a4bf7a5f1e5b9167aba3/diesel_tests/tests/connection.rs#L12-L75 …
1 reply 0 retweets 0 likes -
Replying to @sgrif @garybernhardt
(Our trigger and helper function https://github.com/diesel-rs/diesel/blob/50798e5dd52ae9d3c7c9a4bf7a5f1e5b9167aba3/diesel_cli/src/setup_sql/postgres/initial_setup/up.sql#L8-L36 …)
1 reply 0 retweets 0 likes -
Replying to @sgrif
I agree that using CURRENT_TIMESTAMP is the most sensible. I guess I can write this test to use raw SQL, create the temporary table like you suggest, but `drop table if exists` first to clean up any that get left over from previous test runs.
2 replies 0 retweets 1 like -
Replying to @garybernhardt @sgrif
Does Diesel provide any way to guarantee that this trigger is in place? Or would it let you create a purported `updated_at` column and forget to call that trigger-creation function?
1 reply 0 retweets 0 likes
The latter. Our migrations are just plain SQL files, so it's on you to remember to call `diesel_manage_updated_at` after creating the table. I like the idea of providing a function that tests if any tables exist with a column called `updated_at` but don't have the trigger
-
-
Replying to @sgrif @garybernhardt
An interesting side effect of using SQL files for migrations is that our CLI tool/migration infrastructure isn't actually coupled to Rust or Diesel at all
0 replies 0 retweets 1 likeThanks. Twitter will use this to make your timeline better. UndoUndo
-
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.