
Replace a table with modified data and create a new snapshot
Source:R/replace_table.R
replace_table.RdReplace a table with modified data and create a new snapshot
Details
This function is designed for schema changes or bulk transformations that should create a new versioned snapshot. It:
Collects the transformed data
Drops the existing table
Creates a new table with the updated schema/data
The drop and create run atomically: when no transaction is open,
replace_table() wraps them in one of its own, so a failed create never
leaves the table dropped. Wrap the call in with_transaction() (or
begin_transaction()/commit_transaction()) when you want to record an
author and commit message on the snapshot, or to group the replacement
with other changes.
When to use replace_table():
Bulk transformations - a dplyr pipeline that recomputes, reshapes, or filters most of the table
When to reach elsewhere:
Schema-only changes -
add_table_column(),drop_table_column(),rename_table_column(), andset_column_type()alter the table in place; nothing is collected or rewrittenDerived columns -
add_table_column()followed by amutate()pipeline throughducklake_exec()fills the new column with an in-database UPDATETargeted row changes -
rows_update(),rows_upsert(), orducklake_exec()modify only the affected rows
Both paths create a snapshot: replace_table() via DROP + CREATE, and ducklake_exec() via the in-place UPDATE/DELETE it runs, so either way the change is available for time travel.
See also
Other table operations:
add_data_files(),
create_table(),
create_view(),
drop_view(),
ducklake_exec(),
get_ducklake_table(),
get_metadata_table(),
list_ducklake_tables(),
show_ducklake_query()
Examples
if (FALSE) { # \dontrun{
# Add new derived columns (atomic on its own; creates a new snapshot)
get_ducklake_table("adsl") |>
mutate(
AGE65FL = if_else(AGE >= 65, "Y", "N"),
AGECAT = case_when(
AGE < 65 ~ "<65",
AGE >= 65 & AGE < 75 ~ "65-74",
AGE >= 75 ~ ">=75"
)
) |>
replace_table("adsl")
# Wrap in with_transaction() to record audit metadata on the snapshot
with_transaction(
get_ducklake_table("adsl") |>
select(-AGE65FL, -AGECAT) |>
replace_table("adsl"),
author = "Data Engineer",
commit_message = "Drop derived age columns"
)
} # }