-
-
Save barrywoolgar/c19d18d692b4f0a02ffe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Put this file in the root of your Rails project, | |
# then run it to output the SQL needed to change all | |
# your tables and columns to the same character set | |
# and collation. | |
# | |
# > ruby character_set_and_collation.rb | |
DATABASE = '' | |
CHARACTER_SET = 'utf8' | |
COLLATION = 'utf8_general_ci' | |
schema = File.open('db/schema.rb', 'r').read | |
rows = schema.split("\n") | |
table_name = nil | |
DATABASE = "`#{DATABASE}`." unless DATABASE == '' | |
rows.each do |row| | |
if row =~ /create_table/ | |
table_name = row.match(/create_table "(.+)"/)[1] | |
puts "ALTER TABLE #{DATABASE}`#{table_name}` DEFAULT CHARACTER SET #{CHARACTER_SET} COLLATE #{COLLATION};" | |
elsif row =~ /t\.string/ | |
field_name = row.match(/"([^"]+)"/)[1] | |
puts "ALTER TABLE #{DATABASE}`#{table_name}` CHANGE `#{field_name}` `#{field_name}` VARCHAR(255) CHARACTER SET #{CHARACTER_SET} COLLATE #{COLLATION};" | |
elsif row =~ /t\.text/ | |
field_name = row.match(/"([^"]+)"/)[1] | |
puts "ALTER TABLE #{DATABASE}`#{table_name}` CHANGE `#{field_name}` `#{field_name}` TEXT CHARACTER SET #{CHARACTER_SET} COLLATE #{COLLATION};" | |
end | |
end |
Author
barrywoolgar
commented
May 13, 2014
- Fix to not include default values in field names
- Fix to not include database name if one is not defined
- Removed unnecessary(?) COLUMN
- Hard-coded column type (will need to update this if we override default VARCHAR length)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment