Beispiel #1
0
 def test_get_db_for_doc_type(self):
     config = CouchConfig(config=self._config)
     self.assertEqual(
         config.get_db_for_doc_type('CommCareCase').uri, self.remote_db_uri)
     self.assertEqual(
         config.get_db_for_doc_type('CommCareUser').uri,
         '{}__users'.format(self.remote_db_uri))
Beispiel #2
0
 def test_all_db_uris_by_slug(self):
     config = CouchConfig(config=self._config)
     self.assertDictContainsSubset(
         {
             None: self.remote_db_uri,
             'users': '{}__users'.format(self.remote_db_uri),
             'fixtures': '{}__fixtures'.format(self.remote_db_uri),
             'meta': '{}__meta'.format(self.remote_db_uri),
         }, config.all_db_uris_by_slug)
 def test_all_db_uris_by_slug(self):
     config = CouchConfig(config=self._config)
     for key, value in {
             None: self.remote_db_uri,
             'users': '{}__users'.format(self.remote_db_uri),
             'fixtures': '{}__fixtures'.format(self.remote_db_uri),
             'meta': '{}__meta'.format(self.remote_db_uri),
     }.items():
         self.assertEqual(config.all_db_uris_by_slug[key], value,
                          f"key: {key!r}")
Beispiel #4
0
 def _get_couch_database_configs_from_string(self, db_string):
     sourcedb_parse_result = urlparse(db_string)
     return CouchConfig({
         'default': {
             'COUCH_HTTPS': sourcedb_parse_result.scheme == 'https',
             'COUCH_SERVER_ROOT': sourcedb_parse_result.hostname,
             'COUCH_USERNAME': sourcedb_parse_result.username,
             'COUCH_PASSWORD': sourcedb_parse_result.password,
             'COUCH_DATABASE_NAME': sourcedb_parse_result.path.lstrip('/')
         }
     })
Beispiel #5
0
 def _get_designs(self):
     # Instantiate here to make sure that it's instantiated after the dbs settings
     # are patched for tests
     couch_config = CouchConfig()
     db = Database(
         couch_config.get_db_uri_for_app_label(self.app_config.label), create=True)
     return [
         DesignInfo(app_label=self.app_config.label,
                    db=db,
                    design_path=self.dir)
     ]
Beispiel #6
0
    def handle(self, sourcedb, case_id, domain, **options):
        # FIXME broken b/c https://github.com/dimagi/commcare-hq/pull/15896
        source_couch = CouchConfig(sourcedb)
        doc_ids = [case_id]

        if should_use_sql_backend(domain):
            raise CommandError('This command only works for couch-based domains.')

        def _migrate_case(case_id):
            print('getting case %s' % case_id)
            case = CommCareCase.wrap(source_couch.get_db_for_class(CommCareCase).get(case_id))
            original_domain = case.domain
            if domain is not None:
                case.domain = domain
            case.save(force_update=True)
            return case, original_domain
Beispiel #7
0
    def handle(self, *args, **options):
        if len(args) < 2:
            raise CommandError('Usage is copy_case, %s' % self.args)
        source_couch = CouchConfig(args[0])
        case_id = args[1]
        doc_ids = [case_id]

        domain = args[2] if len(args) > 2 else None
        if should_use_sql_backend(domain):
            raise CommandError('This command only works for couch-based domains.')

        def _migrate_case(case_id):
            print 'getting case %s' % case_id
            case = CommCareCase.wrap(source_couch.get_db_for_class(CommCareCase).get(case_id))
            original_domain = case.domain
            if domain is not None:
                case.domain = domain
            case.save(force_update=True)
            return case, original_domain
Beispiel #8
0
 def test_get_db_for_db_name_not_found(self):
     config = CouchConfig(config=self._config)
     with self.assertRaises(DatabaseNotFound):
         config.get_db_for_db_name('missing')
