From 862f307e5675c42ac1e70c86024b97ab83be2f71 Mon Sep 17 00:00:00 2001 From: imterah Date: Thu, 26 Dec 2024 14:59:01 -0500 Subject: [PATCH] fix: Fixes documentation and migration code. --- backend/api/backup.go | 10 +++++----- docs/nextnet_to_hermes_migration.md | 20 ++++++++++++-------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/backend/api/backup.go b/backend/api/backup.go index 2f67739..ebd5f15 100644 --- a/backend/api/backup.go +++ b/backend/api/backup.go @@ -2,7 +2,7 @@ package main import ( "compress/gzip" - "database/sql" + "context" "encoding/json" "errors" "fmt" @@ -14,7 +14,7 @@ import ( "git.terah.dev/imterah/hermes/api/dbcore" "github.com/charmbracelet/log" "github.com/go-playground/validator/v10" - _ "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5" "github.com/urfave/cli/v2" "gorm.io/gorm" ) @@ -166,7 +166,7 @@ func backupRestoreEntrypoint(cCtx *cli.Context) error { log.Info("Connecting to database...") - db, err := sql.Open("postgres", postgresDSN) + db, err := pgx.Connect(context.Background(), postgresDSN) if err != nil { return fmt.Errorf("failed to connect to database: %s", err.Error()) @@ -174,7 +174,7 @@ func backupRestoreEntrypoint(cCtx *cli.Context) error { log.Info("Dropping database...") - _, err = db.Query("DROP DATABASE ?", postgresDB) + _, err = db.Query(context.Background(), "DROP DATABASE ?", postgresDB) if err != nil { return fmt.Errorf("failed to drop database: %s", err.Error()) @@ -182,7 +182,7 @@ func backupRestoreEntrypoint(cCtx *cli.Context) error { log.Info("Closing database connection...") - err = db.Close() + err = db.Close(context.Background()) if err != nil { return fmt.Errorf("failed to close database connection: %s", err.Error()) diff --git a/docs/nextnet_to_hermes_migration.md b/docs/nextnet_to_hermes_migration.md index 4f0f6f4..716fd06 100644 --- a/docs/nextnet_to_hermes_migration.md +++ b/docs/nextnet_to_hermes_migration.md @@ -15,14 +15,18 @@ Below are new environment variables that may need to be set up: * `HERMES_SQLITE_FILEPATH`: Path for the SQLite database to use. * `HERMES_POSTGRES_DSN`: PostgreSQL DSN for Golang. An example value which should work with minimal changes for PostgreSQL databases is `postgres://username:password@localhost:5432/database_name`. ## Migration steps -1. Add the supporting environment variable for Prisma: `DATABASE_URL` -> `postgresql://$POSTGRES_USERNAME:$POSTGRES_PASSWORD@nextnet-postgres:5432/$POSTGRES_DB`. -2. Remove all old environment variables. -3. Add these variables: +1. Remove all old environment variables. +2. Add these variables: + - `HERMES_MIGRATE_POSTGRES_DATABASE` -> `$POSTGRES_DB` - `HERMES_DATABASE_BACKEND` -> `postgresql` - `HERMES_POSTGRES_DSN` -> `postgres://$POSTGRES_USERNAME:$POSTGRES_PASSWORD@nextnet-postgres:5432/$POSTGRES_DB` - `DATABASE_URL` -> `postgresql://$POSTGRES_USERNAME:$POSTGRES_PASSWORD@nextnet-postgres:5432/$POSTGRES_DB?schema=nextnet` -4. Switch the API docker image from `ghcr.io/imterah/nextnet:latest` to `ghcr.io/imterah/hermes-backend-migration:latest` -5. Go get the container logs, and make sure no errors get output to the console. -6. Copy the backup as instructed in the log file. -7. DO NOT RESTART THE CONTAINER IF SUCCESSFUL. YOU WILL LOSE ALL YOUR DATA. If the migration fails, follow the steps mentioned in the logs. -8. If successful, switch the API docker image from `ghcr.io/imterah/hermes-backend-migration:latest` to `ghcr.io/imterah/hermes:latest`. +3. Switch the API docker image from `ghcr.io/imterah/nextnet:latest` to `ghcr.io/imterah/hermes-backend-migration:latest` +4. Change the exposed ports from `3000:3000` to `3000:8000`. +5. Start the Docker compose stack. +6. Go get the container logs, and make sure no errors get output to the console. +7. Copy the backup as instructed in the log file. +8. DO NOT RESTART THE CONTAINER IF SUCCESSFUL. YOU WILL LOSE ALL YOUR DATA. If the migration fails, follow the steps mentioned in the logs. You do not need to copy the DB backup if it failed to connect or read the database. +9. If successful, remove the environment variable `HERMES_MIGRATE_POSTGRES_DATABASE`. +10. Switch the API docker image from `ghcr.io/imterah/hermes-backend-migration:latest` to `ghcr.io/imterah/hermes:latest`. +11. Start the backend.