Refactor & DRY encoding migration

Dynamic row for MySQL

Set larger column index sizes
This commit is contained in:
Dumitru Ursu 2015-01-08 06:32:07 +02:00
parent dda5f71124
commit a733a774d1
2 changed files with 54 additions and 51 deletions

View file

@ -1,40 +1,43 @@
class SetMysqlToUnicodeMb4 < ActiveRecord::Migration class SetMysqlToUnicodeMb4 < ActiveRecord::Migration
# Converts the tables and strings columns to utf8mb4, which is the true, full
# unicode support in MySQl
UTF8_PAIRS = {} UTF8_PAIRS = {}
tables = ActiveRecord::Base.connection.tables tables = ActiveRecord::Base.connection.tables
tables.each do |table| tables.each do |table|
ActiveRecord::Base.connection.columns(table).each do |column| ActiveRecord::Base.connection.columns(table).each do |column|
# build a hash with all the columns that contain text # build a hash with all the columns that contain text
UTF8_PAIRS[table] = column.name if (column.type == :string) || (column.type == :text) if (column.type == :string) || (column.type == :text)
UTF8_PAIRS[table] = { :name => column.name, :type => column.sql_type }
end
end end
end end
def self.up def self.up
if AppConfig.mysql? # these will only affect tables on InnoDB, and only on rows with the dynamic
execute "ALTER DATABASE `#{ActiveRecord::Base.connection.current_database}` CHARACTER SET utf8mb4;" # format
execute "SET global innodb_file_format = BARRACUDA;"
tables.each do |table| execute "SET global innodb_large_prefix = 1;"
execute "ALTER TABLE `#{table}` CHARACTER SET = utf8mb4;" execute "SET global innodb_file_per_table = 1;"
end change_encoding('utf8mb4', 'ENGINE=InnoDB ROW_FORMAT=DYNAMIC') if AppConfig.mysql?
UTF8_PAIRS.each do |table, col|
execute "ALTER TABLE `#{table}` CHANGE `#{col}` `#{col}` TEXT CHARACTER SET utf8mb4 NULL;"
end
end
end end
def self.down def self.down
if AppConfig.mysql? # let MySQL pick the default engine
execute "ALTER DATABASE `#{ActiveRecord::Base.connection.current_database}` CHARACTER SET utf8;" change_encoding('utf8', '') if AppConfig.mysql?
end
def change_encoding(encoding, engine)
execute "ALTER DATABASE `#{ActiveRecord::Base.connection.current_database}` CHARACTER SET #{encoding};"
tables.each do |table| tables.each do |table|
execute "ALTER TABLE `#{table}` CHARACTER SET = utf8;" execute "ALTER TABLE `#{table}` CHARACTER SET = #{encoding} #{engine};"
end end
UTF8_PAIRS.each do |table, col| UTF8_PAIRS.each do |table, column|
execute "ALTER TABLE `#{table}` CHANGE `#{col}` `#{col}` TEXT CHARACTER SET utf8 NULL;" name = column[:name]
end type = column[:type]
execute "ALTER TABLE `#{table}` CHANGE `#{name}` `#{name}` #{type} CHARACTER SET #{encoding} NULL;"
end end
end end
end end

View file

