Skip to content

Commit 13fea3f

Browse files
Copilotmoio
andcommitted
Add utf8mb4 migration and tests for emoji support
Co-authored-by: moio <250541+moio@users.noreply.github.com> Signed-off-by: Silvio Moioli <silvio@moioli.net>
1 parent c88f340 commit 13fea3f

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

β€Žconfig/database.yml.exampleβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
default: &default
88
adapter: mysql2
9-
encoding: utf8
9+
encoding: utf8mb4
10+
collation: utf8mb4_unicode_ci
1011
username: root
1112
password: root
1213
host: <%= host %>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class ConvertToUtf8mb4 < ActiveRecord::Migration[7.2]
2+
# Tables that need utf8mb4 for emoji support in their text fields
3+
TABLES_TO_CONVERT = %w[
4+
announcements
5+
comments
6+
faqs
7+
projects
8+
updates
9+
].freeze
10+
11+
def up
12+
# Convert the database default charset
13+
execute "ALTER DATABASE `#{connection.current_database}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
14+
15+
TABLES_TO_CONVERT.each do |table|
16+
execute "ALTER TABLE #{table} CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
17+
end
18+
end
19+
20+
def down
21+
TABLES_TO_CONVERT.each do |table|
22+
execute "ALTER TABLE #{table} CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin;"
23+
end
24+
25+
execute "ALTER DATABASE `#{connection.current_database}` CHARACTER SET utf8 COLLATE utf8_general_ci;"
26+
end
27+
end

β€Žspec/models/comment_spec.rbβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,13 @@
66
comment.destroy!
77
expect(Comment.count).to eq 0
88
end
9+
10+
describe 'emoji support' do
11+
it 'saves and retrieves comments with emoji characters' do
12+
emoji_text = 'This is a great idea! πŸ˜±πŸŽ‰πŸ‘'
13+
comment = create(:comment, text: emoji_text)
14+
comment.reload
15+
expect(comment.text).to eq(emoji_text)
16+
end
17+
end
918
end

β€Žspec/models/project_spec.rbβ€Ž

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,20 @@
175175
expect(Notification.where(recipient: user).count).to eq(1)
176176
end
177177
end
178+
179+
describe 'emoji support' do
180+
it 'saves and retrieves project descriptions with emoji characters' do
181+
emoji_description = 'Project with emojis πŸ˜±πŸŽ‰πŸ‘ and more text'
182+
project = create(:project, description: emoji_description)
183+
project.reload
184+
expect(project.description).to eq(emoji_description)
185+
end
186+
187+
it 'saves and retrieves project titles with emoji characters' do
188+
emoji_title = 'My Awesome Project πŸš€'
189+
project = create(:project, title: emoji_title)
190+
project.reload
191+
expect(project.title).to eq(emoji_title)
192+
end
193+
end
178194
end

0 commit comments

Comments
Β (0)