Skip to content

Commit

Permalink
Add H2 database support for the persist SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
daneshk committed Feb 8, 2024
1 parent aa77cd1 commit a78aebd
Show file tree
Hide file tree
Showing 22 changed files with 4,222 additions and 71 deletions.
509 changes: 509 additions & 0 deletions ballerina/Dependencies.toml

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions ballerina/Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

This module provides relation database support for the `bal persist` feature, which provides functionality to store and query data from a relational database conveniently through a data model instead of using SQL query language.

Currently, this package supports MySQL, MSSQL, PostgreSQL as the relational database. However, we are also planning to add support for other relational databases such as Oracle.
Currently, this package supports MySQL, MSSQL, PostgreSQL, H2 as the relational database. However, we are also planning to add support for other relational databases such as Oracle.

The MySQL, MSSQL, PostgreSQL data stores are relational database management systems that stores data in tables. None of these data stores is the default data store for the `bal persist` feature. Therefore, you need to explicitly specify the data store when initializing `bal persist` in your application. as follows,
The MySQL, MSSQL, PostgreSQL, H2 data stores are relational database management systems that stores data in tables. None of these data stores is the default data store for the `bal persist` feature. Therefore, you need to explicitly specify the data store when initializing `bal persist` in your application. as follows,

```
$ bal persist init --datastore mysql
$ bal persist init --datastore mssql
$ bal persist init --datastore postgresql
$ bal persist init --datastore h2
```

## Supported Ballerina Types
Expand Down
124 changes: 62 additions & 62 deletions ballerina/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ static def checkExecResult(executionResult, failText, standardOutput) {
task createMySQLTestDockerImage(type: Exec) {
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
def standardOutput = new ByteArrayOutputStream()
commandLine 'sh', '-c', "docker build --platform linux/amd64 -f $project.projectDir/tests/resources/mysql/Dockerfile -t ballerina-persist-mysql" +
commandLine 'sh', '-c', "docker build --platform linux/x86_64/v8 -f $project.projectDir/tests/resources/mysql/Dockerfile -t ballerina-persist-mysql" +
" -q $project.projectDir/tests/resources/mysql/"
doLast {
checkExecResult(executionResult, 'Error', standardOutput)
Expand Down Expand Up @@ -200,67 +200,67 @@ task stopMySQLTestDockerContainer() {
}
}

task createMSSQLTestDockerImage(type: Exec) {
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
def standardOutput = new ByteArrayOutputStream()
commandLine 'sh', '-c', "docker build -f $project.projectDir/tests/resources/mssql/Dockerfile -t ballerina-persist-mssql" +
" -q $project.projectDir/tests/resources/mssql/"
doLast {
checkExecResult(executionResult, 'Error', standardOutput)
sleep(10 * 1000)
}
}
}
//task createMSSQLTestDockerImage(type: Exec) {
// if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
// def standardOutput = new ByteArrayOutputStream()
// commandLine 'sh', '-c', "docker build -f $project.projectDir/tests/resources/mssql/Dockerfile -t ballerina-persist-mssql" +
// " -q $project.projectDir/tests/resources/mssql/"
// doLast {
// checkExecResult(executionResult, 'Error', standardOutput)
// sleep(10 * 1000)
// }
// }
//}

def checkMSSQLTestDockerContainerStatus(containerName) {
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
try {
return exec {
commandLine 'sh', '-c',
"docker exec ${containerName} /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Test123#"
}.exitValue
} catch (all) {
return 1;
}
}
}
//def checkMSSQLTestDockerContainerStatus(containerName) {
// if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
// try {
// return exec {
// commandLine 'sh', '-c',
// "docker exec ${containerName} /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Test123#"
// }.exitValue
// } catch (all) {
// return 1;
// }
// }
//}

