Skip to content

Instantly share code, notes, and snippets.

@simonthompson99
Last active January 17, 2023 11:04
Show Gist options
  • Save simonthompson99/df0615d25534f8da952792519b16d4ff to your computer and use it in GitHub Desktop.
Save simonthompson99/df0615d25534f8da952792519b16d4ff to your computer and use it in GitHub Desktop.
[Alembic Cheatsheet] Some basic commands for Alembic #python #database #alembic
# initiate alembic in project
alembic init alembic
#####################################
# need to edit alembic/env.py so that gets connection string from settings/local_config python module
# add in below around about line 17
# add parent project to sys path
import sys, os
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
# source db uri
import <settings/config_module> as my_config
config.set_main_option('sqlalchemy.url', my_config.<whatever the connection string object is called>)
# get SQLalchemy model declarative base
from models import Base
target_metadata = Base.metadata
#####################################
# run below to create the alembic_version table so we can start tracking revisions (version)
alembic stamp head
# run below when making revisions
alembic revision --autogenerate -m "<revision message>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment