Skip to main content

Manage sqlalchemy connections to local test and prod db's

Project description

Suppose you’re working on a project that uses Google App Engine, Google Cloud SQL, and sqlalchemy. In the course of testing, deploying etc., we want to connect to a couple of databases in different ways:

  • App Engine project talking to production db

  • Local tools (like mysql client or alembic) talking to production db

  • Tests talking to local db

  • Local tools talking to local db

Getting all the connections strings etc. right is kind annoying. This package helps solve that problem.

We provide two scripts:

  • start_cloud_proxy: Runs the Google Cloud SQL proxy using an authentication token you downloaded when making a service account.

  • start_mysql_client: Runs a client on your machine, connecting either to the production server (through the proxy) or to a local test database.

Whether you’re connecting to a cloud instance or a local server, both of these scripts decide which database to connect to by reading the evironment variable SQL_PROJECT_NAME. This variable must match the name of the database to which you’re trying to connect.

We assume you have your project’s source on your system at PROJECT_ROOT/, where PROJECT_ROOT is probably something like ~/src/<whatever>/. The file layout should look something like this:

├── PROJECT_ROOT/
│    ├── SQL_PROJECT_NAME/  <-- important
│    │   ├── __init__.py
│    │   ├── config.yaml    <-- Important
│    │   ├── alembic.ini
│    │   ├── alembic/
│    │   ├── lib/
│    │   ├── models.py
│    │   └── main.py
│    ├── README.md
│    ├── appengine_config.py
│    └── app.yaml

You must also have a directory at ~/.SQL_PROJECT_NAME with a couple of files described in the paragraph.

This package assumes there are three particular files to be available in your system:

  • ~/.SQL_PROJECT_NAME/config.yaml

    ---
    cloud:
        USER: "root"
        PASSWORD: ...
    local:
        USER: "root"
        PASSWORD: ...
        HOST: "localhost"
        PORT: 3306
  • ~/.SQL_PROJECT_NAME/auth-token.json: This is your proxy’s user account auth token.

  • PROJECT_ROOT/SQL_PROJECT_NAME/config.yaml

    ---
    INSTANCE_CONNECTION_NAME: ...

Installing this package (into a virtualenv!) provides the command line script start_cloud_proxy, which starts the Google Cloud SQL proxy for you.

How to release

$ python setup.py register -r pypi(test)
$ python setup.py sdist upload -r pypi(test)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlemon-0.4.1.tar.gz (3.7 MB view details)

Uploaded Source

File details

Details for the file sqlemon-0.4.1.tar.gz.

File metadata

  • Download URL: sqlemon-0.4.1.tar.gz
  • Upload date:
  • Size: 3.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for sqlemon-0.4.1.tar.gz
Algorithm Hash digest
SHA256 ad327ebd19a6c835daad5344067a7a281921b89534e7b2ad1c2838a0eab287ce
MD5 dc813f97bab0295a2852f0acfa9427d9
BLAKE2b-256 e314b61790524a9b86a2a0f720ead4c7438f3ddd88b696a2891f49f3fd108472

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page