From 4f275aa14584280c2816f396cbff2219e1d2b8b0 Mon Sep 17 00:00:00 2001 From: James Kwon <96548424+hongil0316@users.noreply.github.com> Date: Sun, 26 May 2024 10:49:36 -0400 Subject: [PATCH] Capture latest ent schema state in migration file + applying atlas migration on staging environment --- cloudbuild.yaml | 26 +++++++++++++++++++ .../migrations/20240526144817_migration.sql | 26 +++++++++++++++++++ ent/migrate/migrations/atlas.sum | 2 ++ main.go | 16 +++++------- 4 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 ent/migrate/migrations/20240526144817_migration.sql create mode 100644 ent/migrate/migrations/atlas.sum diff --git a/cloudbuild.yaml b/cloudbuild.yaml index 34e379c..4052e2f 100644 --- a/cloudbuild.yaml +++ b/cloudbuild.yaml @@ -5,6 +5,32 @@ steps: # push container image - name: "gcr.io/cloud-builders/docker" args: ["push", "us-central1-docker.pkg.dev/dreamboothy/registry-backend/registry-backend-image:$SHORT_SHA"] + + # Fetch the database connection string secret for staging + - name: "gcr.io/google.com/cloudsdktool/cloud-sdk" + entrypoint: "bash" + args: + - "-c" + - | + STAGING_SUPABASE_CONNECTION_STRING=$(gcloud secrets versions access latest --secret=STAGING_SUPABASE_CONNECTION_STRING) + export STAGING_SUPABASE_CONNECTION_STRING=$STAGING_SUPABASE_CONNECTION_STRING + + # Install Atlas + - name: 'gcr.io/cloud-builders/curl' + entrypoint: 'sh' + args: + - '-c' + - | + curl -sSL https://atlasgo.sh | sh + + # Run database migrations on staging environment + - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:latest' + entrypoint: 'bash' + args: + - '-c' + - | + atlas migrate apply --dir "ent/migrate/migrations" --url "$STAGING_SUPABASE_CONNECTION_STRING" + # Publish the release - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:458.0.1' entrypoint: 'bash' diff --git a/ent/migrate/migrations/20240526144817_migration.sql b/ent/migrate/migrations/20240526144817_migration.sql new file mode 100644 index 0000000..751f82b --- /dev/null +++ b/ent/migrate/migrations/20240526144817_migration.sql @@ -0,0 +1,26 @@ +-- Create "git_commits" table +CREATE TABLE "git_commits" ("id" uuid NOT NULL, "create_time" timestamptz NOT NULL, "update_time" timestamptz NOT NULL, "commit_hash" text NOT NULL, "branch_name" text NOT NULL, "repo_name" text NOT NULL, "commit_message" text NOT NULL, "commit_timestamp" timestamptz NOT NULL, "author" text NULL, "timestamp" timestamptz NULL, PRIMARY KEY ("id")); +-- Create index "gitcommit_repo_name_commit_hash" to table: "git_commits" +CREATE UNIQUE INDEX "gitcommit_repo_name_commit_hash" ON "git_commits" ("repo_name", "commit_hash"); +-- Create "storage_files" table +CREATE TABLE "storage_files" ("id" uuid NOT NULL, "create_time" timestamptz NOT NULL, "update_time" timestamptz NOT NULL, "bucket_name" text NOT NULL, "object_name" text NULL, "file_path" text NOT NULL, "file_type" text NOT NULL, "file_url" text NULL, PRIMARY KEY ("id")); +-- Create "ci_workflow_results" table +CREATE TABLE "ci_workflow_results" ("id" uuid NOT NULL, "create_time" timestamptz NOT NULL, "update_time" timestamptz NOT NULL, "operating_system" text NOT NULL, "gpu_type" text NULL, "pytorch_version" text NULL, "workflow_name" text NULL, "run_id" text NULL, "status" text NULL, "start_time" bigint NULL, "end_time" bigint NULL, "ci_workflow_result_storage_file" uuid NULL, "git_commit_results" uuid NULL, PRIMARY KEY ("id"), CONSTRAINT "ci_workflow_results_git_commits_results" FOREIGN KEY ("git_commit_results") REFERENCES "git_commits" ("id") ON UPDATE NO ACTION ON DELETE SET NULL, CONSTRAINT "ci_workflow_results_storage_files_storage_file" FOREIGN KEY ("ci_workflow_result_storage_file") REFERENCES "storage_files" ("id") ON UPDATE NO ACTION ON DELETE SET NULL); +-- Create "publishers" table +CREATE TABLE "publishers" ("id" text NOT NULL, "create_time" timestamptz NOT NULL, "update_time" timestamptz NOT NULL, "name" text NOT NULL, "description" text NULL, "website" text NULL, "support_email" text NULL, "source_code_repo" text NULL, "logo_url" text NULL, PRIMARY KEY ("id")); +-- Create "nodes" table +CREATE TABLE "nodes" ("id" text NOT NULL, "create_time" timestamptz NOT NULL, "update_time" timestamptz NOT NULL, "name" text NOT NULL, "description" text NULL, "author" text NULL, "license" text NOT NULL, "repository_url" text NOT NULL, "icon_url" text NULL, "tags" text NOT NULL, "publisher_id" text NOT NULL, PRIMARY KEY ("id"), CONSTRAINT "nodes_publishers_nodes" FOREIGN KEY ("publisher_id") REFERENCES "publishers" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION); +-- Create "node_versions" table +CREATE TABLE "node_versions" ("id" uuid NOT NULL, "create_time" timestamptz NOT NULL, "update_time" timestamptz NOT NULL, "version" text NOT NULL, "changelog" text NULL, "pip_dependencies" text NOT NULL, "deprecated" boolean NOT NULL DEFAULT false, "node_id" text NOT NULL, "node_version_storage_file" uuid NULL, PRIMARY KEY ("id"), CONSTRAINT "node_versions_nodes_versions" FOREIGN KEY ("node_id") REFERENCES "nodes" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT "node_versions_storage_files_storage_file" FOREIGN KEY ("node_version_storage_file") REFERENCES "storage_files" ("id") ON UPDATE NO ACTION ON DELETE SET NULL); +-- Create index "nodeversion_node_id_version" to table: "node_versions" +CREATE UNIQUE INDEX "nodeversion_node_id_version" ON "node_versions" ("node_id", "version"); +-- Create "personal_access_tokens" table +CREATE TABLE "personal_access_tokens" ("id" uuid NOT NULL, "create_time" timestamptz NOT NULL, "update_time" timestamptz NOT NULL, "name" text NOT NULL, "description" text NOT NULL, "token" text NOT NULL, "publisher_id" text NOT NULL, PRIMARY KEY ("id"), CONSTRAINT "personal_access_tokens_publishers_personal_access_tokens" FOREIGN KEY ("publisher_id") REFERENCES "publishers" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION); +-- Create index "personal_access_tokens_token_key" to table: "personal_access_tokens" +CREATE UNIQUE INDEX "personal_access_tokens_token_key" ON "personal_access_tokens" ("token"); +-- Create index "personalaccesstoken_token" to table: "personal_access_tokens" +CREATE UNIQUE INDEX "personalaccesstoken_token" ON "personal_access_tokens" ("token"); +-- Create "users" table +CREATE TABLE "users" ("id" character varying NOT NULL, "create_time" timestamptz NOT NULL, "update_time" timestamptz NOT NULL, "email" character varying NULL, "name" character varying NULL, "is_approved" boolean NOT NULL DEFAULT false, "is_admin" boolean NOT NULL DEFAULT false, PRIMARY KEY ("id")); +-- Create "publisher_permissions" table +CREATE TABLE "publisher_permissions" ("id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, "permission" character varying NOT NULL, "publisher_id" text NOT NULL, "user_id" character varying NOT NULL, PRIMARY KEY ("id"), CONSTRAINT "publisher_permissions_publishers_publisher_permissions" FOREIGN KEY ("publisher_id") REFERENCES "publishers" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT "publisher_permissions_users_publisher_permissions" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION); diff --git a/ent/migrate/migrations/atlas.sum b/ent/migrate/migrations/atlas.sum new file mode 100644 index 0000000..ac024c1 --- /dev/null +++ b/ent/migrate/migrations/atlas.sum @@ -0,0 +1,2 @@ +h1:y3z5P4GWPQWBraiBTLgj3b1AGznQ3rQfzxhn9/WnFdw= +20240526144817_migration.sql h1:Rc0HHsjtSJsJHNAj6pI3oINZE4aCcDRk2P95IB4yUys= diff --git a/main.go b/main.go index 9dbd537..55298bc 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,10 @@ package main import ( - "context" "fmt" "os" "registry-backend/config" "registry-backend/ent" - "registry-backend/ent/migrate" drip_logging "registry-backend/logging" "registry-backend/server" @@ -39,13 +37,13 @@ func main() { } defer client.Close() // Run the auto migration tool for localdev. - if os.Getenv("DRIP_ENV") == "localdev" || os.Getenv("DRIP_ENV") == "staging" { - log.Info().Msg("Running migrations") - if err := client.Schema.Create(context.Background(), migrate.WithDropIndex(true), - migrate.WithDropColumn(true)); err != nil { - log.Fatal().Err(err).Msg("failed creating schema resources.") - } - } + //if os.Getenv("DRIP_ENV") == "localdev" || os.Getenv("DRIP_ENV") == "staging" { + // log.Info().Msg("Running migrations") + // if err := client.Schema.Create(context.Background(), migrate.WithDropIndex(true), + // migrate.WithDropColumn(true)); err != nil { + // log.Fatal().Err(err).Msg("failed creating schema resources.") + // } + //} server := server.NewServer(client, &config) server.Start()