Skip to content

Commit 190f3c9

Browse files
Android Build Filesystem (ABFS) Teamsce-taid
authored andcommitted
fix: refactor DDL comment and empty line removal
PiperOrigin-RevId: 820117718
1 parent 9c2e877 commit 190f3c9

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

modules/server/spanner.tf

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
locals {
16+
# 1. Clean the raw DDL content by removing comment lines and empty lines.
17+
file_lines = split("\n", data.local_file.abfs_spanner_database_schema.content)
18+
lines_no_comments = [for line in local.file_lines : line if ! startswith(trimspace(line), "--")]
19+
ddl_lines = [for line in local.lines_no_comments : line if length(trimspace(line)) > 0]
20+
cleaned_ddl = join("\n", local.ddl_lines)
21+
22+
# 2. Split the content into individual statements using the semicolon as a delimiter.
23+
statements = split(";", local.cleaned_ddl)
24+
25+
# 3. Trim whitespace from each statement and filter out any empty statements.
26+
spanner_ddl_statements = var.abfs_spanner_database_create_tables ? [
27+
for s in local.statements : trimspace(s)
28+
if length(trimspace(s)) > 0
29+
] : []
30+
}
31+
1532
resource "google_spanner_instance" "abfs" {
1633
project = var.project_id
1734
name = var.abfs_spanner_instance_name
@@ -36,17 +53,6 @@ data "local_file" "abfs_spanner_database_schema" {
3653
filename = "${path.module}/../../files/schemas/${var.abfs_spanner_database_schema_version}-schema.sql"
3754
}
3855

39-
locals {
40-
# 1. Remove comment lines from the DDL content.
41-
cleaned_ddl = replace(data.local_file.abfs_spanner_database_schema.content, "(?m)^--.*\\R?", "")
42-
43-
# 2. Split the content into individual statements using the semicolon as a delimiter.
44-
statements = split(";", local.cleaned_ddl)
45-
46-
# 3. Trim whitespace from each statement and filter out any empty statements.
47-
spanner_ddl_statements = var.abfs_spanner_database_create_tables ? [for s in local.statements : trimspace(s) if length(trimspace(s)) > 0] : []
48-
}
49-
5056
resource "google_spanner_database" "abfs" {
5157
project = var.project_id
5258
instance = google_spanner_instance.abfs.name

0 commit comments

Comments
 (0)