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
14 changes: 8 additions & 6 deletions .github/workflows/checkstyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Rubocop
on:
push:
branches:
- master
- "*"

pull_request:
branches:
Expand All @@ -15,18 +15,20 @@ jobs:

steps:
- name: Cancel previous runs
uses: styfle/cancel-workflow-action@0.5.0
uses: styfle/cancel-workflow-action@0.12.1
with:
access_token: ${{ github.token }}

- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Ruby ${{ matrix.ruby-version }}
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.7
bundler: 1
ruby-version: 3.2
bundler-cache: true

- name: Install rubocop
run: gem install rubocop -v "~>1.63"

- name: Run rubocop
run: bundle exec rubocop --parallel --fail-level R
run: rubocop --parallel --fail-level R
15 changes: 8 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Test
on:
push:
branches:
- master
- "*"

pull_request:
branches:
Expand All @@ -18,29 +18,30 @@ jobs:
timeout-minutes: 15

strategy:
fail-fast: false
matrix:
ruby-version: [2.3, 2.4, 2.5, 2.6, 2.7, 3.0]
ruby-version: [2.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3]

steps:
- name: Cancel previous runs
uses: styfle/cancel-workflow-action@0.5.0
uses: styfle/cancel-workflow-action@0.12.1
with:
access_token: ${{ github.token }}

- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Ruby ${{ matrix.ruby-version }}
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler: 1

- uses: actions/cache@v2
- uses: actions/cache@v4
with:
path: vendor/bundle
key: ${{ runner.os }}-bundle-${{ matrix.ruby-version }}-v2-${{ hashFiles('rails-data-migrations.gemspec', 'Appraisals') }}
key: ${{ runner.os }}-bundle-${{ matrix.ruby-version }}-v3-${{ hashFiles('rails-data-migrations.gemspec', 'Appraisals') }}
restore-keys: |
${{ runner.os }}-bundle-${{ matrix.ruby-version }}-v2-
${{ runner.os }}-bundle-${{ matrix.ruby-version }}-v3-

- name: Install dependencies
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
/.idea/
spec/db/
gemfiles/
vendor/
*.gem
19 changes: 18 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ AllCops:
Exclude:
- 'vendor/**/*'
- 'gemfiles/**/*'
TargetRubyVersion: 2.7

Layout/TrailingBlankLines:
Gemspec/RequiredRubyVersion:
Enabled: false

Layout/TrailingEmptyLines:
Enabled: false

Metrics/AbcSize:
Enabled: false

Metrics/BlockLength:
Expand All @@ -12,8 +19,18 @@ Metrics/BlockLength:
Metrics/LineLength:
Max: 120

Metrics/MethodLength:
Enabled: false

Naming/FileName:
Exclude:
- lib/rails-data-migrations.rb

Style/Documentation:
Enabled: false

Style/HashSyntax:
Enabled: false

Style/NumericLiterals:
Enabled: false
18 changes: 18 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

ruby_version = Gem::Version.new(RUBY_VERSION)

if ruby_version < Gem::Version.new('2.7.0')
Expand All @@ -18,19 +20,35 @@ if ruby_version < Gem::Version.new('3.0.0')

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'
gem 'sqlite3', '~> 1.4.0'
end
end
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in activerecord-data-migrations.gemspec
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Rails Data Migrations

![Build](https://github.com/OffgridElectric/rails-data-migrations/workflows/Test/badge.svg?branch=master)
![Rubocop](https://github.com/OffgridElectric/rails-data-migrations/workflows/Rubocop/badge.svg?branch=master)
[![Build](https://github.com/anjlab/rails-data-migrations/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/anjlab/rails-data-migrations/actions/workflows/test.yml)
[![Rubocop](https://github.com/anjlab/rails-data-migrations/actions/workflows/checkstyle.yml/badge.svg?branch=master)](https://github.com/anjlab/rails-data-migrations/actions/workflows/checkstyle.yml)
[![Gem Version](https://badge.fury.io/rb/rails-data-migrations.svg)](https://badge.fury.io/rb/rails-data-migrations)

## Why?
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

Expand Down
2 changes: 2 additions & 0 deletions lib/active_record/data_migration.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rails/version'

module ActiveRecord
Expand Down
4 changes: 3 additions & 1 deletion lib/generators/data_migration_generator.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

require 'rails/generators'
require 'rails-data-migrations'

class DataMigrationGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
source_root File.expand_path('templates', __dir__)

def create_migration_file
migration_file_name =
Expand Down
10 changes: 4 additions & 6 deletions lib/generators/templates/data_migration_generator.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# typed: strict
# typed: true
# frozen_string_literal: true

class ClassName < ActiveRecord::DataMigration
extend T::Sig

disable_ddl_transaction!

sig { void }
def up
Rails.logger.info('starting TODO-message')
# Rails.logger.info('starting TODO-message')

# put your code to modify data and output results here

Rails.logger.info('finished TODO-message')
# Rails.logger.info('finished TODO-message')
end
end
4 changes: 2 additions & 2 deletions lib/rails-data-migrations.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# rubocop:disable Naming/FileName
# frozen_string_literal: true

require 'rails'
require 'active_record'
require 'active_record/data_migration'
require 'rails_data_migrations/version'
require 'rails_data_migrations/log_entry'
require 'rails_data_migrations/migrator'
require 'rails_data_migrations/railtie'
# rubocop:enable Naming/FileName
35 changes: 29 additions & 6 deletions lib/rails_data_migrations/log_entry.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
# frozen_string_literal: true

module RailsDataMigrations
class LogEntry < ::ActiveRecord::SchemaMigration
class << self
def table_name
ActiveRecord::Base.table_name_prefix + ENV.fetch('DATA_MIGRATIONS_TABLE_NAME', 'data_migrations') + ActiveRecord::Base.table_name_suffix
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}"
end

def index_name
"#{table_name_prefix}#{ENV.fetch('DATA_MIGRATIONS_INDEX_NAME', 'unique_data_migrations')}#{table_name_suffix}"
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

def index_name
"#{table_name_prefix}#{ENV.fetch('DATA_MIGRATIONS_INDEX_NAME', 'unique_data_migrations')}#{table_name_suffix}"
::ActiveRecord::Base.connection.schema_migration.create_table
end
end
end
end
Expand Down
24 changes: 21 additions & 3 deletions lib/rails_data_migrations/migrator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module RailsDataMigrations
class Migrator < ::ActiveRecord::Migrator
MIGRATOR_SALT = 2053462855
Expand Down Expand Up @@ -46,8 +48,16 @@ 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_6_0?
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
Expand All @@ -61,12 +71,20 @@ def list_pending_migrations
already_migrated = get_all_versions
list_migrations.reject { |m| already_migrated.include?(m.version) }
else
open(migrations_path).pending_migrations
open(migrations_path).pending_migrations # rubocop:disable Security/Open
end
end

def run_migration(direction, migrations_path, version)
if rails_6_0?
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
Expand Down
2 changes: 2 additions & 0 deletions lib/rails_data_migrations/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rails'

module RailsDataMigrations
Expand Down
4 changes: 3 additions & 1 deletion lib/rails_data_migrations/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module RailsDataMigrations
VERSION = '1.2.1'.freeze
VERSION = '1.3.0.1'
end
6 changes: 5 additions & 1 deletion lib/tasks/data_migrations.rake
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# frozen_string_literal: true

require 'rake'

namespace :data do
def apply_single_migration(direction, version)
raise 'VERSION is required' unless version

RailsDataMigrations::Migrator.run_migration(
direction,
RailsDataMigrations::Migrator.migrations_path,
Expand Down Expand Up @@ -42,7 +45,8 @@ namespace :data do
desc 'Skip single data migration using VERSION'
task skip: :init_migration do
version = ENV['VERSION'].to_i
raise 'VERSION is required' unless version > 0
raise 'VERSION is required' unless version.positive?

if RailsDataMigrations::LogEntry.where(version: version).any?
puts "data migration #{version} was already applied."
else
Expand Down
7 changes: 4 additions & 3 deletions rails-data-migrations.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
lib = File.expand_path('../lib', __FILE__)
# frozen_string_literal: true

lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'rails_data_migrations/version'

Expand All @@ -17,12 +19,11 @@ 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.add_runtime_dependency 'rails', '>= 4.0.0'

spec.add_development_dependency 'appraisal', '~> 2.1'
spec.add_development_dependency 'rake', '>= 12.3.3'
spec.add_development_dependency 'rspec', '3.5.0'
spec.add_development_dependency 'rubocop', '0.52.1'
spec.add_development_dependency 'sqlite3', '~> 1.3'
end
4 changes: 3 additions & 1 deletion spec/data_migrations_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe RailsDataMigrations do
Expand Down Expand Up @@ -44,7 +46,7 @@
end

def load_rake_rasks
load File.expand_path('../../lib/tasks/data_migrations.rake', __FILE__)
load File.expand_path('../lib/tasks/data_migrations.rake', __dir__)
Rake::Task.define_task(:environment)
end

Expand Down
Empty file.
Loading