ActiveRecord::Base.connection.create_database defaults to latin1

159 views
Skip to first unread message

Matt Aimonetti

unread,
May 21, 2007, 8:56:22 PM5/21/07
to Ruby on Rails: Core
If you use ActiveRecord::Base.connection.create_database you'll notice
that by default the created db will use latin1 encoding. I created a
plugin to handle different charset and collations (on top of helping
you with other boring DB tasks).

You can check out the early version of the plugin
svn checkout svn://rubyforge.org/var/svn/raketasks/db_tasks
(I really need to move my projects out of rubyforge, it's a real pain
to browse the code)

Anyway, if people consider that it's an important feature, I'd be glad
to submit a patch. Ohh, by the way, when you set your database with a
specific charset, tables inherit the collation and charset
automatically. Rails handles encoded queries by adding an encoding
value in the environment file. (database.yml) I have a rake tasks
which does that for you if you are too lazy to type 3 times encoding:
utf8 ;)

Matt

DHH

unread,
May 21, 2007, 11:47:37 PM5/21/07
to Ruby on Rails: Core
> If you use ActiveRecord::Base.connection.create_database you'll notice
> that by default the created db will use latin1 encoding. I created a
> plugin to handle different charset and collations (on top of helping
> you with other boring DB tasks).

Which database are you talking about specifically? I believe Rails
will use whatever the current default is. MySQL, for example, has a
installation-wide configuration file that controls that.

Matt Aimonetti

unread,
May 22, 2007, 12:28:52 AM5/22/07
to Ruby on Rails: Core
right, I was talking about the MysqlAdapter, the other database
adapters don't let you create a database.

Here is the create_database method:

def create_database(name) #:nodoc:
execute "CREATE DATABASE `#{name}`"
end

Here is my suggestion:

# Create a new MySQL database allowing you to specify the
charset and collation, by default the database is created with utf8
charset and utf8_bin collation
# usage:
ActiveRecord::Base.connection.create_database('charset_plugin_test',
{:charset => 'latin1', :collation => 'latin1_bin'})
def create_database(name, options = {})
execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET
`#{options[:charset] ||= 'utf8'}` COLLATE `#{options[:collation] ||=
'utf8_bin'}`"
end


Nothing fancy but using utf8 by default means that people have to/
should update their database.yml file to add the encoding to each
environment.


Does it make sense?

-Matt

Mislav Marohnić

unread,
May 22, 2007, 8:14:37 AM5/22/07
to rubyonra...@googlegroups.com
On 5/22/07, Matt Aimonetti <mattAi...@gmail.com> wrote:

Nothing fancy but using utf8 by default means that people have to/
should update their database.yml file to add the encoding to each
environment.

Does it make sense?

To me it does. It's inexpensive and exposes native database functionality to the framework user.

Manfred Stienstra

unread,
May 22, 2007, 12:03:34 PM5/22/07
to rubyonra...@googlegroups.com
> On May 21, 8:47 pm, DHH <david.heineme...@gmail.com> wrote:
>>> If you use ActiveRecord::Base.connection.create_database you'll
>>> notice
>>> that by default the created db will use latin1 encoding. I created a
>>> plugin to handle different charset and collations (on top of helping
>>> you with other boring DB tasks).
>>
>> Which database are you talking about specifically? I believe Rails
>> will use whatever the current default is. MySQL, for example, has a
>> installation-wide configuration file that controls that.

Just a quick note, it's probably best to set the server-wide encoding
to UTF-8. I've seen some instances, mostly with MySQL 4.x where even
with SET NAMES utf8 the MySQL client wouldn't go into UTF-8 mode.
Ending up with mixed charsets in one table is a pita.

Manfred

Matt Aimonetti

unread,
May 23, 2007, 11:26:31 AM5/23/07
to Ruby on Rails: Core
Somebody from the core team? Do you want a patch or sould I keep that
in my plugin?

m|a

Jeremy Kemper

unread,
May 23, 2007, 3:25:59 PM5/23/07
to rubyonra...@googlegroups.com
On 5/23/07, Matt Aimonetti <mattAi...@gmail.com> wrote:
> Somebody from the core team? Do you want a patch or sould I keep that
> in my plugin?

Please do patch. Let's encourage Unicode end-to-end.

jeremy

Matt Aimonetti

unread,
May 23, 2007, 11:12:09 PM5/23/07
to Ruby on Rails: Core
Thanks Jeremy,

Here is the ticket:
http://dev.rubyonrails.org/ticket/8448

if the patch is used, one can start a rails app just like that:

rake my_project
rake db:create
ruby script/server

That's it! ready to go. (compatible with PostgreSQL, MySQL, and
SQLite3)

One thing though, we might want to add the following key/value:
encoding: utf8
to each default environment from database.yml

- Matt


On May 23, 12:25 pm, "Jeremy Kemper" <jer...@bitsweat.net> wrote:

Reply all
Reply to author
Forward
0 new messages