Skip to content

Commit

Permalink
added consistent sql annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Rany Keddo committed Jan 31, 2012
1 parent 95bae33 commit 2cd918e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.0.0.rc7 (January 31, 2012)

* added SqlHelper.annotation into the middle of trigger statements. this
is for the benefit of the killer script which should not kill trigger
statements.

# 1.0.0.rc6 (January 30, 2012)

* added --confirm to kill script; fixes to kill script
Expand Down
8 changes: 5 additions & 3 deletions bin/lhm-kill-queue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby

require 'active_record'
require 'lhm/sql_helper'
require 'optparse'

module Lhm
Expand All @@ -10,6 +11,7 @@ module Lhm
@port = 3306
@grace = 10
@tiny = 0.1
@marker = "%#{ SqlHelper.annotation }%"

OptionParser.new do |opts|
opts.on("-h", "--hostname HOSTNAME") { |v| @hostname = v }
Expand All @@ -33,7 +35,7 @@ module Lhm

def usage
<<-desc.gsub(/^ /, '')
kills queries on the given server after detecting 'lock table% -- lhm'.
kills queries on the given server after detecting 'lock table#{ @marker }'.
usage:
lhm-kill-queue -h hostname -u username -p password -d database \\
(-m kill | -m master | -m slave) [--confirm]
Expand Down Expand Up @@ -97,12 +99,12 @@ module Lhm

def list_non_lhm
select_processes %Q(
info not like '% -- lhm' and time > #{ @grace } and command = 'Query'
info not like '#{ @marker }' and time > #{ @grace } and command = 'Query'
)
end

def trip
until res = select_processes("info like 'lock table% -- lhm'").first
until res = select_processes("info like 'lock table#{ @marker }'").first
sleep @tiny
print '.'
end
Expand Down
6 changes: 3 additions & 3 deletions lib/lhm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# end
#
module Lhm
extend self

# Alters a table with the changes described in the block
#
Expand All @@ -28,9 +27,9 @@ module Lhm
# @option chunk_options [Fixnum] :throttle
# Time to wait between chunks in milliseconds (defaults to: 100)
# @yield [Migrator] Yielded Migrator object records the changes
# @return [Boolean] Returns true if the migration finishs
# @return [Boolean] Returns true if the migration finishes
# @raise [Error] Raises Lhm::Error in case of a error and aborts the migration
def change_table(table_name, chunk_options = {}, &block)
def self.change_table(table_name, chunk_options = {}, &block)
connection = ActiveRecord::Base.connection
origin = Table.parse(table_name, connection)
invoker = Invoker.new(origin, connection)
Expand All @@ -40,3 +39,4 @@ def change_table(table_name, chunk_options = {}, &block)
true
end
end

6 changes: 3 additions & 3 deletions lib/lhm/entangler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def create_insert_trigger
strip %Q{
create trigger `#{ trigger(:ins) }`
after insert on `#{ @origin.name }` for each row
replace into `#{ @destination.name }` (#{ @common.joined })
replace into `#{ @destination.name }` (#{ @common.joined }) #{ SqlHelper.annotation }
values (#{ @common.typed("NEW") })
}
end
Expand All @@ -49,7 +49,7 @@ def create_update_trigger
strip %Q{
create trigger `#{ trigger(:upd) }`
after update on `#{ @origin.name }` for each row
replace into `#{ @destination.name }` (#{ @common.joined })
replace into `#{ @destination.name }` (#{ @common.joined }) #{ SqlHelper.annotation }
values (#{ @common.typed("NEW") })
}
end
Expand All @@ -58,7 +58,7 @@ def create_delete_trigger
strip %Q{
create trigger `#{ trigger(:del) }`
after delete on `#{ @origin.name }` for each row
delete ignore from `#{ @destination.name }`
delete ignore from `#{ @destination.name }` #{ SqlHelper.annotation }
where `#{ @destination.name }`.`id` = OLD.`id`
}
end
Expand Down
8 changes: 6 additions & 2 deletions lib/lhm/sql_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ module Lhm
module SqlHelper
extend self

def annotation
"/* large hadron migration */"
end

def idx_name(table_name, cols)
column_names = column_definition(cols).map(&:first)
"index_#{ table_name }_on_#{ column_names.join("_and_") }"
end

def idx_spec(cols)
column_definition(cols).map do |name, length|
"`#{name}`#{length}"
"`#{ name }`#{ length }"
end.join(', ')
end

Expand All @@ -39,7 +43,7 @@ def update(statements)
private

def tagged(statement)
statement + " -- lhm"
"#{ statement } #{ SqlHelper.annotation }"
end

def column_definition(cols)
Expand Down
2 changes: 1 addition & 1 deletion lib/lhm/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
# Schmidt

module Lhm
VERSION = "1.0.0.rc6"
VERSION = "1.0.0.rc7"
end
6 changes: 3 additions & 3 deletions spec/unit/entangler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
ddl = %Q{
create trigger `lhmt_ins_origin`
after insert on `origin` for each row
replace into `destination` (`info`, `tags`)
replace into `destination` (`info`, `tags`) /* large hadron migration */
values (NEW.`info`, NEW.`tags`)
}

Expand All @@ -41,7 +41,7 @@
ddl = %Q{
create trigger `lhmt_upd_origin`
after update on `origin` for each row
replace into `destination` (`info`, `tags`)
replace into `destination` (`info`, `tags`) /* large hadron migration */
values (NEW.`info`, NEW.`tags`)
}

Expand All @@ -52,7 +52,7 @@
ddl = %Q{
create trigger `lhmt_del_origin`
after delete on `origin` for each row
delete ignore from `destination`
delete ignore from `destination` /* large hadron migration */
where `destination`.`id` = OLD.`id`
}

Expand Down

0 comments on commit 2cd918e

Please sign in to comment.