Skip to content

Commit

Permalink
Merge pull request #4 from duncanbeevers/enum_action_column
Browse files Browse the repository at this point in the history
Use MySQL enums for hadron_action column to reduce necessary storage
  • Loading branch information
TobiTobes committed Sep 13, 2011
2 parents 79b120f + 371f41b commit 931dc86
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
17 changes: 9 additions & 8 deletions lib/large_hadron_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,20 +253,16 @@ def self.with_master
end
end

def self.clone_table(source, dest, window = 0)
execute schema_sql(source, dest, window)
def self.clone_table(source, dest, window = 0, add_action_column = false)
execute schema_sql(source, dest, window, add_action_column)
end

def self.common_columns(t1, t2)
table_column_names(t1) & table_column_names(t2)
end

def self.clone_table_for_changes(table, journal_table)
clone_table(table, journal_table)
execute %Q{
alter table %s
add column hadron_action varchar(15);
} % journal_table
clone_table(table, journal_table, 0, true)
end

def self.rename_tables(tables = {})
Expand Down Expand Up @@ -359,12 +355,17 @@ def self.replay_update_changes(table, journal_table, chunk_size = 10000, wait =
# behavior with the latter where the auto_increment of the source table
# got modified when updating the destination.
#
def self.schema_sql(source, dest, window)
def self.schema_sql(source, dest, window, add_action_column = false)
show_create(source).tap do |schema|
schema.gsub!(/auto_increment=(\d+)/i) do
"auto_increment=#{ $1.to_i + window }"
end

if add_action_column
schema.sub!(/\) ENGINE=/,
", hadron_action ENUM('update', 'insert', 'delete'), INDEX hadron_action (hadron_action) USING BTREE) ENGINE=")
end

schema.gsub!('CREATE TABLE `%s`' % source, 'CREATE TABLE `%s`' % dest)
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/large_hadron_migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
end

it "should create a table for triggered changes" do
truthiness_column "triggerme_changes", "hadron_action", "varchar"
truthiness_column "triggerme_changes", "hadron_action", "enum"
truthiness_index "triggerme_changes", "hadron_action", [ "hadron_action" ], false
end

it "should trigger on insert" do
Expand Down
15 changes: 15 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ def truthiness_rows(table_name1, table_name2, offset = 0, limit = 1000)

end

def truthiness_index(table, expected_index_name, indexed_columns, unique)
index = sql("SHOW INDEXES FROM #{table}").all_hashes.inject({}) do |a, part|
index_name = part['Key_name']
a[index_name] ||= { 'unique' => '0' == part['Non_unique'], 'columns' => [] }
column_index = part['Seq_in_index'].to_i - 1
a[index_name]['columns'][column_index] = part['Column_name']
a
end[expected_index_name]

flunk("no index named #{expected_index_name} found on #{table}") unless index

index['columns'].should == indexed_columns
index['unique'].should == unique
end

end

# Mock Rails Environment
Expand Down

0 comments on commit 931dc86

Please sign in to comment.