예제 #1
0
    def db_exists(cls, db_name=None):
        if not db_name:
            raise RegistryException('db_name is required')

        urls = []
        url = Configuration.get('db_url')
        if url:
            urls.append(url)

        wo_url = Configuration.get('db_wo_url')
        if wo_url:
            urls.append(wo_url)

        for ro_url in Configuration.get('db_ro_urls', []) or []:
            urls.append(ro_url)

        gurl = Configuration.get('get_url', get_url)
        for url in urls:
            url = gurl(db_name=db_name, url=url)
            if not database_exists(url):
                return False
        else:
            return database_exists(gurl(db_name=db_name))

        return True
예제 #2
0
def validate_database(update_structure):
    engine = generate_engine()
    if not database_exists(engine.url):  # Checks for the first time
        Base.metadata.create_all(engine)  # Create new DB
        print("New Database Created" + str(database_exists(
            engine.url)))  # Verifies if database is there or not.
    elif update_structure:
        Base.metadata.create_all(engine)
        print("Database Structure Updated" + str(database_exists(
            engine.url)))  # Verifies if database is there or not
    else:
        print("Database Already Exists")
    return engine
예제 #3
0
def drop_db(section="test"):
    """Cleanup
    """
    LOG.info(f"Drop database")
    CONF = make_conf(section)
    if database_exists(CONF):
        drop_database(CONF)
예제 #4
0
def create_db(section="test"):
    """Create the database
    """
    CONF = make_conf(section)
    LOG.info(f"Created database")
    if not database_exists(CONF):
        create_database(CONF)
예제 #5
0
def install(welcome, force, username, email, password, group):
    """
    Install lucheng.

    If no arguments are used, an interactive setup will be run.
    """
    click.secho("[+] Install Lucheng...", 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")
    save_user_prompt(username, email, password, group)

    if welcome:
        click.secho("[+] Creating welcome forum...", fg="cyan")
        create_welcome_forum()

    click.secho("[+] Lucheng has been successfully installed!",
                fg="green", bold=True)
예제 #6
0
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
예제 #7
0
	def __init__(self, db, name, password, ip, port):
		
		if db == "postgresql":
			connection = "postgresql+psycopg2://" + name + ":" \
			              + password + "@" + ip + ":" + port			
		elif db == "mysql":
			connection = "mysql://" + name + ":" + password \
			              + "@" + ip + ":" + port
		else:
			raise ValueError("db type only \
				support \"mysql\" or \"postgresql\" argument.")
		
		db_name = 'wikilink'
		# Turn off echo
		self.engine = create_engine(connection + "/" + db_name + '?charset=utf8'
									,echo=False, encoding='utf-8' 
									,pool_pre_ping=True 
									,pool_size=10)

		if not functions.database_exists(self.engine.url):
			functions.create_database(self.engine.url)

		# If table don't exist, Create.
		if (not self.engine.dialect.has_table(self.engine, 'link')\
			and not self.engine.dialect.has_table(self.engine, 'page')):
			
			Base.metadata.create_all(self.engine)
예제 #8
0
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
예제 #9
0
def init():
    """Create database."""
    displayed_database = render_url(_db.engine.url)
    click.secho(f"Creating database {displayed_database}", fg="green")
    database_url = str(_db.engine.url)
    if not database_exists(database_url):
        create_database(database_url)
예제 #10
0
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
예제 #11
0
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 create_db(self):
        """
        Creates database if it does not exist
        """

        if not database_exists(self.engine.url):
            create_database(self.engine.url)
예제 #13
0
    def createdb(cls, keep_existing=False, with_demo=False):
        """Create the database specified in configuration.

        ::

            cls.init_configuration_manager()
            cls.createdb()

        :param keep_existing: If false drop the previous db before create it
        """
        url = Configuration.get('get_url')()
        db_template_name = Configuration.get('db_template_name', None)
        if database_exists(url):
            if keep_existing:
                return False

            drop_database(url)

        create_database(url, template=db_template_name)

        registry = RegistryManager.get(Configuration.get('db_name', None))
        if registry is None:
            return

        registry.System.Parameter.set(
            "with-demo", Configuration.get('with_demo', with_demo))

        return True
예제 #14
0
def initialize_database(exist_ok: bool = False, drop_existing: bool = False):
    db_url = engine.url
    typer.echo(f"Using database at {db_url}")

    if database_exists(db_url):
        if drop_existing:
            with wrap_echo("Dropping database"):
                drop_database(db_url)
        elif not exist_ok:
            typer.echo(
                f"Database already exists, aborting.\n"
                f"Use --exist-ok if you are sure the database is uninitialized and contains no data.\n"
                f"Use --drop-existing if you want to recreate it.",
                err=True,
            )
            return

    with wrap_echo("Creating database"):
        create_database(db_url)
        pass

    with engine.connect() as con:
        with wrap_echo("Installing pgcrypto extension"):
            con.execute("CREATE EXTENSION IF NOT EXISTS pgcrypto;")
            pass

    with wrap_echo("Creating metadata"):
        base.metadata.create_all()
        pass

    typer.echo("Database initialization complete.")
예제 #15
0
def create_test_database(main_app):
    SQLALCHEMY_DATABASE_URI = main_app.config.get("SQLALCHEMY_DATABASE_URI")
    print("CREATE SQLALCHEMY_DATABASE_URI ", SQLALCHEMY_DATABASE_URI)

    if database_exists(SQLALCHEMY_DATABASE_URI):
        drop_database(SQLALCHEMY_DATABASE_URI)
    create_database(SQLALCHEMY_DATABASE_URI)
예제 #16
0
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
예제 #17
0
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
예제 #18
0
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)
예제 #19
0
파일: conftest.py 프로젝트: duncanwp/zenodo
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
예제 #20
0
def initialize_database(exist_ok: bool = False, drop_existing: bool = False):
    db_url = engine.url
    typer.echo(f"Using database at {db_url}")

    if database_exists(db_url):
        if drop_existing:
            with wrap_echo("Dropping database"):
                drop_database(db_url)
        elif not exist_ok:
            typer.echo(
                f"Database already exists, aborting.\n"
                f"Use --exist-ok if you are sure the database is uninitialized and contains no data.\n"
                f"Use --drop-existing if you want to recreate it.",
                err=True,
            )
            return

    with wrap_echo("Creating database"):
        create_database(db_url)
        pass

    with wrap_echo("Creating metadata"):
        base.metadata.create_all()
        pass

    typer.echo("Database initialization complete.")
