-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
--------- Co-authored-by: Pavel Salamon <[email protected]>
- Loading branch information
1 parent
b080a34
commit 48a7a32
Showing
22 changed files
with
557 additions
and
13 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
database/src/main/postgres/runs/V1.9.4__get_partitioning_by_id.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,69 @@ | ||
/* | ||
* Copyright 2021 ABSA Group Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
-- Function: runs.get_partitioning_by_id(Long) | ||
CREATE OR REPLACE FUNCTION runs.get_partitioning_by_id( | ||
IN i_id BIGINT, | ||
OUT status INTEGER, | ||
OUT status_text TEXT, | ||
OUT id BIGINT, | ||
OUT partitioning JSONB, | ||
OUT author TEXT | ||
) RETURNS RECORD AS | ||
$$ | ||
------------------------------------------------------------------------------- | ||
-- | ||
-- Function: runs.get_partitioning_by_id(1) | ||
-- Returns partitioning for the given id | ||
-- | ||
-- Parameters: | ||
-- i_id - id that we asking the partitioning for | ||
-- | ||
-- Returns: | ||
-- status - Status code | ||
-- status_text - Status message | ||
-- partitioning - Partitioning value to be returned | ||
-- author - Author of the partitioning | ||
|
||
-- Status codes: | ||
-- 11 - OK | ||
-- 41 - Partitioning not found | ||
-- | ||
------------------------------------------------------------------------------- | ||
BEGIN | ||
|
||
SELECT P.partitioning, | ||
P.id_partitioning, | ||
P.created_by | ||
FROM runs.partitionings AS P | ||
WHERE P.id_partitioning = i_id | ||
INTO get_partitioning_by_id.partitioning, id, author; | ||
|
||
IF FOUND THEN | ||
status := 11; | ||
status_text := 'OK'; | ||
ELSE | ||
status := 41; | ||
status_text := 'Partitioning not found'; | ||
END IF; | ||
|
||
RETURN; | ||
END; | ||
$$ | ||
LANGUAGE plpgsql VOLATILE SECURITY DEFINER; | ||
|
||
ALTER FUNCTION runs.get_partitioning_by_id(BIGINT) OWNER TO atum_owner; | ||
GRANT EXECUTE ON FUNCTION runs.get_partitioning_by_id(BIGINT) TO atum_user; | ||
|
94 changes: 94 additions & 0 deletions
94
...se/src/test/scala/za/co/absa/atum/database/runs/GetPartitioningByIdIntegrationTests.scala
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,94 @@ | ||
/* | ||
* Copyright 2021 ABSA Group Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package za.co.absa.atum.database.runs | ||
|
||
import za.co.absa.balta.DBTestSuite | ||
import za.co.absa.balta.classes.JsonBString | ||
import io.circe.Json | ||
import io.circe.parser._ | ||
|
||
class GetPartitioningByIdIntegrationTests extends DBTestSuite { | ||
|
||
private val fncGetPartitioningById = "runs.get_partitioning_by_id" | ||
|
||
private val partitioning = JsonBString( | ||
""" | ||
|{ | ||
| "version": 1, | ||
| "keys": ["key1", "key3", "key2", "key4"], | ||
| "keysToValues": { | ||
| "key1": "valueX", | ||
| "key2": "valueY", | ||
| "key3": "valueZ", | ||
| "key4": "valueA" | ||
| } | ||
|} | ||
|""".stripMargin | ||
) | ||
|
||
private val expectedPartitioning = parse(partitioning.value).getOrElse(throw new Exception("Failed to parse JSON")) | ||
|
||
test("Partitioning retrieved successfully") { | ||
table("runs.partitionings").insert( | ||
add("partitioning", partitioning) | ||
.add("created_by", "Joseph") | ||
) | ||
|
||
val fkPartitioning1: Long = table("runs.partitionings").fieldValue("partitioning", partitioning, "id_partitioning").get.get | ||
|
||
function(fncGetPartitioningById) | ||
.setParam("i_id", fkPartitioning1) | ||
.execute { queryResult => | ||
assert(queryResult.hasNext) | ||
val row = queryResult.next() | ||
val returnedPartitioning = row.getJsonB("partitioning").get | ||
val returnedPartitioningParsed = parse(returnedPartitioning.value) | ||
.getOrElse(fail("Failed to parse returned partitioning")) | ||
assert(row.getInt("status").contains(11)) | ||
assert(row.getString("status_text").contains("OK")) | ||
assert(row.getLong("id").contains(fkPartitioning1)) | ||
assert(returnedPartitioningParsed == expectedPartitioning) | ||
assert(row.getString("author").contains("Joseph")) | ||
} | ||
|
||
table("runs.partitionings").where(add("id_partitioning", fkPartitioning1)) { partitioningResult => | ||
val row = partitioningResult.next() | ||
val returnedPartitioning = row.getJsonB("partitioning").get | ||
val returnedPartitioningParsed = parse(returnedPartitioning.value) | ||
.getOrElse(fail("Failed to parse returned partitioning")) | ||
assert(returnedPartitioningParsed == expectedPartitioning) | ||
assert(row.getString("created_by").contains("Joseph")) | ||
} | ||
} | ||
|
||
test("Partitioning not found") { | ||
val nonExistentID = 9999L | ||
|
||
function(fncGetPartitioningById) | ||
.setParam("i_id", nonExistentID) | ||
.execute { queryResult => | ||
assert(queryResult.hasNext) | ||
val row = queryResult.next() | ||
assert(row.getInt("status").contains(41)) | ||
assert(row.getString("status_text").contains("Partitioning not found")) | ||
assert(row.getJsonB("partitioning").isEmpty) | ||
assert(row.getString("author").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
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
49 changes: 49 additions & 0 deletions
49
...c/main/scala/za/co/absa/atum/server/api/database/runs/functions/GetPartitioningById.scala
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,49 @@ | ||
/* | ||
* Copyright 2021 ABSA Group Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package za.co.absa.atum.server.api.database.runs.functions | ||
|
||
import doobie.implicits.toSqlInterpolator | ||
import za.co.absa.atum.server.api.database.PostgresDatabaseProvider | ||
import za.co.absa.atum.server.api.database.runs.Runs | ||
import za.co.absa.db.fadb.DBSchema | ||
import za.co.absa.db.fadb.doobie.DoobieEngine | ||
import za.co.absa.db.fadb.doobie.DoobieFunction.DoobieSingleResultFunctionWithStatus | ||
import za.co.absa.db.fadb.status.handling.implementations.StandardStatusHandling | ||
import za.co.absa.atum.server.model.PartitioningFromDB | ||
import zio._ | ||
import za.co.absa.atum.server.api.database.DoobieImplicits.Sequence.get | ||
import doobie.postgres.implicits._ | ||
import za.co.absa.db.fadb.doobie.postgres.circe.implicits.jsonbGet | ||
|
||
class GetPartitioningById(implicit schema: DBSchema, dbEngine: DoobieEngine[Task]) | ||
extends DoobieSingleResultFunctionWithStatus[Long, Option[PartitioningFromDB], Task](values => | ||
Seq( | ||
fr"$values" | ||
) | ||
) | ||
with StandardStatusHandling { | ||
|
||
override def fieldsToSelect: Seq[String] = super.fieldsToSelect ++ Seq("id", "partitioning", "author") | ||
} | ||
|
||
object GetPartitioningById { | ||
val layer: URLayer[PostgresDatabaseProvider, GetPartitioningById] = ZLayer { | ||
for { | ||
dbProvider <- ZIO.service[PostgresDatabaseProvider] | ||
} yield new GetPartitioningById()(Runs, dbProvider.dbEngine) | ||
} | ||
} |
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
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.