Docker (or Podman) is required to run Alembic migrations. No local Python setup is needed.
See main README for development DB setup.
To manage the Staging and Production AlloyDB instances, you must have GCP permissions to access the GCE instance used to manage the cluster. Currently, only Device Manager admins will have access. The following instructions cover staging management. You can replace the host URIs to manage production.
If granted permission to the bastion, create a tunnel to staging. This command puts the tunnel in the background:
ssh -f -N -L localhost:5433:10.40.128.2:5432 \ -i ~/.ssh/google_compute_engine \ ${USERNAME}_google_com@nic0.alloydb-bastion.us-central1-a.c.fleet-device-manager-dev.internal.gcpnode.com
This creates a tunnel to the bastion and maps the DB port 5432 to your localhost:5433 to avoid collisions with the development setup.
To run Alembic against the staging environment, you can automatically inject the password from Secret Manager. Run the following to check if a connection can be established:
# check current alembic version on db docker run --rm -v $(pwd):/app -w /app --network host \ -e ALEMBIC_ENV=alloydb.dev \ -e DB_PASS=$(gcloud secrets versions access latest \ --secret="alloydb-postgres-password" \ --project="fleet-device-manager-dev") \ python:3.9 \ /bin/sh -c "pip install --only-binary :all: alembic==1.13.1 \ sqlalchemy==2.0.25 psycopg2-binary==2.9.9 && alembic current" INFO [alembic.runtime.migration] Context impl PostgresqlImpl. INFO [alembic.runtime.migration] Will assume transactional DDL. 095e5749b6e6 (head) # this hash points to an alembic revision
Once confirmed, you can run the migrations as needed:
# upgrading schema to current version docker run --rm -v $(pwd):/app -w /app --network host \ -e ALEMBIC_ENV=alloydb.dev \ -e DB_PASS=$(gcloud secrets versions access latest \ --secret="alloydb-postgres-password" \ --project="fleet-device-manager-dev") \ python:3.9 \ /bin/sh -c "pip install --only-binary :all: alembic==1.13.1 \ sqlalchemy==2.0.25 psycopg2-binary==2.9.9 && alembic upgrade head" INFO [alembic.runtime.migration] Context impl PostgresqlImpl. INFO [alembic.runtime.migration] Will assume transactional DDL. INFO [alembic.runtime.migration] Running upgrade -> 3303697cfdfd, create Devices table INFO [alembic.runtime.migration] Running upgrade 3303697cfdfd -> 8b7c9cfc4c56, create DeviceLeaseRecords table INFO [alembic.runtime.migration] Running upgrade 8b7c9cfc4c56 -> c10375863126, create indices for device columns INFO [alembic.runtime.migration] Running upgrade c10375863126 -> 095e5749b6e6, create ExtendLeaseRequests table
For Production: Replace alloydb.dev with alloydb.prod and the project ID with fleet-device-manager-prod.
psqlVerify by connecting via psql and running queries.
psql -h localhost -U postgres -p 5433
Once finished, stop the background SSH tunnel:
pkill -f "ssh -f -N -L localhost:5433"
Note: There will be hardening eventually that should prevent these things from happening.
db_config.ini file.