Ejemplo n.º 1
0
def setup_app(command, conf, vars):
    """Called by ``paster setup-app``.

    This script is responsible for:

        * Creating the initial database schema and loading default data.
        * Executing any migrations necessary to bring an existing database
          up-to-date. Your data should be safe but, as always, be sure to
          make backups before using this.
        * Re-creating the default database for every run of the test suite.

    XXX: All your data will be lost IF you run the test suite with a
         config file named 'test.ini'. Make sure you have this configured
         to a different database than in your usual deployment.ini or
         development.ini file because all database tables are dropped a
         and recreated every time this script runs.

    XXX: If you are upgrading from MediaCore v0.7.2 or v0.8.0, run whichever
         one of these that applies:
           ``python batch-scripts/upgrade/upgrade_from_v072.py deployment.ini``
           ``python batch-scripts/upgrade/upgrade_from_v080.py deployment.ini``

    XXX: For search to work, we depend on a number of MySQL triggers which
         copy the data from our InnoDB tables to a MyISAM table for its
         fulltext indexing capability. Triggers can only be installed with
         a mysql superuser like root, so you must run the setup_triggers.sql
         script yourself.

    """
    # paster just scans the source code for a "websetup.py". Due to our
    # compatibility module for the old "mediacore" namespace it actually finds
    # two modules (mediadrop.websetup and mediacore.websetup) even though both
    # actually point to the same source code.
    # Because of that "paster setup-app" actually runs "setup_app()" twice
    # which causes some bad stuff to happen (e.g. duplicate metadata
    # initialization. Until we get rid of the compat package we should make
    # sure the following code is only run once.
    global did_run_setup
    if did_run_setup:
        return
    config = load_environment(conf.global_conf, conf.local_conf)
    plugin_manager = config['pylons.app_globals'].plugin_mgr
    mediadrop_migrator = MediaDropMigrator.from_config(conf, log=log)

    engine = metadata.bind
    db_connection = engine.connect()
    # simplistic check to see if MediaDrop tables are present, just check for
    # the media_files table and assume that all other tables are there as well
    from mediadrop.model.media import media_files
    mediadrop_tables_exist = engine.dialect.has_table(db_connection,
                                                      media_files.name)

    run_migrations = True
    if not mediadrop_tables_exist:
        head_revision = mediadrop_migrator.head_revision()
        log.info('Initializing new database with version %r' % head_revision)
        metadata.create_all(bind=DBSession.bind, checkfirst=True)
        mediadrop_migrator.init_db(revision=head_revision)
        run_migrations = False
        add_default_data()
        for migrator in plugin_manager.migrators():
            migrator.init_db()
        events.Environment.database_initialized()
    elif not mediadrop_migrator.alembic_table_exists():
        if not mediadrop_migrator.migrate_table_exists():
            log.error(
                'No migration table found, probably your MediaDrop install '
                'is too old (< 0.9?). Please upgrade to MediaCore CE 0.9 first.'
            )
            raise AssertionError('no migration table found')
        alembic_revision = mediadrop_migrator.map_migrate_version()
        mediadrop_migrator.stamp(alembic_revision)
    if run_migrations:
        mediadrop_migrator.migrate_db()
        for migrator in plugin_manager.migrators():
            migrator.migrate_db()
        events.Environment.database_migrated()

    cleanup_players_table(enabled=True)

    # Save everything, along with the dummy data if applicable
    DBSession.commit()
    events.Environment.database_ready()

    log.info('Generating appearance.css from your current settings')
    settings = DBSession.query(Setting.key, Setting.value)
    if settings.count() == 0:
        error_msg = (
            u"Unable to find any settings in the database. This may happen if a previous\n"
            u"setup did not complete successfully.\n"
            u"Please inspect your database contents. If you don't have any valuable data in\n"
            u"that db you can try to remove all tables and run the setup process again.\n"
            u"BE CAREFUL: REMOVING ALL TABLES MIGHT CAUSE DATA LOSS!\n")
        sys.stderr.write(error_msg)
        log.error(error_msg.replace('\n', u' '))
        sys.exit(99)
    generate_appearance_css(settings, cache_dir=conf['cache_dir'])

    did_run_setup = True
    log.info('Successfully setup')
Ejemplo n.º 2
0
def setup_app(command, conf, vars):
    """Called by ``paster setup-app``.

    This script is responsible for:

        * Creating the initial database schema and loading default data.
        * Executing any migrations necessary to bring an existing database
          up-to-date. Your data should be safe but, as always, be sure to
          make backups before using this.
        * Re-creating the default database for every run of the test suite.

    XXX: All your data will be lost IF you run the test suite with a
         config file named 'test.ini'. Make sure you have this configured
         to a different database than in your usual deployment.ini or
         development.ini file because all database tables are dropped a
         and recreated every time this script runs.

    XXX: If you are upgrading from MediaCore v0.7.2 or v0.8.0, run whichever
         one of these that applies:
           ``python batch-scripts/upgrade/upgrade_from_v072.py deployment.ini``
           ``python batch-scripts/upgrade/upgrade_from_v080.py deployment.ini``

    XXX: For search to work, we depend on a number of MySQL triggers which
         copy the data from our InnoDB tables to a MyISAM table for its
         fulltext indexing capability. Triggers can only be installed with
         a mysql superuser like root, so you must run the setup_triggers.sql
         script yourself.

    """
    # paster just scans the source code for a "websetup.py". Due to our
    # compatibility module for the old "mediacore" namespace it actually finds
    # two modules (mediadrop.websetup and mediacore.websetup) even though both
    # actually point to the same source code.
    # Because of that "paster setup-app" actually runs "setup_app()" twice
    # which causes some bad stuff to happen (e.g. duplicate metadata
    # initialization. Until we get rid of the compat package we should make
    # sure the following code is only run once.
    global did_run_setup
    if did_run_setup:
        return
    config = load_environment(conf.global_conf, conf.local_conf)
    plugin_manager = config['pylons.app_globals'].plugin_mgr
    mediadrop_migrator = MediaDropMigrator.from_config(conf, log=log)
    
    engine = metadata.bind
    db_connection = engine.connect()
    # simplistic check to see if MediaDrop tables are present, just check for
    # the media_files table and assume that all other tables are there as well
    from mediadrop.model.media import media_files
    mediadrop_tables_exist = engine.dialect.has_table(db_connection, media_files.name)
    
    run_migrations = True
    if not mediadrop_tables_exist:
        head_revision = mediadrop_migrator.head_revision()
        log.info('Initializing new database with version %r' % head_revision)
        metadata.create_all(bind=DBSession.bind, checkfirst=True)
        mediadrop_migrator.init_db(revision=head_revision)
        run_migrations = False
        add_default_data()
        for migrator in plugin_manager.migrators():
            migrator.init_db()
        events.Environment.database_initialized()
    elif not mediadrop_migrator.alembic_table_exists():
        if not mediadrop_migrator.migrate_table_exists():
            log.error('No migration table found, probably your MediaDrop install '
                'is too old (< 0.9?). Please upgrade to MediaCore CE 0.9 first.')
            raise AssertionError('no migration table found')
        alembic_revision = mediadrop_migrator.map_migrate_version()
        mediadrop_migrator.stamp(alembic_revision)
    if run_migrations:
        mediadrop_migrator.migrate_db()
        for migrator in plugin_manager.migrators():
            migrator.migrate_db()
        events.Environment.database_migrated()
    
    cleanup_players_table(enabled=True)
    
    # Save everything, along with the dummy data if applicable
    DBSession.commit()
    events.Environment.database_ready()

    log.info('Generating appearance.css from your current settings')
    settings = DBSession.query(Setting.key, Setting.value)
    if settings.count() == 0:
        error_msg = (
            u"Unable to find any settings in the database. This may happen if a previous\n"
            u"setup did not complete successfully.\n"
            u"Please inspect your database contents. If you don't have any valuable data in\n"
            u"that db you can try to remove all tables and run the setup process again.\n"
            u"BE CAREFUL: REMOVING ALL TABLES MIGHT CAUSE DATA LOSS!\n"
        )
        sys.stderr.write(error_msg)
        log.error(error_msg.replace('\n', u' '))
        sys.exit(99)
    generate_appearance_css(settings, cache_dir=conf['cache_dir'])

    did_run_setup = True
    log.info('Successfully setup')
Ejemplo n.º 3
0
def setup_app(command, conf, vars):
    """Called by ``paster setup-app``.

    This script is responsible for:

        * Creating the initial database schema and loading default data.
        * Executing any migrations necessary to bring an existing database
          up-to-date. Your data should be safe but, as always, be sure to
          make backups before using this.
        * Re-creating the default database for every run of the test suite.

    XXX: All your data will be lost IF you run the test suite with a
         config file named 'test.ini'. Make sure you have this configured
         to a different database than in your usual deployment.ini or
         development.ini file because all database tables are dropped a
         and recreated every time this script runs.

    XXX: If you are upgrading from MediaCore v0.7.2 or v0.8.0, run whichever
         one of these that applies:
           ``python batch-scripts/upgrade/upgrade_from_v072.py deployment.ini``
           ``python batch-scripts/upgrade/upgrade_from_v080.py deployment.ini``

    XXX: For search to work, we depend on a number of MySQL triggers which
         copy the data from our InnoDB tables to a MyISAM table for its
         fulltext indexing capability. Triggers can only be installed with
         a mysql superuser like root, so you must run the setup_triggers.sql
         script yourself.

    """
    config = load_environment(conf.global_conf, conf.local_conf)
    plugin_manager = config['pylons.app_globals'].plugin_mgr
    mediadrop_migrator = MediaDropMigrator.from_config(conf, log=log)
    
    engine = metadata.bind
    db_connection = engine.connect()
    # simplistic check to see if MediaDrop tables are present, just check for
    # the media_files table and assume that all other tables are there as well
    from mediadrop.model.media import media_files
    mediadrop_tables_exist = engine.dialect.has_table(db_connection, media_files.name)
    
    run_migrations = True
    if not mediadrop_tables_exist:
        head_revision = mediadrop_migrator.head_revision()
        log.info('Initializing new database with version %r' % head_revision)
        metadata.create_all(bind=DBSession.bind, checkfirst=True)
        mediadrop_migrator.init_db(revision=head_revision)
        run_migrations = False
        add_default_data()
        for migrator in plugin_manager.migrators():
            migrator.init_db()
        events.Environment.database_initialized()
    elif not mediadrop_migrator.migrate_table_exists():
        log.error('No migration table found, probably your MediaDrop install '
            'is too old (< 0.9?). Please upgrade to MediaCore CE 0.9 first.')
        raise AssertionError('no migration table found')
    elif not mediadrop_migrator.alembic_table_exists():
        alembic_revision = mediadrop_migrator.map_migrate_version()
        mediadrop_migrator.stamp(alembic_revision)
    if run_migrations:
        mediadrop_migrator.migrate_db()
        for migrator in plugin_manager.migrators():
            migrator.migrate_db()
        events.Environment.database_migrated()
    
    cleanup_players_table(enabled=True)
    
    # Save everything, along with the dummy data if applicable
    DBSession.commit()
    events.Environment.database_ready()

    log.info('Generating appearance.css from your current settings')
    settings = DBSession.query(Setting.key, Setting.value)
    generate_appearance_css(settings, cache_dir=conf['cache_dir'])

    log.info('Successfully setup')