File obs-server-2.9-0026-api-Convert-tables-created-without-default-charset-t.patch of Package obs-server

From 79ed6181fbf8684b6000c385d11c52a0b4d4fd35 Mon Sep 17 00:00:00 2001
From: Oleg Girko <ol@infoserver.lv>
Date: Wed, 15 Aug 2018 14:36:46 +0100
Subject: [PATCH] [api] Convert tables created without default charset to utf8.

Some tables were created without default charset explicitly specified.
This causes these table to get theif default charset from database default,
that in turn got its default charset from MySQL/MariaDB server default.

However, tests assume that default charset is utf8, but MySQL/MariaDB server
uses some distribution default that my be something different.
Hence, it would be better to not rely on MySQL/MariaDB server
using utf8 as default and specify charset explicitly.

This change adds a migration that explicitly sets database charset to utf8
and converts tables that were created without default charset to utf8.

Signed-off-by: Oleg Girko <ol@infoserver.lv>
---
 .../20180815091004_set_tables_charset.rb      | 41 +++++++++++++++++++
 src/api/db/structure.sql                      |  1 +
 2 files changed, 42 insertions(+)
 create mode 100644 src/api/db/migrate/20180815091004_set_tables_charset.rb

diff --git a/src/api/db/migrate/20180815091004_set_tables_charset.rb b/src/api/db/migrate/20180815091004_set_tables_charset.rb
new file mode 100644
index 0000000000..70d85632a4
--- /dev/null
+++ b/src/api/db/migrate/20180815091004_set_tables_charset.rb
@@ -0,0 +1,41 @@
+class SetTablesCharset < ActiveRecord::Migration[5.0]
+  def self.up
+    # Change database default charset to UTF-8
+    execute "ALTER DATABASE #{current_database} DEFAULT CHARACTER SET utf8"
+
+    # Convert tables to UTF-8 charset
+    [
+      'ar_internal_metadata',
+      'binary_releases',
+      'bs_request_counter',
+      'cloud_ec2_configurations',
+      'cloud_user_upload_jobs',
+      'data_migrations',
+      'download_repositories',
+      'group_maintainers',
+      'history_elements',
+      'incident_updateinfo_counter_values',
+      'kiwi_descriptions',
+      'kiwi_images',
+      'kiwi_package_groups',
+      'kiwi_packages',
+      'kiwi_preferences',
+      'kiwi_repositories',
+      'maintained_projects',
+      'notifications',
+      'product_media',
+      'product_update_repositories'
+    ].each do |table|
+      execute "ALTER TABLE #{table} CONVERT TO CHARACTER SET utf8"
+    end
+
+    # Convert columns changed to MEDIUMTEXT during conversion back to TEXT
+    execute "ALTER TABLE download_repositories MODIFY pubkey TEXT"
+    execute "ALTER TABLE history_elements MODIFY comment TEXT"
+    execute "ALTER TABLE notifications MODIFY event_payload TEXT NOT NULL"
+  end
+
+  def self.down
+    # Do nothing. It makes no sense to convert tables back.
+  end
+end
diff --git a/src/api/db/structure.sql b/src/api/db/structure.sql
index fd29fe2ab0..188e73d4fe 100644
--- a/src/api/db/structure.sql
+++ b/src/api/db/structure.sql
@@ -1316,5 +1316,6 @@ INSERT INTO `schema_migrations` (version) VALUES
 ('20180109115548'),
 ('20180110074142'),
 ('20180307074538');
+('20180815091004');
 
 
-- 
2.20.1