Skip to content

Commit

Permalink
Allow caller to set the algorithm that will be used for DDL operations
Browse files Browse the repository at this point in the history
  • Loading branch information
chitty committed Sep 4, 2024
1 parent b518f98 commit 23099c1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lib/lhm/migrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ def initialize(table, connection = nil)
# end
#
# @param [String] statement SQL alter statement
# @param [String] algorithm Algorithm that will be used in the DDL operation
# @note
#
# Don't write the table name directly into the statement. Use the #name
# getter instead, because the alter statement will be executed against a
# temporary table.
#
def ddl(statement)
statements << statement
def ddl(statement, algorithm: nil)
full_statement = algorithm ? "#{statement}, ALGORITHM=#{algorithm}" : statement
statements << full_statement
end

# Add a column to a table
Expand All @@ -52,8 +54,9 @@ def ddl(statement)
#
# @param [String] name Name of the column to add
# @param [String] definition Valid SQL column definition
def add_column(name, definition)
ddl('alter table `%s` add column `%s` %s, ALGORITHM=INPLACE' % [@name, name, definition])
# @param [String] algorithm Algorithm that will be used in the DDL operation
def add_column(name, definition, algorithm: 'INPLACE')
ddl('alter table `%s` add column `%s` %s' % [@name, name, definition], algorithm:)
end

# Change an existing column to a new definition
Expand Down Expand Up @@ -84,7 +87,8 @@ def change_column(name, definition)
#
# @param [String] old Name of the column to change
# @param [String] nu New name to use for the column
def rename_column(old, nu)
# @param [String] algorithm Algorithm that will be used in the DDL operation
def rename_column(old, nu, algorithm: 'INPLACE')
col = @origin.columns[old.to_s]

definition = col[:type]
Expand All @@ -94,7 +98,7 @@ def rename_column(old, nu)
definition += " COMMENT #{@connection.quote(col[:comment])}" if col[:comment]
definition += " COLLATE #{@connection.quote(col[:collate])}" if col[:collate]

ddl('alter table `%s` change column `%s` `%s` %s, ALGORITHM=INPLACE' % [@name, old, nu, definition])
ddl('alter table `%s` change column `%s` `%s` %s' % [@name, old, nu, definition], algorithm:)
@renames[old.to_s] = nu.to_s
end

Expand All @@ -107,8 +111,9 @@ def rename_column(old, nu)
# end
#
# @param [String] name Name of the column to delete
def remove_column(name)
ddl('alter table `%s` drop `%s`, ALGORITHM=INPLACE' % [@name, name])
# @param [String] algorithm Algorithm that will be used in the DDL operation
def remove_column(name, algorithm: 'INPLACE')
ddl('alter table `%s` drop `%s`' % [@name, name], algorithm:)
end

# Add an index to a table
Expand Down
30 changes: 30 additions & 0 deletions spec/unit/migrator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@
])
end

it 'should add a column using the passed algorithm' do
@creator.add_column('logins', 'INT(12)', algorithm: 'COPY')

value(@creator.statements).must_equal([
'alter table `lhmn_alt` add column `logins` INT(12), ALGORITHM=COPY'
])
end

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

Expand All @@ -109,6 +117,14 @@
])
end

it 'should remove a column using the passed algorithm' do
@creator.remove_column('logins', algorithm: 'COPY')

value(@creator.statements).must_equal([
'alter table `lhmn_alt` drop `logins`, ALGORITHM=COPY'
])
end

it 'should change a column' do
@creator.change_column('logins', 'INT(11)')

Expand Down Expand Up @@ -157,4 +173,18 @@
.must_equal('alter table `lhmn_alt` add column `last` VARCHAR(64), ALGORITHM=INPLACE')
end
end

describe 'multiple changes using the passed algorithm' do
it 'should add two columns' do
@creator.add_column('first', 'VARCHAR(64)', algorithm: 'COPY')
@creator.add_column('last', 'VARCHAR(64)', algorithm: 'COPY')
value(@creator.statements.length).must_equal(2)

value(@creator.statements[0])
.must_equal('alter table `lhmn_alt` add column `first` VARCHAR(64), ALGORITHM=COPY')

value(@creator.statements[1])
.must_equal('alter table `lhmn_alt` add column `last` VARCHAR(64), ALGORITHM=COPY')
end
end
end

0 comments on commit 23099c1

Please sign in to comment.