Skip to content
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

Upstream and allow engines #2

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9dda061
Delegate to anonymous subclass of AR::SchemaMigration
defsprite Sep 5, 2022
60fc88d
Bump version
ilyakatz Oct 24, 2022
3743ddd
Merge pull request #229 from ilyakatz/foxondo-fix-eager-schema-migration
ilyakatz Oct 24, 2022
0a27cb0
Add delegation to exists? for use by third parties
defsprite Nov 14, 2022
04d3042
Update docs for 8.3.0
ilyakatz Dec 7, 2022
c00488a
Merge pull request #234 from ilyakatz/foxondo-exists
ilyakatz Dec 7, 2022
6358268
Avoid globally accessible functions for all rake tasks to prevent col…
berniechiu Feb 27, 2022
88d4737
Bump version and description
ilyakatz Dec 10, 2022
ee1e0d1
Merge pull request #235 from ilyakatz/berniechiu-fix/avoid-global-fun…
ilyakatz Dec 10, 2022
a0b638a
Fix wrong module class was called
berniechiu Dec 12, 2022
0285b50
Bump version
ilyakatz Dec 12, 2022
19897f5
Merge pull request #238 from ilyakatz/berniechiu-fix/missing-class
ilyakatz Dec 12, 2022
e13667b
Fix current data version lookup in db:migrate:with_data
reidab Dec 13, 2022
e767299
Bump version
ilyakatz Dec 14, 2022
c8bbac3
Merge pull request #240 from ilyakatz/reidab-patch-1
ilyakatz Dec 14, 2022
0351045
[#224] Support custom data migration template
bazay Nov 21, 2022
eb6da04
Add DataMigrate.root
bazay Nov 21, 2022
759a1f8
Raise error if provided file path cannot be found
bazay Nov 22, 2022
92891bb
Bump version
ilyakatz Jan 2, 2023
10229cf
Merge pull request #242 from ilyakatz/bazay-custom-template
ilyakatz Jan 2, 2023
8aa2efd
Pusg to rubygems from main
ilyakatz Jan 2, 2023
b159001
Fix #220
Antronin Jan 4, 2023
3b15bdd
Merge pull request #243 from Antronin/patch-1
ilyakatz Jan 4, 2023
9aefda5
feat: fix generation when engines define data migrations path
gustavodiel Sep 23, 2022
9d81103
feat: support engines
gustavodiel Sep 23, 2022
73ce67b
Merge branch 'master' of github.com:trusted/data-migrate
chrisblatchley Jan 31, 2023
3807de4
fix: iterate through migration dirs
chrisblatchley Jan 31, 2023
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/gempush.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Ruby Gem
on:
push:
branches:
- master
- main

jobs:
build:
Expand Down
16 changes: 16 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Changelog

## 8.5.0

Allow custom templates [bazay](https://github.com/bazay)

## 8.4.0

Avoid Globally Accessible Functions for All Rake Tasks [berniechiu](https://github.com/berniechiu)

## 8.3.0

Add delegation to exists? for use by third parties [foxondo](https://github.com/foxondo)

## 8.2.0

Delegate to anonymous subclass of AR::SchemaMigration [foxondo](https://github.com/foxondo)

## 8.1.1

Revert 8.1.0 changes
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ You can override this setting in `config/initializers/data_migrate.rb`
```ruby
DataMigrate.configure do |config|
config.data_migrations_path = 'db/awesomepath/'
config.data_template_path = Rails.root.join("lib", "awesomepath", "custom_data_migration.rb")
config.db_configuration = {
'host' => '127.0.0.1',
'database' => 'awesome_database',
Expand Down
9 changes: 0 additions & 9 deletions data_migrate.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,4 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.post_install_message = <<-POST_INSTALL_MESSAGE
#{"*" * 80}
data-migrate: IMPORTANT: Breaking change introduced for migrations from v2.

Failure to run the migration can have serious consequences.
See Readme for more info.
#{"*" * 80}
POST_INSTALL_MESSAGE
end
3 changes: 3 additions & 0 deletions lib/data_migrate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@
end

module DataMigrate
def self.root
File.dirname(__FILE__)
end
end
13 changes: 11 additions & 2 deletions lib/data_migrate/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ def config
end

class Config
attr_accessor :data_migrations_gen_path, :data_migrations_path, :db_configuration, :spec_name
attr_accessor :data_migrations_path, :data_migrations_gen_path, :data_template_path, :db_configuration, :spec_name

DEFAULT_DATA_TEMPLATE_PATH = "data_migration.rb"

def initialize
@data_migrations_path = "db/data/"
@data_migrations_gen_path = "db/data/"
@data_migrations_gen_path = "db/data"
@data_template_path = DEFAULT_DATA_TEMPLATE_PATH
@db_configuration = nil
@spec_name = nil
end

def data_template_path=(value)
@data_template_path = value.tap do |path|
raise ArgumentError, "File not found: '#{path}'" unless path == DEFAULT_DATA_TEMPLATE_PATH || File.exists?(path)
end
end
end
end
10 changes: 6 additions & 4 deletions lib/data_migrate/data_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ def migrated

def versions
@versions ||= begin
versions = []
Dir.foreach(DataMigrate::DataMigrator.full_migrations_path) do |file|
match_data = DataMigrate::DataMigrator.match(file)
versions << match_data[1].to_i if match_data
versions = Set.new
DataMigrate::DataMigrator.migrations_paths.each do |path|
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this might be changing the list from an expanded path to a relative path. It might be an issue if running the data_migrate command from somewhere other than Rails.root ? Pretty edge case, would not consider this a blocker for deployment, but maybe something the upstream folks will be concerned about 😸

Dir.foreach(path) do |file|
match_data = DataMigrate::DataMigrator.match(file)
versions << match_data[1].to_i if match_data
end
end
versions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set and Array have pretty different methods available. If it doesn't break anything, I think changing to Set here is an improvement since it removes duplicates. But if we wanted to minimize changes to method signatures, we could put a .to_a here so the method returns the same type as it did before.

end
Expand Down
15 changes: 7 additions & 8 deletions lib/data_migrate/data_schema_migration.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
module DataMigrate
class DataSchemaMigration < ::ActiveRecord::SchemaMigration

class DataSchemaMigration
class << self
def table_name
ActiveRecord::Base.table_name_prefix + 'data_migrations' + ActiveRecord::Base.table_name_suffix
end
delegate :table_name, :primary_key, :create_table, :normalized_versions, :create, :create!, :table_exists?, :exists?, :where, to: :instance

def primary_key
"version"
def instance
@instance ||= Class.new(::ActiveRecord::SchemaMigration) do
define_singleton_method(:table_name) { ActiveRecord::Base.table_name_prefix + 'data_migrations' + ActiveRecord::Base.table_name_suffix }
define_singleton_method(:primary_key) { "version" }
end
end
end
end
end

35 changes: 34 additions & 1 deletion lib/data_migrate/database_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,40 @@ def check_schema_file(filename)
Kernel.abort message
end
end

def pending_migrations
sort_migrations(
pending_schema_migrations,
pending_data_migrations
)
end

def sort_migrations set_1, set_2=nil
migrations = set_1 + (set_2 || [])
migrations.sort{|a,b| sort_string(a) <=> sort_string(b)}
end

def sort_string migration
"#{migration[:version]}_#{migration[:kind] == :data ? 1 : 0}"
end

def data_migrations_path
::DataMigrate.config.data_migrations_path
end

def run_migration(migration, direction)
if migration[:kind] == :data
::ActiveRecord::Migration.write("== %s %s" % ['Data', "=" * 71])
::DataMigrate::DataMigrator.run(direction, data_migrations_path, migration[:version])
else
::ActiveRecord::Migration.write("== %s %s" % ['Schema', "=" * 69])
::DataMigrate::SchemaMigration.run(
direction,
::DataMigrate::SchemaMigration.migrations_paths,
migration[:version]
)
end
end
end

# This overrides ActiveRecord::Tasks::DatabaseTasks
Expand Down Expand Up @@ -121,6 +155,5 @@ def self.past_migrations(sort = nil)

sort == "asc" ? sort_migrations(migrations) : sort_migrations(migrations).reverse
end

end
end
2 changes: 1 addition & 1 deletion lib/data_migrate/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module DataMigrate
VERSION = "8.1.1".freeze
VERSION = "8.5.0".freeze
end
17 changes: 15 additions & 2 deletions lib/generators/data_migrate.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
require 'rails/generators/named_base'

module DataMigrate
module Generators
class DataMigrationGenerator < Rails::Generators::NamedBase #:nodoc:
def self.source_root
@_data_migrate_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), generator_name, 'templates'))
class << self
def source_root
build_data_migrate_source_root
end

private

def build_data_migrate_source_root
if DataMigrate.config.data_template_path == DataMigrate::Config::DEFAULT_DATA_TEMPLATE_PATH
File.expand_path(File.join(File.dirname(__FILE__), generator_name, 'templates'))
else
File.expand_path(File.dirname(DataMigrate.config.data_template_path))
end
end
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/generators/data_migration/data_migration_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DataMigrationGenerator < Rails::Generators::NamedBase

def create_data_migration
set_local_assigns!
migration_template "data_migration.rb", data_migrations_file_path
migration_template template_path, data_migrations_file_path
end

protected
Expand All @@ -26,6 +26,10 @@ def set_local_assigns!
end
end

def template_path
DataMigrate.config.data_template_path
end

def migration_base_class_name
"ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]"
end
Expand Down
45 changes: 42 additions & 3 deletions spec/data_migrate/config_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
require "spec_helper"

describe DataMigrate::Config do

it "sets default data_migrations_path path", :no_override do
expect(DataMigrate.config.data_migrations_path).to eq "db/data/"
end

it "sets default data_template_path path", :no_override do
expect(DataMigrate.config.data_template_path).to eq DataMigrate::Config::DEFAULT_DATA_TEMPLATE_PATH
end

describe "data migration path configured" do
subject { DataMigrate.config.data_migrations_path }
before do
@before = DataMigrate.config.data_migrations_path
DataMigrate.configure do |config|
Expand All @@ -20,8 +24,43 @@
end
end

it do
expect(DataMigrate.config.data_migrations_path).to eq "db/awesome/"
it "equals the custom data migration path" do
is_expected.to eq "db/awesome/"
end
end

describe "data template path configured" do
subject { DataMigrate.config.data_template_path }

before do
@before = DataMigrate.config.data_template_path
DataMigrate.configure do |config|
config.data_template_path = data_template_path
end
end

let(:data_template_path) do
File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb")
end

after do
DataMigrate.configure do |config|
config.data_template_path = @before
end
end

it "equals the custom data template path" do
is_expected.to eq data_template_path
end

context "when path does not exist" do
subject { DataMigrate.config.data_template_path = invalid_path }

let(:invalid_path) { "lib/awesome/templates/data_migration.rb" }

it "checks that file exists on setting config var" do
expect { subject }.to raise_error { ArgumentError.new("File not found: '#{data_template_path}'") }
end
end
end
end
53 changes: 47 additions & 6 deletions spec/generators/data_migration/data_migration_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
require 'generators/data_migration/data_migration_generator'

describe DataMigrate::Generators::DataMigrationGenerator do
let(:subject) { DataMigrate::Generators::DataMigrationGenerator }
subject { DataMigrate::Generators::DataMigrationGenerator }

describe :next_migration_number do
it "next migration" do
Timecop.freeze("2016-12-03 22:15:26 -0800") do
Expand All @@ -19,14 +20,18 @@
end

describe :migration_base_class_name do
let(:subject) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }
subject { generator.send(:migration_base_class_name) }

let(:generator) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }

it "returns the correct base class name" do
expect(subject.send(:migration_base_class_name)).to eq("ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]")
is_expected.to eq("ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]")
end
end

describe :create_data_migration do
let(:subject) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }
subject { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }

let(:data_migrations_file_path) { 'abc/my_migration.rb' }

context 'when custom data migrations path has a trailing slash' do
Expand All @@ -35,7 +40,7 @@
end

it 'returns correct file path' do
expect(subject).to receive(:migration_template).with(
is_expected.to receive(:migration_template).with(
'data_migration.rb', data_migrations_file_path
)

Expand All @@ -49,12 +54,48 @@
end

it 'returns correct file path' do
expect(subject).to receive(:migration_template).with(
is_expected.to receive(:migration_template).with(
'data_migration.rb', data_migrations_file_path
)

subject.create_data_migration
end
end
end

describe ".source_root" do
subject { described_class.source_root }

let(:default_source_root) do
File.expand_path(
File.dirname(File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb"))
)
end

it { is_expected.to eq default_source_root }

context "when DateMigrate.config.data_template_path is set" do
before do
@before = DataMigrate.config.data_template_path
DataMigrate.configure do |config|
config.data_template_path = data_template_path
end
end

let(:data_template_path) do
File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb")
end
let(:expected_source_root) { File.dirname(data_template_path) }

after do
DataMigrate.configure do |config|
config.data_template_path = @before
end
end

it "reads directory from config data template path" do
is_expected.to eq expected_source_root
end
end
end
end
Loading