@ -11,10 +11,10 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150106050734) do ActiveRecord::Schema.define(version: 20150106050733) do
create_table "account_deletions", force: true do |t| create_table "account_deletions", force: true do |t|
t.string "diaspora_handle" t.text "diaspora_handle"
t.integer "person_id" t.integer "person_id"
t.datetime "completed_at" t.datetime "completed_at"
end end
@ -35,7 +35,7 @@ ActiveRecord::Schema.define(version: 20150106050734) do
t.integer "aspect_id", null: false t.integer "aspect_id", null: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "shareable_type", default: "Post", null: false t.string "shareable_type"
end end
add_index "aspect_visibilities", ["aspect_id"], name: "index_aspect_visibilities_on_aspect_id", using: :btree add_index "aspect_visibilities", ["aspect_id"], name: "index_aspect_visibilities_on_aspect_id", using: :btree
@ -43,7 +43,7 @@ ActiveRecord::Schema.define(version: 20150106050734) do
add_index "aspect_visibilities", ["shareable_id", "shareable_type"], name: "index_aspect_visibilities_on_shareable_id_and_shareable_type", using: :btree add_index "aspect_visibilities", ["shareable_id", "shareable_type"], name: "index_aspect_visibilities_on_shareable_id_and_shareable_type", using: :btree
create_table "aspects", force: true do |t| create_table "aspects", force: true do |t|
t.string "name", null: false t.string "name"
t.integer "user_id", null: false t.integer "user_id", null: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
@ -65,7 +65,7 @@ ActiveRecord::Schema.define(version: 20150106050734) do
t.string "jid", null: false t.string "jid", null: false
t.string "name" t.string "name"
t.string "ask", limit: 128 t.string "ask", limit: 128
t.string "subscription", limit: 128, null: false t.string "subscription", limit: 128
end end
add_index "chat_contacts", ["user_id", "jid"], name: "index_chat_contacts_on_user_id_and_jid", unique: true, using: :btree add_index "chat_contacts", ["user_id", "jid"], name: "index_chat_contacts_on_user_id_and_jid", unique: true, using: :btree
@ -74,7 +74,7 @@ ActiveRecord::Schema.define(version: 20150106050734) do
t.integer "user_id", null: false t.integer "user_id", null: false
t.string "root", limit: 256, null: false t.string "root", limit: 256, null: false
t.string "namespace", limit: 256, null: false t.string "namespace", limit: 256, null: false
t.text "xml", null: false t.text "xml"
end end
add_index "chat_fragments", ["user_id"], name: "index_chat_fragments_on_user_id", unique: true, using: :btree add_index "chat_fragments", ["user_id"], name: "index_chat_fragments_on_user_id", unique: true, using: :btree
@ -82,7 +82,7 @@ ActiveRecord::Schema.define(version: 20150106050734) do
create_table "chat_offline_messages", force: true do |t| create_table "chat_offline_messages", force: true do |t|
t.string "from", null: false t.string "from", null: false
t.string "to", null: false t.string "to", null: false
t.text "message", null: false t.text "message"
t.datetime "created_at", null: false t.datetime "created_at", null: false
end end
@ -96,7 +96,7 @@ ActiveRecord::Schema.define(version: 20150106050734) do
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.integer "likes_count", default: 0, null: false t.integer "likes_count", default: 0, null: false
t.string "commentable_type", limit: 60, default: "Post", null: false t.string "commentable_type", limit: 60
end end
add_index "comments", ["author_id"], name: "index_comments_on_person_id", using: :btree add_index "comments", ["author_id"], name: "index_comments_on_person_id", using: :btree
@ -129,7 +129,7 @@ ActiveRecord::Schema.define(version: 20150106050734) do
create_table "conversations", force: true do |t| create_table "conversations", force: true do |t|
t.string "subject" t.string "subject"
t.string "guid", null: false t.string "guid"
t.integer "author_id", null: false t.integer "author_id", null: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
@ -155,7 +155,7 @@ ActiveRecord::Schema.define(version: 20150106050734) do
t.string "service" t.string "service"
t.string "identifier" t.string "identifier"
t.boolean "admin", default: false t.boolean "admin", default: false
t.string "language", default: "en" t.string "language"
end end
add_index "invitations", ["aspect_id"], name: "index_invitations_on_aspect_id", using: :btree add_index "invitations", ["aspect_id"], name: "index_invitations_on_aspect_id", using: :btree
@ -171,7 +171,7 @@ ActiveRecord::Schema.define(version: 20150106050734) do
t.text "parent_author_signature" t.text "parent_author_signature"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "target_type", limit: 60, null: false t.string "target_type", limit: 60
end end
add_index "likes", ["author_id"], name: "likes_author_id_fk", using: :btree add_index "likes", ["author_id"], name: "likes_author_id_fk", using: :btree
@ -238,7 +238,7 @@ ActiveRecord::Schema.define(version: 20150106050734) do
create_table "o_embed_caches", force: true do |t| create_table "o_embed_caches", force: true do |t|
t.string "url", limit: 1024, null: false t.string "url", limit: 1024, null: false
t.text "data", null: false t.text "data"
end end
add_index "o_embed_caches", ["url"], name: "index_o_embed_caches_on_url", length: {"url"=>255}, using: :btree add_index "o_embed_caches", ["url"], name: "index_o_embed_caches_on_url", length: {"url"=>255}, using: :btree
@ -269,7 +269,7 @@ ActiveRecord::Schema.define(version: 20150106050734) do
t.string "guid", null: false t.string "guid", null: false
t.text "url", null: false t.text "url", null: false
t.string "diaspora_handle", null: false t.string "diaspora_handle", null: false
t.text "serialized_public_key", null: false t.text "serialized_public_key"
t.integer "owner_id" t.integer "owner_id"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
@ -466,7 +466,7 @@ ActiveRecord::Schema.define(version: 20150106050734) do
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.boolean "hidden", default: false, null: false t.boolean "hidden", default: false, null: false
t.integer "contact_id", null: false t.integer "contact_id", null: false
t.string "shareable_type", limit: 60, default: "Post", null: false t.string "shareable_type", limit: 60
end end
add_index "share_visibilities", ["contact_id"], name: "index_post_visibilities_on_contact_id", using: :btree add_index "share_visibilities", ["contact_id"], name: "index_post_visibilities_on_contact_id", using: :btree