def main():
    a = parse_args()
    client = ArangoClient(hosts=a.arango_url)
    if a.user:
        if a.pwd_file:
            with open(a.pwd_file) as pwd_file:
                pwd = pwd_file.read().strip()
        else:
            pwd = getpass.getpass()
        db = client.db(a.database, a.user, pwd, verify=True)
    else:
        db = client.db(a.database, verify=True)
    fac = ArangoBatchTimeTravellingDBFactory(db, a.load_registry_collection)

    roll_back_last_load(fac, a.load_namespace)
Exemplo n.º 2
0
def test_rollback_fail_nothing_to_roll_back(arango_db):
    """
    Test that a rollback fails if theres < 2 loads registered.
    """
    create_timetravel_collection(arango_db, 'v')
    create_timetravel_collection(arango_db, 'e', edge=True)
    arango_db.create_collection('r')

    db = ArangoBatchTimeTravellingDB(arango_db, 'r', 'v', default_edge_collection='e')

    db.register_load_start('ns1', 'v1', 1000, 500, 100)
    db.register_load_complete('ns1', 'v1', 150)

    check_exception(
        lambda: roll_back_last_load(db, 'ns1'), ValueError,
        'Nothing to roll back')
def test_rollback_with_merge_collection(arango_db):
    """
    Test rolling back a load including a merge collection.
    """
    vcol = create_timetravel_collection(arango_db, 'v')
    edcol = create_timetravel_collection(arango_db, 'def_e', edge=True)
    e1col = create_timetravel_collection(arango_db, 'e1', edge=True)
    e2col = create_timetravel_collection(arango_db, 'e2', edge=True)
    mcol = create_timetravel_collection(arango_db, 'm', edge=True)
    arango_db.create_collection('r')

    m = ADB_MAX_TIME

    _import_v(vcol, {'id': '1', 'k': '1'}, 0, m, 0, m, 'v1', 'v2')
    _import_v(vcol, {'id': '2', 'k': '2'}, 300, m, 299, m, 'v2', 'v2')
    _import_v(vcol, {'id': '3', 'k': '3'}, 0, 299, 0, 298, 'v1', 'v1')
    _import_v(vcol, {'id': '3', 'k': '3'}, 300, m, 299, m, 'v2', 'v2')
    _import_v(vcol, {'id': '4', 'k': '4'}, 0, 299, 0, 298, 'v1', 'v1')

    _import_e(edcol, {
        'id': '1',
        'to': '1',
        'from': '1',
        'k': '1'
    }, 0, m, 0, m, 'v1', 'v2', 'f')
    _import_e(edcol, {
        'id': '2',
        'to': '2',
        'from': '2',
        'k': '2'
    }, 300, m, 299, m, 'v2', 'v2', 'f')

    _import_e(e1col, {
        'id': '1',
        'to': '1',
        'from': '1',
        'k': '1'
    }, 0, 299, 0, 298, 'v1', 'v1', 'f')
    _import_e(e1col, {
        'id': '1',
        'to': '1',
        'from': '1',
        'k': '1'
    }, 300, m, 299, m, 'v2', 'v2', 'f')

    _import_e(e2col, {
        'id': '1',
        'to': '1',
        'from': '1',
        'k': '1'
    }, 0, 299, 0, 298, 'v1', 'v1', 'f')

    # merge edges are never updated once created
    _import_e(mcol, {
        'id': '1',
        'to': '1',
        'from': '1',
        'k': '1'
    }, 0, m, 0, m, 'v1', 'v1', 'f')
    _import_e(mcol, {
        'id': '2',
        'to': '2',
        'from': '2',
        'k': '2'
    }, 300, m, 299, m, 'v2', 'v2', 'f')

    db = ArangoBatchTimeTravellingDB(arango_db,
                                     'r',
                                     'v',
                                     default_edge_collection='def_e',
                                     edge_collections=['e1', 'e2'],
                                     merge_collection='m')

    db.register_load_start('ns1', 'v1', 0, 0, 4567)
    db.register_load_complete('ns1', 'v1', 5678)
    db.register_load_start('ns1', 'v2', 300, 250, 6789)
    db.register_load_complete('ns1', 'v2', 7890)

    fac = ArangoBatchTimeTravellingDBFactory(arango_db, 'r')

    roll_back_last_load(fac, 'ns1')

    vexpected = [
        {
            'id': '1',
            '_key': '1_v1',
            '_id': 'v/1_v1',
            'first_version': 'v1',
            'last_version': 'v1',
            'created': 0,
            'expired': ADB_MAX_TIME,
            'release_created': 0,
            'release_expired': ADB_MAX_TIME,
            'k': '1'
        },
        {
            'id': '3',
            '_key': '3_v1',
            '_id': 'v/3_v1',
            'first_version': 'v1',
            'last_version': 'v1',
            'created': 0,
            'expired': ADB_MAX_TIME,
            'release_created': 0,
            'release_expired': ADB_MAX_TIME,
            'k': '3'
        },
        {
            'id': '4',
            '_key': '4_v1',
            '_id': 'v/4_v1',
            'first_version': 'v1',
            'last_version': 'v1',
            'created': 0,
            'expired': ADB_MAX_TIME,
            'release_created': 0,
            'release_expired': ADB_MAX_TIME,
            'k': '4'
        },
    ]

    check_docs(arango_db, vexpected, 'v')

    ed_expected = [
        {
            'id': '1',
            'from': '1',
            'to': '1',
            '_key': '1_v1',
            '_id': 'def_e/1_v1',
            '_from': 'f/1_v1',
            '_to': 'f/1_v1',
            'first_version': 'v1',
            'last_version': 'v1',
            'created': 0,
            'expired': ADB_MAX_TIME,
            'release_created': 0,
            'release_expired': ADB_MAX_TIME,
            'k': '1'
        },
    ]

    check_docs(arango_db, ed_expected, 'def_e')

    e1_expected = [
        {
            'id': '1',
            'from': '1',
            'to': '1',
            '_key': '1_v1',
            '_id': 'e1/1_v1',
            '_from': 'f/1_v1',
            '_to': 'f/1_v1',
            'first_version': 'v1',
            'last_version': 'v1',
            'created': 0,
            'expired': ADB_MAX_TIME,
            'release_created': 0,
            'release_expired': ADB_MAX_TIME,
            'k': '1'
        },
    ]

    check_docs(arango_db, e1_expected, 'e1')

    e2_expected = [
        {
            'id': '1',
            'from': '1',
            'to': '1',
            '_key': '1_v1',
            '_id': 'e2/1_v1',
            '_from': 'f/1_v1',
            '_to': 'f/1_v1',
            'first_version': 'v1',
            'last_version': 'v1',
            'created': 0,
            'expired': ADB_MAX_TIME,
            'release_created': 0,
            'release_expired': ADB_MAX_TIME,
            'k': '1'
        },
    ]

    check_docs(arango_db, e2_expected, 'e2')

    m_expected = [
        {
            'id': '1',
            'from': '1',
            'to': '1',
            '_key': '1_v1',
            '_id': 'm/1_v1',
            '_from': 'f/1_v1',
            '_to': 'f/1_v1',
            'first_version': 'v1',
            'last_version': 'v1',
            'created': 0,
            'expired': ADB_MAX_TIME,
            'release_created': 0,
            'release_expired': ADB_MAX_TIME,
            'k': '1'
        },
    ]

    check_docs(arango_db, m_expected, 'm')

    registry_expected = {
        '_key': 'ns1_v1',
        '_id': 'r/ns1_v1',
        'load_namespace': 'ns1',
        'load_version': 'v1',
        'load_timestamp': 0,
        'release_timestamp': 0,
        'start_time': 4567,
        'completion_time': 5678,
        'state': 'complete',
        'vertex_collection': 'v',
        'merge_collection': 'm',
        'edge_collections': ['def_e', 'e1', 'e2']
    }

    _check_registry_doc(arango_db, registry_expected, 'r')
