def script_info(request): """Get ScriptInfo object for testing CLI.""" instance_path = tempfile.mkdtemp() app = Flask('testapp', instance_path=instance_path) app.config.update( ACCOUNTS_USE_CELERY=False, SECRET_KEY="CHANGE_ME", SECURITY_PASSWORD_SALT="CHANGE_ME_ALSO", SQLALCHEMY_DATABASE_URI=os.environ.get( 'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'), TESTING=True, ) FlaskCLI(app) Babel(app) Mail(app) InvenioDB(app) InvenioAccounts(app) with app.app_context(): if not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.drop_all() db.create_all() def teardown(): with app.app_context(): drop_database(str(db.engine.url)) shutil.rmtree(instance_path) request.addfinalizer(teardown) return ScriptInfo(create_app=lambda info: app)
def createdb(application, configuration_groups, **kwargs): """ Create a database and install blok from config :param application: name of the application :param configuration_groups: list configuration groupe to load :param \**kwargs: ArgumentParser named arguments """ format_configuration(configuration_groups, 'create_db', 'install-bloks', 'install-or-update-bloks') load_init_function_from_entry_points() Configuration.load(application, configuration_groups=configuration_groups, **kwargs) BlokManager.load() db_name = Configuration.get('db_name') db_template_name = Configuration.get('db_template_name', None) url = Configuration.get('get_url')(db_name=db_name) create_database(url, template=db_template_name) registry = RegistryManager.get(db_name) if registry is None: return if Configuration.get('install_all_bloks'): bloks = registry.System.Blok.list_by_state('uninstalled') else: install_bloks = Configuration.get('install_bloks') or [] iou_bloks = Configuration.get('install_or_update_bloks') or [] bloks = list(set(install_bloks + iou_bloks)) registry.upgrade(install=bloks) registry.commit() registry.close()
def app(request): """Flask application fixture.""" instance_path = tempfile.mkdtemp() app = Flask('testapp', instance_path=instance_path) app.config.update( LOGIN_DISABLED=False, MAIL_SUPPRESS_SEND=True, SECRET_KEY='changeme', SERVER_NAME='example.com', SQLALCHEMY_DATABASE_URI=os.environ.get( 'SQLALCHEMY_DATABASE_URI', 'sqlite://'), TESTING=True, WTF_CSRF_ENABLED=False, ) Babel(app) Menu(app) Breadcrumbs(app) InvenioDB(app) InvenioAccounts(app) InvenioGroups(app) with app.app_context(): if str(db.engine.url) != 'sqlite://' and \ not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.create_all() def teardown(): with app.app_context(): if str(db.engine.url) != 'sqlite://': drop_database(str(db.engine.url)) shutil.rmtree(instance_path) request.addfinalizer(teardown) return app
def app(request): """Flask application fixture.""" instance_path = tempfile.mkdtemp() app = Flask('testapp', instance_path=instance_path) Babel(app) FlaskCLI(app) InvenioDB(app) LoginManager(app) app.admin_app = InvenioAdmin(app) protected_view = protected_adminview_factory(TestModelView) app.admin_app.admin.add_view(protected_view(TestModel, db.session)) app.config.update( TESTING=True, SECRET_KEY="SECRET_KEY", ) with app.app_context(): if not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.drop_all() db.create_all() def teardown(): with app.app_context(): drop_database(str(db.engine.url)) shutil.rmtree(instance_path) request.addfinalizer(teardown) return app
def records_app(request): """Initialize InvenioRecords.""" app = Flask('records_testapp') FlaskCLI(app) app.config.update( TESTING=True, SQLALCHEMY_DATABASE_URI=os.environ.get( 'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db' ), ) InvenioDB(app) from invenio_records import InvenioRecords from invenio_db import db InvenioRecords(app) InvenioSearch(app) with app.app_context(): if not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.create_all() def teardown(): with app.app_context(): db.drop_all() request.addfinalizer(teardown) return app
def app(request): """Flask application fixture.""" app = Flask('testapp') app.config.update( TESTING=True, SQLALCHEMY_DATABASE_URI=os.getenv('SQLALCHEMY_DATABASE_URI', 'sqlite://'), SERVER_NAME='localhost', ) Babel(app) Menu(app) Breadcrumbs(app) InvenioDB(app) InvenioCollections(app) InvenioRecords(app) app.register_blueprint(blueprint) with app.app_context(): if str(db.engine.url) != 'sqlite://' and \ not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.create_all() def teardown(): with app.app_context(): if str(db.engine.url) != 'sqlite://': drop_database(str(db.engine.url)) request.addfinalizer(teardown) with app.app_context(): db.create_all() return app
def init(db_url, engine_args=None, session_args=None, trans_mgr=None, scoped=False): alembic_d = Path(__file__).parent alembic_cfg = alembic.config.Config(str(alembic_d / "alembic.ini")) alembic_cfg.set_main_option("sqlalchemy.url", db_url) log.debug(f"Checking for database '{safeDbUrl(db_url)}'") if not database_exists(db_url): log.info(f"Creating database '{safeDbUrl(db_url)}'") create_database(db_url, template="template0") log.debug(f"Connecting to database '{safeDbUrl(db_url)}'") args = engine_args or DEFAULT_ENGINE_ARGS engine = create_engine(db_url, **args) connection = engine.connect() args = session_args or DEFAULT_SESSION_ARGS if trans_mgr: args.update({"extension": trans_mgr}) SessionMaker = sessionmaker(bind=engine, **args) if scoped: SessionMaker = scoped_session(SessionMaker) for T in TYPES: T.metadata.bind = engine # Upgrade to head (i.e. this) revision, or no-op if they match with cd(str(alembic_d)): alembic.command.upgrade(alembic_cfg, "head") return DatabaseInfo(engine, SessionMaker, connection)
def app(request): """Flask application fixture.""" # Set temporary instance path for sqlite instance_path = tempfile.mkdtemp() app = Flask("testapp", instance_path=instance_path) InvenioDB(app) InvenioPIDStore(app) app.config.update( SQLALCHEMY_DATABASE_URI=os.environ.get("SQLALCHEMY_DATABASE_URI", "sqlite:///test.db"), TESTING=True ) with app.app_context(): if not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.drop_all() db.create_all() def teardown(): with app.app_context(): drop_database(str(db.engine.url)) shutil.rmtree(instance_path) request.addfinalizer(teardown) return app
def test_db(request): """Test database backend.""" app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get( 'SQLALCHEMY_DATABASE_URI', 'sqlite://' ) FlaskCLI(app) InvenioDB(app) FlaskOAuth(app) InvenioOAuthClient(app) def teardown(): with app.app_context(): db.drop_all() request.addfinalizer(teardown) with app.app_context(): if str(db.engine.url) != 'sqlite://' and \ not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.create_all() tables = list(filter(lambda table: table.startswith('oauthclient'), db.metadata.tables.keys())) assert len(tables) == 3
def app(request): """Flask application fixture.""" app = Flask('testapp') app.config.update( TESTING=True, SERVER_NAME='localhost:5000', SQLALCHEMY_DATABASE_URI=os.environ.get( 'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db' ) ) FlaskCLI(app) InvenioDB(app) InvenioRecords(app) with app.app_context(): if app.config['SQLALCHEMY_DATABASE_URI'] != 'sqlite://': create_database(db.engine.url) db.create_all() def finalize(): with app.app_context(): db.drop_all() if app.config['SQLALCHEMY_DATABASE_URI'] != 'sqlite://': drop_database(db.engine.url) request.addfinalizer(finalize) return app
def anyblok_createdb(): """Create a database and install blok from config""" load_init_function_from_entry_points() Configuration.load('createdb') configuration_post_load() BlokManager.load() db_name = get_db_name() db_template_name = Configuration.get('db_template_name', None) url = Configuration.get('get_url')(db_name=db_name) create_database(url, template=db_template_name) registry = RegistryManager.get(db_name) if registry is None: return if Configuration.get('install_all_bloks'): bloks = registry.System.Blok.list_by_state('uninstalled') else: install_bloks = Configuration.get('install_bloks') or [] iou_bloks = Configuration.get('install_or_update_bloks') or [] bloks = list(set(install_bloks + iou_bloks)) registry.upgrade(install=bloks) registry.commit() registry.close()
def base_app(): """Flask application fixture.""" instance_path = tempfile.mkdtemp() base_app = Flask(__name__, instance_path=instance_path) base_app.config.update( ACCOUNTS_USE_CELERY=False, LOGIN_DISABLED=False, SECRET_KEY='testing_key', SQLALCHEMY_DATABASE_URI=os.getenv('SQLALCHEMY_DATABASE_URI', 'sqlite://'), TEST_USER_EMAIL='*****@*****.**', TEST_USER_PASSWORD='******', TESTING=True, WTF_CSRF_ENABLED=False, ) Babel(base_app) Mail(base_app) Menu(base_app) InvenioDB(base_app) InvenioAccounts(base_app) base_app.register_blueprint(accounts_blueprint) with base_app.app_context(): if str(db.engine.url) != "sqlite://" and \ not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.drop_all() db.create_all() def teardown(): drop_database(str(db.engine.url)) shutil.rmtree(instance_path) return base_app
def clean_app(request, base_app): """Application with database and elasticsearch cleaned.""" with base_app.app_context(): try: db.session.remove() drop_database(db.engine.url) except ProgrammingError: pass create_database(db.engine.url) # reset elasticsearch for deleted in current_search.delete(ignore=[404]): pass # reset queues current_queues.delete() current_queues.declare() yield base_app def finalize(): with base_app.app_context(): db.session.remove() drop_database(db.engine.url) # Dispose the engine in order to close all connections. This is # needed for sqlite in memory databases. db.engine.dispose() current_queues.delete() request.addfinalizer(finalize) return base_app
def app(request): """Flask application fixture.""" instance_path = tempfile.mkdtemp() app = Flask('testapp', instance_path=instance_path) app.config.update( TESTING=True, SERVER_NAME='localhost:5000', SQLALCHEMY_DATABASE_URI=os.environ.get( 'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db' ) ) FlaskCLI(app) InvenioDB(app) InvenioREST(app) InvenioRecords(app) InvenioPIDStore(app) InvenioRecordsREST(app) with app.app_context(): if not database_exists(str(db.engine.url)) and \ app.config['SQLALCHEMY_DATABASE_URI'] != 'sqlite://': create_database(db.engine.url) db.drop_all() db.create_all() def finalize(): with app.app_context(): db.drop_all() if app.config['SQLALCHEMY_DATABASE_URI'] != 'sqlite://': drop_database(db.engine.url) shutil.rmtree(instance_path) request.addfinalizer(finalize) return app
def create_app(): from colorama import init init() from config import add_configs app = add_configs(Flask(__name__)) from sqlalchemy_utils.functions import database_exists, create_database if not database_exists(app.config.get('SQLALCHEMY_DATABASE_URI')): create_database(app.config.get('SQLALCHEMY_DATABASE_URI'), encoding='utf8') with app.app_context(): from models import db db.init_app(app) app.db = db from models.oauth2 import oauth2_provider oauth2_provider.init_app(app) # import models Migrate(app, db, directory='bin/migrations/') mgr = Manager(app) mgr.add_command('db', MigrateCommand) return mgr
def app(request): """Flask application fixture.""" app = Flask('testapp') app.config.update( TESTING=True, SQLALCHEMY_DATABASE_URI=os.environ.get( 'SQLALCHEMY_DATABASE_URI', 'sqlite://' ), CELERY_ALWAYS_EAGER=True, CELERY_RESULT_BACKEND="cache", CELERY_CACHE_BACKEND="memory", CELERY_EAGER_PROPAGATES_EXCEPTIONS=True, RECORDS_UI_DEFAULT_PERMISSION_FACTORY=None, # No permission checking ) FlaskCeleryExt(app) FlaskCLI(app) Babel(app) InvenioDB(app) InvenioPIDStore(app) InvenioRecords(app) with app.app_context(): if app.config['SQLALCHEMY_DATABASE_URI'] != 'sqlite://': create_database(db.engine.url) db.create_all() def finalize(): with app.app_context(): db.drop_all() if app.config['SQLALCHEMY_DATABASE_URI'] != 'sqlite://': drop_database(db.engine.url) request.addfinalizer(finalize) return app
def install(welcome, force, username, email, password, group): """Installs flaskbb. If no arguments are used, an interactive setup will be run. """ click.secho("[+] Installing FlaskBB...", fg="cyan") if database_exists(db.engine.url): if force or click.confirm(click.style( "Existing database found. Do you want to delete the old one and " "create a new one?", fg="magenta") ): drop_database(db.engine.url) else: sys.exit(0) create_database(db.engine.url) upgrade_database() click.secho("[+] Creating default settings...", fg="cyan") create_default_groups() create_default_settings() click.secho("[+] Creating admin user...", fg="cyan") prompt_save_user(username, email, password, group) if welcome: click.secho("[+] Creating welcome forum...", fg="cyan") create_welcome_forum() click.secho("[+] Compiling translations...", fg="cyan") compile_translations() click.secho("[+] FlaskBB has been successfully installed!", fg="green", bold=True)
def test_alembic_and_db_create_match(clean_app): """Check that alembic recipes and alembic models are in sync.""" with clean_app.app_context(): ext = clean_app.extensions['invenio-db'] if db.engine.name == 'sqlite': raise pytest.skip('Upgrades are not supported on SQLite.') # Make sure that alembic upgrades can be run now ext.alembic.upgrade() constraints = get_all_constraints(clean_app) alembic_constraint_names = [x[0] for x in constraints] # Recreate the database with alembic only (without migrating) db.session.remove() drop_database(db.engine.url) db.engine.dispose() create_database(db.engine.url) db.create_all() # Check that the resulting state is in sync with alembic metaData assert not ext.alembic.compare_metadata() # Check that the constraints are the same. This is needed because # alembic.compare_metadata does not check all constraints. constraints = get_all_constraints(clean_app) db_create_constraint_names = [x[0] for x in constraints] assert set(alembic_constraint_names) == set(db_create_constraint_names)
def app(request): """Flask application fixture.""" instance_path = tempfile.mkdtemp() app = Flask('testapp', instance_path=instance_path) app.config.update( BROKER_URL=os.environ.get('BROKER_URL', 'memory://'), CELERY_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND="memory", CELERY_EAGER_PROPAGATES_EXCEPTIONS=True, CELERY_RESULT_BACKEND="cache", SQLALCHEMY_DATABASE_URI=os.environ.get( 'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'), SQLALCHEMY_TRACK_MODIFICATIONS=False, TESTING=True, ) FlaskCLI(app) FlaskCeleryExt(app) InvenioDB(app) InvenioRecords(app) InvenioSearch(app) InvenioIndexer(app) with app.app_context(): if not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.create_all() def teardown(): with app.app_context(): drop_database(str(db.engine.url)) shutil.rmtree(instance_path) request.addfinalizer(teardown) return app
def app(request): """Flask application fixture.""" app = create_app( CELERY_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND="memory", CELERY_EAGER_PROPAGATES_EXCEPTIONS=True, CELERY_RESULT_BACKEND="cache", DEBUG_TB_ENABLED=False, SECRET_KEY="CHANGE_ME", SECURITY_PASSWORD_SALT="CHANGE_ME", MAIL_SUPPRESS_SEND=True, SQLALCHEMY_DATABASE_URI=os.environ.get( 'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'), TESTING=True, ) with app.app_context(): if not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.drop_all() db.create_all() list(current_search.create()) def teardown(): with app.app_context(): drop_database(str(db.engine.url)) list(current_search.delete(ignore=[404])) request.addfinalizer(teardown) return app
def db(app): """Database fixture.""" if not database_exists(str(db_.engine.url)): create_database(str(db_.engine.url)) db_.create_all() yield db_ db_.session.remove() db_.drop_all()
def engine(dsn): if database_exists(dsn): drop_database(dsn) create_database(dsn) engine = create_engine(dsn) engine.execute('CREATE EXTENSION postgis') GeonameBase.metadata.create_all(bind=engine) return engine
def test_db(): """Test database backend.""" app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get( 'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db' ) FlaskCLI(app) InvenioDB(app) InvenioRecords(app) with app.app_context(): create_database(db.engine.url) db.create_all() assert len(db.metadata.tables) == 3 data = {'title': 'Test'} from invenio_records.models import RecordMetadata as RM # Create a record with app.app_context(): assert RM.query.count() == 0 record_uuid = Record.create(data).id db.session.commit() assert RM.query.count() == 1 db.session.commit() # Retrieve created record with app.app_context(): record = Record.get_record(record_uuid) assert record.dumps() == data with pytest.raises(NoResultFound): Record.get_record(uuid.uuid4()) record['field'] = True record = record.patch([ {'op': 'add', 'path': '/hello', 'value': ['world']} ]) assert record['hello'] == ['world'] record.commit() db.session.commit() with app.app_context(): record2 = Record.get_record(record_uuid) assert record2.model.version_id == 2 assert record2['field'] assert record2['hello'] == ['world'] db.session.commit() # Cannot commit record without model (i.e. Record.create_record) with app.app_context(): record3 = Record({'title': 'Not possible'}) with pytest.raises(RecordNotCommitableError): record3.commit() with app.app_context(): db.drop_all() drop_database(db.engine.url)
def db(app): """Ensure that the database schema is created.""" if not database_exists(str(db_.engine.url)): create_database(str(db_.engine.url)) db_.create_all() yield db_ db_.session.remove() db_.drop_all()
def app(request): """Flask application fixture.""" instance_path = tempfile.mkdtemp() app = Flask('testapp', instance_path=instance_path) app.config.update( TESTING=True, SECRET_KEY='SECRET_KEY', ADMIN_LOGIN_ENDPOINT='login', SQLALCHEMY_TRACK_MODIFICATIONS=True, ) Babel(app) InvenioDB(app) Principal(app) LoginManager(app) # Install login and access loading. @app.login_manager.user_loader def load_user(user_id): return TestUser.get(user_id) @app.route('/login/') def login(): from flask import current_app from flask import request as flask_request user = TestUser.get(flask_request.args.get('user', 1)) login_user(user) identity_changed.send( current_app._get_current_object(), identity=Identity(user.id)) return "Logged In" @identity_loaded.connect_via(app) def on_identity_loaded(sender, identity): identity.user = current_user identity.provides.add(UserNeed(current_user.id)) if current_user.id == 1: identity.provides.add(action_admin_access) # Register admin view InvenioAdmin( app, permission_factory=lambda x: Permission(action_admin_access)) app.extensions['invenio-admin'].register_view(TestModelView, TestModel) # Create database with app.app_context(): if not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.drop_all() db.create_all() def teardown(): with app.app_context(): drop_database(str(db.engine.url)) shutil.rmtree(instance_path) request.addfinalizer(teardown) return app
def create_db(): """Creates database if it doesn't exist.""" db_uri = app.config['SQLALCHEMY_DATABASE_URI'] if not database_exists(db_uri): print('Creating database ...') create_database(db_uri) db.create_all() else: print('Database already exists. Nothing to create.')
def engine(request, url=url): assert _schema is not None, "Please call set_schema(filename) first" def fin(): drop_database(url) request.addfinalizer(fin) create_database(url) cmd = "cat {0} | psql -h {1} -U postgres test".format(_schema, ip_addr) os.system(cmd) return create_engine(url)
def app(request): """Flask application fixture.""" instance_path = tempfile.mkdtemp() app = Flask('testapp', instance_path=instance_path) app.config.update( TESTING=True, SECRET_KEY='SECRET_KEY', ADMIN_LOGIN_ENDPOINT='login', SQLALCHEMY_TRACK_MODIFICATIONS=True, ) Babel(app) InvenioDB(app) Principal(app) LoginManager(app) # Install login and access loading. @app.login_manager.user_loader def load_user(user_id): return TestUser.get(user_id) @app.route('/login/') def login(): from flask import current_app from flask import request as flask_request user = TestUser.get(flask_request.args.get('user', 1)) login_user(user) identity_changed.send(current_app._get_current_object(), identity=Identity(user.id)) return "Logged In" @identity_loaded.connect_via(app) def on_identity_loaded(sender, identity): identity.user = current_user identity.provides.add(UserNeed(current_user.id)) if current_user.id == 1: identity.provides.add(action_admin_access) # Register admin view InvenioAdmin(app, permission_factory=lambda x: Permission(action_admin_access)) app.extensions['invenio-admin'].register_view(TestModelView, TestModel) # Create database with app.app_context(): if not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.drop_all() db.create_all() def teardown(): with app.app_context(): drop_database(str(db.engine.url)) shutil.rmtree(instance_path) request.addfinalizer(teardown) return app
def app(request): """Flask application fixture.""" instance_path = tempfile.mkdtemp() app = Flask('testapp', instance_path=instance_path) es_index = 'invenio_records_rest_test_index' app.config.update( TESTING=True, SERVER_NAME='localhost:5000', SQLALCHEMY_DATABASE_URI=os.environ.get( 'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db' ), RECORDS_REST_ENDPOINTS=config.RECORDS_REST_ENDPOINTS, # No permission checking RECORDS_REST_DEFAULT_CREATE_PERMISSION_FACTORY=None, RECORDS_REST_DEFAULT_READ_PERMISSION_FACTORY=None, RECORDS_REST_DEFAULT_UPDATE_PERMISSION_FACTORY=None, RECORDS_REST_DEFAULT_DELETE_PERMISSION_FACTORY=None, RECORDS_REST_DEFAULT_SEARCH_INDEX=es_index, SEARCH_QUERY_ENHANCERS=[filter_record_access_query_enhancer], SEARCH_AUTOINDEX=[], ) app.config['RECORDS_REST_ENDPOINTS']['recid']['search_index'] = es_index # update the application with the configuration provided by the test if hasattr(request, 'param') and 'config' in request.param: app.config.update(**request.param['config']) FlaskCLI(app) InvenioDB(app) InvenioREST(app) InvenioRecords(app) InvenioPIDStore(app) InvenioSearch(app) InvenioAccess(app) InvenioRecordsREST(app) with app.app_context(): if not database_exists(str(db.engine.url)) and \ app.config['SQLALCHEMY_DATABASE_URI'] != 'sqlite://': create_database(db.engine.url) db.drop_all() db.create_all() if current_search_client.indices.exists(es_index): current_search_client.indices.delete(es_index) current_search_client.indices.create(es_index) prepare_indexing(app) def finalize(): with app.app_context(): db.drop_all() if app.config['SQLALCHEMY_DATABASE_URI'] != 'sqlite://': drop_database(db.engine.url) shutil.rmtree(instance_path) request.addfinalizer(finalize) return app
def app(): """Flask application fixture.""" instance_path = tempfile.mkdtemp() app = Flask('testapp', instance_path=instance_path) app.config.update( CELERY_ALWAYS_EAGER=True, CELERY_TASK_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND='memory', CELERY_EAGER_PROPAGATES_EXCEPTIONS=True, CELERY_TASK_EAGER_PROPAGATES=True, CELERY_RESULT_BACKEND='cache', JSONSCHEMAS_HOST='inveniosoftware.org', TESTING=True, SECRET_KEY='CHANGE_ME', SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'), SQLALCHEMY_TRACK_MODIFICATIONS=True, SERVER_NAME='app', OAISERVER_ID_PREFIX='oai:inveniosoftware.org:recid/', OAISERVER_RECORD_INDEX='_all', OAISERVER_REGISTER_SET_SIGNALS=True, ) if not hasattr(app, 'cli'): from flask_cli import FlaskCLI FlaskCLI(app) InvenioDB(app) FlaskCeleryExt(app) InvenioJSONSchemas(app) InvenioRecords(app) InvenioPIDStore(app) InvenioMARC21(app) client = Elasticsearch(hosts=[os.environ.get('ES_HOST', 'localhost')]) search = InvenioSearch(app, client=client) search.register_mappings('records', 'data') InvenioIndexer(app) InvenioOAIServer(app) app.register_blueprint(blueprint) with app.app_context(): if str(db.engine.url) != 'sqlite://' and \ not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.create_all() list(search.create(ignore=[400])) search.flush_and_refresh('_all') with app.app_context(): yield app with app.app_context(): db.session.close() if str(db.engine.url) != 'sqlite://': drop_database(str(db.engine.url)) list(search.delete(ignore=[404])) shutil.rmtree(instance_path)
def app(): """Flask application fixture.""" instance_path = tempfile.mkdtemp() app = Flask('testapp', instance_path=instance_path) app.config.update( CELERY_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND='memory', CELERY_EAGER_PROPAGATES_EXCEPTIONS=True, CELERY_RESULT_BACKEND='cache', JSONSCHEMAS_HOST='inveniosoftware.org', TESTING=True, SECRET_KEY='CHANGE_ME', SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI', 'sqlite://'), SQLALCHEMY_TRACK_MODIFICATIONS=True, SERVER_NAME='app', OAISERVER_RECORD_INDEX='_all', # Disable set signals because the celery tasks cannot be run # synchronously OAISERVER_REGISTER_SET_SIGNALS=False, SEARCH_ELASTIC_KEYWORD_MAPPING={None: ['_all']}, ) if not hasattr(app, 'cli'): from flask_cli import FlaskCLI FlaskCLI(app) InvenioDB(app) FlaskCeleryExt(app) InvenioJSONSchemas(app) InvenioRecords(app) InvenioPIDStore(app) InvenioMARC21(app) client = Elasticsearch(hosts=[os.environ.get('ES_HOST', 'localhost')]) search = InvenioSearch(app, client=client) search.register_mappings('records', 'data') InvenioIndexer(app) InvenioOAIServer(app) app.register_blueprint(blueprint) with app.app_context(): if str(db.engine.url) != 'sqlite://' and \ not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.create_all() list(search.create(ignore=[400])) sleep(5) with app.app_context(): yield app with app.app_context(): db.session.close() if str(db.engine.url) != 'sqlite://': drop_database(str(db.engine.url)) list(search.delete(ignore=[404])) shutil.rmtree(instance_path)
def init(): """Create database repository.""" click.secho('Creating database {0}...'.format(_db.engine.url), bold=True, fg='yellow') if not database_exists(str(_db.engine.url)): create_database(str(_db.engine.url)) click.secho('Database created!', bold=True, fg='green')
def check_db_exist(engine): """Checking if DB exist.""" res = database_exists(engine.url) if not res: try: create_database(connstr) except Exception as exc: pass else: Base.metadata.create_all(engine)
def init_db(db_name='app_todo'): global engine, session CONNSTR = 'postgresql+psycopg2://postgres@database/%s' % db_name # Ensure the db exists if not database_exists(CONNSTR): create_database(CONNSTR) # Connect engine = sa.create_engine(CONNSTR) session = scoped_session(sessionmaker(bind=engine)) Base.query = session.query_property()
def database(app): """Ensure that the database schema is created.""" if not database_exists(str(db_.engine.url)): create_database(str(db_.engine.url)) db_.drop_all() db_.create_all() yield db_ drop_database(str(db_.engine.url))
def db(): """Setup database.""" if not database_exists(str(db_.engine.url)): create_database(str(db_.engine.url)) db_.create_all() yield db_ db_.session.remove() db_.drop_all()
def new_database(): engine = config.get_connection_url() if not database_exists(engine): print "Create database " + engine create_database(engine) return True elif not database.check_tables(): print "Check tables and incomplete tables." return True return False
def create(self): """Create database if exists.""" # Initialize key variables if database_exists(self.url) is False: create_database(self.url) # Alter the encoding for database character set sql_string = ('ALTER DATABASE %s CHARACTER SET utf8mb4 ' 'COLLATE utf8mb4_general_ci') % (self.config.db_name()) self.engine.execute(sql_string)
def main(): retries = MAX_RETRIES database_uri = os.environ.get('DATABASE_URI') if not database_uri: logger.error( 'Migration cannot proceed. Please specify full DATABASE_URI.') sys.exit(1) database_user = os.environ.get('DATABASE_USER') database_password = os.environ.get('DATABASE_PASSWORD') if not database_user or not database_password: logger.error( 'Migration cannot proceed. Please specify full DATABASE_USER & DATABASE_PASSWORD for limited ' 'privilege application user!') sys.exit(1) migration_path = os.environ.get('DATABASE_MIGRATIONS', '/app/migrations') logger.info('Preparing for migration...') time.sleep(1) while retries: try: logger.info('Creating database ...') if not database_exists(database_uri): create_database(database_uri) logger.info('Database created!') else: logger.info('Database exists!') logger.info('Upgrading database ...') if upgrade(migration_path): logger.error('Failed to upgrade') sys.exit(1) logger.info('Creating and assigning user permissions') create_user(database_uri, database_user, database_password) logger.info('Done!') sys.exit(0) except KeyboardInterrupt: sys.exit(0) except Exception as e: logger.error(e) retries -= 1 time.sleep(2) logger.error('Failed to migrate') sys.exit(1)
def create_app(**kwargs): """Create an instance of the flask app.""" api = connexion.App(__name__, specification_dir='schemas/') api.app.config.from_object(config) api.app.config.update(**config.from_env(config)) api.app.config.update(**kwargs) api.app.url_map.strict_slashes = False deployer_url = urlparse(api.app.config.get('DEPLOYER_URL')) api.app.config.setdefault('DEPLOYER_HOST', deployer_url.netloc) api.app.config.setdefault('DEPLOYER_SCHEME', deployer_url.scheme) # Setup Sentry service: if Sentry and api.app.config['SENTRY_DSN']: # pragma: no cover Sentry(api.app, dsn=api.app.config['SENTRY_DSN']) api.add_api( 'renga-deployer-v1.yaml', arguments=api.app.config, resolver=RestyResolver('renga_deployer.api'), swagger_ui=api.app.config['DEPLOYER_SWAGGER_UI'], ) # validate_responses=True) Babel(api.app) db.init_app(api.app) RengaDeployer(api.app) # add extensions if api.app.config['KNOWLEDGE_GRAPH_URL']: from .contrib.knowledge_graph import KnowledgeGraphSync KnowledgeGraphSync(api.app) if api.app.config['RESOURCE_MANAGER_URL']: from .contrib.resource_manager import ResourceManager ResourceManager(api.app) if api.app.config['WSGI_NUM_PROXIES']: from werkzeug.contrib.fixers import ProxyFix api.app.wsgi_app = ProxyFix( api.app.wsgi_app, num_proxies=api.app.config['WSGI_NUM_PROXIES']) # setup logging logging.setup_logging(api.app.config['RENGA_LOGGING_CONFIG']) # create database and tables with api.app.app_context(): if not functions.database_exists(db.engine.url): functions.create_database(db.engine.url) logger.debug('Database created.') db.create_all() logger.debug('Database initialized.') return api.app
def app(request): with create_app.app_context(): if not database_exists(db.engine.url): create_database(db.engine.url) db.create_all() yield create_app drop_database(db.engine.url)
def db(app): """Database fixture.""" if not database_exists(str(db_.engine.url)) and \ app.config['SQLALCHEMY_DATABASE_URI'] != 'sqlite://': create_database(db_.engine.url) db_.create_all() yield db_ db_.session.remove() db_.drop_all()
def create_latest_db(): """Creates the database including the schema using SQLAlchemy's db.create_all method instead of going through all the database revisions. The revision will be set to 'head' which indicates the latest alembic revision. """ if not database_exists(db.engine.url): create_database(db.engine.url) db.create_all() alembic.stamp()
def create_tables(): """Create all the tables needed for the test.""" DB_URI = f'postgresql://{C.DB_USER}:{C.DB_PASSWORD}@{C.DB_HOST}:{C.DB_PORT}/{C.DB_NAME}' if database_exists(DB_URI): drop_database(DB_URI) create_database(DB_URI) engine = create_engine(DB_URI) conn = engine.connect() models = [Event] for model in models: conn.execute(CreateTable(model).compile(engine).__str__())
def base_loaded(request): url = Configuration.get('get_url')() if not database_exists(url): db_template_name = Configuration.get('db_template_name', None) create_database(url, template=db_template_name) BlokManager.load() registry = init_registry_with_bloks([], None) registry.commit() registry.close() BlokManager.unload()
def db(app): """Setup database.""" with app.app_context(): db_.init_app(app) if not database_exists(str(db_.engine.url)): create_database(str(db_.engine.url)) db_.create_all() yield db_ with app.app_context(): db_.session.remove() db_.drop_all()
def create_db(): """Create database. Make sure the variable SQLALCHEMY_DATABASE_URI is set""" click.secho('Creating database {0}'.format(db.engine.url), fg='green') if not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) click.secho('Creating extension postgis...', fg='green') with db.session.begin_nested(): db.session.execute('CREATE EXTENSION IF NOT EXISTS postgis') db.session.commit()
def create_test_database(): url = TEST_DATABASE_URL try: if database_exists(url): drop_database(url) except OperationalError: pass create_database(url) Base.metadata.create_all(bind=engine) yield # run the tests drop_database(url)
def test_migrations_upgrade(): with pytest.raises(OperationalError): User.query.all() # ensure that the database is created create_database(db.engine.url) alembic.upgrade() assert len(User.query.all()) == 0 drop_database(db.engine.url)
def db(base_app): """Initialize database.""" # Init if not database_exists(str(_db.engine.url)): create_database(str(_db.engine.url)) _db.create_all() yield _db # Teardown _db.session.remove() _db.drop_all()
def app(request, tmpdir): """Flask application fixture.""" from b2share.factory import create_api instance_path = tmpdir.mkdir('instance_dir').strpath os.environ.update( B2SHARE_INSTANCE_PATH=os.environ.get( 'INSTANCE_PATH', instance_path), ) app = create_api( TESTING=True, SERVER_NAME='localhost:5000', JSONSCHEMAS_HOST='localhost:5000', DEBUG_TB_ENABLED=False, SQLALCHEMY_DATABASE_URI=os.environ.get( 'SQLALCHEMY_DATABASE_URI', 'sqlite://'), LOGIN_DISABLED=False, WTF_CSRF_ENABLED=False, SECRET_KEY="CHANGE_ME", SECURITY_PASSWORD_SALT='CHANGEME', CELERY_ALWAYS_EAGER=True, CELERY_RESULT_BACKEND="cache", CELERY_CACHE_BACKEND="memory", CELERY_EAGER_PROPAGATES_EXCEPTIONS=True, SUPPORT_EMAIL='*****@*****.**', ) # update the application with the configuration provided by the test if hasattr(request, 'param') and 'config' in request.param: app.config.update(**request.param['config']) with app.app_context(): if app.config['SQLALCHEMY_DATABASE_URI'] != 'sqlite://': try: drop_database(db.engine.url) except ProgrammingError: pass create_database(db.engine.url) db.create_all() for deleted in current_search.delete(ignore=[404]): pass for created in current_search.create(None): pass def finalize(): with app.app_context(): db.drop_all() if app.config['SQLALCHEMY_DATABASE_URI'] != 'sqlite://': drop_database(db.engine.url) request.addfinalizer(finalize) return app
def base_app(request): """Flask application fixture without ShibbolethAuthenticator init.""" instance_path = tempfile.mkdtemp() base_app = Flask('testapp') base_app.config.update( TESTING=True, WTF_CSRF_ENABLED=False, LOGIN_DISABLED=False, CACHE_TYPE='simple', SHIBBOLETH_REMOTE_APPS=dict( hzdr=dict(title='HZDR Shibboleth Authentication', saml_path='data/', mappings=dict( email='email_mapping', user_unique_id='id_mapping', full_name='full_name_mapping', ))), DEBUG=False, EMAIL_BACKEND='flask_email.backends.locmem.Mail', SQLALCHEMY_TRACK_MODIFICATIONS=False, SQLALCHEMY_DATABASE_URI=os.getenv('SQLALCHEMY_DATABASE_URI', 'sqlite://'), SERVER_NAME='localhost', SECRET_KEY='TEST', SECURITY_DEPRECATED_PASSWORD_SCHEMES=[], SECURITY_PASSWORD_HASH='plaintext', SECURITY_PASSWORD_SCHEMES=['plaintext'], ) FlaskMenu(base_app) InvenioDB(base_app) InvenioAccounts(base_app) Mail(base_app) with base_app.app_context(): if str(db.engine.url) != 'sqlite://' and \ not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.create_all() def teardown(): with base_app.app_context(): db.session.close() if str(db.engine.url) != 'sqlite://': drop_database(str(db.engine.url)) print('Path: ' + instance_path) shutil.rmtree(instance_path) request.addfinalizer(teardown) base_app.test_request_context().push() return base_app
def db(app, request): """Setup database.""" if not database_exists(str(db_.engine.url)): create_database(str(db_.engine.url)) db_.create_all() yield db_ drop_database(str(db_.engine.url)) # Delete sessions in kvsession store if hasattr(app, 'kvsession_store') and \ isinstance(app.kvsession_store, RedisStore): app.kvsession_store.redis.flushall()
def create_latest_db(target="default@head"): """Creates the database including the schema using SQLAlchemy's db.create_all method instead of going through all the database revisions. The revision will be set to 'head' which indicates the latest alembic revision. :param target: The target branch. Defaults to 'default@head'. """ if not database_exists(db.engine.url): create_database(db.engine.url) db.create_all() alembic.stamp(target=target)
def db(app): """Database fixture.""" if database_exists(str(db_.engine.url)): db_.session.remove() drop_database(str(db_.engine.url)) create_database(str(db_.engine.url)) db_.create_all() yield db_ db_.session.remove() drop_database(db_.engine.url) # Dispose the engine in order to close all connections. This is # needed for sqlite in memory databases. db_.engine.dispose()
def app(base_app): """Flask application fixture.""" with base_app.app_context(): if database_exists(db.engine.url): # drop_database(db.engine.url) raise RuntimeError('Database exists') create_database(db.engine.url) db.create_all() yield base_app db.drop_all() drop_database(db.engine.url)
def app(request): """Flask application fixture.""" app = Flask('testapp') app.config.update( ACCOUNTS_JWT_ENABLE=False, BROKER_TRANSPORT='redis', CELERY_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND='memory', CELERY_EAGER_PROPAGATES_EXCEPTIONS=True, CELERY_RESULT_BACKEND='cache', CELERY_TRACK_STARTED=True, LOGIN_DISABLED=False, OAUTH2_CACHE_TYPE='simple', OAUTHLIB_INSECURE_TRANSPORT=True, SECRET_KEY='test_key', SECURITY_DEPRECATED_PASSWORD_SCHEMES=[], SECURITY_PASSWORD_HASH='plaintext', SECURITY_PASSWORD_SCHEMES=['plaintext'], SQLALCHEMY_TRACK_MODIFICATIONS=True, SQLALCHEMY_DATABASE_URI=os.getenv('SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'), TESTING=True, WTF_CSRF_ENABLED=False, ) celeryext = FlaskCeleryExt(app) celeryext.celery.flask_app = app # Make sure both apps are the same! Babel(app) Mail(app) Menu(app) Breadcrumbs(app) InvenioDB(app) InvenioAccounts(app) app.register_blueprint(accounts_blueprint) InvenioOAuth2Server(app) InvenioOAuth2ServerREST(app) app.register_blueprint(server_blueprint) app.register_blueprint(settings_blueprint) InvenioWebhooks(app) app.register_blueprint(blueprint) with app.app_context(): if not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.create_all() def teardown(): with app.app_context(): drop_database(str(db.engine.url)) request.addfinalizer(teardown) return app
def base_app(request): """Flask application fixture without OAuthClient initialized.""" instance_path = tempfile.mkdtemp() base_app = Flask('testapp') base_app.config.update( TESTING=True, WTF_CSRF_ENABLED=False, LOGIN_DISABLED=False, CACHE_TYPE='simple', OAUTHCLIENT_REMOTE_APPS=dict( orcid=REMOTE_APP, ), ORCID_APP_CREDENTIALS=dict( consumer_key='changeme', consumer_secret='changeme', ), # use local memory mailbox EMAIL_BACKEND='flask_email.backends.locmem.Mail', SQLALCHEMY_DATABASE_URI=os.getenv('SQLALCHEMY_DATABASE_URI', 'sqlite://'), SERVER_NAME='localhost', DEBUG=False, SECRET_KEY='TEST', SECURITY_PASSWORD_HASH='plaintext', SECURITY_PASSWORD_SCHEMES=['plaintext'], ) FlaskCLI(base_app) FlaskMenu(base_app) Babel(base_app) Mail(base_app) InvenioDB(base_app) InvenioAccounts(base_app) with base_app.app_context(): if str(db.engine.url) != 'sqlite://' and \ not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.create_all() def teardown(): with base_app.app_context(): db.session.close() if str(db.engine.url) != 'sqlite://': drop_database(str(db.engine.url)) shutil.rmtree(instance_path) request.addfinalizer(teardown) base_app.test_request_context().push() return base_app