Giter VIP home page Giter VIP logo

spanner-sqlalchemy-demo's Introduction

Cloud Spanner + SQLAlchemy demo

pytest & flake8 docker_build

This is a demo application for Cloud Spanner SQLAlchemy ORM.
A simple ranking API for gaming use cases.

Building blocks

For who want to play with this code

1. install dependencies to your local machine

git clone https://github.com/kazshinohara/spanner-sqlalchemy-demo
cd spanner-sqlalchemy-demo
poetry install

2. Setup Cloud Spanner and DB Migration

Set environment variables which are needed for the following steps.

export PROJECT_ID=""
export INSTANCE_ID=""
export DATABASE_ID=""
export SA_NAME=""
export SA_KEY_NAME=""

Create a service account for this demo.

gcloud iam service-accounts create ${SA_NAME}

Assign role to the service account.

gcloud projects add-iam-policy-binding ${PROJECT_ID} --member "serviceAccount:${SA_NAME}@${PROJECT_ID}.iam.gserviceaccount.com" --role "roles/spanner.databaseAdmin"

Download the key file to your local machine.

gcloud iam service-accounts keys create ${SA_KEY_NAME} \
--iam-account=${SA_NAME}@${PROJECT_ID}.iam.gserviceaccount.com

Set your key file location as an environment variable.

export GOOGLE_APPLICATION_CREDENTIALS=""

Create Cloud Spanner instance and database.

gcloud spanner instances create ${INSTANCE_ID} --config=regional-asia-northeast1 --description="demo" --nodes=1
gcloud spanner databases create ${DATABASE_ID} --instance=${INSTANCE_ID}

DB migration, Alembic uses your service account's credential to access Cloud Spanner.

cd spanner-sqlalchemy-demo/app
poetry run alembic revision --autogenerate -m "Initial migration"
poetry run alembic upgrade head

3. Start API server at your local machine

Note: The following steps need environment variables which are set at Section 2.

cd spanner-sqlalchemy-demo
poetry run uvicorn app.main:app --reload
open http://127.0.0.1:8000/

4. Run unit test at your local machine

Note: The following steps need environment variables which are set at Section 2.

Run Cloud Spanner emulator.

docker run -p 9010:9010 -p 9020:9020 gcr.io/cloud-spanner-emulator/emulator

Set up Cloud Spanner emulator.

cd spanner-sqlalchemy-demo/tests
chmod u+x spanner_emulator_setup.sh
./spanner_emulator_setup.sh

Set environment variables which are needed for the following steps.

export SPANNER_EMULATOR_HOST=localhost:9010
export K_SERVICE=spanner-sqlalchemy-demo
export K_REVISION=spanner-sqlalchemy-demo-00001-thx

Run the unit test.

cd spanner-sqlalchemy-demo/tests
poetry run pytest

5. Create the container image and deploy to Cloud Run

Note: The following steps need environment variables which are set at Section 2.

Set an environment variable which are needed for the following steps.
Please make sure that you have a docker image repo at Artifact Registry.

export REPOSITORY_NAME=""

Build a container image.

cd spanner-sqlalchemy-demo 
docker build -t asia-northeast1-docker.pkg.dev/${PROJECT_ID}/${REPOSITORY_NAME}/spanner-sqlalchemy-demo:1.0.0 .

Push the image to Artifact Registry

cd spanner-sqlalchemy-demo 
docker push asia-northeast1-docker.pkg.dev/${PROJECT_ID}/${REPOSITORY_NAME}/spanner-sqlalchemy-demo:1.0.0

Deploy the container image to Cloud Run.

gcloud run deploy spanner-sqlalchemy-demo \
--image asia-northeast1-docker.pkg.dev/${PROJECT_ID}/${REPOSITORY_NAME}/spanner-sqlalchemy-demo:1.0.0 \
--allow-unauthenticated \
--set-env-vars=PROJECT_ID=${PROJECT_ID},INSTANCE_ID=${INSTANCE_ID},DATABASE_ID=${DATABASE_ID} \
--service-account=${SA_NAME}@${PROJECT_ID}.iam.gserviceaccount.com \
--region=asia-northeast1

spanner-sqlalchemy-demo's People

Contributors

dependabot[bot] avatar kazshinohara avatar yutako0217 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

spanner-sqlalchemy-demo's Issues

Supports SQLAlchemy 1.4

python-spanner-sqlalchemy v1.2.0 (the latest version as of June 29 2022) supports SQLAlchemy 1.4, this sample application also should support SQLAlchemy 1.4 accordingly (currently it supports 1.3.23)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.