def admin(username, password): """ create an admin account""" db.create_all() user = User.query.first() if user is not None: click.echo("updating user...") user.username = username user.set_password(password) else: click.echo("Creating user...") user = User(username=username, name='Admin') user.set_password(password) db.session.add(user) db.session.commit() click.echo('Done')
def forge(count): """ fake data to database """ db.create_all() fake = Faker('zh_CN') fake.add_provider(lorem) title_list = fake.sentences(nb=100) for i in range(0, count): article = Article(title=title_list[i], content=fake.text(3000), time=datetime.datetime.now()) db.session.add(article) click.echo("created %d articles" % count) db.session.commit() click.echo(".Done") pass
def init_db(): db.drop_all() db.create_all() # adding test user data hashed_pass1 = bcrypt.generate_password_hash('password') hashed_pass2 = bcrypt.generate_password_hash('testing') user = User(username='******', email='*****@*****.**', password=hashed_pass1) user1 = User(username='******', email='*****@*****.**', password=hashed_pass2) db.session.add(user) db.session.add(user1) db.session.commit() # adding testing project data project = Project(title='test', content='test', user_id=user.id, id=1) project1 = Project(title='test', content='test', user_id=user1.id, id=2) db.session.add(project) db.session.add(project1) db.session.commit() #adding test article data article = Article(title='test', content='test', user_id=user.id, id=1) article1 = Article(title='test', content='test', user_id=user1.id, id=2) db.session.add(article) db.session.add(article1) db.session.commit() #adding test comment data reply = Reply(content='test_comment', username=user.username, project_id=project.id) db.session.add(reply) db.session.commit() #adding test reply data tore = Tore(content='test_reply', username=user.username, reply_id=reply.id) db.session.add(tore) db.session.commit() # tests happen HERE! yield db db.drop_all()
def init_db(scope="session"): db.session.close() db.drop_all() db.create_all() # adding test user data hashed_pass1 = bcrypt.generate_password_hash("password").decode("utf-8") hashed_pass2 = bcrypt.generate_password_hash("testing").decode("utf-8") user = User(username="******", email="*****@*****.**", password=hashed_pass1) user1 = User(username="******", email="*****@*****.**", password=hashed_pass2) db.session.add(user) db.session.add(user1) db.session.commit() # adding testing project data project = Project(title="test", content="test", user_id=user.id) project1 = Project(title="test", content="test", user_id=user1.id) db.session.add(project) db.session.add(project1) db.session.commit() #adding test article data article = Article(title="test", content="test", user_id=user.id) article1 = Article(title="test", content="test", user_id=user1.id) db.session.add(article) db.session.add(article1) db.session.commit() #adding test comment data reply = Reply(content="test_comment",username=user.username, project_id=project.id) db.session.add(reply) db.session.commit() #adding test reply data tore = Tore(content="test_reply",username=user.username, reply_id=reply.id) db.session.add(tore) db.session.commit() # tests happen HERE! yield db db.session.close() db.drop_all()
from myblog import create_app, db app = create_app() with app.app_context(): db.create_all() if __name__ == "__main__": app.run(debug=True)
# -*- coding: utf-8 -*- from migrate.versioning import api from configs import SQLALCHEMY_DATABASE_URI from configs import SQLALCHEMY_MIGRATE_REPO from myblog 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))
def initdb(drop): ''' init the database ''' if drop: db.drop_all() db.create_all() click.echo("Initialized database")
def createdb_command(): """Clear the existing data and create new tables""" db.create_all() click.echo("Initialized the database")
def initdb(): from myblog import db db.create_all()
def setup_method(self, method): db.drop_all() db.create_all()