Skip to content

A demo for how SQLAlchemy can be used to create a database to represent Broadway shows.

Notifications You must be signed in to change notification settings

ybressler/sqlalchemy-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SQLAlchemy Demo

The following repo is a demo for how to create a manage a database using sqlalchemy.

Before you get started

You'll need a database configured to your local machine. I'd suggest using MySQL or Postgres. Both are free and are easy to manage. For the purpose of this demo, it does not matter which database you choose to use.

Getting started

The following is written for unix machines...

  1. Launch a new (and empty) database from your local machine:
  1. Download this repository to your local machine:
git clone https://github.com/ybressler/sqlalchemy-demo.git
  1. Create a virtual environment and activate it. Then, install the requirements:
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

You're now ready to begin!

  1. Store your DB_URI as an environment variables or update the value in the code.
    For example, following default values:
export DB_URI='postgresql://postgres:@localhost:5432/postgres'

For more info as to what's going on here, give this a read: FOO

  1. Export the full path of your current project the environment variable PROJECT_PATH:
export PROJECT_PATH=$(pwd)

This will allow you to execute nested files as executables, without dealing with issues like relative imports. In a production setting, your modules will likely be imported properly and executed from a central orchestrator (such as a web app).

Performing migrations

Your database is currently a blank slate. Let's make it reflect the models we've built:

Getting things ready for migrations


Note: I've taken the pleasure of making all the changes for you in this current repo.

  1. Initialize alembic:
alembic init database/alembic
  1. Edit the alembic.init file to your preferences
  • On line 42, set comment out the following:
sqlalchemy.url = driver://user:pass@localhost/dbname
  1. Edit alembic/env.py as follows:
  • Import your db uri at the top of the module, add the following line of code to do so:
    from database import DB_URI
    
  • After creating your config, set the db URI
# After the line where `config = context.config` appears...
# (should be about line 15)
config = context.config
config.set_main_option('sqlalchemy.url', DB_URI)
  • That's it! You're done

Running the actual migration

  1. Run your first migration, automatically detect changes:
alembic revision --autogenerate -m "first migration"
  1. Review the autogenerated file in alembic/versions
  • If it looks kosher, you good!
  • If it doesn't look kosher, take the pleasure of fixing things manually. (Yes, you are supposed to finagle with these files.)
  1. Once you're happy with the changes, run the upgrade command:
alembic upgrade head
  1. Your database is now up to date!
  2. Generate an ERD for the thrill of it!
python3 database/methods/generate_erd.py

Adding data to your new db

We'll add a few records with the following module: populate_db.py

Do a cool query

Querying your database can be done through a python interface. Pretty neat! Check out some of the querying behavior in the following module: some_query.py


Generate an ERD

Create a cool picture representation of your db: generate_erd.py

About

A demo for how SQLAlchemy can be used to create a database to represent Broadway shows.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published