task startMSSQLTestDockerContainer(type: Exec) {
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
def standardOutput = new ByteArrayOutputStream()
commandLine 'sh', '-c',
"docker run --rm -d --name ballerina-persist-mssql -e ACCEPT_EULA=1 -e SA_PASSWORD=Test123# -p 1433:1433 -d ballerina-persist-mssql"
def healthCheck = 1;
def counter = 0;
doLast {
checkExecResult(executionResult, 'Error', standardOutput)
while (healthCheck != 0 && counter < 12) {
sleep(5 * 1000)
healthCheck = checkMSSQLTestDockerContainerStatus("ballerina-persist-mssql")
counter = counter + 1;
}
if (healthCheck != 0) {
throw new GradleException("Docker container 'ballerina-persist-mssql' health test exceeded timeout!")
}
}
}
}
//task startMSSQLTestDockerContainer(type: Exec) {
// if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
// def standardOutput = new ByteArrayOutputStream()
// commandLine 'sh', '-c',
// "docker run --rm -d --name ballerina-persist-mssql -e ACCEPT_EULA=1 -e SA_PASSWORD=Test123# -p 1433:1433 -d ballerina-persist-mssql"
// def healthCheck = 1;
// def counter = 0;
// doLast {
// checkExecResult(executionResult, 'Error', standardOutput)
// while (healthCheck != 0 && counter < 12) {
// sleep(5 * 1000)
// healthCheck = checkMSSQLTestDockerContainerStatus("ballerina-persist-mssql")
// counter = counter + 1;
// }
// if (healthCheck != 0) {
// throw new GradleException("Docker container 'ballerina-persist-mssql' health test exceeded timeout!")
// }
// }
// }
//}

task stopMSSQLTestDockerContainer() {
doLast {
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
try {
def stdOut = new ByteArrayOutputStream()
exec {
commandLine 'sh', '-c', "docker stop ballerina-persist-mssql"
standardOutput = stdOut
}
} catch (all) {
println("Process can safely ignore stopTestDockerContainer task")
}
}
}
}
//task stopMSSQLTestDockerContainer() {
// doLast {
// if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
// try {
// def stdOut = new ByteArrayOutputStream()
// exec {
// commandLine 'sh', '-c', "docker stop ballerina-persist-mssql"
// standardOutput = stdOut
// }
// } catch (all) {
// println("Process can safely ignore stopTestDockerContainer task")
// }
// }
// }
//}

task createPostgreSQLTestDockerImage(type: Exec) {
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
Expand Down Expand Up @@ -325,18 +325,18 @@ task stopPostgreSQLTestDockerContainer() {

updateTomlFiles.dependsOn copyStdlibs
startMySQLTestDockerContainer.dependsOn createMySQLTestDockerImage
startMSSQLTestDockerContainer.dependsOn createMSSQLTestDockerImage
//startMSSQLTestDockerContainer.dependsOn createMSSQLTestDockerImage
startPostgreSQLTestDockerContainer.dependsOn createPostgreSQLTestDockerImage

build.dependsOn "generatePomFileForMavenPublication"
build.dependsOn ":${packageName}-compiler-plugin:build"
build.dependsOn ":${packageName}-native:build"
build.finalizedBy stopMySQLTestDockerContainer
build.finalizedBy stopMSSQLTestDockerContainer
//build.finalizedBy stopMSSQLTestDockerContainer
build.finalizedBy stopPostgreSQLTestDockerContainer

test.dependsOn ":${packageName}-compiler-plugin:build"
test.dependsOn ":${packageName}-native:build"
test.dependsOn startMySQLTestDockerContainer
test.dependsOn startMSSQLTestDockerContainer
//test.dependsOn startMSSQLTestDockerContainer
test.dependsOn startPostgreSQLTestDockerContainer
9 changes: 9 additions & 0 deletions ballerina/constants.bal
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,12 @@ public final DataSourceSpecifics & readonly POSTGRESQL_SPECIFICS = {
duplicateKeyStartIndicator: "Detail: Key ",
duplicateKeyEndIndicator: " already exists."
};

public final DataSourceSpecifics & readonly H2_SPECIFICS = {
quoteOpen: "\"",
quoteClose: "\"",
constraintViolationErrorMessage: "Referential integrity constraint violation",
duplicateEntryErrorMessage: "Unique index or primary key violation",
duplicateKeyStartIndicator: "Unique index or primary key violation: \"",
duplicateKeyEndIndicator: " ON "
};
9 changes: 9 additions & 0 deletions ballerina/tests/Config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
h2Url="jdbc:h2:./build/test"
h2User="sa"
h2Password=""

[mysql]
user="root"
password="Test123#"
Expand All @@ -22,3 +26,8 @@ password="postgres"
host="localhost"
port=5432
database="test"

[h2]
url="jdbc:h2:./build/test"
user="sa"
password=""
Loading

0 comments on commit a78aebd

Please sign in to comment.