db-sync 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/Guardfile +70 -0
- data/LICENSE.txt +21 -0
- data/README.md +38 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/db-sync.gemspec +31 -0
- data/lib/db/sync.rb +122 -0
- data/lib/db/sync/diff.rb +79 -0
- data/lib/db/sync/model.rb +29 -0
- data/lib/db/sync/version.rb +5 -0
- data/lib/tasks/db-sync.rake +13 -0
- metadata +185 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA1:
         | 
| 3 | 
            +
              metadata.gz: b9a3c1fd55f036e03a864d7327dba19fcf657051
         | 
| 4 | 
            +
              data.tar.gz: 3b7b1e8c906bbc3dfadccc51c64b429065918e89
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: 9e71e6b82d3d62db13b1762168d23839ba372ff58b47ec91d5b9797fd0f692bfc1f008244b5dee5329fe32a05be5baf646603b76b09642db6bfa93064c76f98b
         | 
| 7 | 
            +
              data.tar.gz: 58cd9176573229749f4ee020b571b91e77adcf1a2f2efdf0d379e9adde41a862675cbda1768f1e7b12e21c765fd99ba485dac3b210ec44a8b6a64fecfc734000
         | 
    
        data/.gitignore
    ADDED
    
    
    
        data/.rspec
    ADDED
    
    
    
        data/.travis.yml
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    
    
        data/Guardfile
    ADDED
    
    | @@ -0,0 +1,70 @@ | |
| 1 | 
            +
            # A sample Guardfile
         | 
| 2 | 
            +
            # More info at https://github.com/guard/guard#readme
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            ## Uncomment and set this to only include directories you want to watch
         | 
| 5 | 
            +
            # directories %w(app lib config test spec features) \
         | 
| 6 | 
            +
            #  .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            ## Note: if you are using the `directories` clause above and you are not
         | 
| 9 | 
            +
            ## watching the project directory ('.'), then you will want to move
         | 
| 10 | 
            +
            ## the Guardfile to a watched dir and symlink it back, e.g.
         | 
| 11 | 
            +
            #
         | 
| 12 | 
            +
            #  $ mkdir config
         | 
| 13 | 
            +
            #  $ mv Guardfile config/
         | 
| 14 | 
            +
            #  $ ln -s config/Guardfile .
         | 
| 15 | 
            +
            #
         | 
| 16 | 
            +
            # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
         | 
| 17 | 
            +
             | 
| 18 | 
            +
            # Note: The cmd option is now required due to the increasing number of ways
         | 
| 19 | 
            +
            #       rspec may be run, below are examples of the most common uses.
         | 
| 20 | 
            +
            #  * bundler: 'bundle exec rspec'
         | 
| 21 | 
            +
            #  * bundler binstubs: 'bin/rspec'
         | 
| 22 | 
            +
            #  * spring: 'bin/rspec' (This will use spring if running and you have
         | 
| 23 | 
            +
            #                          installed the spring binstubs per the docs)
         | 
| 24 | 
            +
            #  * zeus: 'zeus rspec' (requires the server to be started separately)
         | 
| 25 | 
            +
            #  * 'just' rspec: 'rspec'
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            guard :rspec, cmd: 'bundle exec rspec' do
         | 
| 28 | 
            +
              require 'guard/rspec/dsl'
         | 
| 29 | 
            +
              dsl = Guard::RSpec::Dsl.new(self)
         | 
| 30 | 
            +
             | 
| 31 | 
            +
              # Feel free to open issues for suggestions and improvements
         | 
| 32 | 
            +
             | 
| 33 | 
            +
              # RSpec files
         | 
| 34 | 
            +
              rspec = dsl.rspec
         | 
| 35 | 
            +
              watch(rspec.spec_helper) { rspec.spec_dir }
         | 
| 36 | 
            +
              watch(rspec.spec_support) { rspec.spec_dir }
         | 
| 37 | 
            +
              watch(rspec.spec_files)
         | 
| 38 | 
            +
             | 
| 39 | 
            +
              # Ruby files
         | 
| 40 | 
            +
              ruby = dsl.ruby
         | 
| 41 | 
            +
              dsl.watch_spec_files_for(ruby.lib_files)
         | 
| 42 | 
            +
             | 
| 43 | 
            +
              # Rails files
         | 
| 44 | 
            +
              rails = dsl.rails(view_extensions: %w(erb haml slim))
         | 
| 45 | 
            +
              dsl.watch_spec_files_for(rails.app_files)
         | 
| 46 | 
            +
              dsl.watch_spec_files_for(rails.views)
         | 
| 47 | 
            +
             | 
| 48 | 
            +
              watch(rails.controllers) do |m|
         | 
| 49 | 
            +
                [
         | 
| 50 | 
            +
                  rspec.spec.call("routing/#{m[1]}_routing"),
         | 
| 51 | 
            +
                  rspec.spec.call("controllers/#{m[1]}_controller"),
         | 
| 52 | 
            +
                  rspec.spec.call("acceptance/#{m[1]}")
         | 
| 53 | 
            +
                ]
         | 
| 54 | 
            +
              end
         | 
| 55 | 
            +
             | 
| 56 | 
            +
              # Rails config changes
         | 
| 57 | 
            +
              watch(rails.spec_helper)     { rspec.spec_dir }
         | 
| 58 | 
            +
              watch(rails.routes)          { "#{rspec.spec_dir}/routing" }
         | 
| 59 | 
            +
              watch(rails.app_controller)  { "#{rspec.spec_dir}/controllers" }
         | 
| 60 | 
            +
             | 
| 61 | 
            +
              # Capybara features specs
         | 
| 62 | 
            +
              watch(rails.view_dirs)     { |m| rspec.spec.call("features/#{m[1]}") }
         | 
| 63 | 
            +
              watch(rails.layouts)       { |m| rspec.spec.call("features/#{m[1]}") }
         | 
| 64 | 
            +
             | 
| 65 | 
            +
              # Turnip features and steps
         | 
| 66 | 
            +
              watch(%r{^spec/acceptance/(.+)\.feature$})
         | 
| 67 | 
            +
              watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
         | 
| 68 | 
            +
                Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance'
         | 
| 69 | 
            +
              end
         | 
| 70 | 
            +
            end
         | 
    
        data/LICENSE.txt
    ADDED
    
    | @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            The MIT License (MIT)
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Copyright (c) 2015 Vasil Joskov
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Permission is hereby granted, free of charge, to any person obtaining a copy
         | 
| 6 | 
            +
            of this software and associated documentation files (the "Software"), to deal
         | 
| 7 | 
            +
            in the Software without restriction, including without limitation the rights
         | 
| 8 | 
            +
            to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
         | 
| 9 | 
            +
            copies of the Software, and to permit persons to whom the Software is
         | 
| 10 | 
            +
            furnished to do so, subject to the following conditions:
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            The above copyright notice and this permission notice shall be included in
         | 
| 13 | 
            +
            all copies or substantial portions of the Software.
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
         | 
| 16 | 
            +
            IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
         | 
| 17 | 
            +
            FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
         | 
| 18 | 
            +
            AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
         | 
| 19 | 
            +
            LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
         | 
| 20 | 
            +
            OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
         | 
| 21 | 
            +
            THE SOFTWARE.
         | 
    
        data/README.md
    ADDED
    
    | @@ -0,0 +1,38 @@ | |
| 1 | 
            +
            # Db::Sync
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Database Synchronization Tool
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            ## Installation
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            Add this line to your application's Gemfile:
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            ```ruby
         | 
| 10 | 
            +
            gem 'db-sync'
         | 
| 11 | 
            +
            ```
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            And then execute:
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                $ bundle
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            Or install it yourself as:
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                $ gem install db-sync
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            ## Usage
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            TODO: Write usage instructions here
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            ## Development
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
         | 
| 30 | 
            +
             | 
| 31 | 
            +
            ## Contributing
         | 
| 32 | 
            +
             | 
