Jason R. Coombs avatar Jason R. Coombs committed 8a3e44d

Completed migration support for Quotes

Comments (0)

Files changed (3)

 inane_channel: "#pmxbot"
 # database should be something like a MongoDB URI (mongodb://localhost)
 #  or sqlite URI (sqlite:path/filename)
-database: sqlite:bot_database/pmxbot.sqlite
+database: sqlite:pmxbot.sqlite
 places: ["Palo Alto, CA", "Canton, OH", "Washington, DC", "London, England"]
 lunch_choices:
     PA: ["Pasta?", "Thaiphoon", "Pluto's", "Penninsula Creamery", "Kan Zeman"]

pmxbot/logging.py

 
 from . import storage
 
-class Logger(storage.SelectableStorage): pass
+class Logger(storage.SelectableStorage):
+	"Base Logger class"
 
 init_logger = Logger.from_URI
 
 	def all_messages(self):
 		return self.db.find()
 
+	@staticmethod
+	def extract_legacy_id(oid):
+		"""
+		Given a special OID which includes the legacy sqlite ID, extract
+		the sqlite ID.
+		"""
+		return struct.unpack('L', oid.binary[-4:])[0]
+
 	def import_(self, message):
 		# construct a unique objectid with the correct datetime.
 		dt = message['datetime']
 		orig_id_packed = struct.pack('L', orig_id)
 		oid_new = str(oid_time)[:4] + '\x00'*4 + orig_id_packed
 		oid = storage.pymongo.objectid.ObjectId(oid_new)
+		if not hasattr(Logger, 'log_id_map'): Logger.log_id_map = dict()
+		Logger.log_id_map[orig_id] = oid
 		message['_id'] = oid
 		message['datetime'] = self._fmt_date(dt)
 		self.db.insert(message)
 		query = "SELECT quote FROM quotes WHERE library = ?"
 		return self.db.execute(query, [self.lib])
 
+	def export_all(self):
+		query = "SELECT quote, library, logid from quotes inner join quote_log on quotes.quoteid = quote_log.quoteid"
+		fields = 'quote', 'library', 'log_id'
+		return (dict(zip(fields, res)) for res in self.db.execute(query))
+
 class MongoDBQuotes(Quotes, storage.MongoDBStorage):
 	collection_name = 'quotes'
 
 	def __iter__(self):
 		return self.db.find(library=self.lib)
 
+	def _build_log_id_map(self):
+		from . import logging
+		if not hasattr(logging.Logger, 'log_id_map'):
+			log_db = self.db.database.logs
+			logging.Logger.log_id_map = dict(
+				(logging.MongoDBLogger.extract_legacy_id(rec['_id']), rec['_id'])
+				for rec in log_db.find(fields=[])
+			)
+		return logging.Logger.log_id_map
+		
+
+	def import_(self, quote):
+		log_id_map = self._build_log_id_map()
+		try:
+			quote['log_id'] = log_id_map[quote['log_id']]
+		except KeyError:
+			pass
+		self.db.insert(quote)
+
 def get_html(url):
 	h = httplib2.Http()
 	resp, html = h.request(url,
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.