Skip to content
GitLab
Next
Menu
Why GitLab
Pricing
Contact Sales
Explore
Why GitLab
Pricing
Contact Sales
Explore
Sign in
Get free trial
Primary navigation
Search or go to…
Project
GitLab
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Privacy statement
Keyboard shortcuts
?
What's new
4
Snippets
Groups
Projects
Show more breadcrumbs
GitLab.org
GitLab
Commits
f03de99e
Verified
Commit
f03de99e
authored
9 months ago
by
Michael Kozono
Committed by
GitLab
9 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Add scripts to generate select queries to inform Org Mover
parent
2f05beec
No related branches found
No related tags found
2 merge requests
!162233
Draft: Script to update Topology Service Gem
,
!154180
Add scripts to generate select queries to inform Org Mover
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
scripts/cells/generate-selects-for-missing-sharding-key
+119
-0
119 additions, 0 deletions
scripts/cells/generate-selects-for-missing-sharding-key
scripts/cells/generate-selects-for-organization
+237
-0
237 additions, 0 deletions
scripts/cells/generate-selects-for-organization
with
356 additions
and
0 deletions
scripts/cells/generate-selects-for-missing-sharding-key
0 → 100755
+
119
−
0
View file @
f03de99e
#!/usr/bin/env ruby
# frozen_string_literal: true
require
'optparse'
require
'open3'
class
GenerateSelectsForMissingShardingKey
attr_reader
:psql_command
def
self
.
for_cli
options
=
parse_options
new
(
**
options
)
end
def
self
.
parse_options
options
=
{}
OptionParser
.
new
do
|
opts
|
opts
.
banner
=
"Usage: generate-selects-for-missing-sharding-key [options]"
opts
.
on
(
'-p'
,
'--psql COMMAND'
,
'Default: gdk psql -c'
)
do
|
value
|
options
[
:psql_command
]
=
value
end
opts
.
on
(
'--debug'
,
'Enable debugging output'
)
{
options
[
:debug
]
=
true
}
end
.
parse!
options
end
def
initialize
(
psql_command:
'gdk psql -c'
,
debug:
false
)
@psql_command
=
psql_command
@debug
=
debug
end
def
execute
print_selects_for_tables_without_sharding_key
end
def
print_selects_for_tables_without_sharding_key
table_names
=
get_table_names
(
without_columns:
%w[organization_id namespace_id project_id]
)
table_names
.
each
do
|
table_name
|
copy_select_sql
=
<<~
SQL
COPY (
SELECT
#{
table_name
}
.*
FROM
#{
table_name
}
)
TO STDOUT
WITH (FORMAT CSV, HEADER)
SQL
puts
copy_select_sql
.
split
.
join
(
' '
)
end
end
def
get_table_names
(
with_column:
nil
,
without_columns:
nil
)
command
=
<<-
SQL
COPY (
SELECT DISTINCT table_name
FROM information_schema.columns
WHERE
table_schema = 'public'
#{
with_column_clause
(
with_column
)
}
#{
without_columns_clause
(
without_columns
)
}
) TO STDOUT
SQL
table_names_str
,
status
=
psql
(
command
)
raise
"Failed to get tables with
#{
column_name
}
column"
unless
status
.
success?
table_names_str
.
split
(
"
\n
"
)
end
def
with_column_clause
(
with_column
)
return
unless
with_column
"AND column_name = '
#{
with_column
}
'"
end
def
without_columns_clause
(
without_columns
)
return
unless
without_columns
<<-
SQL
AND table_name NOT IN (
SELECT table_name
FROM information_schema.columns
WHERE
table_schema = 'public'
AND column_name IN ('
#{
without_columns
.
join
(
"','"
)
}
')
)
SQL
end
def
psql
(
query
)
command
=
psql_command
.
split
<<
query
.
split
.
join
(
' '
)
capture2
(
command
)
end
def
capture2
(
command
)
debug
%(Run command:
#{
command
.
inspect
}
)
stdout_str
,
status
=
Open3
.
capture2
(
*
command
)
debug
%(Run command: stdout: "
#{
stdout_str
}
", exitstatus: "
#{
status
.
exitstatus
}
")
[
stdout_str
,
status
]
end
def
debug
(
output
)
puts
"[DEBUG]
#{
output
}
"
if
debug?
end
def
debug?
!!
@debug
end
end
GenerateSelectsForMissingShardingKey
.
for_cli
.
execute
This diff is collapsed.
Click to expand it.
scripts/cells/generate-selects-for-organization
0 → 100755
+
237
−
0
View file @
f03de99e
#!/usr/bin/env ruby
# frozen_string_literal: true
require
'optparse'
require
'open3'
class
GenerateSelectsForOrganization
attr_reader
:organization_ids
,
:root_namespace_ids
,
:psql_command
def
self
.
for_cli
options
=
parse_options
new
(
**
options
)
end
def
self
.
parse_options
options
=
{}
OptionParser
.
new
do
|
opts
|
opts
.
banner
=
"Usage: generate-selects-for-organization [options]"
opts
.
on
(
'-o'
,
'--organization-ids IDS'
,
'Organization ID(s). Integer, or a comma-separated list of integers.
Required if --root-namespace-ids is not specified.'
)
do
|
value
|
options
[
:organization_ids
]
=
value
.
split
(
','
).
map
(
&
:to_i
)
end
opts
.
on
(
'-n'
,
'--root-namespace-ids IDS'
,
'Root namespace ID(s). Integer, or a comma-separated list of integers.
Required if --organization-ids is not specified.'
)
do
|
value
|
options
[
:root_namespace_ids
]
=
value
.
split
(
','
).
map
(
&
:to_i
)
end
opts
.
on
(
'-p'
,
'--psql COMMAND'
,
'Default: gdk psql -c'
)
do
|
value
|
options
[
:psql_command
]
=
value
end
opts
.
on
(
'--debug'
,
'Enable debugging output'
)
{
options
[
:debug
]
=
true
}
end
.
parse!
if
options
[
:organization_ids
].
nil?
&&
options
[
:root_namespace_ids
].
nil?
raise
'--organization-ids or --root-namespace-ids is required'
end
options
end
def
initialize
(
organization_ids:
nil
,
root_namespace_ids:
nil
,
psql_command:
'gdk psql -c'
,
debug:
false
)
@organization_ids
=
organization_ids
.
sort
if
organization_ids
@root_namespace_ids
=
root_namespace_ids
.
sort
if
root_namespace_ids
@psql_command
=
psql_command
@debug
=
debug
end
def
execute
if
organization_ids
print_selects_for_organizations
else
print_selects_for_root_namespaces
end
end
def
print_selects_for_organizations
print_selects_by_organization_id
(
organization_ids
)
namespace_ids
=
get_namespace_ids_for_organizations
(
organization_ids
)
return
unless
namespace_ids
.
any?
print_selects_by_namespace_id
(
namespace_ids
)
project_ids
=
get_project_ids_for_namespaces
(
namespace_ids
)
return
unless
project_ids
.
any?
print_selects_by_project_id
(
project_ids
)
end
def
print_selects_for_root_namespaces
namespace_ids
=
get_namespace_ids_for_root_namespaces
(
root_namespace_ids
)
print_selects_by_namespace_id
(
namespace_ids
)
project_ids
=
get_project_ids_for_namespaces
(
namespace_ids
)
return
unless
project_ids
.
any?
print_selects_by_project_id
(
project_ids
)
end
def
print_selects_by_organization_id
(
organization_ids
)
print_selects_of_tables
(
with_column:
'organization_id'
,
with_ids:
organization_ids
)
end
def
print_selects_by_namespace_id
(
namespace_ids
)
print_selects_of_tables
(
with_column:
'namespace_id'
,
with_ids:
namespace_ids
,
without_columns:
%w[organization_id]
)
end
def
print_selects_by_project_id
(
project_ids
)
print_selects_of_tables
(
with_column:
'project_id'
,
with_ids:
project_ids
,
without_columns:
%w[organization_id namespace_id]
)
end
def
print_selects_of_tables
(
with_column:
nil
,
with_ids:
nil
,
without_columns:
nil
)
table_names
=
get_table_names
(
with_column:
with_column
,
without_columns:
without_columns
)
table_names
.
each
do
|
table_name
|
with_column_and_ids_clause
=
<<~
SQL
WHERE
#{
table_name
}
.
#{
with_column
}
IN (
#{
with_ids
.
join
(
','
)
}
)
SQL
copy_select_sql
=
<<~
SQL
COPY (
SELECT
#{
table_name
}
.*
FROM
#{
table_name
}
#{
with_column_and_ids_clause
if
with_ids
}
)
TO STDOUT
WITH (FORMAT CSV, HEADER)
SQL
puts
copy_select_sql
.
split
.
join
(
' '
)
end
end
def
get_namespace_ids_for_root_namespaces
(
root_namespace_ids
)
command
=
<<-
SQL
COPY (
SELECT DISTINCT namespaces.id
FROM namespaces
WHERE namespaces.traversal_ids[1] IN (
#{
root_namespace_ids
.
join
(
','
)
}
)
ORDER BY namespaces.id ASC
)
TO STDOUT
SQL
namespace_ids_str
,
status
=
psql
(
command
)
raise
"Failed to get namespace ids for root namespaces"
unless
status
.
success?
namespace_ids_str
.
split
(
"
\n
"
)
end
def
get_table_names
(
with_column:
nil
,
without_columns:
nil
)
command
=
<<-
SQL
COPY (
SELECT DISTINCT table_name
FROM information_schema.columns
WHERE
table_schema = 'public'
#{
with_column_clause
(
with_column
)
}
#{
without_columns_clause
(
without_columns
)
}
) TO STDOUT
SQL
table_names_str
,
status
=
psql
(
command
)
raise
"Failed to get tables with
#{
column_name
}
column"
unless
status
.
success?
table_names_str
.
split
(
"
\n
"
)
end
def
with_column_clause
(
with_column
)
return
unless
with_column
"AND column_name = '
#{
with_column
}
'"
end
def
without_columns_clause
(
without_columns
)
return
unless
without_columns
<<-
SQL
AND table_name NOT IN (
SELECT table_name
FROM information_schema.columns
WHERE
table_schema = 'public'
AND column_name IN ('
#{
without_columns
.
join
(
"','"
)
}
')
)
SQL
end
# @return [Array<String>]
def
get_namespace_ids_for_organizations
(
organization_ids
)
namespace_ids_str
,
status
=
psql
(
<<~
SQL
COPY (
SELECT namespaces.id
FROM namespaces
WHERE namespaces.organization_id
IN (
#{
organization_ids
.
join
(
','
)
}
)
ORDER BY namespaces.id ASC
)
TO STDOUT
SQL
)
raise
"Failed to get namespace ids for organizations"
unless
status
.
success?
namespace_ids_str
.
split
(
"
\n
"
)
end
def
get_project_ids_for_namespaces
(
namespace_ids
)
project_ids_str
,
status
=
psql
(
<<~
SQL
COPY (
SELECT projects.id
FROM projects
WHERE projects.namespace_id
IN (
#{
namespace_ids
.
join
(
','
)
}
)
ORDER BY projects.id ASC
)
TO STDOUT
SQL
)
raise
"Failed to get project ids for organizations"
unless
status
.
success?
project_ids_str
.
split
(
"
\n
"
)
end
def
psql
(
query
)
command
=
psql_command
.
split
<<
query
.
split
.
join
(
' '
)
capture2
(
command
)
end
def
capture2
(
command
)
debug
%(Run command:
#{
command
.
inspect
}
)
stdout_str
,
status
=
Open3
.
capture2
(
*
command
)
debug
%(Run command: stdout: "
#{
stdout_str
}
", exitstatus: "
#{
status
.
exitstatus
}
")
[
stdout_str
,
status
]
end
def
debug
(
output
)
puts
"[DEBUG]
#{
output
}
"
if
debug?
end
def
debug?
!!
@debug
end
end
GenerateSelectsForOrganization
.
for_cli
.
execute
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment