Unverified Commit 8e87ee36 authored by Aral Balkan's avatar Aral Balkan
Browse files

Migration script working.

parent d56debfc
LevelUp = require 'levelup'
# LevelPromise = require 'level-promise'
Sublevel = require 'level-sublevel'
WriteStream = require 'write-stream'
......@@ -6,8 +6,15 @@ liveStream = require 'level-live-stream'
path = require 'path-extra'
Promise = require 'thrush'
LevelUp = require 'levelup'
LevelPromisify = require 'level-promisify'
db = LevelUp '/Users/aral/Library/Containers/ind.ie.Heartbeat/Data/db'
level = LevelUp '/Users/aral/Library/Containers/ind.ie.Heartbeat/Data/db'
db = LevelPromisify level
#db = LevelUp 'db'
......@@ -17,45 +24,52 @@ version0toVersion1 = ->
return new Promise ((fulfill, reject) =>
console.log "Getting all entries"
# Create options for a new stream
options = { gt: '\x00', lt: '\uffff' }
allEntriesStream = db.createReadStream options
allEntriesStream.on 'error', (err) ->
console.log "All entries stream error: #{err}"
dataStream = db.root.createReadStream options
toArray = WriteStream.toArray (allEntries) =>
dataStream.on 'error', (err) ->
console.log "Data stream error: #{err}"
for entry in allEntries
toArray = WriteStream.toArray (data) =>
console.log "Checking #{entry.key}"
Promise.series data, (datum) ->
depracatedMessageClockMatcher = /.*?\!(\d{9}-)/
matches = entry.key.match depracatedMessageClockMatcher
matches = datum.key.match depracatedMessageClockMatcher
if matches != null
console.log "Will migrate: #{entry.key}"
# Remove the depracated message clock from message keys.
newKey = entry.key.replace matches[1], ''
newKey = datum.key.replace matches[1], ''
console.log "Migrating #{datum.key} to #{newKey}…"
operations = [
{type: 'put', key: newKey, value: datum.value},
{type: 'del', key: datum.key}
# Carry out the operations in a batch.
.then ->
console.log "OK."
.catch (error) ->
console.log "Error while updating #{datum.key} to #{newKey}: #{error}"
# Put the new key in
console.log "Will put: #{newKey}: #{entry.value}"
#db.put newKey, entry.value
.then ->
# Delete the old entry
console.log "Will delete: #{entry.key}"
#db.delete entry.key
console.log "Migrated all #{data.length} entries."
console.log "Migrated all #{allEntries.length} entries."
console.log "Fullfilling promise."
fulfill true
fulfill true
.catch (error) ->
console.log "Could not migrate the database: #{error}."
allEntriesStream.pipe toArray
dataStream.pipe toArray
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment