working db and tests Nr2
This commit is contained in:
0
database/__init__.py
Normal file
0
database/__init__.py
Normal file
20
database/database.py
Normal file
20
database/database.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from sqlalchemy import create_engine, MetaData
|
||||
from sqlalchemy.orm import scoped_session, sessionmaker
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
|
||||
engine = create_engine('sqlite:////tmp/test.db', convert_unicode=True)
|
||||
db_session = scoped_session(sessionmaker(autocommit=False,
|
||||
autoflush=False,
|
||||
bind=engine))
|
||||
Base = declarative_base()
|
||||
Base.query = db_session.query_property()
|
||||
metadata = MetaData()
|
||||
|
||||
|
||||
def init_db():
|
||||
# import all modules here that might define models so that
|
||||
# they will be registered properly on the metadata. Otherwise
|
||||
# you will have to import them first before calling init_db()
|
||||
import app.models.user
|
||||
import app.models.lock
|
||||
metadata.create_all(bind=engine)
|
||||
13
database/db_create.py
Normal file
13
database/db_create.py
Normal file
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from migrate.versioning import api
|
||||
from backend.config import SQLALCHEMY_DATABASE_URI
|
||||
from backend.config import SQLALCHEMY_MIGRATE_REPO
|
||||
from backend 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))
|
||||
9
database/db_downgrade.py
Normal file
9
database/db_downgrade.py
Normal file
@@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from migrate.versioning import api
|
||||
from backend.config import SQLALCHEMY_DATABASE_URI
|
||||
from backend.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)
|
||||
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
|
||||
print('Current database version: ' + str(v))
|
||||
20
database/db_migrate.py
Normal file
20
database/db_migrate.py
Normal file
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import imp
|
||||
from migrate.versioning import api
|
||||
from backend import db
|
||||
from backend.config import SQLALCHEMY_DATABASE_URI
|
||||
from backend.config import SQLALCHEMY_MIGRATE_REPO
|
||||
|
||||
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
|
||||
migration = SQLALCHEMY_MIGRATE_REPO + ('/versions/%03d_migration.py' % (v+1))
|
||||
tmp_module = imp.new_module('old_model')
|
||||
old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
|
||||
exec(old_model, tmp_module.__dict__)
|
||||
extra_imports = 'import datetime\n' # your imports
|
||||
script = extra_imports + 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)
|
||||
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
|
||||
print('New migration saved as ' + migration)
|
||||
print('Current database version: ' + str(v))
|
||||
13
database/db_populate.py
Normal file
13
database/db_populate.py
Normal file
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from backend import db
|
||||
from backend.models import example_model, user_model, post_model
|
||||
|
||||
user = user_model.User(nickname="tobi", email="privat@t-kurze.de")
|
||||
user.set_password("abcxyz")
|
||||
db.session.add(user)
|
||||
|
||||
example_data = example_model.ExampleDataItem(name="tolles data item", mac="12:34:56:78:90:AB")
|
||||
db.session.add(example_data)
|
||||
|
||||
db.session.commit()
|
||||
8
database/db_upgrade.py
Normal file
8
database/db_upgrade.py
Normal file
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from migrate.versioning import api
|
||||
from backend.config import SQLALCHEMY_DATABASE_URI
|
||||
from backend.config import SQLALCHEMY_MIGRATE_REPO
|
||||
api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
|
||||
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
|
||||
print('Current database version: ' + str(v))
|
||||
Reference in New Issue
Block a user