mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Revert "Swtich to regexp for DbHelper.remap."
Regexp is so much slower.
This reverts commit c3f89e3cd7.
This commit is contained in:
@@ -8,8 +8,10 @@ class DbHelper
|
|||||||
AND (data_type LIKE 'char%' OR data_type LIKE 'text%')
|
AND (data_type LIKE 'char%' OR data_type LIKE 'text%')
|
||||||
ORDER BY table_name, column_name"
|
ORDER BY table_name, column_name"
|
||||||
|
|
||||||
def self.remap(from, to, exclude_tables: [])
|
def self.remap(from, to, anchor_left: false, anchor_right: false, exclude_tables: [])
|
||||||
results = DB.query(REMAP_SQL).to_a
|
results = DB.query(REMAP_SQL).to_a
|
||||||
|
like = "#{anchor_left ? '' : "%"}#{from}#{anchor_right ? '' : "%"}"
|
||||||
|
|
||||||
remappable_columns = {}
|
remappable_columns = {}
|
||||||
|
|
||||||
results.each do |result|
|
results.each do |result|
|
||||||
@@ -22,14 +24,14 @@ class DbHelper
|
|||||||
remappable_columns.each do |table_name, column_names|
|
remappable_columns.each do |table_name, column_names|
|
||||||
next if exclude_tables.include?(table_name)
|
next if exclude_tables.include?(table_name)
|
||||||
set_clause = column_names.map do |column_name|
|
set_clause = column_names.map do |column_name|
|
||||||
"#{column_name} = REGEXP_REPLACE(#{column_name}, :from, :to)"
|
"#{column_name} = REPLACE(#{column_name}, :from, :to)"
|
||||||
end.join(", ")
|
end.join(", ")
|
||||||
|
|
||||||
where_clause = column_names.map do |column_name|
|
where_clause = column_names.map do |column_name|
|
||||||
"#{column_name} ~* :from"
|
"#{column_name} LIKE :like"
|
||||||
end.join(" OR ")
|
end.join(" OR ")
|
||||||
|
|
||||||
DB.exec(<<~SQL, from: from, to: to, from: from)
|
DB.exec(<<~SQL, from: from, to: to, like: like)
|
||||||
UPDATE #{table_name}
|
UPDATE #{table_name}
|
||||||
SET #{set_clause}
|
SET #{set_clause}
|
||||||
WHERE #{where_clause}
|
WHERE #{where_clause}
|
||||||
|
|||||||
@@ -40,29 +40,5 @@ RSpec.describe DbHelper do
|
|||||||
|
|
||||||
expect(post.reload.cooked).to eq('test')
|
expect(post.reload.cooked).to eq('test')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'accepts a POSIX regular expression' do
|
|
||||||
url = "https://some.complicated.url/that/I/can/come/up.with"
|
|
||||||
|
|
||||||
post = Fabricate(:post,
|
|
||||||
cooked: "something something#{url}something something"
|
|
||||||
)
|
|
||||||
|
|
||||||
url2 = "https://some.other.complicated/url/I/can/come/up.with"
|
|
||||||
|
|
||||||
post2 = Fabricate(:post,
|
|
||||||
cooked: "something #{url2} something something"
|
|
||||||
)
|
|
||||||
|
|
||||||
DbHelper.remap("(#{url}|#{url2})", "DISCOURSE!")
|
|
||||||
|
|
||||||
expect(post.reload.cooked).to eq(
|
|
||||||
'something somethingDISCOURSE!something something'
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(post2.reload.cooked).to eq(
|
|
||||||
"something DISCOURSE! something something"
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user