def create_db_if_not_exists(): exists = path.exists("/backend/database/database.db") if not exists: print("No DB found, creating a new one in /backend/database.") db.create_all() else: print("DB already exists, skipping.")
def check_database(): try: ### Create Tables db.create_all() ### Add Admin User admin_user = User.register(username="******", password="******", usertype=0) except Exception as e: print(e)
def init_db(): """ Drops and re-creates the SQL schema """ db.drop_all() db.configure_mappers() db.create_all() db.session.commit()
def setup_db(): db.create_all() if not Role.query.filter_by(authorisation='admin').first() or not Role.query.filter_by(authorisation='user').first(): role_teacher = Role(name='teacher', authorisation='admin') role_student = Role(name='student', authorisation='user') with app.app_context(): db.session.add_all([role_teacher, role_student]) db.session.commit()
def database(): db.session.remove() db.reflect() db.drop_all() db.create_all() yield db # this is where the testing happens! db.drop_all()
def client(): """ My custom test client. """ app.config['TESTING'] = True app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' # app.config['SQLALCHEMY_DATABASE_URI'] = \ # 'mysql+mysqlconnector://test:Test12345678!@localhost/iot_test' with app.app_context(): db.drop_all() db.create_all() # Add test user user = User(username='******', email='*****@*****.**') user.set_password('test') db.session.add(user) db.session.commit() # Add test device device = Device(name='test', display_name='test', user=user) db.session.add(device) db.session.commit() schema = { 'test1': ['test1', 2, 1, 0], 'test2': ['test2', 2, 1, 0], 'control': ['control', 3, 1, 1], } device.set_schema(schema) # Add test data new_data = DeviceData(time=datetime.utcfromtimestamp(1500000000), data={ 'test1': 10.0, 'test2': 12.0, 'control': False, }, device=device) db.session.add(new_data) db.session.commit() # Get token access_token = create_access_token(identity=user.username) refresh_token = create_refresh_token(identity=user.username) my_client = MyTestClient(access_token, refresh_token, default_auth=TokenType.access) yield my_client with app.app_context(): db.session.remove() db.drop_all()
def clear_db(db): confirm = input('Clear all tables? [yes/no]: ') if confirm == 'yes': # drop/create tables print('#'*60) print('Dropping all tables') print('#'*60) db.drop_all() print('#'*60) print('Creating all tables') print('#'*60) db.create_all() print('All tables cleared') elif confirm == 'no': exit(1) else: print('Please type yes or no') clear_db(db)
def app(): """Set up and tear down the test app.""" from run import create_app, db app = create_app('testing') app.config['TESTING'] = True app_context = app.app_context() app_context.push() db.create_all() yield app db.session.remove() db.drop_all() app_context.pop()
def initial_db(): ### Drop All tables db.reflect() db.drop_all() ### Clear sessions session.clear() session.pop('username', None) ### Create Tables db.create_all() ### Create Roles administrative_staff = models.Role(name='IdariPersonel') teacher = models.Role(name='Ogretmen') student = models.Role(name='Ogrenci') db.session.add(administrative_staff) db.session.add(teacher) db.session.add(student) db.session.commit() ### Create Role Funcs func_create_user = models.RoleFuncs(name='CreateUser', role_id=administrative_staff.id) func_see_all_user = models.RoleFuncs(name='SeeAllUser', role_id=administrative_staff.id) db.session.add(func_create_user) db.session.add(func_see_all_user) db.session.commit() func_danisman_ata = models.RoleFuncs(name='AssignCons', role_id=administrative_staff.id) db.session.add(func_danisman_ata) db.session.commit() ###################### Create Administrative Staff new_administrative_staff = create_new_user( uname="admin", password="******", fname= "Mustafa", mname= "Kemal", sname= "Ataturk", email= "*****@*****.**", role_id=administrative_staff.id ) return '<a href="/">Ok!</a>'
def setUp(self): self.app = self.create_app().test_client() self.database = db db.create_all() # Add dummy data for test purposes user = UserModel(username="******") user.email = '*****@*****.**' user.password = '******' user.save_to_db() user2 = UserModel(username="******") user2.email = '*****@*****.**' user2.password = '******' user2.save_to_db() user3 = UserModel(username="******") user3.email = '*****@*****.**' user3.password = '******' user3.save_to_db() category1 = CategoryModel(name="somerecipecategory", created_by=1) category1.save_to_db() category2 = CategoryModel(name="somerecipecategory2", created_by=1) category2.save_to_db() recipe1 = RecipeModel(name="somerecipe1", description="Add one spoonfuls of...", created_by=1, category_id=1) recipe1.save_to_db() recipe2 = RecipeModel(name="somerecipe2", description="Add two spoonfuls of...", created_by=1, category_id=2) recipe2.save_to_db()
from run import db; db.create_all()
def run(): db.create_all() print('run_db')
def insertData(): Course() db.create_all() courses = data.load_data() res = insertCoursesIntoDatabase(courses) return jsonify(res)
def init_db(): db.create_all() db.session.commit()
def setUp(self): db.create_all()
from migrate.versioning import api from config import SQLALCHEMY_DATABASE_URI from config import SQLALCHEMY_MIGRATE_REPO from run 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))
@validates("message_type") def validate_type(self, key, value): assert value in MESSAGE_TYPES return value def __init__(self, body, message_type): self.body = body self.message_type = message_type @property def sms_body(self): return "{}: {!r}".format(self.body, self.product) def to_dict(self): return { "pk": self.pk, "body": self.body, "message_type": self.message_type, "product_id": self.product_id, } if __name__ == "__main__": # Run this file directly to create the database tables. from run import app print("Creating database tables...") db.create_all(app=app) print("Done!")
def setUp(self): db.create_all() db.session.commit()
id = Column('id', Integer, primary_key=True) username = Column('username', String(50)) phone = Column('phone', String(20)) def __repr__(self): return '<Users {}>'.format(self.username) class Orders(db.Model): __tablename__ = "orders" id = Column('id', Integer, primary_key=True) product_id = Column(Integer, ForeignKey('products.id')) customer_phone = Column('customer_phone', String(15)) shipped = Column('shipped', Boolean()) class Products(db.Model): __tablename__ = "products" id = Column('id', Integer, primary_key=True) user_id = Column(Integer, ForeignKey('users.id')) message = Column('message', String(200)) if __name__ == "__main__": db.create_all()
def api_delete_all(): db.drop_all() db.create_all() create_test_data() return {'status': 'Successfully deleted all database data'}, 200