예제 #21
0
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
예제 #22
0
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
예제 #23
0
파일: database.py 프로젝트: nicfit/mishmash
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)
예제 #24
0
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
예제 #25
0
파일: main.py 프로젝트: djsilcock/flaskbb
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)
예제 #26
0
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
예제 #27
0
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.create_all()

    yield base_app

    with base_app.app_context():
        drop_database(str(db.engine.url))
    shutil.rmtree(instance_path)
예제 #28
0
파일: run.py 프로젝트: zwant/ngcd
def main():
    from sqlalchemy import create_engine
    from sqlalchemy.orm import scoped_session
    from sqlalchemy.orm import sessionmaker

    config = get_config()
    setup_logging(config)
    if not database_exists(config.SQLALCHEMY_DATABASE_URI):
        create_database(config.SQLALCHEMY_DATABASE_URI)

    db_engine = create_engine(config.SQLALCHEMY_DATABASE_URI)
    db_session = scoped_session(sessionmaker(autocommit=False,
                                             autoflush=False,
                                             bind=db_engine))

    model.EventBase.query = db_session.query_property()
    if config.CLEAN_DB == True:
        logger.info('Cleaning DB')
        model.EventBase.metadata.drop_all(db_engine)
    else:
        logger.info('Not cleaning DB')

    model.EventBase.metadata.create_all(db_engine)

    with Connection('amqp://*****:*****@{}//'.format(config.RABBITMQ_HOST)) as conn:
        logger.info('[*] Connected to RabbitMQ at [%s]. Waiting for data. To exit press CTRL+C', config.RABBITMQ_HOST)
        Worker(conn, db_session).run()
예제 #29
0
파일: conftest.py 프로젝트: N03/invenio
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
예제 #30
0
def init_postgres_databases(mongo_databases, postgres_users):
    """ Initialize target databases in PostgreSQL
    - Create target database that are not already defined
    - Create postgresql schemas for each non-root user
    """

    for mongo_db in mongo_databases:
        postgres_db = to_sql_identifier(mongo_db)
        target_url = "postgres://{user}:{password}@{host}:{port}/{db}".format(
            user=POSTGRES_ADMIN_USER,
            password=POSTGRES_ADMIN_PASSWORD,
            host=POSTGRES_HOST,
            port=POSTGRES_PORT,
            db=postgres_db)

        if not database_exists(target_url):
            logger.info(
                "Creating missing target database {}".format(postgres_db))
            create_database(target_url)
            with psycopg2.connect(
                    target_url
            ) as con:  # with statement automatically commit changes if no errors occurs
                cur = con.cursor()

                # Create a schema for every non default user
                cur.execute("DROP SCHEMA public;")
                for user in postgres_users:
                    cur.execute(
                        "CREATE SCHEMA IF NOT EXISTS AUTHORIZATION {user};".
                        format(user=user))
