def get_or_create_db(database_name: str, sqlalchemy_uri: str, always_create: Optional[bool] = True) -> Database: # pylint: disable=import-outside-toplevel from superset import db from superset.models import core as models database = (db.session.query( models.Database).filter_by(database_name=database_name).first()) # databases with a fixed UUID uuids = { "examples": EXAMPLES_DB_UUID, } if not database and always_create: logger.info("Creating database reference for %s", database_name) database = models.Database(database_name=database_name, uuid=uuids.get(database_name)) db.session.add(database) database.set_sqlalchemy_uri(sqlalchemy_uri) db.session.commit() # todo: it's a bad idea to do an update in a get/create function if database and database.sqlalchemy_uri_decrypted != sqlalchemy_uri: database.set_sqlalchemy_uri(sqlalchemy_uri) db.session.commit() return database
def create_database(form): # create database connection database_name = form.get('database_name') if database_name is None: return None sqlalchemy_uri = form.get('sqlalchemy_uri') extra = form.get('extra') impersonate_user = eval(form.get('impersonate_user')) db_models = (db.session.query( models.Database).filter_by(database_name=database_name).all()) if len(db_models) == 0: db_model = models.Database(database_name=database_name, sqlalchemy_uri=sqlalchemy_uri, extra=extra, impersonate_user=impersonate_user) db.session.add(db_model) db.session.commit() database_id = db_model.id logging.info('database connection is created with id = ' + str(database_id)) return database_id else: # pick first one logging.info('reused database connection with id = ' + str(db_models[0].id)) return db_models[0].id
def get_or_create_main_db(): logging.info('Creating database reference') dbobj = (db.session.query( models.Database).filter_by(database_name='main').first()) if not dbobj: dbobj = models.Database(database_name='main') dbobj.set_sqlalchemy_uri(conf.get('SQLALCHEMY_DATABASE_URI')) dbobj.expose_in_sqllab = True dbobj.allow_run_sync = True db.session.add(dbobj) db.session.commit() return dbobj
def get_or_create_datasource(name, uri, **kwargs): dbobj = db.session.query( models.Database).filter_by(database_name=name).first() if not dbobj: dbobj = models.Database(database_name=name, expose_in_sqllab=True, **kwargs) dbobj.set_sqlalchemy_uri(uri) db.session.add(dbobj) db.session.commit() return dbobj
def get_or_create_main_db(): from superset import conf, db from superset.models import core as models logging.info('Creating database reference') dbobj = get_main_database(db.session) if not dbobj: dbobj = models.Database(database_name='main') dbobj.set_sqlalchemy_uri(conf.get('SQLALCHEMY_DATABASE_URI')) dbobj.expose_in_sqllab = True dbobj.allow_csv_upload = True db.session.add(dbobj) db.session.commit() return dbobj
def get_or_create_metadata_db(): database_name = 'metadata' dbobj = db.session.query(models.Database).filter_by( database_name=database_name).first() if not dbobj: dbobj = models.Database( database_name=database_name, expose_in_sqllab=True) dbobj.set_sqlalchemy_uri(conf.get('METADATA_DATABASE_URI')) db.session.add(dbobj) db.session.commit() return dbobj
def get_or_create_gitbase_db(): database_name = 'gitbase' dbobj = db.session.query( models.Database).filter_by(database_name=database_name).first() if not dbobj: dbobj = models.Database(database_name=database_name, expose_in_sqllab=True, allow_run_async=True, allow_dml=True) dbobj.set_sqlalchemy_uri(conf.get('GITBASE_DATABASE_URI')) db.session.add(dbobj) db.session.commit() return dbobj
def get_or_create_db(database_name, sqlalchemy_uri, *args, **kwargs): from superset import db from superset.models import core as models database = ( db.session.query(models.Database).filter_by(database_name=database_name).first() ) if not database: logging.info(f"Creating database reference for {database_name}") database = models.Database(database_name=database_name, *args, **kwargs) db.session.add(database) database.set_sqlalchemy_uri(sqlalchemy_uri) db.session.commit() return database
def get_or_create_db(database_name: str, sqlalchemy_uri: str, *args: Any, **kwargs: Any) -> "Database": from superset import db from superset.models import core as models database = (db.session.query( models.Database).filter_by(database_name=database_name).first()) if not database: logger.info("Creating database reference for %s", database_name) database = models.Database(database_name=database_name, *args, **kwargs) db.session.add(database) database.set_sqlalchemy_uri(sqlalchemy_uri) db.session.commit() return database
def get_or_create_db(database_name: str, sqlalchemy_uri: str, always_create: Optional[bool] = True) -> "Database": from superset import db from superset.models import core as models database = (db.session.query( models.Database).filter_by(database_name=database_name).first()) if not database and always_create: logger.info("Creating database reference for %s", database_name) database = models.Database(database_name=database_name) db.session.add(database) if database: database.set_sqlalchemy_uri(sqlalchemy_uri) db.session.commit() return database
def decode_dashboards(o): """ Function to be passed into json.loads obj_hook parameter Recreates the dashboard object from a json representation. """ import superset.models.core as models from superset import security_manager from superset.connectors.sqla.models import ( SqlaTable, SqlMetric, TableColumn, ) if '__Dashboard__' in o: return models.Dashboard(**o['__Dashboard__']) elif '__Slice__' in o: return models.Slice(**o['__Slice__']) elif '__TableColumn__' in o: return TableColumn(**o['__TableColumn__']) elif '__SqlaTable__' in o: return SqlaTable(**o['__SqlaTable__']) elif '__SqlMetric__' in o: return SqlMetric(**o['__SqlMetric__']) elif '__Database__' in o: return models.Database(**o['__Database__']) elif '__User__' in o: return security_manager.user_model(**o['__User__']) elif '__Role__' in o: return security_manager.role_model(**o['__Role__']) elif '__ViewMenu__' in o: return security_manager.viewmenu_model(**o['__ViewMenu__']) elif '__PermissionView__' in o: return security_manager.permissionview_model(**o['__PermissionView__']) elif '__datetime__' in o: return datetime.strptime(o['__datetime__'], '%Y-%m-%dT%H:%M:%S') else: return o