Skip to main content
slice(list, start_index, end_index) returns the elements of list from index start_index (inclusive) to end_index (exclusive).

Signature

slice(list list, start_index number, end_index number) list

Example

Run only the first batch of migration files in a staged rollout, deferring the rest to a later run:
variable "migration_files" {
  description = "Ordered list of SQL migration files"
  type        = list(string)
  default = [
    "001_create_users.sql",
    "002_add_email_index.sql",
    "003_create_sessions.sql",
    "004_add_audit_log.sql",
  ]
}

variable "batch_size" {
  description = "Number of migrations to run in this batch"
  type        = number
  default     = 2
}

computed "batch" {
  description = "Current batch of migration files"
  expression  = slice(var.migration_files, 0, var.batch_size)
}

task "migrate-batch" {
  description = "Apply the current migration batch"
  commands = [
    "for f in ${join(" ", computed.batch)}; do psql -f migrations/$f; done",
  ]
}
With the default batch_size of 2, only 001_create_users.sql and 002_add_email_index.sql are applied.

Notes

  • start_index is inclusive, end_index is exclusive. slice(list, 0, 2) returns elements at index 0 and 1.
  • If end_index equals start_index, the result is an empty list.
  • slice also works on tuples. When applied to a tuple, the result is a tuple.
  • Indices out of bounds produce an error.