Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions R/frs_break.R
Original file line number Diff line number Diff line change
Expand Up @@ -582,20 +582,20 @@ frs_break_apply <- function(conn, table, breaks,
# Get writable columns only (excludes GENERATED ALWAYS columns)
cols_writable <- .frs_table_columns(conn, table, exclude_generated = TRUE)

# Split columns: these come from the temp table, not the parent
# Split columns: segment_id, measures, and geom come from the split
cols_split_all <- c(sid, "downstream_route_measure",
"upstream_route_measure", "geom")
# Only include split columns that are actually writable
cols_split <- intersect(cols_split_all, cols_writable)

# Carry columns: everything writable that isn't a split column
# This preserves linear_feature_id (FWA provenance) from parent
cols_carry <- setdiff(cols_writable, cols_split_all)

# Build INSERT column list and SELECT expressions
cols_insert_parts <- character(0)
select_parts <- character(0)

# segment_id — new ID from max + row_number
# segment_id — new unique ID from max + row_number
if (sid %in% cols_split) {
cols_insert_parts <- c(cols_insert_parts, sid)
select_parts <- c(select_parts, sprintf(
Expand All @@ -604,7 +604,7 @@ frs_break_apply <- function(conn, table, breaks,
)", sid, table))
}

# Carried columns from parent
# Carried columns from parent (includes linear_feature_id for provenance)
if (length(cols_carry) > 0) {
cols_insert_parts <- c(cols_insert_parts, cols_carry)
select_parts <- c(select_parts, paste0("s.", cols_carry))
Expand Down
20 changes: 20 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@
}


#' Add id_segment column to a working table
#'
#' Assigns a unique integer ID to every row. Uses `linear_feature_id`
#' as the starting value (so original FWA segments keep their ID),
#' then generates new IDs for any rows added later by
#' [frs_break_apply()].
#'
#' @param conn DBI connection.
#' @param table Schema-qualified table name.
#' @noRd
.frs_add_id_segment <- function(conn, table) {
if (!inherits(conn, "DBIConnection")) return(invisible(NULL))
.frs_db_execute(conn, sprintf(
"ALTER TABLE %s ADD COLUMN IF NOT EXISTS id_segment integer", table))
.frs_db_execute(conn, sprintf(
"UPDATE %s SET id_segment = linear_feature_id
WHERE id_segment IS NULL", table))
}


#' Add indexes to a working table based on available columns
#'
#' Checks which index-worthy columns exist and creates appropriate indexes.
Expand Down
Loading