def init_db(): with app.app_context(): # 需要用这句来加载app的上下文环境 # 测试前按以下配置重置数据库 db.drop_all() db.create_all() def add_items(): u1 = User(username='******', password='******', email='*****@*****.**') u2 = User(username='******', password='******', email='*****@*****.**') c1 = Club(club_name='yuanhuo', introduction="yuanhuo introduction", president_id=1) c2 = Club(club_name='feiying', introduction="feiying introduction", president_id=2) po1 = Post(title='one', text='jd is too strong', club_id=1) po2 = Post(title='two', text="let's compliment jd", club_id=1) po3 = Post(title='three', text="let's compliment j", club_id=2) po4 = Post(title='four', text="let's compliment j", club_id=2) u1.followed_clubs.append(c1) u2.managed_clubs.append(c1) db.session.add_all([u1, u2, po1, po2, po3, po4, c1, c2]) db.session.commit() add_items()
def get_pager(): db.drop_all() db.create_all() for i in range(100): user = User(name='{}'.format(str(i)), password='******'.format(str(i))) db.session.add(user) db.session.commit() page = request.args.get('page', 1, type=int) pagination = User.query.paginate(page, 10) messages = pagination.items return render_template('get_pager.html', pagination=pagination, messages=messages)
def init_db_command(): '''Clear existing data and create new tables.''' db.drop_all() db.create_all() db.engine.execute("INSERT INTO pink (id, name, active) " f"VALUES ('{'0' * 9}', 'SYSTEM', False);") now = datetime.datetime.utcnow() txn_info = f'None_0_0_init_{"0" * 9}_{now}' id_ = sha3_256(txn_info.encode('utf-16')).hexdigest() db.engine.execute( "INSERT INTO txn (id, previous_id, debit, credit, reason, pink_id, timestamp) " f"VALUES ('{id_}', '{id_}', 0, 0, 'init', '{'0' * 9}', '{now.isoformat()}');" ) click.echo('\nDatabase Initialized')
def dropdb(): db.drop_all() return "dropdb success"
def tearDown(self) -> None: db.session.remove() db.drop_all() self.app_context.pop()
def test_init(self): with app.app_context(): db.drop_all() db.create_all() add_items()
def test_init(self): with app.app_context(): # 需要用这句来加载app的上下文环境 # 按以下配置重置数据库 db.drop_all() db.create_all() add_items()
def init(): db.drop_all() db.create_all() return 'init success'
def create(): """重置所有数据表""" from model.models import BostonHousingDataSet, Predict db.drop_all() db.create_all() return {'message': 'successful'}
def restartDB(): db.drop_all() print('DB dropped.')
def restartDB(): db.drop_all() db.create_all() print('DB restarted.')