From b677c29365c605400d1e143b7d4685e63e9d7ee9 Mon Sep 17 00:00:00 2001 From: Niklas van Schrick Date: Sat, 27 Dec 2025 16:01:29 +0100 Subject: [PATCH] Add compatibility with rails 8.1 --- .github/workflows/main.yml | 13 +++++++++-- code0-zero_track.gemspec | 2 +- lib/code0/zero_track.rb | 1 + .../database/schema_migrations/formatter.rb | 22 +++++++++++++++++++ .../active_record_schema_migrations.rb | 15 ++++++++----- lib/code0/zero_track/railtie.rb | 2 +- 6 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 lib/code0/zero_track/database/schema_migrations/formatter.rb diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index db12120..6bc2fab 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,13 +10,20 @@ on: jobs: build: runs-on: ubuntu-latest - name: Ruby ${{ matrix.ruby }} + name: Ruby ${{ matrix.ruby }} / Rails ${{ matrix.rails }} strategy: + fail-fast: false matrix: ruby: - '3.2.2' - '3.3.6' - '3.4.7' + rails: + - '~> 8.0.0' + - '~> 8.1.0' + + env: + RAILS_VERSION: ${{ matrix.rails }} steps: - uses: actions/checkout@v4 @@ -24,6 +31,8 @@ jobs: uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} - bundler-cache: true + bundler-cache: false + - name: Install gems + run: bundle install - name: Run the default task run: bin/rake diff --git a/code0-zero_track.gemspec b/code0-zero_track.gemspec index f60c092..dfd81a0 100644 --- a/code0-zero_track.gemspec +++ b/code0-zero_track.gemspec @@ -21,7 +21,7 @@ Gem::Specification.new do |spec| Dir['{app,config,db,lib,rubocop}/**/*', 'LICENSE', 'Rakefile', 'README.md'] end - spec.add_dependency 'rails', '>= 8.0.1' + spec.add_dependency 'rails', ENV.fetch('RAILS_VERSION', '>= 8.0.1') spec.add_dependency 'zeitwerk', '~> 2.7' spec.add_development_dependency 'rake', '~> 13.0' diff --git a/lib/code0/zero_track.rb b/lib/code0/zero_track.rb index 4b17087..53e9302 100644 --- a/lib/code0/zero_track.rb +++ b/lib/code0/zero_track.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'active_support/core_ext/module/delegation' require 'rails/railtie' require 'zeitwerk' diff --git a/lib/code0/zero_track/database/schema_migrations/formatter.rb b/lib/code0/zero_track/database/schema_migrations/formatter.rb new file mode 100644 index 0000000..a6a19db --- /dev/null +++ b/lib/code0/zero_track/database/schema_migrations/formatter.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Code0 + module ZeroTrack + module Database + module SchemaMigrations + class Formatter + def initialize(connection) + @connection = connection + end + + def format(_) + # rubocop:disable Rails/SkipsModelValidations -- not an active record object + Database::SchemaMigrations.touch_all(@connection) unless Rails.env.production? + # rubocop:enable Rails/SkipsModelValidations + nil + end + end + end + end + end +end diff --git a/lib/code0/zero_track/injectors/active_record_schema_migrations.rb b/lib/code0/zero_track/injectors/active_record_schema_migrations.rb index d482bdb..0e602fe 100644 --- a/lib/code0/zero_track/injectors/active_record_schema_migrations.rb +++ b/lib/code0/zero_track/injectors/active_record_schema_migrations.rb @@ -4,12 +4,17 @@ module Code0 module ZeroTrack module Injectors class ActiveRecordSchemaMigrations - def self.inject! - # Patch to write version information as empty files under the db/schema_migrations directory - # This is intended to reduce potential for merge conflicts in db/structure.sql - ActiveSupport.on_load(:active_record_postgresqladapter) do - prepend Database::PostgresqlAdapter::DumpSchemaVersionsMixin + def self.inject!(config) + if Rails.gem_version >= Gem::Version.create('8.1') + config.active_record.schema_versions_formatter = Database::SchemaMigrations::Formatter + else + # Patch to write version information as empty files under the db/schema_migrations directory + # This is intended to reduce potential for merge conflicts in db/structure.sql + ActiveSupport.on_load(:active_record_postgresqladapter) do + prepend Database::PostgresqlAdapter::DumpSchemaVersionsMixin + end end + # Patch to load version information from empty files under the db/schema_migrations directory ActiveRecord::Tasks::PostgreSQLDatabaseTasks .prepend Database::PostgresqlDatabaseTasks::LoadSchemaVersionsMixin diff --git a/lib/code0/zero_track/railtie.rb b/lib/code0/zero_track/railtie.rb index bcc9115..d5dd4e3 100644 --- a/lib/code0/zero_track/railtie.rb +++ b/lib/code0/zero_track/railtie.rb @@ -16,7 +16,7 @@ class Railtie < ::Rails::Railtie config.after_initialize do Injectors::ActiveRecordTimestamps.inject! if config.zero_track.active_record.timestamps - Injectors::ActiveRecordSchemaMigrations.inject! if config.zero_track.active_record.schema_migrations + Injectors::ActiveRecordSchemaMigrations.inject!(config) if config.zero_track.active_record.schema_migrations end end end