From fd9f31b32797f0872574e59c4a81fb79d516526a Mon Sep 17 00:00:00 2001 From: Arun M Date: Fri, 3 Jun 2016 16:12:08 +0530 Subject: [PATCH 1/3] Add time_stamp and limit table name to 64 characters --- lib/lhm/table.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/lhm/table.rb b/lib/lhm/table.rb index 7ff30fd2..1ab121a0 100644 --- a/lib/lhm/table.rb +++ b/lib/lhm/table.rb @@ -21,7 +21,8 @@ def satisfies_id_column_requirement? end def destination_name - "lhmn_#{ @name }" + time_stamp = Time.now.strftime "%Y_%m_%d_%H_%M_%S_#{ '%03d' % (@start.usec / 1000) }" + "lhmn_#{ time_stamp }_#{ @name }"[0...64] end def self.parse(table_name, connection) From 9ec028ab77d4c8b3d20a5e1b443d118a857752b8 Mon Sep 17 00:00:00 2001 From: Arun M Date: Fri, 3 Jun 2016 16:39:43 +0530 Subject: [PATCH 2/3] Fixed Travis CI build failure --- lib/lhm/table.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/lhm/table.rb b/lib/lhm/table.rb index 1ab121a0..4eb19630 100644 --- a/lib/lhm/table.rb +++ b/lib/lhm/table.rb @@ -21,7 +21,8 @@ def satisfies_id_column_requirement? end def destination_name - time_stamp = Time.now.strftime "%Y_%m_%d_%H_%M_%S_#{ '%03d' % (@start.usec / 1000) }" + time = Time.now + time_stamp = time.strftime "%Y_%m_%d_%H_%M_%S_#{ '%03d' % (time.usec / 1000) }" "lhmn_#{ time_stamp }_#{ @name }"[0...64] end From 8c7d82acf604b10b76b1c20382cb4189a622d3c7 Mon Sep 17 00:00:00 2001 From: Arun M Date: Fri, 3 Jun 2016 17:55:23 +0530 Subject: [PATCH 3/3] Fixed testcases to expect table name with timestamp --- lib/lhm/table.rb | 9 ++++++--- spec/unit/migrator_spec.rb | 32 +++++++++++++++++--------------- spec/unit/table_spec.rb | 6 ++++-- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/lib/lhm/table.rb b/lib/lhm/table.rb index 4eb19630..da7262ab 100644 --- a/lib/lhm/table.rb +++ b/lib/lhm/table.rb @@ -7,12 +7,13 @@ module Lhm class Table attr_reader :name, :columns, :indices, :pk, :ddl - def initialize(name, pk = 'id', ddl = nil) + def initialize(name, pk = 'id', ddl = nil, time = Time.now) @name = name @columns = {} @indices = {} @pk = pk @ddl = ddl + @time = time end def satisfies_id_column_requirement? @@ -21,11 +22,13 @@ def satisfies_id_column_requirement? end def destination_name - time = Time.now - time_stamp = time.strftime "%Y_%m_%d_%H_%M_%S_#{ '%03d' % (time.usec / 1000) }" "lhmn_#{ time_stamp }_#{ @name }"[0...64] end + def time_stamp + @time.strftime "%Y_%m_%d_%H_%M_%S_#{ '%03d' % (@time.usec / 1000) }" + end + def self.parse(table_name, connection) Parser.new(table_name, connection).parse end diff --git a/spec/unit/migrator_spec.rb b/spec/unit/migrator_spec.rb index e38e47cc..dc6026d6 100644 --- a/spec/unit/migrator_spec.rb +++ b/spec/unit/migrator_spec.rb @@ -10,7 +10,9 @@ include UnitHelper before(:each) do - @table = Lhm::Table.new('alt') + @time = Time.now + @stamp = "%Y_%m_%d_%H_%M_%S_#{ '%03d' % (@time.usec / 1000) }" + @table = Lhm::Table.new('alt', 'id', nil, @time) @creator = Lhm::Migrator.new(@table) end @@ -19,7 +21,7 @@ @creator.add_index(:a) @creator.statements.must_equal([ - 'create index `index_alt_on_a` on `lhmn_alt` (`a`)' + "create index `index_alt_on_a` on `lhmn_#{ @time.strftime(@stamp) }_alt` (`a`)" ]) end @@ -27,7 +29,7 @@ @creator.add_index([:a, :b]) @creator.statements.must_equal([ - 'create index `index_alt_on_a_and_b` on `lhmn_alt` (`a`, `b`)' + "create index `index_alt_on_a_and_b` on `lhmn_#{ @time.strftime(@stamp) }_alt` (`a`, `b`)" ]) end @@ -35,7 +37,7 @@ @creator.add_index(['a(10)', 'b']) @creator.statements.must_equal([ - 'create index `index_alt_on_a_and_b` on `lhmn_alt` (`a`(10), `b`)' + "create index `index_alt_on_a_and_b` on `lhmn_#{ @time.strftime(@stamp) }_alt` (`a`(10), `b`)" ]) end @@ -43,7 +45,7 @@ @creator.add_index([:a, :b], :custom_index_name) @creator.statements.must_equal([ - 'create index `custom_index_name` on `lhmn_alt` (`a`, `b`)' + "create index `custom_index_name` on `lhmn_#{ @time.strftime(@stamp) }_alt` (`a`, `b`)" ]) end @@ -57,7 +59,7 @@ @creator.add_unique_index(['a(5)', :b]) @creator.statements.must_equal([ - 'create unique index `index_alt_on_a_and_b` on `lhmn_alt` (`a`(5), `b`)' + "create unique index `index_alt_on_a_and_b` on `lhmn_#{ @time.strftime(@stamp) }_alt` (`a`(5), `b`)" ]) end @@ -65,7 +67,7 @@ @creator.add_unique_index([:a, :b], :custom_index_name) @creator.statements.must_equal([ - 'create unique index `custom_index_name` on `lhmn_alt` (`a`, `b`)' + "create unique index `custom_index_name` on `lhmn_#{ @time.strftime(@stamp) }_alt` (`a`, `b`)" ]) end @@ -79,7 +81,7 @@ @creator.remove_index(['b', 'a']) @creator.statements.must_equal([ - 'drop index `index_alt_on_b_and_a` on `lhmn_alt`' + "drop index `index_alt_on_b_and_a` on `lhmn_#{ @time.strftime(@stamp) }_alt`" ]) end @@ -87,7 +89,7 @@ @creator.remove_index([:a, :b], :custom_index_name) @creator.statements.must_equal([ - 'drop index `custom_index_name` on `lhmn_alt`' + "drop index `custom_index_name` on `lhmn_#{ @time.strftime(@stamp) }_alt`" ]) end end @@ -97,7 +99,7 @@ @creator.add_column('logins', 'INT(12)') @creator.statements.must_equal([ - 'alter table `lhmn_alt` add column `logins` INT(12)' + "alter table `lhmn_#{ @time.strftime(@stamp) }_alt` add column `logins` INT(12)" ]) end @@ -105,7 +107,7 @@ @creator.remove_column('logins') @creator.statements.must_equal([ - 'alter table `lhmn_alt` drop `logins`' + "alter table `lhmn_#{ @time.strftime(@stamp) }_alt` drop `logins`" ]) end @@ -113,7 +115,7 @@ @creator.change_column('logins', 'INT(11)') @creator.statements.must_equal([ - 'alter table `lhmn_alt` modify column `logins` INT(11)' + "alter table `lhmn_#{ @time.strftime(@stamp) }_alt` modify column `logins` INT(11)" ]) end end @@ -123,7 +125,7 @@ @creator.ddl('alter table `%s` add column `f` tinyint(1)' % @creator.name) @creator.statements.must_equal([ - 'alter table `lhmn_alt` add column `f` tinyint(1)' + "alter table `lhmn_#{ @time.strftime(@stamp) }_alt` add column `f` tinyint(1)" ]) end end @@ -136,11 +138,11 @@ @creator. statements[0]. - must_equal('alter table `lhmn_alt` add column `first` VARCHAR(64)') + must_equal("alter table `lhmn_#{ @time.strftime(@stamp) }_alt` add column `first` VARCHAR(64)") @creator. statements[1]. - must_equal('alter table `lhmn_alt` add column `last` VARCHAR(64)') + must_equal("alter table `lhmn_#{ @time.strftime(@stamp) }_alt` add column `last` VARCHAR(64)") end end end diff --git a/spec/unit/table_spec.rb b/spec/unit/table_spec.rb index c75181f4..db22ae95 100644 --- a/spec/unit/table_spec.rb +++ b/spec/unit/table_spec.rb @@ -10,8 +10,10 @@ describe 'names' do it 'should name destination' do - @table = Lhm::Table.new('users') - @table.destination_name.must_equal 'lhmn_users' + @time = Time.now + @table = Lhm::Table.new('users', 'id', nil, @time) + stamp = "%Y_%m_%d_%H_%M_%S_#{ '%03d' % (@time.usec / 1000) }" + @table.destination_name.must_equal "lhmn_#{ @time.strftime(stamp) }_users" end end