Skip to content

Commit

Permalink
add triggers for re-generate sql files and re-install poetry (#48)
Browse files Browse the repository at this point in the history
* remove unnecessary dependency declarations

* automatic terraform validation

* add triggers on file content

* ignore error when files does not exist

* add trigger on pyproject.toml configuration for poetry install
  • Loading branch information
kingman authored Oct 6, 2023
1 parent 62371ed commit 8ddcc63
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions infrastructure/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ locals {
config_file_name = "config"
poetry_run_alias = "${var.poetry_cmd} run"
mds_dataset_suffix = var.create_prod_environment ? "prod" : var.create_dev_environment ? "dev" : "staging"

project_toml_file_path = "${local.source_root_dir}/pyproject.toml"
project_toml_content_hash = filesha512(local.project_toml_file_path)

generated_sql_queries_directory_path = "${local.source_root_dir}/sql/query"
generated_sql_queries_fileset = [for f in fileset(local.generated_sql_queries_directory_path, "*.sql") : "${local.generated_sql_queries_directory_path}/${f}"]
generated_sql_queries_content_hash = sha512(join("", [for f in local.generated_sql_queries_fileset : fileexists(f) ? filesha512(f) : sha512("file-not-found")]))

generated_sql_procedures_directory_path = "${local.source_root_dir}/sql/procedure"
generated_sql_procedures_fileset = [for f in fileset(local.generated_sql_procedures_directory_path, "*.sql") : "${local.generated_sql_procedures_directory_path}/${f}"]
generated_sql_procedures_content_hash = sha512(join("", [for f in local.generated_sql_procedures_fileset : fileexists(f) ? filesha512(f) : sha512("file-not-found")]))
}

resource "local_file" "feature_store_configuration" {
Expand All @@ -75,39 +86,51 @@ resource "local_file" "feature_store_configuration" {
}

resource "null_resource" "poetry_install" {
triggers = {
create_command = "${var.poetry_cmd} install"
source_contents_hash = local.project_toml_content_hash
}

provisioner "local-exec" {
command = "${var.poetry_cmd} install"
when = create
command = self.triggers.create_command
working_dir = local.source_root_dir
}
}

resource "null_resource" "generate_sql_queries" {

triggers = {
create_command = <<-EOT
${local.poetry_run_alias} inv apply-env-variables-queries --env-name=${local.config_file_name}
${local.poetry_run_alias} inv apply-env-variables-procedures --env-name=${local.config_file_name}
EOT

destroy_command = <<-EOT
rm -f sql/query/*.sql
rm -f sql/procedure/*.sql
EOT

working_dir = local.source_root_dir

poetry_installed = null_resource.poetry_install.id

source_contents_hash = local_file.feature_store_configuration.content_sha512
destination_queries_hash = local.generated_sql_queries_content_hash
destination_procedures_hash = local.generated_sql_procedures_content_hash
}

provisioner "local-exec" {
command = <<-EOT
${local.poetry_run_alias} inv apply-env-variables-queries --env-name=${local.config_file_name}
${local.poetry_run_alias} inv apply-env-variables-procedures --env-name=${local.config_file_name}
EOT
when = create
command = self.triggers.create_command
working_dir = self.triggers.working_dir
}

provisioner "local-exec" {
when = destroy
command = <<-EOT
rm sql/query/*.sql
rm sql/procedure/*.sql
EOT
command = self.triggers.destroy_command
working_dir = self.triggers.working_dir
}

depends_on = [
local_file.feature_store_configuration,
null_resource.poetry_install
]
}

module "feature_store" {
Expand Down

0 comments on commit 8ddcc63

Please sign in to comment.