comparison migrate_couch_to_arango.py @ 255:b4c83e9b9c7a

migration from couchdb to arangodb
author Dirk Olmes <dirk@xanthippe.ping.de>
date Thu, 29 Nov 2018 18:46:21 +0100
parents
children f79be01821c4
comparison
equal deleted inserted replaced
253:8b1678851b54 255:b4c83e9b9c7a
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import couchdb
5 import pyArango.connection
6
7 def get_or_cretae_arango_collection(arango_db, collection_name):
8 if arango_db.hasCollection(collection_name):
9 return arango_db[collection_name]
10 else:
11 return arango_db.createCollection(name=collection_name)
12
13 def migrate_feed(couch_doc):
14 couch_id = couch_doc['_id']
15 if couch_id.startswith('_design'):
16 return
17 try:
18 if couch_doc['doctype'] == 'feed':
19 arango_doc = arango_feed.createDocument()
20 copy(couch_doc, arango_doc)
21 arango_doc.save()
22 feed_mapping[couch_id] = arango_doc['_key']
23 except KeyError:
24 print('**** migrate error ' + str(document))
25
26 def migrate_rest(document):
27 if document['_id'].startswith('_design'):
28 return
29 try:
30 doctype = document['doctype']
31 if doctype == 'feed':
32 return
33 if doctype == 'feedEntry':
34 migrate_feed_entry(document)
35 elif doctype == 'preferences':
36 migrate_preferences(document)
37 else:
38 print('how to migrate ' + document['_id'])
39 except KeyError:
40 print('**** migrate error ' + str(document))
41
42 def migrate_feed_entry(couch_doc):
43 arango_doc = arango_feed_entry.createDocument()
44 copy(couch_doc, arango_doc)
45 feed_id = arango_doc['feed']
46 feed_id = feed_mapping[feed_id]
47 arango_doc['feed'] = feed_id
48 arango_doc.save()
49
50 def migrate_preferences(couch_doc):
51 arango_doc = arango_preferences.createDocument()
52 copy(couch_doc, arango_doc)
53 arango_doc.save()
54
55 def copy(couch_doc, arango_doc):
56 for key in couch_doc:
57 if not key.startswith('_'):
58 arango_doc[key] = couch_doc[key]
59
60 if __name__ == '__main__':
61 couch_server = couchdb.Server('http://192.168.2.1:5984/')
62 couch_db = couch_server['feedworm']
63
64 arango_connection = pyArango.connection.Connection(arangoURL='http://xanthippe:8529', username='root', password='kahbHeotQDYL4R5o')
65 arango_db = arango_connection['feedworm']
66 arango_feed = get_or_cretae_arango_collection(arango_db, 'feed')
67 arango_feed_entry = get_or_cretae_arango_collection(arango_db, 'feed_entry')
68 arango_preferences = get_or_cretae_arango_collection(arango_db, 'preferences')
69
70 feed_mapping = {}
71
72 for id in couch_db:
73 doc = couch_db[id]
74 migrate_feed(doc)
75
76 for id in couch_db:
77 doc = couch_db[id]
78 migrate_rest(doc)