diff --git a/main.tf b/main.tf index cb1bb4d..0d9c1c0 100644 --- a/main.tf +++ b/main.tf @@ -87,7 +87,11 @@ module "database" { postgres_ro_username = var.postgres_ro_username database_version = var.database_version database_edition = var.database_edition - common_tags = var.common_tags + + point_in_time_recovery_enabled = var.point_in_time_recovery_enabled + transaction_log_retention_days = var.transaction_log_retention_days + + common_tags = var.common_tags depends_on = [module.project_factory_project_services] } diff --git a/modules/database/main.tf b/modules/database/main.tf index 5060038..e247404 100644 --- a/modules/database/main.tf +++ b/modules/database/main.tf @@ -1,3 +1,14 @@ +locals { + # PITR (continuous WAL archiving) defaults to on for ENTERPRISE_PLUS and off + # otherwise (including when the edition is null/auto-determined). An explicit + # value for point_in_time_recovery_enabled always wins. + point_in_time_recovery_enabled = ( + var.point_in_time_recovery_enabled != null + ? var.point_in_time_recovery_enabled + : coalesce(var.database_edition, "") == "ENTERPRISE_PLUS" + ) +} + # https://registry.terraform.io/modules/GoogleCloudPlatform/sql-db/google/latest/submodules/private_service_access module "db_private_service_access" { source = "GoogleCloudPlatform/sql-db/google//modules/private_service_access" @@ -49,8 +60,8 @@ module "db" { enabled = true start_time = "20:00" location = null - point_in_time_recovery_enabled = false - transaction_log_retention_days = null + point_in_time_recovery_enabled = local.point_in_time_recovery_enabled + transaction_log_retention_days = var.transaction_log_retention_days retained_backups = 7 retention_unit = "COUNT" } diff --git a/modules/database/variables.tf b/modules/database/variables.tf index f9caeff..c290dde 100644 --- a/modules/database/variables.tf +++ b/modules/database/variables.tf @@ -89,6 +89,27 @@ variable "database_edition" { } } +variable "point_in_time_recovery_enabled" { + type = bool + default = null + description = <<-EOT + Enable point-in-time recovery (continuous WAL archiving) for the PostgreSQL + instance. When null, defaults to true for ENTERPRISE_PLUS edition and false + otherwise. Note: changing this triggers an instance restart, and on + ENTERPRISE edition transaction logs consume the data disk. +EOT +} + +variable "transaction_log_retention_days" { + type = number + default = null + description = <<-EOT + Number of days to retain transaction logs for point-in-time recovery. + Null uses the provider/edition default (1-7 days for ENTERPRISE, up to 35 + for ENTERPRISE_PLUS). +EOT +} + variable "common_tags" { type = map(string) } diff --git a/variables.tf b/variables.tf index 946f46e..7543e4a 100644 --- a/variables.tf +++ b/variables.tf @@ -372,6 +372,27 @@ variable "database_edition" { } } +variable "point_in_time_recovery_enabled" { + type = bool + default = null + description = <<-EOT + Enable point-in-time recovery (continuous WAL archiving) for the PostgreSQL + instance. When null, defaults to true for ENTERPRISE_PLUS edition and false + otherwise. Note: changing this triggers an instance restart, and on + ENTERPRISE edition transaction logs consume the data disk. +EOT +} + +variable "transaction_log_retention_days" { + type = number + default = null + description = <<-EOT + Number of days to retain transaction logs for point-in-time recovery. + Null uses the provider/edition default (1-7 days for ENTERPRISE, up to 35 + for ENTERPRISE_PLUS). +EOT +} + # ┏━╸╻ ╻┏━╸╻┏ ╻ ╻┏━┓╻ ╻┏━┓┏━╸ # ┃ ┃ ┃┃ ┣┻┓┣━┫┃ ┃┃ ┃┗━┓┣╸ # ┗━╸┗━╸╹┗━╸╹ ╹╹ ╹┗━┛┗━┛┗━┛┗━╸