Skip to content

feat: MariaDB/MySQL support #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Aug 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
94f1b64
fix: make sure the default value of translatable attributes work in M…
did Aug 8, 2025
576b823
feat: support for MySQL/MariaDB
did Aug 18, 2025
fcec49e
chore: attempt to fix the CI
did Aug 18, 2025
bc2cc90
chore: Rubocop
did Aug 18, 2025
4fb71f3
chore: another way of registering the LocalesType
did Aug 18, 2025
a146b9a
fix: use Arel.sql instead (in order to support Rails 7.0)
did Aug 18, 2025
7954309
chore: MariaDB / MySQL support only for Rails 8
did Aug 18, 2025
d6bdbc1
fix: healthcheck doesn't exist
did Aug 18, 2025
d11d9c7
chore: fixing the MySQL setup
did Aug 18, 2025
210a1ee
chore: fixing the MySQL setup
did Aug 18, 2025
81ffade
chore: fixing the MySQL setup
did Aug 18, 2025
80833c0
chore: fixing the MySQL setup
did Aug 18, 2025
48407f6
chore: fixing the MySQL setup
did Aug 18, 2025
f9ab744
chore: fixing the MySQL setup
did Aug 18, 2025
8a17002
chore: fixing the MySQL setup
did Aug 18, 2025
510fa9d
chore: fixing the MySQL setup
did Aug 18, 2025
363fe8e
chore: fixing the MySQL setup
did Aug 18, 2025
c3d3d96
chore: fixing the MySQL setup
did Aug 18, 2025
5769b93
chore: fixing the MySQL setup
did Aug 18, 2025
fcd2d35
chore: fixing the MySQL setup
did Aug 18, 2025
14b6a50
chore: fixing the MariaDB setup
did Aug 18, 2025
f95aaae
fix: fix a bad refactoring
did Aug 18, 2025
c4a82d7
chore: one last attempt to support MySQL/MariaDB with Rails 7.x
did Aug 18, 2025
39788ad
chore(ci): update the test matrix (databases / rails versions)
did Aug 18, 2025
4d349ce
chore(ci): wrong schema.rb file for the legacy app
did Aug 18, 2025
2df3774
fix(ci): automatic MySQL reconnection
did Aug 18, 2025
3648187
chore(ci): MySQL/MariaDB doesnt work well with Rails 7.0
did Aug 18, 2025
e4199f4
chore(ci): MySQL/MariaDB doesnt work well with Rails 7.0 (attempt #2)
did Aug 18, 2025
b59a537
chore: Rubocop
did Aug 18, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 95 additions & 17 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,37 @@ jobs:
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

mysql:
image: mysql:8.0
env:
MYSQL_DATABASE: "maglev_engine_test"
MYSQL_ROOT_PASSWORD: "password"
ports:
- 3306:3306
options: >-
--health-cmd "mysqladmin ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5

mariadb:
image: mariadb:11.8
env:
MARIADB_DATABASE: "maglev_engine_test"
MARIADB_ROOT_PASSWORD: "password"
ports:
- 3307:3306
options: --health-cmd="healthcheck.sh --connect --innodb_initialized" --health-interval=10s --health-timeout=5s --health-retries=3

strategy:
matrix:
node: [20, 23]
gemfile: ["Gemfile.rails_7_0", "Gemfile.rails_7_2", "Gemfile"]
gemfile: ["Gemfile", "Gemfile.rails_7_0", "Gemfile.rails_7_2"]
database: [postgres, sqlite, mysql, mariadb]
exclude:
- gemfile: Gemfile.rails_7_0
database: mysql
- gemfile: Gemfile.rails_7_0
database: mariadb

steps:
- name: Checkout code
Expand All @@ -41,17 +68,22 @@ jobs:
run: |
corepack enable

- name: Use Node.js ${{ matrix.node }}
- name: Use Node.js v23
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
node-version: 23
cache: yarn

- name: Install packages
run: |
yarn install

- name: Setup test database
- name: Run Javascript tests
run: yarn test

# === Postgresql 🐘 ===
- name: Setup test database (Postgresql) 🐘
if: ${{ matrix.database == 'postgres' }}
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
RAILS_ENV: test
Expand All @@ -60,14 +92,17 @@ jobs:
run: |
bin/rails db:setup

- name: Run Rails tests
- name: Run Rails tests (Postgresql) 🐘
if: ${{ matrix.database == 'postgres' }}
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
MAGLEV_APP_DATABASE_USERNAME: "maglev"
MAGLEV_APP_DATABASE_PASSWORD: "password"
run: bundle exec rspec
run: bundle exec rspec

- name: Setup test database (SQLite)
# === SQLite 🪽 ===
- name: Setup test database (SQLite) 🪽
if: ${{ matrix.database == 'sqlite' }}
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
RAILS_ENV: test
Expand All @@ -79,21 +114,64 @@ jobs:
cp spec/legacy_dummy/db/schema.sqlite.rb spec/legacy_dummy/db/schema.rb
bin/rails db:setup

- name: Run Rails tests (SQLite)
- name: Run Rails tests (SQLite) 🪽
if: ${{ matrix.database == 'sqlite' }}
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
USE_SQLITE: true
USE_SQLITE: 1
run: bundle exec rspec

- name: Cleanup DB schema files
# === MYSQL 🐬 ===
- name: Setup test database (MySQL) 🐬
if: ${{ matrix.database == 'mysql' }}
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
RAILS_ENV: test
USE_MYSQL: 1
MAGLEV_APP_DATABASE_HOST: "127.0.0.1"
MAGLEV_APP_DATABASE_USERNAME: "root"
MAGLEV_APP_DATABASE_PASSWORD: "password"
run: |
cp spec/dummy/db/schema.pg.rb spec/dummy/db/schema.rb
cp spec/legacy_dummy/db/schema.pg.rb spec/legacy_dummy/db/schema.rb
rm -f spec/dummy/db/maglev_engine_test.sqlite3
rm -f spec/legacy_dummy/db/maglev_engine_test.sqlite3
cp spec/dummy/db/schema.mysql.rb spec/dummy/db/schema.rb
cp spec/legacy_dummy/db/schema.mysql.rb spec/legacy_dummy/db/schema.rb
bin/rails db:setup

- name: Run Javascript tests
run: yarn test
- name: Run Rails tests (MySQL) 🐬
if: ${{ matrix.database == 'mysql' }}
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
USE_MYSQL: 1
MAGLEV_APP_DATABASE_HOST: "127.0.0.1"
MAGLEV_APP_DATABASE_USERNAME: "root"
MAGLEV_APP_DATABASE_PASSWORD: "password"
run: bundle exec rspec

# === MariaDB 🦭===
- name: Setup test database (MariaDB) 🦭
if: ${{ matrix.database == 'mariadb' }}
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
RAILS_ENV: test
USE_MYSQL: 1
MAGLEV_APP_DATABASE_HOST: "127.0.0.1"
MAGLEV_APP_DATABASE_PORT: 3307
MAGLEV_APP_DATABASE_USERNAME: "root"
MAGLEV_APP_DATABASE_PASSWORD: "password"
run: |
cp spec/dummy/db/schema.mariadb.rb spec/dummy/db/schema.rb
cp spec/legacy_dummy/db/schema.mariadb.rb spec/legacy_dummy/db/schema.rb
bin/rails db:setup

- name: Run Rails tests (MariaDB) 🦭
if: ${{ matrix.database == 'mariadb' }}
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
USE_MYSQL: 1
MAGLEV_APP_DATABASE_HOST: "127.0.0.1"
MAGLEV_APP_DATABASE_PORT: 3307
MAGLEV_APP_DATABASE_USERNAME: "root"
MAGLEV_APP_DATABASE_PASSWORD: "password"
run: bundle exec rspec

# NOTE: disabled because an error of eslint in the GH env
# - name: Run Javascript linter
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@ bin/test
spec/dummy/db/*.sqlite3
spec/legacy_dummy/db/*.sqlite3

docker-compose.yml
db/mysql/init/01-init-databases.sql

docs/
TODO.md
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ gem 'puma'
# To use a debugger
# gem 'byebug', group: [:development, :test]

# Use SQLite/PostgreSQL for development and test
# Use SQLite/PostgreSQL/MariaDB for development and test
gem 'mysql2'
gem 'pg', '~> 1.5.9'
gem 'sqlite3'

Expand All @@ -55,6 +56,8 @@ group :development, :test do
gem 'annotaterb'

gem 'rdoc', '>= 6.6.3.1'

gem 'dotenv'
end

group :test do
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ GEM
date (3.4.1)
diff-lcs (1.5.1)
docile (1.4.1)
dotenv (3.1.8)
drb (2.2.3)
dry-cli (1.2.0)
erubi (1.13.0)
Expand Down Expand Up @@ -161,6 +162,7 @@ GEM
mini_portile2 (2.8.9)
minitest (5.25.5)
mutex_m (0.3.0)
mysql2 (0.5.6)
net-imap (0.5.8)
date
net-protocol
Expand Down Expand Up @@ -369,10 +371,12 @@ PLATFORMS
DEPENDENCIES
annotaterb
bcrypt
dotenv
factory_bot_rails (~> 6.2.0)
generator_spec
image_processing (~> 1.12.2)
maglevcms!
mysql2
nokogiri (>= 1.15.6)
observer
ostruct
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.rails_7_0
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ gem 'puma'
# Use SQLite/PostgreSQL for development and test
gem 'pg', '~> 1.5.9'
gem 'sqlite3', '~> 1.4'
gem 'mysql2', '~> 0.5.6'

# Gems no longer be part of the default gems from Ruby 3.5.0
gem 'observer'
Expand All @@ -45,6 +46,7 @@ gem 'bigdecimal'
gem 'mutex_m'
gem 'drb'
gem 'fiddle'
gem 'benchmark'

group :development, :test do
# Use SCSS for stylesheets
Expand All @@ -60,6 +62,10 @@ group :development, :test do
gem 'generator_spec'

gem 'nokogiri', '>= 1.13.10'

gem 'dotenv'

gem 'database_cleaner-active_record'
end

group :test do
Expand Down
11 changes: 11 additions & 0 deletions Gemfile.rails_7_0.lock
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,19 @@ GEM
ast (2.4.2)
base64 (0.2.0)
bcrypt (3.1.20)
benchmark (0.4.1)
bigdecimal (3.1.9)
builder (3.3.0)
concurrent-ruby (1.3.4)
crass (1.0.6)
database_cleaner-active_record (2.2.2)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0)
database_cleaner-core (2.0.1)
date (3.4.0)
diff-lcs (1.5.1)
docile (1.4.1)
dotenv (3.1.8)
drb (2.2.1)
dry-cli (1.2.0)
erubi (1.13.0)
Expand Down Expand Up @@ -141,6 +147,7 @@ GEM
mini_portile2 (2.8.9)
minitest (5.25.1)
mutex_m (0.3.0)
mysql2 (0.5.6)
net-imap (0.5.1)
date
net-protocol
Expand Down Expand Up @@ -302,7 +309,10 @@ PLATFORMS
DEPENDENCIES
base64
bcrypt
benchmark
bigdecimal
database_cleaner-active_record
dotenv
drb
factory_bot_rails (~> 6.2.0)
fiddle
Expand All @@ -311,6 +321,7 @@ DEPENDENCIES
maglevcms!
mini_magick (~> 4.11)
mutex_m
mysql2 (~> 0.5.6)
nokogiri (>= 1.13.10)
observer
ostruct
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.rails_7_2
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ gem 'puma'
# Use SQLite/PostgreSQL for development and test
gem 'pg', '~> 1.5.9'
gem 'sqlite3'
gem 'mysql2', '~> 0.5.6'

# Gems no longer be part of the default gems from Ruby 3.5.0
gem 'observer'
Expand All @@ -60,6 +61,10 @@ group :development, :test do
gem 'nokogiri', '>= 1.15.6'

gem 'rdoc', '>= 6.6.3.1'

gem 'dotenv'

gem 'database_cleaner-active_record'
end

group :test do
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.rails_7_2.lock
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,14 @@ GEM
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
crass (1.0.6)
database_cleaner-active_record (2.2.2)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0)
database_cleaner-core (2.0.1)
date (3.4.0)
diff-lcs (1.5.1)
docile (1.4.1)
dotenv (3.1.8)
drb (2.2.1)
dry-cli (1.2.0)
erubi (1.13.0)
Expand Down Expand Up @@ -151,6 +156,7 @@ GEM
mini_portile2 (2.8.9)
minitest (5.25.2)
mutex_m (0.3.0)
mysql2 (0.5.6)
net-imap (0.5.1)
date
net-protocol
Expand Down Expand Up @@ -343,10 +349,13 @@ PLATFORMS

DEPENDENCIES
bcrypt
database_cleaner-active_record
dotenv
factory_bot_rails (~> 6.2.0)
generator_spec
image_processing (~> 1.12.2)
maglevcms!
mysql2 (~> 0.5.6)
nokogiri (>= 1.15.6)
observer
ostruct
Expand Down
25 changes: 22 additions & 3 deletions app/models/concerns/maglev/translatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,38 @@ class UnavailableLocaleError < RuntimeError; end
extend ActiveSupport::Concern

def translations_for(attr)
public_send("#{attr}_translations")
# With MySQL, there is no default value for JSON columns, so we need to check for nil
public_send("#{attr}_translations").presence || {}
end

def translate_attr_in(attr, locale, source_locale)
translations_for(attr)[locale.to_s] ||= translations_for(attr)[source_locale.to_s]
end

# rubocop:disable Metrics/BlockLength
class_methods do
def order_by_translated(attr, direction)
order(Arel.sql("#{attr}_translations->>'#{Maglev::I18n.current_locale}'") => direction)
order(translated_arel_attribute(attr, Maglev::I18n.current_locale) => direction)
end

def translated_arel_attribute(attr, locale)
return Arel.sql("#{attr}_translations->>'#{locale}'") unless mysql?

# MySQL and MariaDB JSON support 🤬🤬🤬
# Note: doesn't work with Rails 7.0.x
json_extract = Arel::Nodes::NamedFunction.new(
'json_extract',
[Arel::Nodes::SqlLiteral.new("#{attr}_translations"), Arel::Nodes.build_quoted("$.#{locale}")]
)
Arel::Nodes::NamedFunction.new('json_unquote', [json_extract])
end

def translates(*attributes, presence: false)
attributes.each { |attr| setup_accessors(attr) }
attributes.each do |attr|
# MariaDB doesn't support native JSON columns (longtext instead), we need to force it.
attribute("#{attr}_translations", :json) if respond_to?(:attribute)
setup_accessors(attr)
end
add_presence_validator(attributes) if presence
end

Expand All @@ -45,5 +63,6 @@ def setup_accessors(attr)
define_method("default_#{attr}") { translations_for(attr)[Maglev::I18n.default_locale.to_s] }
end
end
# rubocop:enable Metrics/BlockLength
end
end
Loading