from sqlalchemy import * |
from migrate import * |
from datetime import datetime |
from migrate.changeset import * |
# ^^^^^this is very important line without it it won't work
|
meta = MetaData(migrate_engine)
|
news = Table('news', meta, |
Column('id', Integer, autoincrement=True, primary_key=True), |
Column('date', DateTime,nullable=False,default=datetime.now), |
Column('title', Unicode(255)), |
Column('short_text', UnicodeText), |
Column('text', UnicodeText), |
Column('author_id', Integer, ForeignKey('tg_user.user_id')), |
)
# ^^^^ existing table in database
|
def upgrade(): |
frontPage = Column('frontPage',Boolean, default=False) |
frontPage.create(news) |
|
def downgrade(): |
frontPage = Column('frontPage',Boolean, default=False) |
frontPage.drop(news) |
Since many database systems do not support altering column types, but
only adding / dropping /renaming them, here's what I did to change the
name and type of a column (but be aware that this will destroy all the
old data in that column):
HTH, Chris
"""Migration script to change the 'old_name' column in table 'foo'."""
# Version 6 database schema
from migrate import migrate_engine
# need to import this to add .alter method to Column objects
# don't remove!
from migrate.changeset import drop_column
from sqlalchemy import MetaData, Table, Column, String, Boolean
metadata = MetaData(migrate_engine)
foo_table = Table('foo', metadata, autoload=True)
def upgrade():
"""Change 'old_name' column."""
# we can not use ALTER to change a String type column into Boolean
# so we drop the column and add a new one
drop_column(af_table.columns['old_name'])
Column('new_name', String(10), default="none").create(foo_table)
def downgrade():
"""Change 'new_name' column back."""
# we can not use ALTER to change a String type column into Boolean
# so we drop the column and add a new one
drop_column(af_table.columns['new_name'])
Column('old_name', Boolean, default=True).create(foo_table)
robneville73 schrieb:
> Does anybody have decent examples of using migrate?? The documentationSince many database systems do not support altering column types, but
> is OK, but pretty sparse...
>
> For example, how do I alter a column on an existing table? I'm not
> following the example provided in
> http://packages.python.org/sqlalchemy-migrate/changeset.html#changeset-system
only adding / dropping /renaming them, here's what I did to change the
name and type of a column (but be aware that this will destroy all the
old data in that column):
HTH, Chris