database
This commit is contained in:
parent
e3bc41b9f5
commit
df88f0f7a0
|
@ -1,6 +1,9 @@
|
|||
from flask import Flask
|
||||
from flask.ext.sqlalchemy import SQLAlchemy
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.from_object('config')
|
||||
db = SQLAlchemy(app)
|
||||
|
||||
from app import views, models
|
||||
|
||||
from app import views
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
from app import db
|
||||
|
||||
ROLE_USER = 0
|
||||
ROLE_ADMIN = 1
|
||||
|
||||
class User(db.Model):
|
||||
id = db.Column(db.Integer, primary_key = True)
|
||||
nickname = db.Column(db.String(64), index = True, unique = True)
|
||||
email = db.Column(db.String(120), index = True, unique = True)
|
||||
role = db.Column(db.SmallInteger, default = ROLE_USER)
|
||||
posts = db.relationship('Post', backref = 'author', lazy = 'dynamic')
|
||||
|
||||
def __repr__(self):
|
||||
return '<User %r>' % (self.nickname)
|
||||
|
||||
class Post(db.Model):
|
||||
id = db.Column(db.Integer, primary_key = True)
|
||||
body = db.Column(db.String(140))
|
||||
timestamp = db.Column(db.DateTime)
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
||||
|
||||
def __repr__(self):
|
||||
return '<Post %r>' % (self.body)
|
|
@ -1,3 +1,6 @@
|
|||
import os
|
||||
basedir = os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
CSRF_ENABLED = True
|
||||
SECRET_KEY = 'you-will-never-guess'
|
||||
|
||||
|
@ -6,4 +9,7 @@ OPENID_PROVIDERS = [
|
|||
{ 'name': 'Yahoo', 'url': 'https://me.yahoo.com' },
|
||||
{ 'name': 'AOL', 'url': 'http://openid.aol.com/<username>' },
|
||||
{ 'name': 'Flickr', 'url': 'http://www.flickr.com/<username>' },
|
||||
{ 'name': 'MyOpenID', 'url': 'https://www.myopenid.com' }]
|
||||
{ 'name': 'MyOpenID', 'url': 'https://www.myopenid.com' }]
|
||||
|
||||
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db')
|
||||
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')
|
|
@ -0,0 +1,12 @@
|
|||
#!flask/bin/python
|
||||
from migrate.versioning import api
|
||||
from config import SQLALCHEMY_DATABASE_URI
|
||||
from config import SQLALCHEMY_MIGRATE_REPO
|
||||
from app import db
|
||||
import os.path
|
||||
db.create_all()
|
||||
if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
|
||||
api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')
|
||||
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
|
||||
else:
|
||||
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO))
|
|
@ -0,0 +1,7 @@
|
|||
#!flask/bin/python
|
||||
from migrate.versioning import api
|
||||
from config import SQLALCHEMY_DATABASE_URI
|
||||
from config import SQLALCHEMY_MIGRATE_REPO
|
||||
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
|
||||
api.downgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, v - 1)
|
||||
print 'Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO))
|
|
@ -0,0 +1,15 @@
|
|||
#!flask/bin/python
|
||||
import imp
|
||||
from migrate.versioning import api
|
||||
from app import db
|
||||
from config import SQLALCHEMY_DATABASE_URI
|
||||
from config import SQLALCHEMY_MIGRATE_REPO
|
||||
migration = SQLALCHEMY_MIGRATE_REPO + '/versions/%03d_migration.py' % (api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + 1)
|
||||
tmp_module = imp.new_module('old_model')
|
||||
old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
|
||||
exec old_model in tmp_module.__dict__
|
||||
script = api.make_update_script_for_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, tmp_module.meta, db.metadata)
|
||||
open(migration, "wt").write(script)
|
||||
api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
|
||||
print 'New migration saved as ' + migration
|
||||
print 'Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO))
|
|
@ -0,0 +1,4 @@
|
|||
This is a database migration repository.
|
||||
|
||||
More information at
|
||||
http://code.google.com/p/sqlalchemy-migrate/
|
|
@ -0,0 +1,5 @@
|
|||
#!/usr/bin/env python
|
||||
from migrate.versioning.shell import main
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
|
@ -0,0 +1,25 @@
|
|||
[db_settings]
|
||||
# Used to identify which repository this database is versioned under.
|
||||
# You can use the name of your project.
|
||||
repository_id=database repository
|
||||
|
||||
# The name of the database table used to track the schema version.
|
||||
# This name shouldn't already be used by your project.
|
||||
# If this is changed once a database is under version control, you'll need to
|
||||
# change the table name in each database too.
|
||||
version_table=migrate_version
|
||||
|
||||
# When committing a change script, Migrate will attempt to generate the
|
||||
# sql for all supported databases; normally, if one of them fails - probably
|
||||
# because you don't have that database installed - it is ignored and the
|
||||
# commit continues, perhaps ending successfully.
|
||||
# Databases in this list MUST compile successfully during a commit, or the
|
||||
# entire commit will fail. List the databases your application will actually
|
||||
# be using to ensure your updates to that database work properly.
|
||||
# This must be a list; example: ['postgres','sqlite']
|
||||
required_dbs=[]
|
||||
|
||||
# When creating new change scripts, Migrate will stamp the new script with
|
||||
# a version number. By default this is latest_version + 1. You can set this
|
||||
# to 'true' to tell Migrate to use the UTC timestamp instead.
|
||||
use_timestamp_numbering=False
|
|
@ -0,0 +1,28 @@
|
|||
from sqlalchemy import *
|
||||
from migrate import *
|
||||
|
||||
|
||||
from migrate.changeset import schema
|
||||
pre_meta = MetaData()
|
||||
post_meta = MetaData()
|
||||
user = Table('user', post_meta,
|
||||
Column('id', Integer, primary_key=True, nullable=False),
|
||||
Column('nickname', String(length=64)),
|
||||
Column('email', String(length=120)),
|
||||
Column('role', SmallInteger, default=ColumnDefault(0)),
|
||||
)
|
||||
|
||||
|
||||
def upgrade(migrate_engine):
|
||||
# Upgrade operations go here. Don't create your own engine; bind
|
||||
# migrate_engine to your metadata
|
||||
pre_meta.bind = migrate_engine
|
||||
post_meta.bind = migrate_engine
|
||||
post_meta.tables['user'].create()
|
||||
|
||||
|
||||
def downgrade(migrate_engine):
|
||||
# Operations to reverse the above upgrade go here.
|
||||
pre_meta.bind = migrate_engine
|
||||
post_meta.bind = migrate_engine
|
||||
post_meta.tables['user'].drop()
|
|
@ -0,0 +1,28 @@
|
|||
from sqlalchemy import *
|
||||
from migrate import *
|
||||
|
||||
|
||||
from migrate.changeset import schema
|
||||
pre_meta = MetaData()
|
||||
post_meta = MetaData()
|
||||
post = Table('post', post_meta,
|
||||
Column('id', Integer, primary_key=True, nullable=False),
|
||||
Column('body', String(length=140)),
|
||||
Column('timestamp', DateTime),
|
||||
Column('user_id', Integer),
|
||||
)
|
||||
|
||||
|
||||
def upgrade(migrate_engine):
|
||||
# Upgrade operations go here. Don't create your own engine; bind
|
||||
# migrate_engine to your metadata
|
||||
pre_meta.bind = migrate_engine
|
||||
post_meta.bind = migrate_engine
|
||||
post_meta.tables['post'].create()
|
||||
|
||||
|
||||
def downgrade(migrate_engine):
|
||||
# Operations to reverse the above upgrade go here.
|
||||
pre_meta.bind = migrate_engine
|
||||
post_meta.bind = migrate_engine
|
||||
post_meta.tables['post'].drop()
|
|
@ -0,0 +1,6 @@
|
|||
#!flask/bin/python
|
||||
from migrate.versioning import api
|
||||
from config import SQLALCHEMY_DATABASE_URI
|
||||
from config import SQLALCHEMY_MIGRATE_REPO
|
||||
api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
|
||||
print 'Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO))
|
Loading…
Reference in New Issue