예제 #31
0
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
예제 #32
0
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
예제 #33
0
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)
예제 #34
0
def install(welcome, force, username, email, password):
    """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")):
            db.drop_all()
        else:
            sys.exit(0)

    # creating database from scratch and 'stamping it'
    create_latest_db()

    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, "admin")

    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)
예제 #35
0
def test_db(request):
    """Test database backend."""
    app = Flask(__name__)
    app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get(
        'SQLALCHEMY_DATABASE_URI', 'sqlite://')
    InvenioDB(app)
    FlaskOAuth(app)
    InvenioOAuthClient(app)

    def teardown():
        with app.app_context():
            db.drop_all()

    request.addfinalizer(teardown)

    with app.app_context():
        is_sqllite = str(db.engine.url) == 'sqlite://'
        db_exists = database_exists(str(db.engine.url))
        if not is_sqllite and not db_exists:
            create_database(str(db.engine.url))
        db.create_all()
        tables = list(
            filter(lambda table: table.startswith('oauthclient'),
                   db.metadata.tables.keys()))
        assert len(tables) == 2
예제 #36
0
def create_db(new_quiz=False):
    """ Создание БД """
    if not database_exists(engine.url):
        Base.metadata.create_all(engine)
        set_new_quiz()
    if new_quiz == True:
        set_new_quiz()
def check_if_database_exists(db_engine):
    """
    use existing sqlalchemy functionality to check if the database exists.
    :param db_engine: Specifies the connection to the database
    :return: True if database exists, False otherwise
    """
    return database_exists(db_engine.url)
예제 #38
0
def app(request):
    """Basic Flask application."""
    instance_path = tempfile.mkdtemp()
    app = Flask("testapp")
    DB = os.getenv("SQLALCHEMY_DATABASE_URI", "sqlite://")
    app.config.update(
        SQLALCHEMY_DATABASE_URI=DB,
        SQLALCHEMY_TRACK_MODIFICATIONS=False,
    )

    InvenioDB(app)
    InvenioPIDStore(app)

    with app.app_context():
        db_url = str(db.engine.url)
        if db_url != "sqlite://" and not database_exists(db_url):
            create_database(db_url)
        db.create_all()

    def teardown():
        with app.app_context():
            db_url = str(db.engine.url)
            db.session.close()
            if db_url != "sqlite://":
                drop_database(db_url)
            shutil.rmtree(instance_path)

    request.addfinalizer(teardown)
    app.test_request_context().push()

    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
예제 #40
0
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("Checking for database '%s'" % db_url)
    if not database_exists(db_url):
        log.info("Creating database '%s'" % db_url)
        create_database(db_url, template="template0")

    log.debug("Connecting to database '%s'" % 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)
예제 #41
0
def xtest_db_is_automatically_migrated(disk_store):
    db_conn_str = 'postgresql://localhost/test_provenance_automigrate'
    if sql_utils.database_exists(db_conn_str):
        sql_utils.drop_database(db_conn_str)

    sql_utils.create_database(db_conn_str)

    repo = r.PostgresRepo(db_conn_str,
                          disk_store,
                          read=True,
                          write=True,
                          delete=True,
                          create_db=False,
                          upgrade_db=True)
    p.set_default_repo(repo)

    @p.provenance()
    def calculate(a, b):
        return a + b

    # make sure it all works
    assert calculate(1, 2) == 3

    p.set_default_repo(None)
    sql_utils.drop_database(db_conn_str)
예제 #42
0
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
예제 #43
0
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
예제 #44
0
def app(request):
    """Flask application fixture."""
    instance_path = tempfile.mkdtemp()

    def init_app(app):
        app.config.update(
            LOGIN_DISABLED=False,
            MAIL_SUPPRESS_SEND=True,
            OAUTH2_CACHE_TYPE='simple',
            OAUTHLIB_INSECURE_TRANSPORT=True,
            SECRET_KEY='CHANGE_ME',
            SECURITY_DEPRECATED_PASSWORD_SCHEMES=[],
            SECURITY_PASSWORD_HASH='plaintext',
            SECURITY_PASSWORD_SALT='CHANGE_ME_ALSO',
            SECURITY_PASSWORD_SCHEMES=['plaintext'],
            SQLALCHEMY_DATABASE_URI=os.getenv(
                'SQLALCHEMY_DATABASE_URI',
                'sqlite:///' + os.path.join(instance_path, 'test.db')),
            SQLALCHEMY_TRACK_MODIFICATIONS=True,
            TESTING=True,
            WTF_CSRF_ENABLED=False,
        )
        Babel(app)
        Mail(app)
        Menu(app)
        Breadcrumbs(app)
        InvenioDB(app)
        InvenioOAuth2Server(app)

    api_app = Flask('testapiapp', instance_path=instance_path)
    api_app.config.update(APPLICATION_ROOT='/api',
                          ACCOUNTS_REGISTER_BLUEPRINT=True)
    init_app(api_app)
    InvenioAccountsREST(api_app)
    InvenioOAuth2ServerREST(api_app)

    app = Flask('testapp', instance_path=instance_path)
    init_app(app)
    InvenioAccountsUI(app)
    app.register_blueprint(accounts_blueprint)
    app.register_blueprint(server_blueprint)
    app.register_blueprint(settings_blueprint)

    app.wsgi_app = DispatcherMiddleware(app.wsgi_app,
                                        {'/api': api_app.wsgi_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
예제 #45
0
 def isEnabled(configs):
     return database_exists(SQLALCHEMY_DATABASE_URI(
         name=__class__.__bind_key__,
         project_path=configs['PROJECT_PATH'],
         category='Census',
         flags=configs['MARRYBIRD_FLAGS'],
     ))
예제 #46
0
def records_app(request):
    """Initialize InvenioRecords."""
    app = Flask('records_testapp')
    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
예제 #47
0
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()
예제 #48
0
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
예제 #49
0
 def start(self):
     self.new_engine()
     self.session_maker = scoped_session(sessionmaker(bind=self.engine, autoflush=False))
     self.new_session()
     self.base.metadata.bind = self.engine
     if not database_exists(self.engine.url):
         self.make_database()
     self.base.metadata.create_all()
예제 #50
0
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
예제 #51
0
 def teardown():
     with app.app_context():
         if database_exists(str(db.engine.url)):
             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()
     shutil.rmtree(app.instance_path)
예제 #52
0
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.')
예제 #53
0
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()
예제 #54
0
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
예제 #55
0
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)
예제 #56
0
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
예제 #57
0
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()
예제 #58
0
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()
예제 #59
0
def app(request):
    """Flask application fixture for E2E/integration/selenium tests.
    Overrides the `app` fixture found in `../conftest.py`. Tests/files in this
    folder and subfolders will see this variant of the `app` fixture.
    """
    app = create_app()
    app.config.update(dict(
        TESTING=True,
        TEST_RUNNER="celery.contrib.test_runner.CeleryTestSuiteRunner",
        CELERY_ALWAYS_EAGER=True,
        CELERY_RESULT_BACKEND="cache",
        CELERY_CACHE_BACKEND="memory",
        MAIL_SUPPRESS_SEND=True,
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        SQLALCHEMY_DATABASE_URI=os.environ.get(
            'SQLALCHEMY_DATABASE_URI', 'postgresql+psycopg2://localhost/hepdata_test')
    ))

    with app.app_context():
        if not database_exists(str(db.engine.url)):
            create_database(str(db.engine.url))
        db.create_all()

    with app.app_context():
        db.drop_all()
        db.create_all()
        reindex_all(recreate=True)

        ctx = app.test_request_context()
        ctx.push()

        user_count = User.query.filter_by(email='*****@*****.**').count()
        if user_count == 0:
            user = User(email='*****@*****.**', password='******', active=True)
            admin_role = Role(name='admin')
            coordinator_role = Role(name='coordinator')

            user.roles.append(admin_role)
            user.roles.append(coordinator_role)

            db.session.add(admin_role)
            db.session.add(coordinator_role)
            db.session.add(user)
            db.session.commit()

        load_default_data(app)

    def teardown():
        with app.app_context():
            db.drop_all()
            ctx.pop()

    request.addfinalizer(teardown)

    return app
예제 #60
0
파일: common.py 프로젝트: jssuzanne/ERPBlok
def drop_database(database):
    """ Close the registry instance of the database and drop the database

    :param: database's name
    """
    url = Configuration.get('get_url')(db_name=database)
    if not database_exists(url):
        raise Exception("Database %r does not already exist")

    registry = RegistryManager.get(database)
    registry.close()
    SU_drop_database(url)