Skip to content

Commit

Permalink
try db_migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
realmadrid committed May 1, 2017
1 parent bcaf70e commit 3fd6c51
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 4 deletions.
Binary file modified app.db
Binary file not shown.
14 changes: 12 additions & 2 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask_sqlalchemy import SQLAlchemy
from app import views, models

import os
from flask.ext.login import LoginManager
from flask.ext.openid import OpenID
from config import basedir
# from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)

from app import views, models

lm = LoginManager()
lm.init_app(app)
oid = OpenID(app, os.path.join(basedir, 'tmp'))
3 changes: 2 additions & 1 deletion app/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask.ext.wtf import Form
from flask_wtf import Form
# from flask.ext.wtf import Form
from wtforms import StringField, BooleanField
from wtforms.validators import DataRequired

Expand Down
16 changes: 15 additions & 1 deletion app/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
from app import db


class User(db.Model):
# 对于一个一对多的关系,db.relationship 字段通常是定义在“一”这一边
# 在这种关系下,得到一个 user.posts 成员,它给出一个用户所有的 blog
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)
post = 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 '<User %r>' % (self.nickname)
return '<Post %r>' % self.body
28 changes: 28 additions & 0 deletions db_repository/versions/002_migration.py
Original file line number Diff line number Diff line change
@@ -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()
49 changes: 49 additions & 0 deletions tmp/add_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from app import db, models
'''
u = models.User(nickname='Scott', email='[email protected]')
db.session.add(u)
db.session.commit()
u = models.User(nickname='Lucine', email='[email protected]')
db.session.add(u)
db.session.commit()
users = models.User.query.all()
print(users)
u = models.User.query.get(1)
print(u)
import datetime
u = models.User.query.get(1)
p = models.Post(body='my first post!', timestamp=datetime.datetime.utcnow(), author=u)
db.session.add(p)
db.session.commit()
# get all posts from a user
u = models.User.query.get(1)
print(u)
posts = u.posts.all()
print(posts)
# obtain author of each post
for p in posts:
print(p.id,p.author.nickname,p.body)
# a user that has no posts
u = models.User.query.get(2)
print(u)
print(u.posts.all())
# get all users in reverse alphabetical order
print(models.User.query.order_by('nickname desc').all())
'''


users = models.User.query.all()
for u in users:
db.session.delete(u)
posts = models.Post.query.all()
for p in posts:
db.session.delete(p)
db.session.commit()

0 comments on commit 3fd6c51

Please sign in to comment.