Skip to content

Instantly share code, notes, and snippets.

@spickermann
Last active September 27, 2022 04:13
Show Gist options
  • Save spickermann/4433125 to your computer and use it in GitHub Desktop.
Save spickermann/4433125 to your computer and use it in GitHub Desktop.
Ruby On Rails - default charset migration
class SetDatabaseDefaults < ActiveRecord::Migration
def up
case ActiveRecord::Base.connection.adapter_name
when "MySQL", "Mysql2"
execute "ALTER DATABASE #{current_database} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci"
when "SQLite"
# do nothing since SQLite does not support changing the database encoding and only supports unicode charsets
when "PostgreSQL"
# do nothing since PostgreSQL does not support changing the database encoding
end
end
def down
raise ActiveRecord::IrreversibleMigration
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment