Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
2f95e6a0
Commit
2f95e6a0
authored
Apr 08, 2017
by
Bob Van Landuyt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move `replace_sql` into `Database::MigrationHelpers`
parent
bccf8d86
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
0 deletions
+56
-0
lib/gitlab/database/migration_helpers.rb
lib/gitlab/database/migration_helpers.rb
+23
-0
spec/lib/gitlab/database/migration_helpers_spec.rb
spec/lib/gitlab/database/migration_helpers_spec.rb
+33
-0
No files found.
lib/gitlab/database/migration_helpers.rb
View file @
2f95e6a0
...
...
@@ -498,6 +498,29 @@ module Gitlab
columns
(
table
).
find
{
|
column
|
column
.
name
==
name
}
end
# This will replace the first occurance of a string in a column with
# the replacement
# On postgresql we can use `regexp_replace` for that.
# On mysql we find the location of the pattern, and overwrite it
# with the replacement
def
replace_sql
(
column
,
pattern
,
replacement
)
quoted_pattern
=
Arel
::
Nodes
::
Quoted
.
new
(
pattern
.
to_s
)
quoted_replacement
=
Arel
::
Nodes
::
Quoted
.
new
(
replacement
.
to_s
)
if
Database
.
mysql?
locate
=
Arel
::
Nodes
::
NamedFunction
.
new
(
'locate'
,
[
quoted_pattern
,
column
])
insert_in_place
=
Arel
::
Nodes
::
NamedFunction
.
new
(
'insert'
,
[
column
,
locate
,
pattern
.
size
,
quoted_replacement
])
Arel
::
Nodes
::
SqlLiteral
.
new
(
insert_in_place
.
to_sql
)
else
replace
=
Arel
::
Nodes
::
NamedFunction
.
new
(
"regexp_replace"
,
[
column
,
quoted_pattern
,
quoted_replacement
])
Arel
::
Nodes
::
SqlLiteral
.
new
(
replace
.
to_sql
)
end
end
end
end
end
spec/lib/gitlab/database/migration_helpers_spec.rb
View file @
2f95e6a0
...
...
@@ -726,4 +726,37 @@ describe Gitlab::Database::MigrationHelpers, lib: true do
expect
(
model
.
column_for
(
:users
,
:kittens
)).
to
be_nil
end
end
describe
'#replace_sql'
do
context
'using postgres'
do
before
do
allow
(
Gitlab
::
Database
).
to
receive
(
:mysql?
).
and_return
(
false
)
end
it
'builds the sql with correct functions'
do
expect
(
model
.
replace_sql
(
Arel
::
Table
.
new
(
:users
)[
:first_name
],
"Alice"
,
"Eve"
).
to_s
).
to
include
(
'regexp_replace'
)
end
end
context
'using mysql'
do
before
do
allow
(
Gitlab
::
Database
).
to
receive
(
:mysql?
).
and_return
(
true
)
end
it
'builds the sql with the correct functions'
do
expect
(
model
.
replace_sql
(
Arel
::
Table
.
new
(
:users
)[
:first_name
],
"Alice"
,
"Eve"
).
to_s
).
to
include
(
'locate'
,
'insert'
)
end
end
describe
'results'
do
let!
(
:user
)
{
create
(
:user
,
name:
'Kathy Alice Aliceson'
)
}
it
'replaces the correct part of the string'
do
model
.
update_column_in_batches
(
:users
,
:name
,
model
.
replace_sql
(
Arel
::
Table
.
new
(
:users
)[
:name
],
'Alice'
,
'Eve'
))
expect
(
user
.
reload
.
name
).
to
eq
(
'Kathy Eve Aliceson'
)
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment