Skip to content

Commit

Permalink
Changed merb active record rake tasks to create utf8 dabases when usi…
Browse files Browse the repository at this point in the history
…ng rake db:create / db:reset

Added rake db:charset  rake db:collation
Support for charset/collation arguments: rake db:create CHARSET=latin1, COLLATION=latin1_bin
  • Loading branch information
mattetti committed Mar 8, 2008
1 parent 6e64672 commit e9e9c51
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
58 changes: 31 additions & 27 deletions merb_activerecord/lib/active_record/merbtasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ def create_local_database(config)
rescue
case config[:adapter]
when 'mysql'
#~ @charset = ENV['CHARSET'] || 'utf8'
#~ @collation = ENV['COLLATION'] || 'utf8_general_ci'
@charset = ENV['CHARSET'] || 'utf8'
@collation = ENV['COLLATION'] || 'utf8_general_ci'
begin
ActiveRecord::Base.establish_connection(config.merge({:database => nil}))
ActiveRecord::Base.connection.create_database(config[:database]) #, {:charset => @charset, :collation => @collation})
ActiveRecord::Base.connection.create_database(config[:database], {:charset => (config[:database][:charset] || @charset), :collation => (config[:database][:collation] || @collation)})
ActiveRecord::Base.establish_connection(config)
puts "MySQL #{config[:database]} database succesfully created"
rescue
$stderr.puts "Couldn't create database for #{config.inspect}"
$stderr.puts "Couldn't create database for #{config.inspect}, charset: #{@charset}, collation: #{@collation} (if you set the charset manually, make sure you have a matching collation)"
end
when 'postgresql'
`createdb "#{config[:database]}" -E utf8`
Expand All @@ -58,7 +58,11 @@ def create_local_database(config)
config = ActiveRecord::Base.configurations[Merb.environment.to_sym || :development]
case config[:adapter]
when 'mysql'
ActiveRecord::Base.connection.drop_database config[:database]
begin
ActiveRecord::Base.connection.drop_database config[:database]
rescue
puts "#{config[:database]} seems to have been dropped already"
end
when /^sqlite/
FileUtils.rm_f File.join(Merb.root, config[:database])
when 'postgresql'
Expand All @@ -77,29 +81,29 @@ def create_local_database(config)
desc 'Drops, creates and then migrates the database for the current environment. Target specific version with VERSION=x'
task :reset => ['db:drop', 'db:create', 'db:migrate']

# desc "Retrieves the charset for the current environment's database"
# task :charset => :environment do
# config = ActiveRecord::Base.configurations[Merb.environment || :development]
# case config[:adapter]
# when 'mysql'
# ActiveRecord::Base.establish_connection(config)
# puts ActiveRecord::Base.connection.charset
# else
# puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
# end
# end
desc "Retrieves the charset for the current environment's database"
task :charset => :merb_start do
config = ActiveRecord::Base.configurations[Merb.environment.to_sym || :development]
case config[:adapter]
when 'mysql'
ActiveRecord::Base.establish_connection(config)
puts ActiveRecord::Base.connection.charset
else
puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
end
end

# desc "Retrieves the collation for the current environment's database"
# task :collation => :environment do
# config = ActiveRecord::Base.configurations[Merb.environment || :development]
# case config[:adapter]
# when 'mysql'
# ActiveRecord::Base.establish_connection(config)
# puts ActiveRecord::Base.connection.collation
# else
# puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
# end
# end
desc "Retrieves the collation for the current environment's database"
task :collation => :merb_start do
config = ActiveRecord::Base.configurations[Merb.environment.to_sym || :development]
case config[:adapter]
when 'mysql'
ActiveRecord::Base.establish_connection(config)
puts ActiveRecord::Base.connection.collation
else
puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
end
end

desc "Retrieves the current schema version number"
task :version => :merb_start do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
:password: secrets
:host: localhost
:socket: /tmp/mysql.sock
:encoding: utf8

:test:
<<: *defaults
Expand Down

0 comments on commit e9e9c51

Please sign in to comment.