Fix search within Environments folder
What does this MR do and why?
Searching for Environments within a folder does not always return all matches. (For more details of the problem, see related issue: Environment Search not returning all matches (#427537 - closed).)
This bug happens because the SQL query statement to search for Environments within a folder (i.e.: of the format folder/env-name
) uses the ltrim
function incorrectly.
The ltrim
call below is supposed to trim out the folder and the slash (environment_type || '/'
) from the full environment name.
ltrim(environments.name, environments.environment_type || '/')
It assumes that ltrim
trims by the exact environment folder. For example, with a full environment name "stg/static-files-1", the assumption is that it would trim out the leading stg/
alone. However, ltrim
seems to evaluate each character in "stg/static-files-1" one by one and trim it from the start of the string until it finds a character that is not in "stg/".
To fix this, we need to do the ltrim
calls below instead:
ltrim(ltrim(environments.name, environments.environment_type), '/')
So the first ltrim
will stop at the /
, and the second ltrim
will trim out the /
.
For a more detailed explanation, please see this comment thread.
MR acceptance checklist
Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
Screenshots or screen recordings
Before | After |
---|---|
![]() |
![]() |
Running the newly-introduced tests without the change results in an error
Rspec output
$ bundle exec rspec spec/models/environment_spec.rb:338
Test environment set up in 2.817859 seconds
....FF....
Failures:
1) Environment.for_name_like_within_folder when the environment folder is the same as the starting characters of the environment name returns a found name
Failure/Error: is_expected.to contain_exactly(environment)
expected collection contained: [#<Environment id: 9, project_id: 5, name: "test/test-app", created_at: "2024-02-29 05:47:40.63753600..., merge_request_id: nil, cluster_agent_id: nil, kubernetes_namespace: nil, flux_resource_path: nil>]
actual collection contained: []
the missing elements were: [#<Environment id: 9, project_id: 5, name: "test/test-app", created_at: "2024-02-29 05:47:40.63753600..., merge_request_id: nil, cluster_agent_id: nil, kubernetes_namespace: nil, flux_resource_path: nil>]
<snip>
2) Environment.for_name_like_within_folder when the environment folder has characters in the starting characters of the environment name returns a found name
Failure/Error: is_expected.to contain_exactly(environment)
expected collection contained: [#<Environment id: 11, project_id: 6, name: "atr/test-app", created_at: "2024-02-29 05:47:41.13624900..., merge_request_id: nil, cluster_agent_id: nil, kubernetes_namespace: nil, flux_resource_path: nil>]
actual collection contained: []
the missing elements were: [#<Environment id: 11, project_id: 6, name: "atr/test-app", created_at: "2024-02-29 05:47:41.13624900..., merge_request_id: nil, cluster_agent_id: nil, kubernetes_namespace: nil, flux_resource_path: nil>]
<snip>
Finished in 11.97 seconds (files took 42.47 seconds to load)
10 examples, 2 failures
Failed examples:
rspec ./spec/models/environment_spec.rb:373 # Environment.for_name_like_within_folder when the environment folder is the same as the starting characters of the environment name returns a found name
rspec ./spec/models/environment_spec.rb:381 # Environment.for_name_like_within_folder when the environment folder has characters in the starting characters of the environment name returns a found name
ltrim
function usage
Illustration of the Before (ltrim(environments.name, environments.environment_type || '/')
)
Expand for SQL query
> select name, environment_type, LOWER(ltrim(environments.name, environments.environment_type || '/')) as within_folder_name from environments where project_id=40;
name | environment_type | within_folder_name
-------------------------------+------------------+--------------------
dev/static-files-1 | dev | static-files-1
dev/static-files-2 | dev | static-files-2
stg/static-files-1 | stg | atic-files-1
stg/static-files-2 | stg | atic-files-2
uat/static-files-1 | uat | static-files-1
uat/static-files-2 | uat | static-files-2
After (ltrim(ltrim(environments.name, environments.environment_type), '/')
)
Expand for SQL query
> select name, environment_type, LOWER(ltrim(ltrim(environments.name, environments.environment_type), '/')) as within_folder_name from environments where project_id=40;
name | environment_type | within_folder_name
-------------------------------+------------------+--------------------
dev/static-files-1 | dev | static-files-1
dev/static-files-2 | dev | static-files-2
stg/static-files-1 | stg | static-files-1
stg/static-files-2 | stg | static-files-2
uat/static-files-1 | uat | static-files-1
uat/static-files-2 | uat | static-files-2
How to set up and validate locally
Setup
Create a project with the below environments:
dev/static-files-1
dev/static-files-2
stg/static-files-1
stg/static-files-2
uat/static-files-1
uat/static-files-2
Expand for the pipeline configuration to create the environments:
deploy_dev_1:
stage: deploy
script: echo "deploying..."
environment:
name: dev/static-files-1
action: start
only:
refs:
- main
deploy_dev_2:
stage: deploy
script: echo "deploying..."
environment:
name: dev/static-files-2
action: start
only:
refs:
- main
deploy_stg_1:
stage: deploy
script: echo "deploying..."
environment:
name: stg/static-files-1
action: start
only:
refs:
- main
deploy_stg_2:
stage: deploy
script: echo "deploying..."
environment:
name: stg/static-files-2
action: start
only:
refs:
- main
deploy_uat_1:
stage: deploy
script: echo "deploying..."
environment:
name: uat/static-files-1
action: start
only:
refs:
- main
deploy_uat_2:
stage: deploy
script: echo "deploying..."
environment:
name: uat/static-files-2
action: start
only:
refs:
- main
Test Before
- Go to the Environments page of the project (Operate -> Environments)
- Search for "static"
- Only the environments under
dev
anduat
would be in the result (see "before" screenshot above).
Test After
- Go to the Environments page of the project (Operate -> Environments)
- Search for "static"
- All environments with "static" in the name should be in the result (see "after" screenshot above).
Related to #427537 (closed)
Merge request reports
Activity
changed milestone to %16.10
assigned to @partiaga
- A deleted user
added databasereview pending label
1 Message This merge request adds or changes files that require a review from the Database team. This merge request requires a database review. To make sure these changes are reviewed, take the following steps:
- Ensure the merge request has database and databasereview pending labels. If the merge request modifies database files, Danger will do this for you.
- Prepare your MR for database review according to the docs.
- Assign and mention the database reviewer suggested by Reviewer Roulette.
If you no longer require a database review, you can remove this suggestion by removing the database label and re-running the
danger-review
job.Reviewer roulette
Category Reviewer Maintainer backend @panoskanell
(UTC+2, 9 hours behind author)
@splattael
(UTC+1, 10 hours behind author)
database @subashis
(UTC-7, 18 hours behind author)
@l.rosa
(UTC-3, 14 hours behind author)
Please check reviewer's status!
Please refer to documentation page for guidance on how you can benefit from the Reviewer Roulette, or use the GitLab Review Workload Dashboard to find other available reviewers.
If needed, you can retry the
danger-review
job that generated this comment.Generated by
Dangerremoved databasereview pending label
- A deleted user
added databasereview pending label
added database label
removed workflowin dev label
- Resolved by Michał Zając
Hi @syarynovskyi would you mind doing the initial backend review here please? Thanks
requested review from @syarynovskyi
- Resolved by Michał Zając
Hi @tianwenchen can you please do the initial database review here?
I don't know if this needs a Database Labs query plan. I tried to run some EXPLAINs on the "Ask Joe", but I'm getting this error:
ERROR: failed to create a Database Lab clone: failed to create a new clone: failed to watch the clone status: context deadline exceeded
I'm assuming this is a different problem unrelated to the query statements
But in case you need the query statements, this is an example that can be run on the production DB:
Before
SELECT "environments".* FROM "environments" WHERE "environments"."project_id" = 49114813 AND (LOWER(ltrim(environments.name, environments.environment_type || '/')) LIKE LOWER('podtato') || '%') LIMIT 5
After
SELECT "environments".* FROM "environments" WHERE "environments"."project_id" = 49114813 AND (LOWER(ltrim(ltrim(environments.name, environments.environment_type), '/')) LIKE LOWER('podtato') || '%') LIMIT 5
requested review from @tianwenchen
added pipeline:mr-approved label
- Resolved by Michał Zając
@syarynovskyi
, thanks for approving this merge request.This is the first time the merge request has been approved. To ensure we don't only run predictive pipelines, and we don't break
master
, a new pipeline will be started shortly.Please wait for the pipeline to start before resolving this discussion and set auto-merge for the new pipeline. See merging a merge request for more details.
requested review from @dgruzd
removed review request for @syarynovskyi
E2E Test Result Summary
allure-report-publisher
generated test report!e2e-test-on-gdk:
test report for 0e5e87e4expand test summary
+------------------------------------------------------------------+ | suites summary | +-------------+--------+--------+---------+-------+-------+--------+ | | passed | failed | skipped | flaky | total | result | +-------------+--------+--------+---------+-------+-------+--------+ | Create | 66 | 0 | 9 | 0 | 75 | ✅ | | Govern | 66 | 0 | 0 | 0 | 66 | ✅ | | Plan | 53 | 0 | 0 | 0 | 53 | ✅ | | Data Stores | 31 | 0 | 0 | 0 | 31 | ✅ | | Verify | 35 | 0 | 1 | 0 | 36 | ✅ | | Package | 24 | 0 | 2 | 0 | 26 | ✅ | | Manage | 0 | 0 | 1 | 0 | 1 | ➖ | | Monitor | 7 | 0 | 0 | 0 | 7 | ✅ | | Release | 5 | 0 | 0 | 0 | 5 | ✅ | | Analytics | 2 | 0 | 0 | 0 | 2 | ✅ | +-------------+--------+--------+---------+-------+-------+--------+ | Total | 289 | 0 | 13 | 0 | 302 | ✅ | +-------------+--------+--------+---------+-------+-------+--------+
added workflowin review label
removed review request for @dgruzd
mentioned in commit e7086d04
mentioned in merge request !146266 (merged)
added databasereviewed label and removed databasereview pending label
requested review from @Quintasan
removed review request for @tianwenchen
mentioned in issue #427537 (closed)
mentioned in commit 484a5a31
removed workflowin review label
mentioned in commit 9517c2cb
mentioned in merge request !146732 (merged)
enabled an automatic merge when the pipeline for 71135244 succeeds
Hello @partiaga
The database team is looking for ways to improve the database review process and we would love your help!
If you'd be open to someone on the database team reaching out to you for a chat, or if you'd like to leave some feedback asynchronously, just post a reply to this comment mentioning:
@gitlab-org/database-team
And someone will be by shortly!
Thanks for your help!
This message was generated automatically. You're welcome to improve it.
mentioned in commit f3b281c6
added workflowstaging label
mentioned in commit e342b166
mentioned in merge request kubitus-project/kubitus-installer!2869 (merged)
added releasedpublished label
added pipelinetier-3 label