# If your goodmem DB was based on PG 16, you need to migrate to PG 17 after the metatdata filter support was added circa Oct 1, 2025.

# Based on Weisi Fan's instructions in Markdown. 

# Part 1: Backup your existing DB
# Step 1.1: Execute `pg_dump` inside the running container.
#    This command creates a compressed, custom-format backup of the database. We write it to the `/var/lib/postgresql/` directory, which is writable by the postgres user.

docker exec goodmem-db pg_dump -U goodmem_admin -d goodmem_db -Fc -f /var/lib/postgresql/backup.dump

# Step 1.2: Copy the backup file from the container to your host machine.
#     This command copies the backup file from the container to the `/tmp/` directory on the host machine. You can change the destination path as needed.
docker cp goodmem-db:/var/lib/postgresql/backup.dump /tmp/goodmem_backup.dump

# Part 2: Restoring the Database
# This procedure describes how to restore a backup to a new database container and safely swap it with the live one.

# Step 2.1: Start a new temporary database container.
# Start a new pgvector container that will be used for the restore. Use the same credentials as your main database, 
# but map it to a different host port (e.g., 5434) to avoid conflicts.
docker run -d --name goodmem-db-restore \
    -e POSTGRES_USER=goodmem_admin \
    -e POSTGRES_PASSWORD=ayk317gk \
    -p 5434:5432 \
    pgvector/pgvector:pg17

# Step 2.2: Create a clean database.
# Connect to the new container and create an empty database with the same name as the original.
docker exec goodmem-db-restore psql -U goodmem_admin -d postgres -c "CREATE DATABASE goodmem_db;"

# Step 2.3: Copy the backup file into the new container.
docker cp /tmp/goodmem_backup.dump goodmem-db-restore:/tmp/backup.dump

# Step 2.4: Run pg_restore.
# Execute the restore command inside the new container.
docker exec goodmem-db-restore pg_restore -U goodmem_admin -d goodmem_db --no-owner /tmp/backup.dump

# Part 3: Swapping the Databases
# This is the final step to make the newly restored database live. This process ensures minimal downtime.

# Step 3.1: Stop and rename the original database.
# This takes the original database offline and renames it, keeping it as a safe backup.
docker stop goodmem-db
docker rename goodmem-db goodmem-db-original

# Step 3.2: Rename the restored database.
# Rename the goodmem-db-restore container to goodmem-db, which is the name the application server expects.
docker rename goodmem-db-restore goodmem-db

# Step 3.3: Connect the new database to the network.
# Connect the new container to the application's network with the required db alias so the server can find it.
docker network connect --alias db goodmem_default goodmem-db

# Step 3.4: Restart the application server.
# Restart the goodmem-server to force it to establish a new connection with the restored database.
docker restart goodmem-server

echo "Migration process complete. Your application is now running on the newly restored database."
