-
Notifications
You must be signed in to change notification settings - Fork 9
Create CRUD triggers for raw tables #163
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
base: main
Are you sure you want to change the base?
Conversation
rkistner
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add tests for the generated trigger SQL? Not sure if Dart has something like snapshot tests for this - it's fine if it changes over time, but it would be good to see the specific record of that.
Then I'm also wondering - is there a nice way to handle "local-only" columns like documented here? https://docs.powersync.com/client-sdks/advanced/raw-tables#exclude-local-only-columns-from-triggers (recently added). Otherwise we just need to be explicit that those are considered "advanced" use cases that needs manually-defined triggers.
There aren't vitest-like automated snapshots that I'm aware of, but I've added a few hardcoded examples as matches.
That was what I had in mind originally, but it seems like a fairly simple pattern to handle internally. I've added the |
This adds the
powersync_create_raw_table_crud_triggerfunction, taking three arguments:INSERT,UPDATEorDELETEfor which to generate a trigger). Typically users would create all three, but maybe they want the default trigger for some types only and customize the rest 🤷.The function generates and executes a
CREATE TRIGGERstatement mirroring our suggestions from the documentation. To do this,include_oldand boolean flags likeinsert_only) we already have for regular tables. Also, the optionallocal_namefield has been added to describe the name of the table in the local schema (sincenamein raw tables describes the sync optype to match).pragma_table_infoat the time the trigger is created.Triggers are not created automatically, and users have full control over their name. So this is a tool that users would invoke in their schema migrations if they can use the default patterns for triggers (which we assume to be the case in most scenarios).
The created trigger follows the scheme of the one we create for JSON-based views. Insert-only tables are implemented by raising on updates and deletes.
I've introduced the
SchemaTableenum to wrap either a JSON-based table or a raw tables + inferred column names. This allows some methods generating JSON fragments to be reused between the two. I've also removed type arguments onSqlBuffer::insert_into_powersync_crudin favor of&dynreferences to avoid monomorphization on that fairly large function.