| 33 | 
            +
            Bug reports and pull requests are welcome on GitHub at https://github.com/joskov/db-sync.
         | 
| 34 | 
            +
             | 
| 35 | 
            +
             | 
| 36 | 
            +
            ## License
         | 
| 37 | 
            +
             | 
| 38 | 
            +
            The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
         | 
    
        data/Rakefile
    ADDED
    
    
    
        data/bin/console
    ADDED
    
    | @@ -0,0 +1,14 @@ | |
| 1 | 
            +
            #!/usr/bin/env ruby
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'bundler/setup'
         | 
| 4 | 
            +
            require 'db/sync'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            # You can add fixtures and/or initialization code here to make experimenting
         | 
| 7 | 
            +
            # with your gem easier. You can also use a different console, if you like.
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            # (If you use this, don't forget to add pry to your Gemfile!)
         | 
| 10 | 
            +
            # require "pry"
         | 
| 11 | 
            +
            # Pry.start
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            require 'irb'
         | 
| 14 | 
            +
            IRB.start
         | 
    
        data/bin/setup
    ADDED
    
    
    
        data/db-sync.gemspec
    ADDED
    
    | @@ -0,0 +1,31 @@ | |
| 1 | 
            +
            # coding: utf-8
         | 
| 2 | 
            +
            lib = File.expand_path('../lib', __FILE__)
         | 
| 3 | 
            +
            $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
         | 
| 4 | 
            +
            require 'db/sync'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            Gem::Specification.new do |spec|
         | 
| 7 | 
            +
              spec.name          = 'db-sync'
         | 
| 8 | 
            +
              spec.version       = Db::Sync::VERSION
         | 
| 9 | 
            +
              spec.authors       = ['Vasil Joskov']
         | 
| 10 | 
            +
              spec.date          = '2016-04-10'
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              spec.summary       = 'Database Synchronization Tool'
         | 
| 13 | 
            +
              spec.description   = 'Tool for synchronizing static tables across environments.'
         | 
| 14 | 
            +
              spec.homepage      = 'https://github.com/joskov/db-sync'
         | 
| 15 | 
            +
              spec.license       = 'MIT'
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              spec.files         = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
         | 
| 18 | 
            +
              spec.bindir        = 'exe'
         | 
| 19 | 
            +
              spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
         | 
| 20 | 
            +
              spec.require_paths = ['lib']
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              spec.add_development_dependency 'bundler', '~> 1.10'
         | 
| 23 | 
            +
              spec.add_development_dependency 'rake', '~> 10.0'
         | 
| 24 | 
            +
              spec.add_development_dependency 'rails'
         | 
| 25 | 
            +
              spec.add_development_dependency 'rspec'
         | 
| 26 | 
            +
              spec.add_development_dependency 'rspec-rails'
         | 
| 27 | 
            +
              spec.add_development_dependency 'guard'
         | 
| 28 | 
            +
              spec.add_development_dependency 'guard-rspec'
         | 
| 29 | 
            +
              spec.add_development_dependency 'terminal-notifier-guard'
         | 
| 30 | 
            +
              spec.add_development_dependency 'sqlite3'
         | 
| 31 | 
            +
            end
         | 
    
        data/lib/db/sync.rb
    ADDED
    
    | @@ -0,0 +1,122 @@ | |
| 1 | 
            +
            require 'db/sync/version'
         | 
| 2 | 
            +
            require 'db/sync/diff'
         | 
| 3 | 
            +
            require 'rails'
         | 
| 4 | 
            +
            require 'active_record'
         | 
| 5 | 
            +
            require 'db/sync/model'
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            module Db
         | 
| 8 | 
            +
              # Databse Sync
         | 
| 9 | 
            +
              class Sync
         | 
| 10 | 
            +
                include ActiveSupport::Configurable
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                def self.sync_up
         | 
| 13 | 
            +
                  # TODO: change to row by row loading
         | 
| 14 | 
            +
                  working_tables.each do |table|
         | 
| 15 | 
            +
                    table_model = data_model(table)
         | 