Beispiel #9
0
 def test_get_db_for_db_name(self):
     config = CouchConfig(config=self._config)
     self.assertEqual(self.remote_db_uri,
                      config.get_db_for_db_name('test_cchq').uri)
     self.assertEqual('{}__users'.format(self.remote_db_uri),
                      config.get_db_for_db_name('test_cchq__users').uri)
Beispiel #10
0
 def test_remote_db_uri(self):
     config = CouchConfig(config=self._config)
     self.assertEqual(config.db_uri, self.remote_db_uri)
Beispiel #11
0
 def test_default_db_uri(self):
     config = CouchConfig()
     self.assertEqual(config.db_uri, settings.COUCH_DATABASE)
Beispiel #12
0
    def handle(self, *args, **options):
        if len(args) not in [2, 3]:
            raise CommandError('Usage is copy_domain %s' % self.args)
        self.exclude_dbs = (
            # these have data we don't want to copy
            'receiverwrapper',
            'couchlog',
            'auditcare',
            'fluff-bihar',
            'fluff-opm',
            'fluff-mc',
            'fluff-cvsu',
            'mvp-indicators',
            'm4change',
            # todo: missing domain/docs, but probably want to add back
            'meta',
        )
        self.source_couch = source_couch = CouchConfig(args[0])
        domain = args[1].strip()
        simulate = options['simulate']
        exclude_attachments = options['exclude_attachments']
        self.run_multi_process = options['run_multi_process']

        since = json_format_date(iso_string_to_date(
            options['since'])) if options['since'] else None

        if options['list_types']:
            for sourcedb_name, sourcedb in self.iter_source_dbs():
                self.list_types(sourcedb, domain, since)
            sys.exit(0)

        if simulate:
            print "\nSimulated run, no data will be copied.\n"

        if options['postgres_db'] and options['postgres_password']:
            settings.DATABASES[options['postgres_db']]['PASSWORD'] = options[
                'postgres_password']

        self.targetdb = CouchConfig(
            args[2]) if len(args) == 3 else CouchConfig()

        try:
            domain_doc = Domain.get_by_name(domain)
        except ResourceNotFound:
            domain_doc = None

        if domain_doc is None:
            self.copy_domain(source_couch, domain)

        if options['doc_types']:
            doc_types = options['doc_types'].split(',')
            for doc_type in doc_types:
                sourcedb = source_couch.get_db_for_doc_type(doc_type)
                startkey = [
                    x for x in [domain, doc_type, since] if x is not None
                ]
                endkey = [x for x in [domain, doc_type, {}] if x is not None]
                self.copy_docs(sourcedb,
                               domain,
                               simulate,
                               startkey,
                               endkey,
                               doc_type=doc_type,
                               since=since,
                               postgres_db=options['postgres_db'],
                               exclude_attachments=exclude_attachments)
        elif options['id_file']:
            path = options['id_file']
            if not os.path.isfile(path):
                print "Path '%s' does not exist or is not a file" % path
                sys.exit(1)

            with open(path) as input:
                doc_ids = [line.rstrip('\n') for line in input]

            if not doc_ids:
                print "Path '%s' does not contain any document ID's" % path
                sys.exit(1)

            for sourcedb_name, sourcedb in self.iter_source_dbs():
                self.copy_docs(sourcedb,
                               domain,
                               simulate,
                               doc_ids=doc_ids,
                               postgres_db=options['postgres_db'],
                               exclude_attachments=exclude_attachments)
        else:
            startkey = [domain]
            endkey = [domain, {}]
            exclude_types = DEFAULT_EXCLUDE_TYPES + options[
                'doc_types_exclude'].split(',')
            for sourcedb_name, sourcedb in self.iter_source_dbs():
                self.copy_docs(sourcedb,
                               domain,
                               simulate,
                               startkey,
                               endkey,
                               exclude_types=exclude_types,
                               postgres_db=options['postgres_db'],
                               exclude_attachments=exclude_attachments)