Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby-version: [2.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3]
ruby-version: [3.1, 3.2, 3.3]

steps:
- name: Cancel previous runs
Expand Down
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.2.2
50 changes: 3 additions & 47 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,9 @@

ruby_version = Gem::Version.new(RUBY_VERSION)

if ruby_version < Gem::Version.new('2.7.0')
['4.0.0', '4.1.0', '4.2.0'].each do |rails_version|
appraise "rails-#{rails_version}" do
gem 'rails', "~> #{rails_version}"
gem 'bigdecimal', '1.3.5'
gem 'sqlite3', '~> 1.3.6'
end
end
end

if ruby_version < Gem::Version.new('3.0.0')
appraise 'rails-5.0' do
gem 'rails', '~> 5.0.0'
gem 'sqlite3', '~> 1.3.6'
end

appraise 'rails-5.1' do
gem 'rails', '~> 5.1.0'
gem 'sqlite3', '~> 1.3.6'
end

appraise 'rails-5.2' do
gem 'rails', '~> 5.2.0'
gem 'sqlite3', '~> 1.3.6'
end
end

if ruby_version >= Gem::Version.new('2.5.0')
appraise 'rails-6.0' do
gem 'rails', '~> 6.0.0'
gem 'sqlite3', '~> 1.4.0'
end

appraise 'rails-6.1' do
gem 'rails', '~> 6.1.0'
gem 'sqlite3', '~> 1.4.0'
end
end

if ruby_version >= Gem::Version.new('2.7.0')
appraise 'rails-7.0' do
gem 'rails', '~> 7.0.0'
gem 'sqlite3', '~> 1.4.0'
end

appraise 'rails-7.1' do
gem 'rails', '~> 7.1.0'
if ruby_version >= Gem::Version.new('3.1.0')
appraise 'rails-7.2' do
gem 'rails', '~> 7.2.0'
gem 'sqlite3', '~> 1.4.0'
end
end
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,16 @@ You can change the migration folder by setting the DATA_MIGRATIONS_PATH environm
```ruby
DATA_MIGRATIONS_PATH=new/path
```
You can also change the table and index names by setting the corresponding environment variables:
You can also change the table name by setting the corresponding environment variables:
```ruby
DATA_MIGRATIONS_TABLE_NAME=new_table_name
DATA_MIGRATIONS_INDEX_NAME=new_index_name
```
This can enable more complex use cases, such as data migrations intended for different contexts,
or running multiple sets of data migrations in a specific order. It may also avoid conflict with existing path or table names

## Rails Support

Rails 4.0 and higher
Rails 7.2 and higher

## Installation

Expand Down
39 changes: 12 additions & 27 deletions lib/rails_data_migrations/log_entry.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,21 @@
# frozen_string_literal: true

module RailsDataMigrations
module SharedMethods
def table_name
tbl_name = ENV.fetch('DATA_MIGRATIONS_TABLE_NAME', 'data_migrations')
"#{ActiveRecord::Base.table_name_prefix}#{tbl_name}#{ActiveRecord::Base.table_name_suffix}"
class LogEntry < ::ActiveRecord::Base
def self.table_name
ENV.fetch('DATA_MIGRATIONS_TABLE_NAME', 'data_migrations')
end

def index_name
"#{table_name_prefix}#{ENV.fetch('DATA_MIGRATIONS_INDEX_NAME', 'unique_data_migrations')}#{table_name_suffix}"
def self.create_table
schema_migration_instance.create_table
end
end

if Gem::Version.new('7.1.0') >= Gem::Version.new(::ActiveRecord.version)
class LogEntry < ::ActiveRecord::SchemaMigration
class << self
include SharedMethods
end
end
else
class LogEntry < ::ActiveRecord::Base
class << self
include SharedMethods
def create_table
::ActiveRecord::SchemaMigration.define_method(:table_name) do
tbl_name = ENV.fetch('DATA_MIGRATIONS_TABLE_NAME', 'data_migrations')
"#{::ActiveRecord::Base.table_name_prefix}#{tbl_name}#{::ActiveRecord::Base.table_name_suffix}"
end

::ActiveRecord::Base.connection.schema_migration.create_table
end
end
def self.schema_migration_instance
connection_pool = ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool
schema_migration = ActiveRecord::SchemaMigration.new(connection_pool)
schema_migration.define_singleton_method(:table_name) { LogEntry.table_name }
schema_migration.instance_variable_set(:@arel_table, Arel::Table.new(LogEntry.table_name))
schema_migration
end
end
end
end
77 changes: 19 additions & 58 deletions lib/rails_data_migrations/migrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module RailsDataMigrations
class Migrator < ::ActiveRecord::Migrator
self.migrations_paths = [ENV.fetch('DATA_MIGRATIONS_PATH', 'db/data_migrations')]

MIGRATOR_SALT = 2053462855

def record_version_state_after_migrating(version)
Expand All @@ -15,13 +17,8 @@ def record_version_state_after_migrating(version)
end

class << self
def migrations_table_exists?(connection = ActiveRecord::Base.connection)
table_check_method = connection.respond_to?(:data_source_exists?) ? :data_source_exists? : :table_exists?
connection.send(table_check_method, schema_migrations_table_name)
end

def get_all_versions(connection = ActiveRecord::Base.connection)
if migrations_table_exists?(connection)
def get_all_versions # rubocop:disable Naming/AccessorMethodName
if LogEntry.table_exists?
LogEntry.all.map { |x| x.version.to_i }.sort
else
[]
Expand All @@ -36,61 +33,25 @@ def schema_migrations_table_name
LogEntry.table_name
end

def migrations_path
ENV.fetch('DATA_MIGRATIONS_PATH', 'db/data_migrations')
end

def rails_6_0?
Rails::VERSION::MAJOR >= 6
end

def rails_5_2?
Rails::VERSION::MAJOR > 5 || (Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR >= 2)
end

def rails_7_1?
Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR >= 1
end

def list_migrations
if rails_7_1?
::ActiveRecord::MigrationContext.new(
migrations_path, ::ActiveRecord::Base.connection.schema_migration
).migrations
elsif rails_6_0?
::ActiveRecord::MigrationContext.new(migrations_path, ::ActiveRecord::SchemaMigration).migrations
elsif rails_5_2?
::ActiveRecord::MigrationContext.new(migrations_path).migrations
else
migrations(migrations_path)
end
::ActiveRecord::MigrationContext.new(migrations_path).migrations
end

def list_pending_migrations
if rails_5_2?
already_migrated = get_all_versions
list_migrations.reject { |m| already_migrated.include?(m.version) }
else
open(migrations_path).pending_migrations # rubocop:disable Security/Open
end
end

def run_migration(direction, migrations_path, version)
if rails_7_1?
new(
direction,
list_migrations,
::ActiveRecord::Base.connection.schema_migration,
::ActiveRecord::InternalMetadata.new(ActiveRecord::Base.connection),
version
).run
elsif rails_6_0?
new(direction, list_migrations, ::ActiveRecord::SchemaMigration, version).run
elsif rails_5_2?
new(direction, list_migrations, version).run
else
run(direction, migrations_path, version)
end
already_migrated = get_all_versions
list_migrations.reject { |m| already_migrated.include?(m.version) }
end

def run_migration(direction, version)
connection_pool = ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool
internal_metadata = ActiveRecord::InternalMetadata.new(connection_pool)
new(
direction,
list_migrations,
LogEntry.schema_migration_instance,
internal_metadata,
version
).run
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_data_migrations/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RailsDataMigrations
VERSION = '1.3.0.1'
VERSION = '1.3.0.2'
end
6 changes: 1 addition & 5 deletions lib/tasks/data_migrations.rake
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ namespace :data do
def apply_single_migration(direction, version)
raise 'VERSION is required' unless version

RailsDataMigrations::Migrator.run_migration(
direction,
RailsDataMigrations::Migrator.migrations_path,
version.to_i
)
RailsDataMigrations::Migrator.run_migration(direction, version.to_i)
end

task init_migration: :environment do
Expand Down
4 changes: 2 additions & 2 deletions rails-data-migrations.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ Gem::Specification.new do |spec|
end
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 2.3'
spec.required_ruby_version = '>= 3.1.0'

spec.add_runtime_dependency 'rails', '>= 4.0.0'
spec.add_runtime_dependency 'rails', '>= 7.2.0'

spec.add_development_dependency 'appraisal', '~> 2.1'
spec.add_development_dependency 'rake', '>= 12.3.3'
Expand Down
1 change: 0 additions & 1 deletion spec/data_migrations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

describe RailsDataMigrations do
it 'checks for migration log table existence' do
expect(RailsDataMigrations::Migrator.migrations_table_exists?(ActiveRecord::Base.connection)).to be_truthy
expect(RailsDataMigrations::Migrator.get_all_versions).to be_blank
end

Expand Down