1 | """ |
---|
2 | Migration script to add support for "Pages". |
---|
3 | 1) Creates Page and PageRevision tables |
---|
4 | 2) Adds username column to User table |
---|
5 | """ |
---|
6 | |
---|
7 | from sqlalchemy import * |
---|
8 | from migrate import * |
---|
9 | from migrate.changeset import * |
---|
10 | |
---|
11 | import datetime |
---|
12 | now = datetime.datetime.utcnow |
---|
13 | |
---|
14 | import logging |
---|
15 | log = logging.getLogger( __name__ ) |
---|
16 | |
---|
17 | metadata = MetaData( migrate_engine ) |
---|
18 | |
---|
19 | Page_table = Table( "page", metadata, |
---|
20 | Column( "id", Integer, primary_key=True ), |
---|
21 | Column( "create_time", DateTime, default=now ), |
---|
22 | Column( "update_time", DateTime, default=now, onupdate=now ), |
---|
23 | Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True, nullable=False ), |
---|
24 | Column( "latest_revision_id", Integer, |
---|
25 | ForeignKey( "page_revision.id", use_alter=True, name='page_latest_revision_id_fk' ), index=True ), |
---|
26 | Column( "title", TEXT ), |
---|
27 | Column( "slug", TEXT, unique=True, index=True ), |
---|
28 | ) |
---|
29 | |
---|
30 | PageRevision_table = Table( "page_revision", metadata, |
---|
31 | Column( "id", Integer, primary_key=True ), |
---|
32 | Column( "create_time", DateTime, default=now ), |
---|
33 | Column( "update_time", DateTime, default=now, onupdate=now ), |
---|
34 | Column( "page_id", Integer, ForeignKey( "page.id" ), index=True, nullable=False ), |
---|
35 | Column( "title", TEXT ), |
---|
36 | Column( "content", TEXT ) |
---|
37 | ) |
---|
38 | |
---|
39 | def upgrade(): |
---|
40 | print __doc__ |
---|
41 | metadata.reflect() |
---|
42 | try: |
---|
43 | Page_table.create() |
---|
44 | except: |
---|
45 | log.debug( "Could not create page table" ) |
---|
46 | try: |
---|
47 | PageRevision_table.create() |
---|
48 | except: |
---|
49 | log.debug( "Could not create page_revision table" ) |
---|
50 | |
---|
51 | # Add 1 column to the user table |
---|
52 | User_table = Table( "galaxy_user", metadata, autoload=True ) |
---|
53 | col = Column( 'username', String(255), index=True, unique=True, default=False ) |
---|
54 | col.create( User_table ) |
---|
55 | assert col is User_table.c.username |
---|
56 | |
---|
57 | def downgrade(): |
---|
58 | metadata.reflect() |
---|
59 | Page_table.drop() |
---|
60 | PageRevision_table.drop() |
---|
61 | User_table = Table( "galaxy_user", metadata, autoload=True ) |
---|
62 | User_table.c.username.drop() |
---|