cloudant / python-cloudant

A Python library for Cloudant and CouchDB
Apache License 2.0
163 stars 55 forks source link

Replicator create_target can't be used since you must pass a Database instance #452

Closed segevfiner closed 4 years ago

segevfiner commented 4 years ago

Bug Description

Replicator supports creating a replication with create_target, but this can't actually be used as target_db only supports a database instance which means it must be an existing database.

2. What you expected to happen

That Replicator supports using create_target without pre-creating the database.

3. What actually happened

You can only pass it an already created database.

Environment details

python-cloudant 2.12.0 Python 3.7.4

bessbd commented 4 years ago

Hi @segevfiner ,

Even though Replicator requires database instances, it does not mean those databases have to exist. Consider the following example:

import time

from cloudant import couchdb_admin_party
from cloudant.database import CouchDatabase
from cloudant.document import Document
from cloudant.replicator import Replicator

RANDOM_DOC_ID = 'random_id'

with couchdb_admin_party(url='http://localhost:5984') as client:
    source_db = client.create_database('test_source_db')
    source_db.create_document({
        '_id': RANDOM_DOC_ID,
        'foo': 'bar'
    })

    target_db = CouchDatabase(client, 'test_target_db')

    assert not target_db.exists()

    replicator = Replicator(client)
    replication_document = replicator.create_replication(
        source_db=source_db,
        target_db=target_db,
        create_target=True
    )

    while replicator.replication_state(
            replication_document['_id']) != 'completed':
        time.sleep(1)

    assert Document(target_db, RANDOM_DOC_ID).exists()
    print('done')

I'll go ahead and close this issue. Please reopen or create a new one if this answer does not help with your use case or if you need any further clarification.