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
2 changes: 2 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ module "database" {

deletion_protection = var.deletion_protection

max_database_connections = var.max_database_connections

depends_on = [module.networking]
}

Expand Down
8 changes: 8 additions & 0 deletions modules/database/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ resource "google_sql_database_instance" "this" {
insights_config {
query_insights_enabled = true
}

dynamic "database_flags" {
for_each = var.max_database_connections != "" ? [1] : []
content {
name = "max_connections"
value = var.max_database_connections
}
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions modules/database/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ variable "deletion_protection" {
description = "Whether to enable deletion protection for the database instance."
type = bool
}

variable "max_database_connections" {
description = "The maximum number of connections to the database"
type = string
default = ""
}
Comment on lines +26 to +31
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Align module variable type with root or add validation.
Just like in the root, the database module’s max_database_connections should either:

  • Be converted to a number with default = null and validation, or
  • Retain string type but include a validation clause to enforce numeric content.

Consistency prevents runtime errors when passing values across modules.

 variable "max_database_connections" {
   description = "The maximum number of connections to the database"
-  type        = string
-  default     = ""
+  type        = number
+  default     = null

+  validation {
+    condition     = var.max_database_connections == null || var.max_database_connections >= 0
+    error_message = "max_database_connections must be a non-negative number or null"
+  }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
variable "max_database_connections" {
description = "The maximum number of connections to the database"
type = string
default = ""
}
variable "max_database_connections" {
description = "The maximum number of connections to the database"
type = number
default = null
validation {
condition = var.max_database_connections == null || var.max_database_connections >= 0
error_message = "max_database_connections must be a non-negative number or null"
}
}

6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ variable "postgres_version" {
default = "POSTGRES_17"
}

variable "max_database_connections" {
description = "The maximum number of connections to the database"
type = string
default = ""
}
Comment on lines +23 to +27
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider using a numeric type or adding validation for max_database_connections.
Right now this variable is a string with an empty default. Since it ultimately represents a numeric value for the max_connections flag, we should:

  • Change the type to number with a default of null and update the dynamic block to check for != null.
  • Or add a validation block to enforce a digits-only string (e.g. ^[0-9]+$).

This ensures users can’t pass invalid inputs and improves type safety.

 variable "max_database_connections" {
   description = "The maximum number of connections to the database"
-  type        = string
-  default     = ""
+  type        = number
+  default     = null

+  validation {
+    condition     = var.max_database_connections == null || var.max_database_connections >= 0
+    error_message = "max_database_connections must be a non-negative number or null"
+  }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
variable "max_database_connections" {
description = "The maximum number of connections to the database"
type = string
default = ""
}
variable "max_database_connections" {
description = "The maximum number of connections to the database"
type = number
default = null
validation {
condition = var.max_database_connections == null || var.max_database_connections >= 0
error_message = "max_database_connections must be a non-negative number or null"
}
}


variable "redis_tier" {
description = "The tier for the Redis instance"
type = string
Expand Down
Loading