Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add time_stamp and limit table name to 64 characters #140

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/lhm/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -21,7 +22,11 @@ def satisfies_id_column_requirement?
end

def destination_name
"lhmn_#{ @name }"
"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)
Expand Down
32 changes: 17 additions & 15 deletions spec/unit/migrator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -19,31 +21,31 @@
@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

it 'should add a composite index' do
@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

it 'should add an index with prefix length' do
@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

it 'should add an index with a custom name' do
@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

Expand All @@ -57,15 +59,15 @@
@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

it 'should add a unique index with a custom name' do
@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

Expand All @@ -79,15 +81,15 @@
@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

it 'should remove an index with a custom name' do
@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
Expand All @@ -97,23 +99,23 @@
@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

it 'should remove a column' do
@creator.remove_column('logins')

@creator.statements.must_equal([
'alter table `lhmn_alt` drop `logins`'
"alter table `lhmn_#{ @time.strftime(@stamp) }_alt` drop `logins`"
])
end

it 'should change a column' do
@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
Expand All @@ -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
Expand All @@ -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
6 changes: 4 additions & 2 deletions spec/unit/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down