+ def create_admin(self):
+ """
+ Creates new 'admin' user and project into database if database is empty. Useful for initialization.
+ :return: _id identity of the inserted data, or None
+ """
+ project_id = self.create_admin_project()
+ user_id = self.create_admin_user()
+ if not project_id and not user_id:
+ return None
+ else:
+ return {'project_id': project_id, 'user_id': user_id}
+
+ def upgrade_db(self, current_version, target_version):
+ if not target_version or current_version == target_version:
+ return
+ if target_version == '1.0':
+ if not current_version:
+ # create database version
+ serial = urandom(32)
+ version_data = {
+ "_id": 'version', # Always 'version'
+ "version_int": 1000, # version number
+ "version": '1.0', # version text
+ "date": "2018-10-25", # version date
+ "description": "added serial", # changes in this version
+ 'status': 'ENABLED', # ENABLED, DISABLED (migration in process), ERROR,
+ 'serial': b64encode(serial)
+ }
+ self.db.create("admin", version_data)
+ self.db.set_secret_key(serial)
+ return
+ # TODO add future migrations here
+
+ raise EngineException("Wrong database version '{}'. Expected '{}'"
+ ". It cannot be up/down-grade".format(current_version, target_version),
+ http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
+