tree: dc3332446d9168801b8b5311f0b73efccd12ad4b [search]
  1. versions/
  2. db_config.ini
  3. Dockerfile
  4. env.py
  5. README.md
  6. script.py.mako
go/src/infra/device_manager/database/README.md

Alembic DB Management

Prerequisites

Docker (or Podman) is required to run Alembic migrations. No local Python setup is needed.

Development

See main README for development DB setup.

Staging / Production

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.

1. Tunnel to AlloyDB Bastion

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.

2. Run Alembic Migrations

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.

3. Verify with psql

Verify by connecting via psql and running queries.

psql -h localhost -U postgres -p 5433

4. Stop the Tunnel

Once finished, stop the background SSH tunnel:

pkill -f "ssh -f -N -L localhost:5433"

Warnings

Note: There will be hardening eventually that should prevent these things from happening.

  1. Do NOT run migrations at any branch not ToT. This may cause inconsistencies in data that will be difficult to rollback.
  2. Do NOT modify any data directly.
  3. Do NOT check in a modified db_config.ini file.