Created
March 20, 2014 17:51
-
-
Save goFrendiAsgard/9669803 to your computer and use it in GitHub Desktop.
SQLAlchemy and alembic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A very simple sqlalchemy + alembic demonstration.
After making pokemon table, a new column added to the table.