2013-06-15 17:29:20 +08:00
|
|
|
require "digest/sha1"
|
|
|
|
|
2015-05-25 23:59:00 +08:00
|
|
|
################################################################################
|
|
|
|
# backfill_shas #
|
|
|
|
################################################################################
|
|
|
|
|
2013-06-15 17:29:20 +08:00
|
|
|
task "uploads:backfill_shas" => :environment do
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do |db|
|
|
|
|
puts "Backfilling #{db}"
|
|
|
|
Upload.select([:id, :sha, :url]).find_each do |u|
|
|
|
|
if u.sha.nil?
|
|
|
|
putc "."
|
|
|
|
path = "#{Rails.root}/public/#{u.url}"
|
|
|
|
sha = Digest::SHA1.file(path).hexdigest
|
|
|
|
begin
|
|
|
|
Upload.update_all ["sha = ?", sha], ["id = ?", u.id]
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
# not a big deal if we've got a few duplicates
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
puts "done"
|
|
|
|
end
|
2014-06-24 21:35:15 +08:00
|
|
|
|
2015-05-25 23:59:00 +08:00
|
|
|
################################################################################
|
|
|
|
# migrate_from_s3 #
|
|
|
|
################################################################################
|
|
|
|
|
2014-06-24 21:35:15 +08:00
|
|
|
task "uploads:migrate_from_s3" => :environment do
|
2015-05-25 23:59:00 +08:00
|
|
|
require "file_store/local_store"
|
|
|
|
require "file_helper"
|
2014-06-24 21:35:15 +08:00
|
|
|
|
2015-05-26 22:39:41 +08:00
|
|
|
max_file_size_kb = [SiteSetting.max_image_size_kb, SiteSetting.max_attachment_size_kb].max.kilobytes
|
2014-06-24 21:35:15 +08:00
|
|
|
local_store = FileStore::LocalStore.new
|
|
|
|
|
2015-03-19 01:23:55 +08:00
|
|
|
puts "Deleting all optimized images..."
|
|
|
|
puts
|
|
|
|
|
|
|
|
OptimizedImage.destroy_all
|
|
|
|
|
2014-06-24 21:35:15 +08:00
|
|
|
puts "Migrating uploads from S3 to local storage"
|
|
|
|
puts
|
|
|
|
|
2015-05-12 15:37:48 +08:00
|
|
|
Upload.find_each do |upload|
|
2014-06-24 21:35:15 +08:00
|
|
|
|
|
|
|
# remove invalid uploads
|
|
|
|
if upload.url.blank?
|
|
|
|
upload.destroy!
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
# no need to download an upload twice
|
|
|
|
if local_store.has_been_uploaded?(upload.url)
|
2015-05-25 23:59:00 +08:00
|
|
|
putc "."
|
2014-06-24 21:35:15 +08:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
# try to download the upload
|
|
|
|
begin
|
|
|
|
# keep track of the previous url
|
|
|
|
previous_url = upload.url
|
|
|
|
# fix the name of pasted images
|
|
|
|
upload.original_filename = "blob.png" if upload.original_filename == "blob"
|
|
|
|
# download the file (in a temp file)
|
2015-05-26 22:39:41 +08:00
|
|
|
temp_file = FileHelper.download("http:" + previous_url, max_file_size_kb, "from_s3")
|
2014-06-24 21:35:15 +08:00
|
|
|
# store the file locally
|
|
|
|
upload.url = local_store.store_upload(temp_file, upload)
|
|
|
|
# save the new url
|
|
|
|
if upload.save
|
|
|
|
# update & rebake the posts (if any)
|
|
|
|
Post.where("raw ILIKE ?", "%#{previous_url}%").find_each do |post|
|
2015-03-19 01:23:55 +08:00
|
|
|
post.raw = post.raw.gsub(previous_url, upload.url)
|
|
|
|
post.save
|
2014-06-24 21:35:15 +08:00
|
|
|
end
|
|
|
|
|
2015-05-25 23:59:00 +08:00
|
|
|
putc "#"
|
2014-06-24 21:35:15 +08:00
|
|
|
else
|
2015-05-25 23:59:00 +08:00
|
|
|
putc "X"
|
2014-06-24 21:35:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# close the temp_file
|
|
|
|
temp_file.close! if temp_file.respond_to? :close!
|
|
|
|
rescue
|
2015-05-25 23:59:00 +08:00
|
|
|
putc "X"
|
2014-06-24 21:35:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
puts
|
|
|
|
|
|
|
|
end
|
2014-09-30 00:31:53 +08:00
|
|
|
|
2015-05-25 23:59:00 +08:00
|
|
|
################################################################################
|
|
|
|
# migrate_to_s3 #
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
task "uploads:migrate_to_s3" => :environment do
|
|
|
|
require "file_store/s3_store"
|
|
|
|
require "file_store/local_store"
|
|
|
|
|
|
|
|
ENV["RAILS_DB"] ? migrate_to_s3 : migrate_to_s3_all_sites
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrate_to_s3_all_sites
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection { migrate_to_s3 }
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrate_to_s3
|
|
|
|
# make sure s3 is enabled
|
|
|
|
if !SiteSetting.enable_s3_uploads
|
|
|
|
puts "You must enable s3 uploads before running that task"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
db = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
|
|
|
|
puts "Migrating uploads to S3 (#{SiteSetting.s3_upload_bucket}) for '#{db}'..."
|
|
|
|
|
|
|
|
# will throw an exception if the bucket is missing
|
|
|
|
s3 = FileStore::S3Store.new
|
|
|
|
local = FileStore::LocalStore.new
|
|
|
|
|
|
|
|
# Migrate all uploads
|
|
|
|
Upload.where.not(sha1: nil)
|
|
|
|
.where("url NOT LIKE '#{s3.absolute_base_url}%'")
|
|
|
|
.find_each do |upload|
|
|
|
|
# remove invalid uploads
|
|
|
|
if upload.url.blank?
|
|
|
|
upload.destroy!
|
|
|
|
next
|
|
|
|
end
|
|
|
|
# store the old url
|
|
|
|
from = upload.url
|
|
|
|
# retrieve the path to the local file
|
|
|
|
path = local.path_for(upload)
|
|
|
|
# make sure the file exists locally
|
|
|
|
if !File.exists?(path)
|
|
|
|
putc "X"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
file = File.open(path)
|
|
|
|
content_type = `file --mime-type -b #{path}`.strip
|
|
|
|
to = s3.store_upload(file, upload, content_type)
|
|
|
|
rescue
|
|
|
|
putc "X"
|
|
|
|
next
|
|
|
|
ensure
|
|
|
|
file.try(:close!) rescue nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# remap the URL
|
|
|
|
remap(from, to)
|
|
|
|
|
|
|
|
putc "."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# clean_up #
|
|
|
|
################################################################################
|
|
|
|
|
2014-09-30 00:31:53 +08:00
|
|
|
task "uploads:clean_up" => :environment do
|
|
|
|
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do |db|
|
|
|
|
puts "Cleaning up uploads and thumbnails for '#{db}'..."
|
|
|
|
|
|
|
|
if Discourse.store.external?
|
|
|
|
puts "This task only works for internal storages."
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
public_directory = "#{Rails.root}/public"
|
|
|
|
|
|
|
|
##
|
|
|
|
## DATABASE vs FILE SYSTEM
|
|
|
|
##
|
|
|
|
|
|
|
|
# uploads & avatars
|
2015-05-12 15:37:48 +08:00
|
|
|
Upload.find_each do |upload|
|
2014-09-30 00:31:53 +08:00
|
|
|
path = "#{public_directory}#{upload.url}"
|
|
|
|
if !File.exists?(path)
|
|
|
|
upload.destroy rescue nil
|
|
|
|
putc "#"
|
|
|
|
else
|
|
|
|
putc "."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# optimized images
|
2015-05-12 15:37:48 +08:00
|
|
|
OptimizedImage.find_each do |optimized_image|
|
2014-09-30 00:31:53 +08:00
|
|
|
path = "#{public_directory}#{optimized_image.url}"
|
|
|
|
if !File.exists?(path)
|
|
|
|
optimized_image.destroy rescue nil
|
|
|
|
putc "#"
|
|
|
|
else
|
|
|
|
putc "."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
## FILE SYSTEM vs DATABASE
|
|
|
|
##
|
|
|
|
|
|
|
|
uploads_directory = "#{public_directory}/uploads/#{db}"
|
|
|
|
|
|
|
|
# avatars (no avatar should be stored in that old directory)
|
|
|
|
FileUtils.rm_rf("#{uploads_directory}/avatars") rescue nil
|
|
|
|
|
|
|
|
# uploads
|
|
|
|
Dir.glob("#{uploads_directory}/*/*.*").each do |f|
|
|
|
|
url = "/uploads/#{db}/" << f.split("/uploads/#{db}/")[1]
|
|
|
|
if !Upload.where(url: url).exists?
|
|
|
|
FileUtils.rm(f) rescue nil
|
|
|
|
putc "#"
|
|
|
|
else
|
|
|
|
putc "."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# optimized images
|
|
|
|
Dir.glob("#{uploads_directory}/_optimized/*/*/*.*").each do |f|
|
|
|
|
url = "/uploads/#{db}/_optimized/" << f.split("/uploads/#{db}/_optimized/")[1]
|
|
|
|
if !OptimizedImage.where(url: url).exists?
|
|
|
|
FileUtils.rm(f) rescue nil
|
|
|
|
putc "#"
|
|
|
|
else
|
|
|
|
putc "."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2015-05-11 08:30:22 +08:00
|
|
|
|
2015-05-25 23:59:00 +08:00
|
|
|
################################################################################
|
|
|
|
# missing #
|
|
|
|
################################################################################
|
2015-05-11 08:30:22 +08:00
|
|
|
|
|
|
|
# list all missing uploads and optimized images
|
|
|
|
task "uploads:missing" => :environment do
|
|
|
|
|
|
|
|
public_directory = "#{Rails.root}/public"
|
|
|
|
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do |db|
|
|
|
|
|
|
|
|
if Discourse.store.external?
|
|
|
|
puts "This task only works for internal storages."
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-05-12 15:37:48 +08:00
|
|
|
Upload.find_each do |upload|
|
2015-05-11 08:30:22 +08:00
|
|
|
|
|
|
|
# could be a remote image
|
2015-05-12 15:28:43 +08:00
|
|
|
next unless upload.url =~ /^\/[^\/]/
|
2015-05-11 08:30:22 +08:00
|
|
|
|
|
|
|
path = "#{public_directory}#{upload.url}"
|
|
|
|
bad = true
|
|
|
|
begin
|
|
|
|
bad = false if File.size(path) != 0
|
|
|
|
rescue
|
|
|
|
# something is messed up
|
|
|
|
end
|
|
|
|
puts path if bad
|
|
|
|
end
|
|
|
|
|
2015-05-12 15:37:48 +08:00
|
|
|
OptimizedImage.find_each do |optimized_image|
|
2015-05-11 08:30:22 +08:00
|
|
|
|
|
|
|
# remote?
|
2015-05-12 15:28:43 +08:00
|
|
|
next unless optimized_image.url =~ /^\/[^\/]/
|
2015-05-11 08:30:22 +08:00
|
|
|
|
|
|
|
path = "#{public_directory}#{optimized_image.url}"
|
|
|
|
|
|
|
|
bad = true
|
|
|
|
begin
|
|
|
|
bad = false if File.size(path) != 0
|
|
|
|
rescue
|
|
|
|
# something is messed up
|
|
|
|
end
|
|
|
|
puts path if bad
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2015-05-11 18:59:50 +08:00
|
|
|
|
2015-05-25 23:59:00 +08:00
|
|
|
################################################################################
|
|
|
|
# regenerate_missing_optimized #
|
|
|
|
################################################################################
|
|
|
|
|
2015-05-11 18:59:50 +08:00
|
|
|
# regenerate missing optimized images
|
|
|
|
task "uploads:regenerate_missing_optimized" => :environment do
|
2015-05-11 22:19:16 +08:00
|
|
|
ENV["RAILS_DB"] ? regenerate_missing_optimized : regenerate_missing_optimized_all_sites
|
|
|
|
end
|
|
|
|
|
|
|
|
def regenerate_missing_optimized_all_sites
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection { regenerate_missing_optimized }
|
|
|
|
end
|
|
|
|
|
|
|
|
def regenerate_missing_optimized
|
2015-05-12 01:07:39 +08:00
|
|
|
db = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
|
|
|
|
puts "Regenerating missing optimized images for '#{db}'..."
|
2015-05-11 18:59:50 +08:00
|
|
|
|
|
|
|
if Discourse.store.external?
|
|
|
|
puts "This task only works for internal storages."
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
public_directory = "#{Rails.root}/public"
|
|
|
|
missing_uploads = Set.new
|
|
|
|
|
|
|
|
OptimizedImage.includes(:upload)
|
|
|
|
.where("LENGTH(COALESCE(url, '')) > 0")
|
|
|
|
.where("width > 0 AND height > 0")
|
|
|
|
.find_each do |optimized_image|
|
|
|
|
|
2015-05-12 01:07:39 +08:00
|
|
|
upload = optimized_image.upload
|
|
|
|
|
2015-05-12 15:28:43 +08:00
|
|
|
next unless optimized_image.url =~ /^\/[^\/]/
|
|
|
|
next unless upload.url =~ /^\/[^\/]/
|
2015-05-11 21:41:52 +08:00
|
|
|
|
2015-05-11 18:59:50 +08:00
|
|
|
thumbnail = "#{public_directory}#{optimized_image.url}"
|
2015-05-12 01:07:39 +08:00
|
|
|
original = "#{public_directory}#{upload.url}"
|
2015-05-11 18:59:50 +08:00
|
|
|
|
|
|
|
if !File.exists?(thumbnail) || File.size(thumbnail) <= 0
|
2015-05-11 23:03:48 +08:00
|
|
|
# make sure the original image exists locally
|
2015-05-12 01:07:39 +08:00
|
|
|
if (!File.exists?(original) || File.size(original) <= 0) && upload.origin.present?
|
2015-05-11 23:03:48 +08:00
|
|
|
# try to fix it by redownloading it
|
|
|
|
begin
|
2015-05-12 01:07:39 +08:00
|
|
|
downloaded = FileHelper.download(upload.origin, SiteSetting.max_image_size_kb.kilobytes, "discourse-missing", true) rescue nil
|
|
|
|
if downloaded && downloaded.size > 0
|
|
|
|
FileUtils.mkdir_p(File.dirname(original))
|
|
|
|
File.open(original, "wb") { |f| f.write(downloaded.read) }
|
|
|
|
end
|
2015-05-11 23:03:48 +08:00
|
|
|
ensure
|
|
|
|
downloaded.try(:close!) if downloaded.respond_to?(:close!)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-12 01:07:39 +08:00
|
|
|
if File.exists?(original) && File.size(original) > 0
|
2015-05-11 18:59:50 +08:00
|
|
|
FileUtils.mkdir_p(File.dirname(thumbnail))
|
2015-05-12 01:07:39 +08:00
|
|
|
OptimizedImage.resize(original, thumbnail, optimized_image.width, optimized_image.height)
|
2015-05-11 18:59:50 +08:00
|
|
|
putc "#"
|
|
|
|
else
|
2015-05-12 01:07:39 +08:00
|
|
|
missing_uploads << original
|
2015-05-11 18:59:50 +08:00
|
|
|
putc "X"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
putc "."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "", "Done"
|
|
|
|
|
|
|
|
if missing_uploads.size > 0
|
|
|
|
puts "Missing uploads:"
|
|
|
|
missing_uploads.sort.each { |u| puts u }
|
|
|
|
end
|
|
|
|
end
|
2015-05-19 18:31:51 +08:00
|
|
|
|
2015-05-25 23:59:00 +08:00
|
|
|
################################################################################
|
|
|
|
# migrate_to_new_pattern #
|
|
|
|
################################################################################
|
|
|
|
|
2015-05-19 18:31:51 +08:00
|
|
|
task "uploads:migrate_to_new_pattern" => :environment do
|
2015-06-06 00:37:34 +08:00
|
|
|
require "file_helper"
|
|
|
|
require "file_store/local_store"
|
|
|
|
|
2015-05-19 18:31:51 +08:00
|
|
|
ENV["RAILS_DB"] ? migrate_to_new_pattern : migrate_to_new_pattern_all_sites
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrate_to_new_pattern_all_sites
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection { migrate_to_new_pattern }
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrate_to_new_pattern
|
|
|
|
db = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
|
|
|
|
puts "Migrating uploads to new pattern for '#{db}'..."
|
|
|
|
migrate_uploads_to_new_pattern
|
|
|
|
|
|
|
|
puts "Migrating optimized images to new pattern for '#{db}'..."
|
|
|
|
migrate_optimized_images_to_new_pattern
|
|
|
|
|
|
|
|
puts "Done!"
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrate_uploads_to_new_pattern
|
2015-06-06 00:37:34 +08:00
|
|
|
puts "Moving uploads to new location..."
|
|
|
|
|
|
|
|
max_file_size_kb = [SiteSetting.max_image_size_kb, SiteSetting.max_attachment_size_kb].max.kilobytes
|
|
|
|
local_store = FileStore::LocalStore.new
|
2015-05-19 18:31:51 +08:00
|
|
|
|
2015-06-06 00:37:34 +08:00
|
|
|
Upload.where("LENGTH(COALESCE(url, '')) = 0").destroy_all
|
|
|
|
|
|
|
|
Upload.where("url NOT LIKE '%/original/_X/%'").find_each do |upload|
|
|
|
|
begin
|
|
|
|
successful = false
|
|
|
|
# keep track of the url
|
|
|
|
previous_url = upload.url.dup
|
|
|
|
# where is the file currently stored?
|
|
|
|
external = previous_url =~ /^\/\//
|
|
|
|
# download if external
|
|
|
|
if external
|
|
|
|
url = SiteSetting.scheme + ":" + previous_url
|
|
|
|
file = FileHelper.download(url, max_file_size_kb, "discourse", true) rescue nil
|
|
|
|
next unless file
|
|
|
|
path = file.path
|
|
|
|
else
|
|
|
|
path = local_store.path_for(upload)
|
|
|
|
next unless File.exists?(path)
|
|
|
|
end
|
|
|
|
# compute SHA if missing
|
|
|
|
if upload.sha1.blank?
|
2015-05-19 18:31:51 +08:00
|
|
|
upload.sha1 = Digest::SHA1.file(path).hexdigest
|
2015-06-06 00:37:34 +08:00
|
|
|
end
|
|
|
|
# optimize if image
|
|
|
|
if FileHelper.is_image?(File.basename(path))
|
|
|
|
ImageOptim.new.optimize_image!(path)
|
|
|
|
end
|
|
|
|
# store to new location & update the filesize
|
|
|
|
File.open(path) do |f|
|
|
|
|
upload.url = Discourse.store.store_upload(f, upload)
|
|
|
|
upload.filesize = f.size
|
2015-05-19 18:31:51 +08:00
|
|
|
upload.save
|
|
|
|
end
|
2015-06-06 00:37:34 +08:00
|
|
|
# remap the URLs
|
|
|
|
remap(previous_url, upload.url)
|
|
|
|
# remove the old file (when local)
|
|
|
|
unless external
|
|
|
|
FileUtils.rm(path, force: true) rescue nil
|
|
|
|
end
|
|
|
|
# succesfully migrated
|
|
|
|
successful = true
|
|
|
|
rescue => e
|
|
|
|
puts e.message
|
|
|
|
puts e.backtrace.join("\n")
|
|
|
|
ensure
|
|
|
|
putc successful ? '.' : 'X'
|
|
|
|
file.try(:unlink) rescue nil
|
|
|
|
file.try(:close) rescue nil
|
2015-05-19 18:31:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrate_optimized_images_to_new_pattern
|
2015-06-06 00:37:34 +08:00
|
|
|
max_file_size_kb = SiteSetting.max_image_size_kb.kilobytes
|
|
|
|
local_store = FileStore::LocalStore.new
|
|
|
|
|
|
|
|
OptimizedImage.where("LENGTH(COALESCE(url, '')) = 0").destroy_all
|
2015-05-19 18:31:51 +08:00
|
|
|
|
2015-06-06 00:37:34 +08:00
|
|
|
OptimizedImage.where("url NOT LIKE '%/original/_X/%'").find_each do |optimized_image|
|
|
|
|
begin
|
|
|
|
successful = false
|
|
|
|
# keep track of the url
|
|
|
|
previous_url = optimized_image.url.dup
|
|
|
|
# where is the file currently stored?
|
|
|
|
external = previous_url =~ /^\/\//
|
|
|
|
# download if external
|
|
|
|
if external
|
|
|
|
url = SiteSetting.scheme + ":" + previous_url
|
|
|
|
file = FileHelper.download(url, max_file_size_kb, "discourse", true) rescue nil
|
|
|
|
next unless file
|
|
|
|
path = file.path
|
|
|
|
else
|
|
|
|
path = local_store.path_for(optimized_image)
|
|
|
|
next unless File.exists?(path)
|
|
|
|
file = File.open(path)
|
|
|
|
end
|
|
|
|
# compute SHA if missing
|
|
|
|
if optimized_image.sha1.blank?
|
2015-05-19 18:31:51 +08:00
|
|
|
optimized_image.sha1 = Digest::SHA1.file(path).hexdigest
|
2015-06-06 00:37:34 +08:00
|
|
|
end
|
|
|
|
# optimize if image
|
|
|
|
ImageOptim.new.optimize_image!(path)
|
|
|
|
# store to new location & update the filesize
|
|
|
|
File.open(path) do |f|
|
|
|
|
optimized_image.url = Discourse.store.store_optimized_image(f, optimized_image)
|
2015-05-19 18:31:51 +08:00
|
|
|
optimized_image.save
|
|
|
|
end
|
2015-06-06 00:37:34 +08:00
|
|
|
# remap the URLs
|
|
|
|
remap(previous_url, optimized_image.url)
|
|
|
|
# remove the old file (when local)
|
|
|
|
unless external
|
|
|
|
FileUtils.rm(path, force: true) rescue nil
|
|
|
|
end
|
|
|
|
# succesfully migrated
|
|
|
|
successful = true
|
|
|
|
rescue => e
|
|
|
|
puts e.message
|
|
|
|
puts e.backtrace.join("\n")
|
|
|
|
ensure
|
|
|
|
putc successful ? '.' : 'X'
|
|
|
|
file.try(:unlink) rescue nil
|
|
|
|
file.try(:close) rescue nil
|
2015-05-19 18:31:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts
|
|
|
|
end
|
|
|
|
|
|
|
|
REMAP_SQL ||= "
|
|
|
|
SELECT table_name, column_name
|
|
|
|
FROM information_schema.columns
|
|
|
|
WHERE table_schema = 'public'
|
|
|
|
AND is_updatable = 'YES'
|
|
|
|
AND (data_type LIKE 'char%' OR data_type LIKE 'text%')
|
|
|
|
ORDER BY table_name, column_name
|
|
|
|
"
|
|
|
|
|
|
|
|
def remap(from, to)
|
|
|
|
connection ||= ActiveRecord::Base.connection.raw_connection
|
|
|
|
remappable_columns ||= connection.async_exec(REMAP_SQL).to_a
|
|
|
|
|
|
|
|
remappable_columns.each do |rc|
|
|
|
|
table_name = rc["table_name"]
|
|
|
|
column_name = rc["column_name"]
|
|
|
|
begin
|
|
|
|
connection.async_exec("
|
|
|
|
UPDATE #{table_name}
|
|
|
|
SET #{column_name} = REPLACE(#{column_name}, $1, $2)
|
|
|
|
WHERE #{column_name} IS NOT NULL
|
|
|
|
AND #{column_name} <> REPLACE(#{column_name}, $1, $2)", [from, to])
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|