| 16 | 
            +
                    fail 'Tables without id are not supported!' unless table_model.include_id?
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                    data = File.read(table_filename(table))
         | 
| 19 | 
            +
                    all_records = YAML.load(data)
         | 
| 20 | 
            +
                    current_records = table_model.records.map(&:attributes)
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                    diff = Db::Sync::Diff.new(current_records, all_records, table_model.pkey)
         | 
| 23 | 
            +
                    insert_records(table, diff.inserts)
         | 
| 24 | 
            +
                    delete_records(table, diff.deletes)
         | 
| 25 | 
            +
                    update_records(table, diff.updates)
         | 
| 26 | 
            +
                  end
         | 
| 27 | 
            +
                end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                def self.insert_records(table, inserts)
         | 
| 30 | 
            +
                  inserts.each do |record|
         | 
| 31 | 
            +
                    insert_manager = Arel::InsertManager.new(ActiveRecord::Base)
         | 
| 32 | 
            +
                    arel_model = Arel::Table.new(table)
         | 
| 33 | 
            +
                    insert_data = record.map do |key, value|
         | 
| 34 | 
            +
                      [arel_model[key], value]
         | 
| 35 | 
            +
                    end
         | 
| 36 | 
            +
                    insert_manager.insert(insert_data)
         | 
| 37 | 
            +
                    # print "#{insert_manager.to_sql}\n"
         | 
| 38 | 
            +
                    ActiveRecord::Base.connection.execute(insert_manager.to_sql)
         | 
| 39 | 
            +
                  end
         | 
| 40 | 
            +
                end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                def self.delete_records(table, deletes)
         | 
| 43 | 
            +
                  arel_model = Arel::Table.new(table)
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                  deletes.each do |delete_params|
         | 
| 46 | 
            +
                    delete_manager = Arel::DeleteManager.new(ActiveRecord::Base)
         | 
| 47 | 
            +
                    delete_manager.from(arel_model)
         | 
| 48 | 
            +
                    delete_data = delete_params.map do |key, value|
         | 
| 49 | 
            +
                      [arel_model[key].eq(value)]
         | 
| 50 | 
            +
                    end
         | 
| 51 | 
            +
                    delete_manager.where(delete_data)
         | 
| 52 | 
            +
                    # print "#{delete_manager.to_sql}\n"
         | 
| 53 | 
            +
                    ActiveRecord::Base.connection.execute(delete_manager.to_sql)
         | 
| 54 | 
            +
                  end
         | 
| 55 | 
            +
                end
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                def self.update_records(table, updates)
         | 
| 58 | 
            +
                  arel_model = Arel::Table.new(table)
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                  updates.each do |update|
         | 
| 61 | 
            +
                    update_manager = Arel::UpdateManager.new(ActiveRecord::Base)
         | 
| 62 | 
            +
                    update_key = update[:key].map do |key, value|
         | 
| 63 | 
            +
                      [arel_model[key].eq(value)]
         | 
| 64 | 
            +
                    end
         | 
| 65 | 
            +
                    update_changes = update[:changes].map do |key, value|
         | 
| 66 | 
            +
                      [arel_model[key], value]
         | 
| 67 | 
            +
                    end
         | 
| 68 | 
            +
                    update_manager.table(arel_model).where(update_key).set(update_changes)
         | 
| 69 | 
            +
                    # print "#{update_manager.to_sql}\n"
         | 
| 70 | 
            +
                    ActiveRecord::Base.connection.execute(update_manager.to_sql)
         | 
| 71 | 
            +
                  end
         | 
| 72 | 
            +
                end
         | 
| 73 | 
            +
             | 
| 74 | 
            +
                def self.sync_down
         | 
| 75 | 
            +
                  # TODO: change to row by row saving
         | 
| 76 | 
            +
                  working_tables.each do |table|
         | 
| 77 | 
            +
                    File.open(table_filename(table), 'w') do |f|
         | 
| 78 | 
            +
                      current_records = table_model_records(table)
         | 
| 79 | 
            +
                      f << current_records.to_yaml
         | 