def test_rollback_without_merge_collection(arango_db):
    """
    Test rolling back a load with no merge collection and only one edge collection.
    """
    vcol = create_timetravel_collection(arango_db, 'v')
    ecol = create_timetravel_collection(arango_db, 'e', edge=True)
    arango_db.create_collection('r')

    m = ADB_MAX_TIME

    _import_v(vcol, {'id': '1', 'k': '1'}, 0, m, 0, m, 'v1', 'v2')
    _import_v(vcol, {'id': '2', 'k': '2'}, 300, m, 299, m, 'v2', 'v2')
    _import_v(vcol, {'id': '3', 'k': '3'}, 0, 299, 0, 298, 'v1', 'v1')
    _import_v(vcol, {'id': '3', 'k': '3'}, 300, m, 299, m, 'v2', 'v2')
    _import_v(vcol, {'id': '4', 'k': '4'}, 0, 299, 0, 298, 'v1', 'v1')

    _import_e(ecol, {
        'id': '1',
        'to': '1',
        'from': '1',
        'k': '1'
    }, 0, m, 0, m, 'v1', 'v2', 'f')
    _import_e(ecol, {
        'id': '2',
        'to': '2',
        'from': '2',
        'k': '2'
    }, 300, m, 299, m, 'v2', 'v2', 'f')
    _import_e(ecol, {
        'id': '3',
        'to': '3',
        'from': '3',
        'k': '3'
    }, 0, 299, 0, 298, 'v1', 'v1', 'f')
    _import_e(ecol, {
        'id': '3',
        'to': '3',
        'from': '3',
        'k': '3'
    }, 300, m, 399, 0, 'v2', 'v2', 'f')
    _import_e(ecol, {
        'id': '4',
        'to': '4',
        'from': '4',
        'k': '4'
    }, 0, 299, 0, 298, 'v1', 'v1', 'f')

    db = ArangoBatchTimeTravellingDB(arango_db,
                                     'r',
                                     'v',
                                     default_edge_collection='e')

    db.register_load_start('ns1', 'v1', 0, 0, 4567)
    db.register_load_complete('ns1', 'v1', 5678)
    db.register_load_start('ns1', 'v2', 300, 250, 6789)
    db.register_load_complete('ns1', 'v2', 7890)

    fac = ArangoBatchTimeTravellingDBFactory(arango_db, 'r')

    roll_back_last_load(fac, 'ns1')

    vexpected = [
        {
            'id': '1',
            '_key': '1_v1',
            '_id': 'v/1_v1',
            'first_version': 'v1',
            'last_version': 'v1',
            'created': 0,
            'expired': ADB_MAX_TIME,
            'release_created': 0,
            'release_expired': ADB_MAX_TIME,
            'k': '1'
        },
        {
            'id': '3',
            '_key': '3_v1',
            '_id': 'v/3_v1',
            'first_version': 'v1',
            'last_version': 'v1',
            'created': 0,
            'expired': ADB_MAX_TIME,
            'release_created': 0,
            'release_expired': ADB_MAX_TIME,
            'k': '3'
        },
        {
            'id': '4',
            '_key': '4_v1',
            '_id': 'v/4_v1',
            'first_version': 'v1',
            'last_version': 'v1',
            'created': 0,
            'expired': ADB_MAX_TIME,
            'release_created': 0,
            'release_expired': ADB_MAX_TIME,
            'k': '4'
        },
    ]

    check_docs(arango_db, vexpected, 'v')

    e_expected = [
        {
            'id': '1',
            'from': '1',
            'to': '1',
            '_key': '1_v1',
            '_id': 'e/1_v1',
            '_from': 'f/1_v1',
            '_to': 'f/1_v1',
            'first_version': 'v1',
            'last_version': 'v1',
            'created': 0,
            'expired': ADB_MAX_TIME,
            'release_created': 0,
            'release_expired': ADB_MAX_TIME,
            'k': '1'
        },
        {
            'id': '3',
            'from': '3',
            'to': '3',
            '_key': '3_v1',
            '_id': 'e/3_v1',
            '_from': 'f/3_v1',
            '_to': 'f/3_v1',
            'first_version': 'v1',
            'last_version': 'v1',
            'created': 0,
            'expired': ADB_MAX_TIME,
            'release_created': 0,
            'release_expired': ADB_MAX_TIME,
            'k': '3'
        },
        {
            'id': '4',
            'from': '4',
            'to': '4',
            '_key': '4_v1',
            '_id': 'e/4_v1',
            '_from': 'f/4_v1',
            '_to': 'f/4_v1',
            'first_version': 'v1',
            'last_version': 'v1',
            'created': 0,
            'expired': ADB_MAX_TIME,
            'release_created': 0,
            'release_expired': ADB_MAX_TIME,
            'k': '4'
        },
    ]

    check_docs(arango_db, e_expected, 'e')

    registry_expected = {
        '_key': 'ns1_v1',
        '_id': 'r/ns1_v1',
        'load_namespace': 'ns1',
        'load_version': 'v1',
        'load_timestamp': 0,
        'release_timestamp': 0,
        'start_time': 4567,
        'completion_time': 5678,
        'state': 'complete',
        'vertex_collection': 'v',
        'merge_collection': None,
        'edge_collections': ['e']
    }

    _check_registry_doc(arango_db, registry_expected, 'r')