Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mutate queries to fix missing usernames #145

Merged
merged 4 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions parts/21-mutate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,66 @@ mutate_purge-old-job-metrics() { ##? [--commit]: Purge job metrics older than 1
QUERY="$txn_pre $QUERY; $txn_pos"
}

mutate_derive-missing-username-from-email() { ##? [--commit]: Set empty username to email address for users created before 2011
meta <<-EOF
ADDED: 22
AUTHORS: mvdbeek
EOF
handle_help "$@" <<-EOF
Galaxy did not require setting a username for users registered prior to 2011.
This will set the username to the lowercased substring of the email addres before the first @.
The username for a user with the email address "[email protected]"
will be set to "jane.doe" if the the user did not have a username and no other user
has been registered with that username.
It is recommended that usernames that could not be changed due to conflicts are fixed
using mutate set-missing-username-to-random-uuid()
EOF

read -r -d '' QUERY <<-EOF
WITH extracted_emails AS (
SELECT LOWER(SPLIT_PART(email, '@', 1)) AS extracted_email
FROM galaxy_user gu
WHERE username IS NULL
AND NOT EXISTS (
SELECT 1
FROM galaxy_user
WHERE LOWER(SPLIT_PART(email, '@', 1)) = LOWER(SPLIT_PART(gu.email, '@', 1))
AND username IS NOT NULL
)
)
UPDATE galaxy_user gu
SET username = e.extracted_email
FROM extracted_emails e
WHERE gu.username IS NULL
AND LOWER(SPLIT_PART(gu.email, '@', 1)) = e.extracted_email
EOF

txn_pre=$(txn_prefix "$arg_commit")
txn_pos=$(txn_postfix "$arg_commit")
QUERY="$txn_pre $QUERY; $txn_pos"
}

mutate_set-missing-username-to-random-uuid() { ##? [--commit]: Set empty username to random uuid
meta <<-EOF
ADDED: 22
AUTHORS: mvdbeek
EOF
handle_help "$@" <<-EOF
Galaxy did not require setting a username for users registered prior to 2011.
This will set the username column to a random uuid.
EOF

read -r -d '' QUERY <<-EOF
UPDATE galaxy_user gu
SET username = gen_random_uuid()
WHERE gu.username IS NULL
EOF

txn_pre=$(txn_prefix "$arg_commit")
txn_pos=$(txn_postfix "$arg_commit")
QUERY="$txn_pre $QUERY; $txn_pos"
}

mutate_scale-table-autovacuum() { ##? [--shift=16] [--commit]: Update autovacuum and autoanalyze scale for large tables.
meta <<-EOF
ADDED: 22
Expand Down
8 changes: 8 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,11 @@ GXADMIN=./.tmpgxadmin
fi
[ "$result" -eq 0 ]
}

@test "Ensure query names are standardised and match [type]_q-u-e-r-y" {
result=$(grep -P '^[a-z]+_[a-z-]*_[a-z-_]*\(\)' ${GXADMIN} -c)
if (( result > 20 )); then
grep -P '^[a-z]+_[a-z-]*_[a-z-_]*\(\)' parts/2*
fi
[ "$result" -eq 20 ]
}
Loading