| 80 | 
            +
                    end
         | 
| 81 | 
            +
                  end
         | 
| 82 | 
            +
                end
         | 
| 83 | 
            +
             | 
| 84 | 
            +
                def self.table_model_records(table)
         | 
| 85 | 
            +
                  # TODO: Some kind of paging
         | 
| 86 | 
            +
                  table_model = data_model(table)
         | 
| 87 | 
            +
                  table_model.records.map(&:attributes)
         | 
| 88 | 
            +
                end
         | 
| 89 | 
            +
             | 
| 90 | 
            +
                def self.working_tables
         | 
| 91 | 
            +
                  config.tables || all_tables
         | 
| 92 | 
            +
                end
         | 
| 93 | 
            +
             | 
| 94 | 
            +
                def self.all_tables
         | 
| 95 | 
            +
                  ActiveRecord::Base.connection.tables.reject do |table|
         | 
| 96 | 
            +
                    %w(schema_info, schema_migrations).include?(table)
         | 
| 97 | 
            +
                  end
         | 
| 98 | 
            +
                end
         | 
| 99 | 
            +
             | 
| 100 | 
            +
                def self.table_filename(table)
         | 
| 101 | 
            +
                  # TODO: change data with custom dir
         | 
| 102 | 
            +
                  File.join(Rails.root || '.', 'db', 'data', "#{table}.yml")
         | 
| 103 | 
            +
                end
         | 
| 104 | 
            +
             | 
| 105 | 
            +
                def self.configure
         | 
| 106 | 
            +
                  yield(config)
         | 
| 107 | 
            +
                end
         | 
| 108 | 
            +
             | 
| 109 | 
            +
                def self.data_model(table)
         | 
| 110 | 
            +
                  result = Class.new(Db::Sync::Model)
         | 
| 111 | 
            +
                  result.table_name = table
         | 
| 112 | 
            +
                  result
         | 
| 113 | 
            +
                end
         | 
| 114 | 
            +
             | 
| 115 | 
            +
                # Railtie needed, so that rake tasks are appended, when used as a gem
         | 
| 116 | 
            +
                class Railtie < Rails::Railtie
         | 
| 117 | 
            +
                  rake_tasks do
         | 
| 118 | 
            +
                    load File.expand_path('../../tasks/db-sync.rake', __FILE__)
         | 
| 119 | 
            +
                  end
         | 
| 120 | 
            +
                end
         | 
| 121 | 
            +
              end
         | 
| 122 | 
            +
            end
         | 
    
        data/lib/db/sync/diff.rb
    ADDED
    
    | @@ -0,0 +1,79 @@ | |
| 1 | 
            +
            class Db::Sync::Diff
         | 
| 2 | 
            +
              attr_accessor :original, :replace_with, :pkey
         | 
| 3 | 
            +
              attr_reader :inserts, :deletes, :updates
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              def initialize(original, replace_with, pkey)
         | 
| 6 | 
            +
                self.original = original.collect
         | 
| 7 | 
            +
                self.replace_with = replace_with.collect
         | 
| 8 | 
            +
                self.pkey = pkey
         | 
| 9 | 
            +
                check_items
         | 
| 10 | 
            +
              end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              def next_original_item
         | 
| 13 | 
            +
                original.next
         | 
| 14 | 
            +
              rescue StopIteration
         | 
| 15 | 
            +
                nil
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              def next_replace_item
         | 
| 19 | 
            +
                replace_with.next
         | 
| 20 | 
            +
              rescue StopIteration
         | 
| 21 | 
            +
                nil
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
              def check_items
         | 
| 25 | 
            +
                @inserts = []
         | 
| 26 | 
            +
                @deletes = []
         | 
| 27 | 
            +
                @updates = []
         | 
| 28 | 
            +
                original.rewind
         | 
| 29 | 
            +
                replace_with.rewind
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                original_item = next_original_item
         | 
| 32 | 
            +
                replace_item = next_replace_item
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                loop do
         | 
| 35 | 
            +
                  cmp = compare(original_item, replace_item)
         | 
