Skip to content

Commit bde867e

Browse files
authored
Add option to force tables to use default engine (#175)
1 parent 37f2922 commit bde867e

File tree

5 files changed

+48
-10
lines changed

5 files changed

+48
-10
lines changed

lib/lhm.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ module Lhm
5454
def change_table(table_name, options = {}, &block)
5555
with_flags(options) do
5656
origin = Table.parse(table_name, connection)
57-
invoker = Invoker.new(origin, connection)
57+
invoker = Invoker.new(origin, connection, options)
5858
block.call(invoker.migrator)
59-
invoker.run(options)
59+
invoker.run
6060
true
6161
end
6262
end

lib/lhm/invoker.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ class Invoker
2121

2222
attr_reader :migrator, :connection
2323

24-
def initialize(origin, connection)
24+
def initialize(origin, connection, options = {})
2525
@connection = connection
26-
@migrator = Migrator.new(origin, connection)
26+
@migrator = Migrator.new(origin, connection, options)
27+
@options = options
28+
normalize_options(@options)
2729
end
2830

2931
def set_session_lock_wait_timeouts
@@ -45,17 +47,16 @@ def set_session_lock_wait_timeouts
4547
end
4648
end
4749

48-
def run(options = {})
49-
normalize_options(options)
50+
def run
5051
set_session_lock_wait_timeouts
5152
migration = @migrator.run
5253
entangler = Entangler.new(migration, @connection)
5354

5455
entangler.run do
55-
options[:verifier] ||= Proc.new { |conn| triggers_still_exist?(conn, entangler) }
56-
Chunker.new(migration, @connection, options).run
56+
@options[:verifier] ||= Proc.new { |conn| triggers_still_exist?(conn, entangler) }
57+
Chunker.new(migration, @connection, @options).run
5758
raise "Required triggers do not exist" unless triggers_still_exist?(@connection, entangler)
58-
if options[:atomic_switch]
59+
if @options[:atomic_switch]
5960
AtomicSwitcher.new(migration, @connection).run
6061
else
6162
LockedSwitcher.new(migration, @connection).run

lib/lhm/migrator.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ class Migrator
1515

1616
attr_reader :name, :statements, :connection, :conditions, :renames, :origin
1717

18-
def initialize(table, connection = nil)
18+
def initialize(table, connection = nil, options = {})
1919
@connection = connection
2020
@origin = table
2121
@name = table.destination_name
2222
@statements = []
2323
@renames = {}
24+
@options = options
2425
end
2526

2627
# Alter a table with a custom statement
@@ -222,6 +223,11 @@ def destination_create
222223
original = %{CREATE TABLE `#{ @origin.name }`}
223224
replacement = %{CREATE TABLE `#{ @origin.destination_name }`}
224225
stmt = @origin.ddl.gsub(original, replacement)
226+
227+
if @options[:force_default_engine]
228+
stmt = stmt.sub(/ENGINE=\w+\s/, "")
229+
end
230+
225231
@connection.execute(tagged(stmt))
226232

227233
Lhm.logger.info("Created destination table #{@origin.destination_name}")

spec/fixtures/myisam_users.ddl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE TABLE `myisam_users` (
2+
`id` int(11) NOT NULL AUTO_INCREMENT,
3+
`reference` int(11) DEFAULT NULL,
4+
`username` varchar(255) DEFAULT NULL,
5+
`group` varchar(255) DEFAULT 'Superfriends',
6+
`created_at` datetime DEFAULT NULL,
7+
`comment` varchar(20) DEFAULT NULL,
8+
`description` text,
9+
PRIMARY KEY (`id`)
10+
) ENGINE=MyISAM CHARSET=utf8

spec/integration/lhm_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,27 @@
553553
end
554554
end
555555

556+
it 'creates the shadow table with the default engine when the `force_default_engine` option is used' do
557+
table_create(:myisam_users)
558+
559+
engine = select_value("SELECT ENGINE FROM information_schema.TABLES WHERE TABLE_NAME = 'myisam_users'")
560+
value(engine).must_equal("MyISAM")
561+
562+
Lhm.change_table(:myisam_users) do |t|
563+
t.add_column(:logins, "INT(12) DEFAULT '0'", algorithm: "COPY")
564+
end
565+
566+
engine = select_value("SELECT ENGINE FROM information_schema.TABLES WHERE TABLE_NAME = 'myisam_users'")
567+
value(engine).must_equal("MyISAM")
568+
569+
Lhm.change_table(:myisam_users, force_default_engine: true) do |t|
570+
t.remove_column(:logins)
571+
end
572+
573+
engine = select_value("SELECT ENGINE FROM information_schema.TABLES WHERE TABLE_NAME = 'myisam_users'")
574+
value(engine).must_equal("InnoDB")
575+
end
576+
556577
describe 'parallel' do
557578
it 'should perserve inserts during migration' do
558579
50.times { |n| execute("insert into users set reference = '#{ n }'") }

0 commit comments

Comments
 (0)