Using Dataset#with
(CTE) with deletion query
#2062
v-kolesnikov
started this conversation in
General
Replies: 1 comment 1 reply
-
DB[:deleted_records].
with(:deleted,
DB[:table1].
returning.
where(:id=>1).
with_sql(:delete_sql)
).
returning.
insert(
[:original_table, :original_id, :data],
DB[:deleted].
select{['table1', :id, to_json(deleted.*)]}.
returning()
) WITH "deleted" AS (
DELETE FROM "table1"
WHERE ("id" = 1)
RETURNING *
)
INSERT INTO "deleted_records"
("original_table", "original_id", "data")
SELECT 'table1', "id", to_json("deleted".*)
FROM "deleted"
RETURNING * |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi!
I'm working around of a custom soft-deletion mechanics in my Sequel-based application. Originally my solution was written as plain SQL and successfully tested - it works as expected. Then I tried to port it to Sequel DSL.
SQL solution: delete a record by ID and insert into another table (deleted_records) deleted ID, table name and row data encoded as json. Code:
I could not get exactly the same but achieved 'almost' the same:
The difference here as I see is that Sequel reads deleted row (app -> db -> app roundtrip) instead of do all in a single database request.
Is there a way to reduce fetching the data of deleted record and perform an insertion during the deletion?
I've played with
Dataset#with
method but this wasn't successful.Thanks!
Beta Was this translation helpful? Give feedback.
All reactions