Skip to content

Commit

Permalink
Ensure MySQL migrator to create/drop databases with dash in name (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Feb 28, 2017
1 parent 5737794 commit 4218b5b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/hanami/model/migrator/mysql_adapter.rb
Expand Up @@ -19,7 +19,7 @@ class MySQLAdapter < Adapter
# @since 0.4.0
# @api private
def create
new_connection(global: true).run %(CREATE DATABASE #{database};)
new_connection(global: true).run %(CREATE DATABASE `#{database}`;)
rescue Sequel::DatabaseError => e
message = if e.message.match(/database exists/) # rubocop:disable Performance/RedundantMatch
DB_CREATION_ERROR
Expand All @@ -33,7 +33,7 @@ def create
# @since 0.4.0
# @api private
def drop
new_connection(global: true).run %(DROP DATABASE #{database};)
new_connection(global: true).run %(DROP DATABASE `#{database}`;)
rescue Sequel::DatabaseError => e
message = if e.message.match(/doesn\'t exist/) # rubocop:disable Performance/RedundantMatch
"Cannot find database: #{database}"
Expand Down
21 changes: 21 additions & 0 deletions test/migrator/mysql.rb
Expand Up @@ -51,6 +51,16 @@
exception.message.must_include 'then its console may be open. See this issue for more details:'
exception.message.must_include 'https://github.com/hanami/model/issues/250'
end

# See https://github.com/hanami/model/issues/381
describe 'when database name contains a dash' do
let(:database) { "db-name-create_#{random}" }

it 'creates the database' do
connection = Sequel.connect(url)
connection.tables.must_be :empty?
end
end
end

describe 'drop' do
Expand All @@ -70,6 +80,17 @@
exception = -> { migrator.drop }.must_raise Hanami::Model::MigrationError
exception.message.must_equal "Cannot find database: #{database}"
end

# See https://github.com/hanami/model/issues/381
describe 'when database name contains a dash' do
let(:database) { "db-name-drop_#{random}" }

it 'drops the database' do
migrator.drop

-> { Sequel.connect(url).tables }.must_raise Sequel::DatabaseConnectionError
end
end
end

describe 'migrate' do
Expand Down

0 comments on commit 4218b5b

Please sign in to comment.