8000 Rails 5 support by orangewolf · Pull Request #6 · mokolabs/trucker · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Rails 5 support #6

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ Use Trucker to migrate legacy data into your Rails app.

## Installation

1. Install the trucker gem
```bash
sudo gem install trucker
1. Add the trucker gem in to your Gemfile
```ruby
gem 'trucker'
```

2. Add trucker to your `config.gem` block in `environment.rb`.
```ruby
config.gem "trucker"
2. Bundle
```bash
bundle install
```

3. Generate the basic trucker files
```bash
script/generate truck
rails g truck
```

This will do the following things:
Expand Down
4 changes: 2 additions & 2 deletions VERSION.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
:patch: 1
:build:
:major: 0
:minor: 5
:major: 1
:minor: 0
11 changes: 0 additions & 11 deletions generators/truck/templates/legacy_model.erb
View file Open in desktop

This file was deleted.

36 changes: 0 additions & 36 deletions generators/truck/truck_generator.rb

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Mostly pinched from http://github.com/ryanb/nifty-generators/tree/master

Rails::Generator::Commands::Base.class_eval do
Rails::Generators::Actions.class_eval do
def file_contains?(relative_destination, line)
File.read(destination_path(relative_destination)).include?(line)
end
end

Rails::Generator::Commands::Create.class_eval do
def insert_before(file, line, stop='^(class|module) .+$')
logger.insert "#{line} into #{file}"
unless options[:pretend] || file_contains?(file, line)
Expand Down Expand Up @@ -37,9 +35,6 @@ def append(file, line)
end
end

end

Rails::Generator::Commands::Destroy.class_eval do
def insert_before(file, line, stop='')
logger.remove "#{line} from #{file}"
unless options[:pretend]
Expand All @@ -58,9 +53,6 @@ def append(file, line)
logger.insert "added legacy adapter to end of database.yml"
end

end

Rails::Generator::Commands::List.class_eval do
def insert_before(file, line, stop='')
logger.insert "#{line} into #{file}"
end
Expand Down
11 changes: 11 additions & 0 deletions lib/generators/truck/templates/legacy_model.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Legacy<%= config[:model_name].classify %> < LegacyBase
self.table_name = "<%= config[:model_name].pluralize %>"

def map
{
:field_one => self.old_field_one.squish,
:field_two => self.old_field_two.squish
}
end

end
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ include Trucker
namespace :db do
namespace :migrate do

<%- unless legacy_models.size > 0 -%>
<%- unless config[:legacy_models].size > 0 -%>
# desc 'Migrates products'
# task :products => :environment do
# migrate :products
# end
<%- end -%>

<%- legacy_models.each do |model_name| -%>
<%- config[:legacy_models].each do |model_name| -%>
desc 'Migrates <%= model_name.pluralize.downcase %>'
task :<%= model_name.pluralize.downcase %> => :environment do
Trucker.migrate :<%= model_name.pluralize.downcase %>
Expand All @@ -20,4 +20,4 @@ namespace :db do
<%- end -%>

end
end
end
33 changes: 33 additions & 0 deletions lib/generators/truck/truck_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require File.expand_path(File.dirname(__FILE__) + "/lib/insert_commands.rb")

class TruckGenerator < Rails::Generators::Base
source_root File.expand_path("../templates", __FILE__)
def manifest
@legacy_models = Dir.glob(Rails.root.join('app/models/*.rb')).collect { |model_path| File.basename(model_path).gsub('.rb', '') }
empty_directory 'app/models/legacy'
copy_file 'legacy_base.rb', 'app/models/legacy/legacy_base.rb'

@legacy_models.each do |model_name|
template 'legacy_model.erb', "app/models/legacy/legacy_#{model_name.downcase}.rb", { :model_name => model_name }
end

empty_directory 'lib/tasks'
template 'legacy_task.erb', 'lib/tasks/legacy.rake', { :legacy_models => @legacy_models }

snippet = <<EOS
legacy:
adapter: mysql2
database: #{Rails.root.to_s.split('/').last}_legacy
encoding: utf8
username:
password:
EOS

append_to_file "config/database.yml", snippet

snippet = ' config.autoload_paths << "#{Rails.root}/app/models/legacy"'
insert_after "config/application.rb", snippet, " # -- all .rb files in that directory are automatically loaded.\n"

end

end
8 changes: 2 additions & 6 deletions lib/trucker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ def self.migrate(name, options={})
# Set import counter
counter = 0
counter += offset_for_records if offset_for_records
total_records = "Legacy#{model}".constantize.find(:all).size
total_records = "Legacy#{model}".constantize.count

# Start import
"Legacy#{model}".constantize.find(:all, with(options)).each do |record|
"Legacy#{model}".constantize.limit(number_of_records).offset(offset_for_records).each do |record|
counter += 1
puts status + " (#{counter}/#{total_records})"
record.migrate
Expand All @@ -35,10 +35,6 @@ def self.migrate(name, options={})

protected

def self.with(options={})
{:limit => number_of_records, :offset => offset_for_records}.merge(options)
end

def self.number_of_records
nil || ENV['limit'].to_i if ENV['limit'].to_i > 0
end
Expand Down
0