| 36 | 
            +
                  if cmp == 0
         | 
| 37 | 
            +
                    check_for_update(original_item, replace_item)
         | 
| 38 | 
            +
                    original_item = next_original_item
         | 
| 39 | 
            +
                    replace_item = next_replace_item
         | 
| 40 | 
            +
                  elsif cmp > 0
         | 
| 41 | 
            +
                    @inserts << replace_item
         | 
| 42 | 
            +
                    replace_item = next_replace_item
         | 
| 43 | 
            +
                  else
         | 
| 44 | 
            +
                    @deletes << original_item.slice(*pkey)
         | 
| 45 | 
            +
                    original_item = next_original_item
         | 
| 46 | 
            +
                  end
         | 
| 47 | 
            +
                  break if original_item.nil? && replace_item.nil?
         | 
| 48 | 
            +
                end
         | 
| 49 | 
            +
                nil
         | 
| 50 | 
            +
              end
         | 
| 51 | 
            +
             | 
| 52 | 
            +
              def check_for_update(original_item, replace_item)
         | 
| 53 | 
            +
                return if original_item.nil? || replace_item.nil?
         | 
| 54 | 
            +
                changes = {}
         | 
| 55 | 
            +
                replace_item.each do |key, value|
         | 
| 56 | 
            +
                  next if value == original_item[key]
         | 
| 57 | 
            +
                  changes[key] = value
         | 
| 58 | 
            +
                end
         | 
| 59 | 
            +
                return if changes.blank?
         | 
| 60 | 
            +
                @updates << { key: original_item.slice(*pkey), changes: changes }
         | 
| 61 | 
            +
              end
         | 
| 62 | 
            +
             | 
| 63 | 
            +
              def compare(item1, item2)
         | 
| 64 | 
            +
                return 0 if item1.nil? && item2.nil?
         | 
| 65 | 
            +
                return -1 if item2.nil?
         | 
| 66 | 
            +
                return 1 if item1.nil?
         | 
| 67 | 
            +
                self.class.compare(item1.slice(*pkey), item2.slice(*pkey))
         | 
| 68 | 
            +
              end
         | 
| 69 | 
            +
             | 
| 70 | 
            +
              def self.compare(item1, item2)
         | 
| 71 | 
            +
                result = item1.values <=> item2.values
         | 
| 72 | 
            +
                compare_count
         | 
| 73 | 
            +
                result
         | 
| 74 | 
            +
              end
         | 
| 75 | 
            +
             | 
| 76 | 
            +
              # TODO: remove, checks for number of comparisons
         | 
| 77 | 
            +
              def self.compare_count
         | 
| 78 | 
            +
              end
         | 
| 79 | 
            +
            end
         | 
| @@ -0,0 +1,29 @@ | |
| 1 | 
            +
            class Db::Sync::Model < ActiveRecord::Base
         | 
| 2 | 
            +
              @abstract_class = true
         | 
| 3 | 
            +
             | 
| 4 | 
            +
              def unique_data
         | 
| 5 | 
            +
                attributes.slice(*self.class.pkey)
         | 
| 6 | 
            +
              end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              def compare_unique_data(other)
         | 
| 9 | 
            +
                self.class.pkey.each do |key|
         | 
| 10 | 
            +
                  attributes[key] <=> other[key]
         | 
| 11 | 
            +
                end
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              def self.include_id?
         | 
| 15 | 
            +
                attribute_names.include?('id')
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              def self.pkey
         | 
| 19 | 
            +
                if include_id?
         | 
| 20 | 
            +
                  ['id']
         | 
| 21 | 
            +
                else
         | 
| 22 | 
            +
                  attribute_names.sort
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
              end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
              def self.records
         | 
| 27 | 
            +
                order(pkey)
         | 
| 28 | 
            +
              end
         | 
| 29 | 
            +
            end
         | 
| @@ -0,0 +1,13 @@ | |
| 1 | 
            +
            namespace :db do
         | 
| 2 | 
            +
              namespace :sync do
         | 
| 3 | 
            +
                desc 'Download data from the databse into files.'
         | 
| 4 | 
            +
                task down: :environment do
         | 
| 5 | 
            +
                  Db::Sync.sync_down
         | 
| 6 | 
            +
                end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                desc 'Upload data from the files into the database.'
         | 
| 9 | 
            +
                task up: :environment do
         | 
| 10 | 
            +
                  Db::Sync.sync_up
         | 
| 11 | 
            +
                end
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,185 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: db-sync
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.0.4
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Vasil Joskov
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: exe
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2016-04-10 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies:
         | 
| 13 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 14 | 
            +
              name: bundler
         | 
| 15 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 16 | 
            +
                requirements:
         | 
| 17 | 
            +
                - - "~>"
         | 
| 18 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 19 | 
            +
                    version: '1.10'
         | 
| 20 | 
            +
              type: :development
         | 
| 21 | 
            +
              prerelease: false
         | 
| 22 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 23 | 
            +
                requirements:
         | 
| 24 | 
            +
                - - "~>"
         | 
| 25 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 26 | 
            +
                    version: '1.10'
         | 
| 27 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 28 | 
            +
              name: rake
         | 
| 29 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 30 | 
            +
                requirements:
         | 
| 31 | 
            +
                - - "~>"
         | 
| 32 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 33 | 
            +
                    version: '10.0'
         | 
| 34 | 
            +
              type: :development
         | 
| 35 | 
            +
              prerelease: false
         | 
| 36 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 37 | 
            +
                requirements:
         | 
| 38 | 
            +
                - - "~>"
         | 
| 39 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 40 | 
            +
                    version: '10.0'
         | 
| 41 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 42 | 
            +
              name: rails
         | 
| 43 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 44 | 
            +
                requirements:
         | 
| 45 | 
            +
                - - ">="
         | 
| 46 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 47 | 
            +
                    version: '0'
         | 
| 48 | 
            +
              type: :development
         | 
| 49 | 
            +
              prerelease: false
         | 
| 50 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 51 | 
            +
                requirements:
         | 
| 52 | 
            +
                - - ">="
         | 
| 53 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 54 | 
            +
                    version: '0'
         | 
| 55 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 56 | 
            +
              name: rspec
         | 
| 57 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 58 | 
            +
                requirements:
         | 
| 59 | 
            +
                - - ">="
         | 
| 60 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 61 | 
            +
                    version: '0'
         | 
| 62 | 
            +
              type: :development
         | 
| 63 | 
            +
              prerelease: false
         | 
| 64 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 65 | 
            +
                requirements:
         | 
| 66 | 
            +
                - - ">="
         | 
| 67 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 68 | 
            +
                    version: '0'
         | 
| 69 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 70 | 
            +
              name: rspec-rails
         | 
| 71 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 72 | 
            +
                requirements:
         | 
| 73 | 
            +
                - - ">="
         | 
| 74 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 75 | 
            +
                    version: '0'
         | 
| 76 | 
            +
              type: :development
         | 
| 77 | 
            +
              prerelease: false
         | 
| 78 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 79 | 
            +
                requirements:
         | 
| 80 | 
            +
                - - ">="
         | 
| 81 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 82 | 
            +
                    version: '0'
         | 
| 83 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 84 | 
            +
              name: guard
         | 
| 85 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 86 | 
            +
                requirements:
         | 
| 87 | 
            +
                - - ">="
         | 
| 88 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 89 | 
            +
                    version: '0'
         | 
| 90 | 
            +
              type: :development
         | 
| 91 | 
            +
              prerelease: false
         | 
| 92 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 93 | 
            +
                requirements:
         | 
| 94 | 
            +
                - - ">="
         | 
| 95 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 96 | 
            +
                    version: '0'
         | 
| 97 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 98 | 
            +
              name: guard-rspec
         | 
| 99 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 100 | 
            +
                requirements:
         | 
| 101 | 
            +
                - - ">="
         | 
| 102 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 103 | 
            +
                    version: '0'
         | 
| 104 | 
            +
              type: :development
         | 
| 105 | 
            +
              prerelease: false
         | 
| 106 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 107 | 
            +
                requirements:
         | 
| 108 | 
            +
                - - ">="
         | 
| 109 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 110 | 
            +
                    version: '0'
         | 
| 111 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 112 | 
            +
              name: terminal-notifier-guard
         | 
| 113 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 114 | 
            +
                requirements:
         | 
| 115 | 
            +
                - - ">="
         | 
| 116 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 117 | 
            +
                    version: '0'
         | 
| 118 | 
            +
              type: :development
         | 
| 119 | 
            +
              prerelease: false
         | 
| 120 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 121 | 
            +
                requirements:
         | 
| 122 | 
            +
                - - ">="
         | 
| 123 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 124 | 
            +
                    version: '0'
         | 
| 125 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 126 | 
            +
              name: sqlite3
         | 
| 127 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 128 | 
            +
                requirements:
         | 
| 129 | 
            +
                - - ">="
         | 
| 130 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 131 | 
            +
                    version: '0'
         | 
| 132 | 
            +
              type: :development
         | 
| 133 | 
            +
              prerelease: false
         | 
| 134 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 135 | 
            +
                requirements:
         | 
| 136 | 
            +
                - - ">="
         | 
| 137 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 138 | 
            +
                    version: '0'
         | 
| 139 | 
            +
            description: Tool for synchronizing static tables across environments.
         | 
| 140 | 
            +
            email: 
         | 
| 141 | 
            +
            executables: []
         | 
| 142 | 
            +
            extensions: []
         | 
| 143 | 
            +
            extra_rdoc_files: []
         | 
| 144 | 
            +
            files:
         | 
| 145 | 
            +
            - ".gitignore"
         | 
| 146 | 
            +
            - ".rspec"
         | 
| 147 | 
            +
            - ".travis.yml"
         | 
| 148 | 
            +
            - Gemfile
         | 
| 149 | 
            +
            - Guardfile
         | 
| 150 | 
            +
            - LICENSE.txt
         | 
| 151 | 
            +
            - README.md
         | 
| 152 | 
            +
            - Rakefile
         | 
| 153 | 
            +
            - bin/console
         | 
| 154 | 
            +
            - bin/setup
         | 
| 155 | 
            +
            - db-sync.gemspec
         | 
| 156 | 
            +
            - lib/db/sync.rb
         | 
| 157 | 
            +
            - lib/db/sync/diff.rb
         | 
| 158 | 
            +
            - lib/db/sync/model.rb
         | 
| 159 | 
            +
            - lib/db/sync/version.rb
         | 
| 160 | 
            +
            - lib/tasks/db-sync.rake
         | 
| 161 | 
            +
            homepage: https://github.com/joskov/db-sync
         | 
| 162 | 
            +
            licenses:
         | 
| 163 | 
            +
            - MIT
         | 
| 164 | 
            +
            metadata: {}
         | 
| 165 | 
            +
            post_install_message: 
         | 
| 166 | 
            +
            rdoc_options: []
         | 
| 167 | 
            +
            require_paths:
         | 
| 168 | 
            +
            - lib
         | 
| 169 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 170 | 
            +
              requirements:
         | 
| 171 | 
            +
              - - ">="
         | 
| 172 | 
            +
                - !ruby/object:Gem::Version
         | 
| 173 | 
            +
                  version: '0'
         | 
| 174 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 175 | 
            +
              requirements:
         | 
| 176 | 
            +
              - - ">="
         | 
| 177 | 
            +
                - !ruby/object:Gem::Version
         | 
| 178 | 
            +
                  version: '0'
         | 
| 179 | 
            +
            requirements: []
         | 
| 180 | 
            +
            rubyforge_project: 
         | 
| 181 | 
            +
            rubygems_version: 2.4.5.1
         | 
| 182 | 
            +
            signing_key: 
         | 
| 183 | 
            +
            specification_version: 4
         | 
| 184 | 
            +
            summary: Database Synchronization Tool
         | 
| 185 | 
            +
            test_files: []
         |