stripe-archive / mosql

MongoDB → PostgreSQL streaming replication
MIT License
1.63k stars 225 forks source link

Update tailers for mongoriver 0.4 (tokumx support) #63

Closed macobo closed 10 years ago

macobo commented 10 years ago

This commit is a companion to https://github.com/stripe/mongoriver/pull/11, which adds support for tokumx as mongodb alternative and revamps much of the tailer api.

r? @ebroder


For reference, notes on how I test this since this took a little bit of puzzling:

# Collection.yaml
test:
  test_collection:
    :columns:
    - id:
      :source: _id
      :type: TEXT
    - name:
      :source: nested.name
      :type: TEXT
    - number:
      :source: nested.int
      :type: INTEGER
    - title: TEXT
    :meta:
      :table: blog_posts
      :extra_props: true

Create an mongodb collection, insert a sample: conn["test"]["test_collection"].insert({title: "title", nested: {name: "name1", int: 5}})

Start mosql, see if it creates table, see if database contains the record:

sql = Sequel.connect("postgres://postgres@localhost")
tailer_db = sql[:mosql_tailers]
tailer_db.all

db2 = sql[:blog_posts]
db2.all

Try to update and insert more to see if new ones are added:

conn["test"]["test_collection"].insert({title: "title2", nested: {name: "name1", int: 5}})
conn["test"]["test_collection"].update({title: "title2"}, {'$set' => {'nested.int' => 99}})
# restart mosql here, look to make sure no new fields are added and changes are picked up
db2.all
conn["test"]["test_collection"].update({title: "title2"}, {'$set' => {'nested.int' => 90}})

Testing if smooth updating works:

info = tailer_db.first
Oplog_collection.remove()
conn.drop_database("test")
sql.drop_table('blog_posts')
sql.drop_table('mosql_tailers')

create old style table and put an entry in it
sql.create_table?('mosql_tailers') do
  column :service,   'TEXT'
  column :timestamp, 'INTEGER'
  primary_key [:service]
end
new_info = info.dup
# new_info[:timestamp] = 1409262430
new_info.delete(:placeholder)
tailer_db = sql[:mosql_tailers]
tailer_db.insert(new_info)
# verify
tailer_db.all()

After restarting mosql, the blog_posts table should still be empty, but the tailers table should have an extra column filled with the new info. Adding inserts should also work.

ebroder commented 10 years ago

Yeah, I think this makes sense overall

nelhage commented 10 years ago

This seems pretty sensible to me. I feel like we should probably write some automated tests for actual oplog tailing, even if we won't be able to run them on Travis because their mongo's are not configured with an oplog -- we can just skip them, and run them locally when necessary