Skip to content

Commit

Permalink
Change public interface
Browse files Browse the repository at this point in the history
  • Loading branch information
grobie committed Jan 18, 2012
1 parent e35e49d commit fe1e9c3
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 104 deletions.
9 changes: 2 additions & 7 deletions README.md
Expand Up @@ -42,22 +42,17 @@ 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)
end
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
Expand Down
2 changes: 1 addition & 1 deletion lhm.gemspec
Expand Up @@ -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"
Expand Down
9 changes: 5 additions & 4 deletions lib/lhm.rb
Expand Up @@ -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

1 change: 0 additions & 1 deletion lib/lhm/chunker.rb
Expand Up @@ -3,7 +3,6 @@
# Schmidt
#

require 'lhm/migration'
require 'lhm/command'

module Lhm
Expand Down
1 change: 0 additions & 1 deletion lib/lhm/invoker.rb
Expand Up @@ -12,7 +12,6 @@
require 'lhm/chunker'
require 'lhm/entangler'
require 'lhm/locked_switcher'
require 'lhm/migration'
require 'lhm/migrator'

module Lhm
Expand Down
1 change: 0 additions & 1 deletion lib/lhm/migration.rb
Expand Up @@ -3,7 +3,6 @@
# Schmidt
#

require 'lhm/table'
require 'lhm/intersection'

module Lhm
Expand Down
17 changes: 7 additions & 10 deletions lib/lhm/migrator.rb
Expand Up @@ -8,6 +8,7 @@

require 'lhm/command'
require 'lhm/migration'
require 'lhm/table'

module Lhm
class Migrator
Expand All @@ -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
#
Expand All @@ -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
#
Expand All @@ -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
#
Expand All @@ -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
#
Expand All @@ -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
#
Expand All @@ -91,9 +92,7 @@ def remove_index(cols)
statements << ddl.strip
end

#
# Command implementation
#
private

def validate
unless table?(@origin.name)
Expand All @@ -111,8 +110,6 @@ def validate
end
end

private

def execute
destination_create
sql(@statements)
Expand Down
8 changes: 8 additions & 0 deletions 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
106 changes: 27 additions & 79 deletions spec/integration/lhm_spec.rb
Expand Up @@ -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

Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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

Expand All @@ -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

Expand Down

0 comments on commit fe1e9c3

Please sign in to comment.