Exemplo n.º 1
0
    def _creator(items, prefix, creator):
        N = len(items)
        prefix = prefix.format(N)
        e = get_time_estimator(N)
        created = 0
        if quiet:
            print(prefix)

        for i, table in enumerate(items):
            try:
                if not quiet:
                    print_progress(
                        1.0 * (i+1) / N, prefix=prefix,
                        suffix=str(datetime.timedelta(seconds=e()[0])))
                creator(table)
                created += 1
            except Exception:
                print('\r>>> problem with creating {0}'.format(table))
                current_app.logger.exception(table)

        if created == N:
            print(">>> Everything has been created successfully.")
        else:
            print("ERROR: not all items were properly created.")
            print(">>> Created", created, 'out of', N)
Exemplo n.º 2
0
def create(default_data=True):
    """Creates database tables from sqlalchemy models"""

    print(">>> Going to create tables...")

    from sqlalchemy import event
    from invenio.utils.date import get_time_estimator
    from invenio.legacy.inveniocfg import test_db_connection
    from invenio.ext.sqlalchemy import db, models
    try:
        test_db_connection()
    except Exception as e:
        from invenio.ext.logging.wrappers import get_traceback
        print('Cannot connect with the db:', e.message)
        print(get_traceback())
        return

    list(models)

    def cfv_after_create(target, connection, **kw):
        print
        print(">>> Modifing table structure...")
        from invenio.legacy.dbquery import run_sql
        run_sql('ALTER TABLE collection_field_fieldvalue DROP PRIMARY KEY')
        run_sql('ALTER TABLE collection_field_fieldvalue ADD INDEX id_collection(id_collection)')
        run_sql('ALTER TABLE collection_field_fieldvalue CHANGE id_fieldvalue id_fieldvalue mediumint(9) unsigned')
        #print(run_sql('SHOW CREATE TABLE collection_field_fieldvalue'))

    from invenio.modules.search.models import CollectionFieldFieldvalue
    event.listen(CollectionFieldFieldvalue.__table__, "after_create", cfv_after_create)

    tables = db.metadata.sorted_tables
    N = len(tables)

    prefix = '>>> Creating %d tables ...' % N

    e = get_time_estimator(N)
    created = 0

    for i, table in enumerate(tables):
        try:
            print_progress(1.0 * i / N, prefix=prefix,
                           suffix=str(datetime.timedelta(seconds=e()[0])))
            table.create(bind=db.engine)
            created += 1
        except:
            print('\r', '>>> problem with creating table', table)

    print('\n')

    if created == N:
        print(">>> Tables created successfully.")
    else:
        print("ERROR: not all tables were properly created.")
        print(">>> Created", created, 'out of', N)

    populate(default_data)
Exemplo n.º 3
0
    def _dropper(items, prefix, dropper):
        N = len(items)
        prefix = prefix.format(N)
        e = get_time_estimator(N)
        dropped = 0
        if quiet:
            print(prefix)

        for i, table in enumerate(items):
            try:
                if not quiet:
                    print_progress(1.0 * (i + 1) / N, prefix=prefix, suffix=str(datetime.timedelta(seconds=e()[0])))
                dropper(table)
                dropped += 1
            except Exception:
                print("\r", ">>> problem with dropping ", table)
                current_app.logger.exception(table)

        if dropped == N:
            print(">>> Everything has been dropped successfully.")
        else:
            print("ERROR: not all items were properly dropped.")
            print(">>> Dropped", dropped, "out of", N)
Exemplo n.º 4
0
def drop(yes_i_know=False):
    """Drops database tables"""

    print(">>> Going to drop tables and related data on filesystem ...")

    from sqlalchemy import event
    from invenio.utils.date import get_time_estimator
    from invenio.utils.text import wrap_text_in_a_box, wait_for_user
    from invenio.legacy.webstat.api import destroy_customevents
    from invenio.legacy.inveniocfg import test_db_connection
    from invenio.ext.sqlalchemy import db, models
    from invenio.legacy.bibdocfile.api import _make_base_dir

    ## Step 0: confirm deletion
    wait_for_user(wrap_text_in_a_box(
        "WARNING: You are going to destroy your database tables and related "
        "data on filesystem!"))

    ## Step 1: test database connection
    test_db_connection()
    list(models)

    ## Step 2: disable foreign key checks
    if db.engine.name == 'mysql':
        db.engine.execute('SET FOREIGN_KEY_CHECKS=0;')

    ## Step 3: destroy associated data
    try:
        msg = destroy_customevents()
        if msg:
            print(msg)
    except:
        print("ERROR: Could not destroy customevents.")

    ## FIXME: move to bibedit_model
    def bibdoc_before_drop(target, connection_dummy, **kw_dummy):
        print
        print(">>> Going to remove records data...")
        for (docid,) in db.session.query(target.c.id).all():
            directory = _make_base_dir(docid)
            if os.path.isdir(directory):
                print('    >>> Removing files for docid =', docid)
                shutil.rmtree(directory)
        db.session.commit()
        print(">>> Data has been removed.")

    from invenio.modules.editor.models import Bibdoc
    event.listen(Bibdoc.__table__, "before_drop", bibdoc_before_drop)

    tables = list(reversed(db.metadata.sorted_tables))
    N = len(tables)

    prefix = '>>> Dropping %d tables ...' % N

    e = get_time_estimator(N)
    dropped = 0

    for i, table in enumerate(tables):
        try:
            print_progress(1.0 * i / N, prefix=prefix,
                           suffix=str(datetime.timedelta(seconds=e()[0])))
            table.drop(bind=db.engine)
            dropped += 1
        except:
            print('\r', '>>> problem with dropping table', table)

    print
    if dropped == N:
        print(">>> Tables dropped successfully.")
    else:
        print("ERROR: not all tables were properly dropped.")
        print(">>> Dropped", dropped, 'out of', N)