Skip to content

Instantly share code, notes, and snippets.

@goFrendiAsgard
Created March 20, 2014 17:51
Show Gist options
  • Save goFrendiAsgard/9669803 to your computer and use it in GitHub Desktop.
Save goFrendiAsgard/9669803 to your computer and use it in GitHub Desktop.
SQLAlchemy and alembic
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
############################### SQL ALCHEMY SCRIPT ####################################
# create Base
Base = declarative_base()
# create Pokemon class
class Pokemon(Base):
__tablename__ = 'pokemon'
id = Column(Integer, primary_key=True)
name = Column(String)
image = Column(String)
def __init__(self, name, image):
self.name = name
self.image = image
# create engine
engine = create_engine('sqlite:///db/pokemon.db', echo=True)
# just a simple upgrade
def upgrade():
from alembic.migration import MigrationContext
from alembic.operations import Operations
from sqlalchemy import DateTime
conn = engine.connect()
ctx = MigrationContext.configure(conn)
op = Operations(ctx)
op.add_column('pokemon', Column('birth', DateTime))
# create the table
Base.metadata.create_all(bind=engine)
# do simple upgrade, add birth column
upgrade()
@goFrendiAsgard
Copy link
Author

A very simple sqlalchemy + alembic demonstration.

After making pokemon table, a new column added to the table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment