Skip to content
Open
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
38 changes: 38 additions & 0 deletions lib/compressed_fax.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'bundler'
Bundler.require

credentials = {
adapter: 'mysql2',
encoding: 'utf8',
collation: 'utf8_general_ci',
host: ENV['HOST'] || 'localhost',
port: ENV['PORT'] || 3306,
database: ENV['DB'],
username: ENV['USER'],
password: ENV['PASS']
}

ActiveRecord::Base.establish_connection(credentials)
ActiveRecord::Base.logger = Logger.new('log/sql.log')
logger = Logger.new('log/batch.log')

class Address < ActiveRecord::Base; end


def compressed!(logger)
Address.all.each do |addr|
Copy link
Owner Author

Choose a reason for hiding this comment

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

全行ロードするのはメモリ食い過ぎ

if fax?(addr.fax)
addr.update_attributes!(compress_fax: addr.fax.gsub('-', ''))
Copy link
Owner Author

Choose a reason for hiding this comment

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

1行ずつupdateするのは効率悪い

else
logger.error "Found id: #{addr.id}, fax: #{addr.fax}"
end
end
end

def fax?(fax_no)
!(fax_no =~ /^([\d|-]+)$/).nil?
end

logger.info 'Start compressed!'
compressed!(logger)
logger.info 'End compressed!'