def import_data(): from settings import DB n = [] n.append("Beginning import process.") # yield "<p>" + n n.append("Cleaning DB.") # yield "<p>" + n try: DB.clean_database() except: from core.models import init_db init_db.recreate_database() DB.remove_indexes() n.append("Clearing tables.") # yield "<p>" + n xdb = DataSet(DB.dataset_connection()) # with xdb.atomic() as txn: try: with xdb.transaction(): for table_name in xdb.tables: n.append("Loading table " + table_name) # yield "<p>" + n try: table = xdb[table_name] except: n.append("<p>Sorry, couldn't create table ", table_name) else: filename = (APPLICATION_PATH + EXPORT_FILE_PATH + '/dump-' + table_name + '.json') if os.path.exists(filename): try: table.thaw(format='json', filename=filename, strict=True) except Exception as e: n.append("<p>Sorry, error:{}".format(e)) else: n.append("No data for table " + table_name) # yield "<p>" + n except Exception as e: n.append('Ooops: {}'.e) else: xdb.query(DB.post_import()) xdb.close() DB.recreate_indexes() n.append( "Import process ended. <a href='{}'>Click here to continue.</a>". format(BASE_URL)) return '<p>'.join(n)
def setUp(self): # TODO: copy in custom ini file from test folder # maybe have it so that we're using a custom data directory as well? # or at least rename existing data directory # tear down and create new DB from core.models import init_db from core.auth import role init_db.recreate_database() new_user = mgmt.user_create( name="Administrative User", email="*****@*****.**", password="******", ) mgmt.add_user_permission(new_user, permission=role.SYS_ADMIN, site=0)
def recreate_database(): db.close() try: os.remove(settings.FULL_SQLITE_DATABASE_PATH) except FileNotFoundError: pass try: os.mkdir(settings.APPLICATION_PATH + settings._sep + settings.DATA_FILE_PATH) except OSError as e: import errno if e.errno == errno.EEXIST: pass else: raise e init_db.recreate_database()
def make_db_connection(): _stderr("Looking for database in " + settings.DATABASE_PATH + "\n") if settings.RESET or not os.path.exists( settings.FULL_SQLITE_DATABASE_PATH): _stderr("No database found or settings.RESET was set.\n") # db.close() # throws spurious AttributeError when no DB present try: os.remove(settings.FULL_SQLITE_DATABASE_PATH) except FileNotFoundError: _stderr("Database already removed.\n") else: _stderr("Database removed.\n") finally: _stderr("Re-initializing database.\n") init_db.recreate_database()
def recreate_database(): from core.models import init_db init_db.recreate_database()