mirror of
https://github.com/discourse/discourse.git
synced 2025-10-03 17:21:20 +08:00
DEV: Import uploads
from UploadsDB (#33259)
This commit is contained in:
parent
a51fb04864
commit
95f4460394
7 changed files with 100 additions and 11 deletions
|
@ -1,2 +1,3 @@
|
||||||
intermediate_db: /shared/import/intermediate.db
|
intermediate_db: /shared/import/intermediate.db
|
||||||
mappings_db: /shared/import/mappings.db
|
mappings_db: /shared/import/mappings.db
|
||||||
|
uploads_db: /shared/import/uploads.db
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "extralite"
|
|
||||||
|
|
||||||
module Migrations::CLI
|
module Migrations::CLI
|
||||||
class ImportCommand
|
class ImportCommand
|
||||||
def initialize(options)
|
def initialize(options)
|
||||||
|
|
|
@ -9,6 +9,10 @@ module Migrations::CLI
|
||||||
def execute
|
def execute
|
||||||
puts "Starting uploads..."
|
puts "Starting uploads..."
|
||||||
|
|
||||||
|
::Migrations.load_rails_environment(quiet: true)
|
||||||
|
|
||||||
|
adjust_db_pool_size
|
||||||
|
|
||||||
validate_settings_file!
|
validate_settings_file!
|
||||||
settings = load_settings
|
settings = load_settings
|
||||||
|
|
||||||
|
@ -21,7 +25,7 @@ module Migrations::CLI
|
||||||
|
|
||||||
def load_settings
|
def load_settings
|
||||||
settings = ::Migrations::SettingsParser.parse!(@options.settings)
|
settings = ::Migrations::SettingsParser.parse!(@options.settings)
|
||||||
merge_settings_from_cli_args!(@options, settings)
|
merge_settings_from_cli_args!(settings)
|
||||||
|
|
||||||
settings
|
settings
|
||||||
end
|
end
|
||||||
|
@ -36,5 +40,20 @@ module Migrations::CLI
|
||||||
|
|
||||||
raise ::Migrations::NoSettingsFound, "Settings file not found: #{path}" if !File.exist?(path)
|
raise ::Migrations::NoSettingsFound, "Settings file not found: #{path}" if !File.exist?(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def adjust_db_pool_size
|
||||||
|
max_db_connections = DB.query_single("SHOW max_connections").first.to_i
|
||||||
|
current_size = ActiveRecord::Base.connection_pool.size
|
||||||
|
|
||||||
|
if current_size < max_db_connections
|
||||||
|
db_config = ActiveRecord::Base.connection_db_config.configuration_hash.dup
|
||||||
|
db_config[:pool] = max_db_connections
|
||||||
|
ActiveRecord::Base.establish_connection(db_config)
|
||||||
|
|
||||||
|
puts "Adjusted DB pool size from #{current_size} to #{ActiveRecord::Base.connection_pool.size}"
|
||||||
|
else
|
||||||
|
puts "DB pool size: #{current_size} (max connections: #{max_db_connections})"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,6 +8,7 @@ module Migrations::Importer
|
||||||
@shared_data = SharedData.new(@discourse_db)
|
@shared_data = SharedData.new(@discourse_db)
|
||||||
|
|
||||||
attach_mappings_db(config[:mappings_db])
|
attach_mappings_db(config[:mappings_db])
|
||||||
|
attach_uploads_db(config[:uploads_db])
|
||||||
end
|
end
|
||||||
|
|
||||||
def start
|
def start
|
||||||
|
@ -24,12 +25,16 @@ module Migrations::Importer
|
||||||
private
|
private
|
||||||
|
|
||||||
def attach_mappings_db(db_path)
|
def attach_mappings_db(db_path)
|
||||||
# ::Migrations::Database.reset!(db_path)
|
migrate_and_attach(db_path, ::Migrations::Database::MAPPINGS_DB_SCHEMA_PATH, "mapped")
|
||||||
::Migrations::Database.migrate(
|
end
|
||||||
db_path,
|
|
||||||
migrations_path: ::Migrations::Database::MAPPINGS_DB_SCHEMA_PATH,
|
def attach_uploads_db(db_path)
|
||||||
)
|
migrate_and_attach(db_path, ::Migrations::Database::UPLOADS_DB_SCHEMA_PATH, "files")
|
||||||
@intermediate_db.execute("ATTACH DATABASE ? AS mapped", db_path)
|
end
|
||||||
|
|
||||||
|
def migrate_and_attach(db_path, schema_path, alias_name)
|
||||||
|
::Migrations::Database.migrate(db_path, migrations_path: schema_path)
|
||||||
|
@intermediate_db.execute("ATTACH DATABASE ? AS #{alias_name}", db_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
def step_classes
|
def step_classes
|
||||||
|
|
|
@ -3,5 +3,6 @@
|
||||||
module Migrations::Importer
|
module Migrations::Importer
|
||||||
module MappingType
|
module MappingType
|
||||||
USERS = 1
|
USERS = 1
|
||||||
|
UPLOADS = 10
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
65
migrations/lib/importer/steps/uploads.rb
Normal file
65
migrations/lib/importer/steps/uploads.rb
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Migrations::Importer::Steps
|
||||||
|
class Uploads < ::Migrations::Importer::CopyStep
|
||||||
|
depends_on :users
|
||||||
|
store_mapped_ids true
|
||||||
|
|
||||||
|
requires_set :existing_sha1s, "SELECT sha1 FROM uploads"
|
||||||
|
|
||||||
|
column_names %i[
|
||||||
|
user_id
|
||||||
|
original_filename
|
||||||
|
filesize
|
||||||
|
width
|
||||||
|
height
|
||||||
|
url
|
||||||
|
created_at
|
||||||
|
updated_at
|
||||||
|
sha1
|
||||||
|
origin
|
||||||
|
retain_hours
|
||||||
|
extension
|
||||||
|
thumbnail_width
|
||||||
|
thumbnail_height
|
||||||
|
etag
|
||||||
|
secure
|
||||||
|
access_control_post_id
|
||||||
|
original_sha1
|
||||||
|
animated
|
||||||
|
verification_status
|
||||||
|
security_last_changed_at
|
||||||
|
security_last_changed_reason
|
||||||
|
dominant_color
|
||||||
|
]
|
||||||
|
|
||||||
|
total_rows_query <<~SQL, MappingType::UPLOADS
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM files.uploads up
|
||||||
|
LEFT JOIN mapped.ids mup ON up.id = mup.original_id AND mup.type = ?
|
||||||
|
WHERE up.upload IS NOT NULL
|
||||||
|
AND mup.original_id IS NULL
|
||||||
|
SQL
|
||||||
|
|
||||||
|
rows_query <<~SQL, MappingType::UPLOADS
|
||||||
|
SELECT up.id, up.upload
|
||||||
|
FROM files.uploads up
|
||||||
|
LEFT JOIN mapped.ids mup ON up.id = mup.original_id AND mup.type = ?
|
||||||
|
WHERE up.upload IS NOT NULL
|
||||||
|
AND mup.original_id IS NULL
|
||||||
|
ORDER BY up.ROWID
|
||||||
|
SQL
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def transform_row(row)
|
||||||
|
upload = JSON.parse(row[:upload], symbolize_names: true)
|
||||||
|
return nil unless @existing_sha1s.add?(upload[:sha1])
|
||||||
|
|
||||||
|
upload[:original_id] = row[:id]
|
||||||
|
upload.delete(:id)
|
||||||
|
|
||||||
|
super(upload)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -62,7 +62,7 @@ module Migrations::Uploader
|
||||||
end
|
end
|
||||||
|
|
||||||
def configure_logging
|
def configure_logging
|
||||||
@original_exifr_logger = EXIFR.logger
|
@original_exifr_logger = ::EXIFR.logger
|
||||||
|
|
||||||
# disable logging for EXIFR which is used by ImageOptim
|
# disable logging for EXIFR which is used by ImageOptim
|
||||||
EXIFR.logger = Logger.new(nil)
|
EXIFR.logger = Logger.new(nil)
|
||||||
|
@ -74,7 +74,7 @@ module Migrations::Uploader
|
||||||
|
|
||||||
def cleanup_resources
|
def cleanup_resources
|
||||||
databases.values.each(&:close)
|
databases.values.each(&:close)
|
||||||
EXIFR.logger = @original_exifr_logger
|
::EXIFR.logger = @original_exifr_logger
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue