mirror of
https://github.com/discourse/discourse.git
synced 2025-05-25 19:29:34 +08:00
DEV: Add Upload
to IntermediateDB (#29780)
This commit is contained in:
15
migrations/lib/common/id.rb
Normal file
15
migrations/lib/common/id.rb
Normal file
@ -0,0 +1,15 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "digest/xxhash"
|
||||
|
||||
module Migrations
|
||||
module ID
|
||||
def self.hash(value)
|
||||
Digest::XXH3_128bits.base64digest(value)
|
||||
end
|
||||
|
||||
def self.build(part1, part2, *others)
|
||||
[part1, part2, *others].join("-")
|
||||
end
|
||||
end
|
||||
end
|
92
migrations/lib/database/intermediate_db/upload.rb
Normal file
92
migrations/lib/database/intermediate_db/upload.rb
Normal file
@ -0,0 +1,92 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Migrations::Database::IntermediateDB
|
||||
module Upload
|
||||
SQL = <<~SQL
|
||||
INSERT OR IGNORE INTO uploads (
|
||||
id,
|
||||
filename,
|
||||
path,
|
||||
data,
|
||||
url,
|
||||
type,
|
||||
description,
|
||||
origin,
|
||||
user_id
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
SQL
|
||||
|
||||
class << self
|
||||
def create_for_file!(
|
||||
path:,
|
||||
filename: nil,
|
||||
type: nil,
|
||||
description: nil,
|
||||
origin: nil,
|
||||
user_id: nil
|
||||
)
|
||||
create!(
|
||||
id: ::Migrations::ID.hash(path),
|
||||
filename: filename || File.basename(path),
|
||||
path:,
|
||||
type:,
|
||||
description:,
|
||||
origin:,
|
||||
user_id:,
|
||||
)
|
||||
end
|
||||
|
||||
def create_for_url!(url:, filename:, type: nil, description: nil, origin: nil, user_id: nil)
|
||||
create!(
|
||||
id: ::Migrations::ID.hash(url),
|
||||
filename:,
|
||||
url:,
|
||||
type:,
|
||||
description:,
|
||||
origin:,
|
||||
user_id:,
|
||||
)
|
||||
end
|
||||
|
||||
def create_for_data!(data:, filename:, type: nil, description: nil, origin: nil, user_id: nil)
|
||||
create!(
|
||||
id: ::Migrations::ID.hash(data),
|
||||
filename:,
|
||||
data: ::Migrations::Database.to_blob(data),
|
||||
type:,
|
||||
description:,
|
||||
origin:,
|
||||
user_id:,
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create!(
|
||||
id:,
|
||||
filename:,
|
||||
path: nil,
|
||||
data: nil,
|
||||
url: nil,
|
||||
type: nil,
|
||||
description: nil,
|
||||
origin: nil,
|
||||
user_id: nil
|
||||
)
|
||||
::Migrations::Database::IntermediateDB.insert(
|
||||
SQL,
|
||||
id,
|
||||
filename,
|
||||
path,
|
||||
data,
|
||||
url,
|
||||
type,
|
||||
description,
|
||||
origin,
|
||||
user_id,
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -45,7 +45,12 @@ module Migrations
|
||||
loader.log! if ENV["DEBUG"]
|
||||
|
||||
loader.inflector.inflect(
|
||||
{ "cli" => "CLI", "intermediate_db" => "IntermediateDB", "uploads_db" => "UploadsDB" },
|
||||
{
|
||||
"cli" => "CLI",
|
||||
"id" => "ID",
|
||||
"intermediate_db" => "IntermediateDB",
|
||||
"uploads_db" => "UploadsDB",
|
||||
},
|
||||
)
|
||||
|
||||
loader.push_dir(File.join(::Migrations.root_path, "lib"), namespace: ::Migrations)
|
||||
|
Reference in New Issue
Block a user