diff --git a/README.md b/README.md index 9383050d..e6b599a8 100644 --- a/README.md +++ b/README.md @@ -42,14 +42,9 @@ twitter solution [1], it does not require the presence of an indexed ## Usage -After extending Lhm, `hadron_change_table` becomes available -with the following methods: - class MigrateArbitrary < ActiveRecord::Migration - extend Lhm - def self.up - hadron_change_table(:users) do |t| + Lhm.change_table(:users) do |t| t.add_column(:arbitrary, "INT(12)") t.add_index([:arbitrary, :created_at]) t.ddl("alter table %s add column flag tinyint(1)" % t.name) @@ -57,7 +52,7 @@ with the following methods: end def self.down - hadron_change_table(:users) do |t| + Lhm.change_table(:users) do |t| t.remove_index([:arbitrary, :created_at]) t.remove_column(:arbitrary) end diff --git a/lhm.gemspec b/lhm.gemspec index c9f13804..5862b95a 100644 --- a/lhm.gemspec +++ b/lhm.gemspec @@ -3,7 +3,7 @@ lib = File.expand_path('../lib', __FILE__) $:.unshift(lib) unless $:.include?(lib) -require 'lhm' +require 'lhm/version' Gem::Specification.new do |s| s.name = "lhm" diff --git a/lib/lhm.rb b/lib/lhm.rb index cdd3dc54..b760037f 100644 --- a/lib/lhm.rb +++ b/lib/lhm.rb @@ -3,18 +3,19 @@ # Schmidt # +require 'active_record' require 'lhm/table' require 'lhm/invoker' -require 'lhm/migration' +require 'lhm/version' module Lhm - VERSION = "1.0.0.rc2" + extend self - def hadron_change_table(table_name, chunk_options = {}, &block) + def change_table(table_name, chunk_options = {}, &block) + connection = ActiveRecord::Base.connection origin = Table.parse(table_name, connection) invoker = Invoker.new(origin, connection) block.call(invoker.migrator) invoker.run(chunk_options) end end - diff --git a/lib/lhm/chunker.rb b/lib/lhm/chunker.rb index e7f405dd..4ff16b09 100644 --- a/lib/lhm/chunker.rb +++ b/lib/lhm/chunker.rb @@ -3,7 +3,6 @@ # Schmidt # -require 'lhm/migration' require 'lhm/command' module Lhm diff --git a/lib/lhm/invoker.rb b/lib/lhm/invoker.rb index 92523ac8..c8234c18 100644 --- a/lib/lhm/invoker.rb +++ b/lib/lhm/invoker.rb @@ -12,7 +12,6 @@ require 'lhm/chunker' require 'lhm/entangler' require 'lhm/locked_switcher' -require 'lhm/migration' require 'lhm/migrator' module Lhm diff --git a/lib/lhm/migration.rb b/lib/lhm/migration.rb index 604a2522..91c97e79 100644 --- a/lib/lhm/migration.rb +++ b/lib/lhm/migration.rb @@ -3,7 +3,6 @@ # Schmidt # -require 'lhm/table' require 'lhm/intersection' module Lhm diff --git a/lib/lhm/migrator.rb b/lib/lhm/migrator.rb index 41c717b1..8ca21c67 100644 --- a/lib/lhm/migrator.rb +++ b/lib/lhm/migrator.rb @@ -8,6 +8,7 @@ require 'lhm/command' require 'lhm/migration' +require 'lhm/table' module Lhm class Migrator @@ -29,7 +30,7 @@ def ddl(statement) # # Add a column to a table: # - # hadron_change_table("users") do |t| + # Lhm.change_table("users") do |t| # t.add_column(:logins, "INT(12) DEFAULT '0'") # end # @@ -42,7 +43,7 @@ def add_column(name, definition = "") # # Remove a column from a table: # - # hadron_change_table("users") do |t| + # Lhm.change_table("users") do |t| # t.remove_column(:comment) # end # @@ -55,7 +56,7 @@ def remove_column(name) # # Add an index to a table: # - # hadron_change_table("users") do |t| + # Lhm.change_table("users") do |t| # t.add_index([:comment, :created_at]) # end # @@ -68,7 +69,7 @@ def add_index(cols) # # Add a unique index to a table: # - # hadron_change_table("users") do |t| + # Lhm.change_table("users") do |t| # t.add_unique_index([:comment, :created_at]) # end # @@ -81,7 +82,7 @@ def add_unique_index(cols) # # Remove an index from a table # - # hadron_change_table("users") do |t| + # Lhm.change_table("users") do |t| # t.remove_index(:username, :created_at) # end # @@ -91,9 +92,7 @@ def remove_index(cols) statements << ddl.strip end - # - # Command implementation - # + private def validate unless table?(@origin.name) @@ -111,8 +110,6 @@ def validate end end - private - def execute destination_create sql(@statements) diff --git a/lib/lhm/version.rb b/lib/lhm/version.rb new file mode 100644 index 00000000..f5ccdffe --- /dev/null +++ b/lib/lhm/version.rb @@ -0,0 +1,8 @@ +# +# Copyright (c) 2011, SoundCloud Ltd., Rany Keddo, Tobias Bielohlawek, Tobias +# Schmidt +# + +module Lhm + VERSION = "1.0.0.rc2" +end diff --git a/spec/integration/lhm_spec.rb b/spec/integration/lhm_spec.rb index 091ea768..27a45366 100644 --- a/spec/integration/lhm_spec.rb +++ b/spec/integration/lhm_spec.rb @@ -6,76 +6,6 @@ require 'lhm' require File.expand_path(File.dirname(__FILE__)) + '/integration_helper' -class AddColumnTestMigration < ActiveRecord::Migration - extend Lhm - - def self.up - hadron_change_table(:users) do |t| - t.add_column(:logins, "INT(12) DEFAULT '0'") - end - end -end - -class RemoveColumnTestMigration < ActiveRecord::Migration - extend Lhm - - def self.up - hadron_change_table(:users) do |t| - t.remove_column(:comment) - end - end -end - -class AddIndexTestMigration < ActiveRecord::Migration - extend Lhm - - def self.up - hadron_change_table(:users) do |t| - t.add_index([:comment, :created_at]) - end - end -end - -class AddUniqueIndexTestMigration < ActiveRecord::Migration - extend Lhm - - def self.up - hadron_change_table(:users) do |t| - t.add_unique_index(:comment) - end - end -end - -class RemoveIndexTestMigration < ActiveRecord::Migration - extend Lhm - - def self.up - hadron_change_table(:users) do |t| - t.remove_index([:username, :created_at]) - end - end -end - -class DdlTestMigration < ActiveRecord::Migration - extend Lhm - - def self.up - hadron_change_table(:users) do |t| - t.ddl("alter table %s add column flag tinyint(1)" % t.name) - end - end -end - -class ParallelTestMigration < ActiveRecord::Migration - extend Lhm - - def self.up - hadron_change_table(:users, :stride => 10, :throttle => 97) do |t| - t.add_column(:parallel, "INT(10) DEFAULT '0'") - end - end -end - describe Lhm do include IntegrationHelper @@ -87,7 +17,9 @@ def self.up end it "should add a column" do - AddColumnTestMigration.up + Lhm.change_table(:users) do |t| + t.add_column(:logins, "INT(12) DEFAULT '0'") + end table_read(:users).columns["logins"].must_equal({ :type => "int(12)", @@ -98,37 +30,49 @@ def self.up it "should copy all rows" do 23.times { |n| execute("insert into users set reference = '#{ n }'") } - AddColumnTestMigration.up + Lhm.change_table(:users) do |t| + t.add_column(:logins, "INT(12) DEFAULT '0'") + end count_all("users").must_equal(23) end it "should remove a column" do - RemoveColumnTestMigration.up + Lhm.change_table(:users) do |t| + t.remove_column(:comment) + end table_read(:users).columns["comment"].must_equal nil end it "should add an index" do - AddIndexTestMigration.up + Lhm.change_table(:users) do |t| + t.add_index([:comment, :created_at]) + end key?(table_read(:users), [:comment, :created_at]).must_equal(true) end it "should add a unqiue index" do - AddUniqueIndexTestMigration.up + Lhm.change_table(:users) do |t| + t.add_unique_index(:comment) + end key?(table_read(:users), :comment, :unique).must_equal(true) end it "should remove an index" do - RemoveIndexTestMigration.up + Lhm.change_table(:users) do |t| + t.remove_index([:username, :created_at]) + end key?(table_read(:users), [:username, :created_at]).must_equal(false) end it "should apply a ddl statement" do - DdlTestMigration.up + Lhm.change_table(:users) do |t| + t.ddl("alter table %s add column flag tinyint(1)" % t.name) + end table_read(:users).columns["flag"].must_equal({ :type => "tinyint(1)", @@ -147,7 +91,9 @@ def self.up end end - ParallelTestMigration.up + Lhm.change_table(:users, :stride => 10, :throttle => 97) do |t| + t.add_column(:parallel, "INT(10) DEFAULT '0'") + end insert.join @@ -164,7 +110,9 @@ def self.up end end - ParallelTestMigration.up + Lhm.change_table(:users, :stride => 10, :throttle => 97) do |t| + t.add_column(:parallel, "INT(10) DEFAULT '0'") + end delete.join