-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use generated user name column for make query DB only [DHIS2-172…
…00] (#19294) * fix: use generated user name column for make query DB only [DHIS2-17200] * fix: User name hibernate mapping * fix: fallback to compute name * fix: H2 controller tests * fix: postgres integration tests * chore: cleanup SQL
- Loading branch information
Showing
13 changed files
with
219 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...ain/resources/org/hisp/dhis/db/migration/2.42/V2_42_32__User_name_as_generated_column.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
Adds the "name" column to users as a computed column based on first and last name | ||
to move named based filters into the DB. | ||
This is important as the web API "query" search is a combination of multiple columns | ||
one of which is name. Such combinations must stay all in DB to be correct. | ||
Since both source columns are not null the name can and should also be not null | ||
and the computation does not need to handle null. | ||
*/ | ||
alter table userinfo | ||
add column if not exists name character varying(321) not null generated always as ( firstname || ' ' || surname ) stored; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...api/src/test/java/org/hisp/dhis/webapi/controller/AbstractGistControllerPostgresTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright (c) 2004-2024, University of Oslo | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* Neither the name of the HISP project nor the names of its contributors may | ||
* be used to endorse or promote products derived from this software without | ||
* specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package org.hisp.dhis.webapi.controller; | ||
|
||
import static org.hisp.dhis.http.HttpAssertions.assertStatus; | ||
import static org.hisp.dhis.http.HttpClientAdapter.Body; | ||
|
||
import org.hisp.dhis.http.HttpStatus; | ||
import org.hisp.dhis.test.webapi.PostgresControllerIntegrationTestBase; | ||
import org.hisp.dhis.user.User; | ||
import org.junit.jupiter.api.BeforeEach; | ||
|
||
/** | ||
* Base class for controller tests of the Gist API that require an actual postgres DB. | ||
* | ||
* <p>This usually is because of the use of JSONB functions. Another reason is use of generated | ||
* columns. | ||
* | ||
* @author Jan Bernitt | ||
*/ | ||
abstract class AbstractGistControllerPostgresTest extends PostgresControllerIntegrationTestBase { | ||
|
||
protected String userGroupId; | ||
protected String orgUnitId; | ||
protected String dataSetId; | ||
protected User userA; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
userA = createUserWithAuth("userGist", "ALL"); | ||
|
||
switchContextToUser(userA); | ||
|
||
userGroupId = | ||
assertStatus( | ||
HttpStatus.CREATED, | ||
POST("/userGroups/", "{'name':'groupX', 'users':[{'id':'" + getAdminUid() + "'}]}")); | ||
assertStatus( | ||
HttpStatus.OK, | ||
PATCH( | ||
"/users/{id}?importReportMode=ERRORS", | ||
getAdminUid(), | ||
Body("[{'op': 'add', 'path': '/birthday', 'value': '1980-12-12'}]"))); | ||
orgUnitId = | ||
assertStatus( | ||
HttpStatus.CREATED, | ||
POST( | ||
"/organisationUnits/", | ||
"{'name':'unitA', 'shortName':'unitA', 'openingDate':'2021-01-01'}")); | ||
dataSetId = | ||
assertStatus( | ||
HttpStatus.CREATED, | ||
POST( | ||
"/dataSets/", | ||
"{'name':'set1', 'shortName':'set1', 'organisationUnits': [{'id':'" | ||
+ orgUnitId | ||
+ "'}], 'periodType':'Daily'}")); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...est-web-api/src/test/java/org/hisp/dhis/webapi/controller/GistControllerPostgresTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (c) 2004-2024, University of Oslo | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* Neither the name of the HISP project nor the names of its contributors may | ||
* be used to endorse or promote products derived from this software without | ||
* specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package org.hisp.dhis.webapi.controller; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
||
import org.hisp.dhis.jsontree.JsonArray; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Tests for the Gist API that cannot run on H2 but require an actual postgres DB. | ||
* | ||
* @author Jan Bernitt | ||
*/ | ||
class GistControllerPostgresTest extends AbstractGistControllerPostgresTest { | ||
|
||
/** | ||
* Note: this test was moved here unchanged to verify the functionality in the API hasn't changed. | ||
* However, when the "name" became a generated column instead of using a from transformation it | ||
* has to run with an actual postgres DB. The test name is kept to allow seeing the evolution | ||
* looking back to versions that do use from instead of a generated column to synthesize the name | ||
* from firstName and surname post DB query. | ||
*/ | ||
@Test | ||
void testField_UserNameAutomaticFromTransformation() { | ||
JsonArray users = GET("/users/gist?fields=id,name&headless=true").content(); | ||
assertEquals( | ||
"FirstNameuserGist SurnameuserGist", users.getObject(1).getString("name").string()); | ||
} | ||
|
||
/** Note: now that user "name" is a generated column we can also filter on it */ | ||
@Test | ||
void testFilter_UserName() { | ||
JsonArray users = | ||
GET("/users/gist?fields=id,name&headless=true&filter=name:like:Gist").content(); | ||
assertFalse(users.isEmpty()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.