Skip to content

Commit d3dae95

Browse files
authored
Adding Code Example and scripts from OraOperator blog (#301)
* Added sample code and accompanying scripts for Oracle DB advanced Data Types * Updated license * Added examples and updates for Blog * Added cicd-github-actions
1 parent dd120a8 commit d3dae95

18 files changed

+420
-0
lines changed

java/cicd-github-actions/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
venv
2+
.idea
3+
.DS_Store

java/cicd-github-actions/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Source code for the blog: Database CI/CD with the Oracle DB Operator, GitHub Actions and Liquibase
2+
3+
Secrets required
4+
DB_WALLET_PASSWORD - Password for DB wallet
5+
FT_DEFAULT_ADMIN_PASSWORD - Default DB password
6+
OCI_COMPARTMENT_OCID - Compartment OCID
7+
OCI_CLI_USER - User OCID
8+
OCI_CLI_TENANCY - User Tenancy OCID
9+
OCI_CLI_FINGERPRINT - User Fingerprint
10+
OCI_CLI_KEY_CONTENT - User Private Key Content
11+
OCI_CLI_REGION - User Region
12+
OKE_CLUSTER_OCID - OKE Cluster OCID
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
metadata:
4+
name: arbitrary
5+
6+
7+
resources:
8+
- sidb.yaml
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#
2+
# Copyright (c) 2023, Oracle and/or its affiliates.
3+
# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
4+
#
5+
6+
apiVersion: database.oracle.com/v1alpha1
7+
kind: SingleInstanceDatabase
8+
metadata:
9+
name: db-
10+
spec:
11+
12+
# Setup with LoadBalancer
13+
loadBalancer: true
14+
15+
## Use only alphanumeric characters for sid
16+
sid: XE
17+
18+
## DB edition
19+
edition: express
20+
21+
## Secret containing SIDB password mapped to secretKey
22+
adminPassword:
23+
secretName: default-admin-password
24+
25+
## Database image details
26+
image:
27+
## Oracle Database Free is only supported from DB version 23.2 onwards
28+
pullFrom: container-registry.oracle.com/database/express:latest
29+
prebuiltDB: true
30+
31+
## size is the required minimum size of the persistent volume
32+
## storageClass is specified for automatic volume provisioning
33+
## accessMode can only accept one of ReadWriteOnce, ReadWriteMany
34+
persistence:
35+
size: 50Gi
36+
## oci-bv applies to OCI block volumes. Use "standard" storageClass for dynamic provisioning in Minikube. Update as appropriate for other cloud service providers
37+
storageClass: "oci-bv"
38+
accessMode: "ReadWriteOnce"
39+
40+
## Count of Database Pods. Should be 1 for express edition.
41+
replicas: 1
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
<databaseChangeLog
3+
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
6+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
7+
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.6.xsd
8+
http://www.liquibase.org/xml/ns/pro
9+
http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.6.xsd ">
10+
11+
<include file="schema_a.sql" />
12+
<include file="schema_b.sql" />
13+
14+
</databaseChangeLog>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- liquibase formatted sql
2+
3+
-- changeset liquibase:1
4+
CREATE USER "SCHEMA_A" NO AUTHENTICATION;
5+
ALTER USER "SCHEMA_A" GRANT CONNECT THROUGH SYSTEM;
6+
GRANT UNLIMITED TABLESPACE TO "SCHEMA_A";
7+
GRANT CONNECT, RESOURCE TO "SCHEMA_A";
8+
ALTER USER "SCHEMA_A" DEFAULT ROLE CONNECT, RESOURCE;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- liquibase formatted sql
2+
3+
-- changeset liquibase:2
4+
CREATE USER "SCHEMA_B" NO AUTHENTICATION;
5+
ALTER USER "SCHEMA_B" GRANT CONNECT THROUGH SYSTEM;
6+
GRANT UNLIMITED TABLESPACE TO "SCHEMA_B";
7+
GRANT CONNECT, RESOURCE TO "SCHEMA_B";
8+
ALTER USER "SCHEMA_B" DEFAULT ROLE CONNECT, RESOURCE;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<databaseChangeLog
2+
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
5+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
6+
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.6.xsd
7+
http://www.liquibase.org/xml/ns/pro
8+
http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.6.xsd ">
9+
10+
<include file="departments.sql" />
11+
<include file="departments_lookup.sql" />
12+
<!-- employees has a foreign key constraint on departments -->
13+
<include file="employees.sql" />
14+
15+
16+
</databaseChangeLog>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- liquibase formatted sql
2+
3+
-- changeset liquibase:1
4+
CREATE TABLE SCHEMA_A.DEPARTMENTS (
5+
DEPARTMENT_ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY,
6+
DEPARTMENT_NAME VARCHAR2(80) NOT NULL
7+
) LOGGING;
8+
9+
10+
--rollback DROP TABLE SCHEMA_A.DEPARTMENTS;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- liquibase formatted sql
2+
3+
-- changeset liquibase:Initial
4+
INSERT INTO SCHEMA_A.DEPARTMENTS (DEPARTMENT_ID, DEPARTMENT_NAME) VALUES (1, 'Engineering');
5+
INSERT INTO SCHEMA_A.DEPARTMENTS (DEPARTMENT_ID, DEPARTMENT_NAME) VALUES (2, 'Sales');
6+
INSERT INTO SCHEMA_A.DEPARTMENTS (DEPARTMENT_ID, DEPARTMENT_NAME) VALUES (3, 'HR');
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- liquibase formatted sql
2+
3+
-- changeset liquibase:2
4+
CREATE TABLE SCHEMA_A.EMPLOYEES (
5+
EMPLOYEE_ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY,
6+
EMPLOYEE_FIRSTNAME VARCHAR2 (255) NOT NULL,
7+
EMPLOYEE_LASTNAME VARCHAR2 (255) NOT NULL,
8+
DEPARTMENT_ID NUMBER REFERENCES SCHEMA_A.DEPARTMENTS(DEPARTMENT_ID)
9+
) LOGGING;
10+
11+
12+
--rollback DROP TABLE SCHEMA_A.EMPLOYEES;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
<databaseChangeLog
3+
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
6+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
7+
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.6.xsd
8+
http://www.liquibase.org/xml/ns/pro
9+
http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.6.xsd ">
10+
11+
<include file="tasks.sql" />
12+
13+
</databaseChangeLog>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- liquibase formatted sql
2+
3+
-- changeset liquibase:1
4+
CREATE TABLE SCHEMA_B.TASKS (
5+
TASK_ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY,
6+
TASK_NAME VARCHAR2(300) NOT NULL,
7+
TASK_DESCRIPTION VARCHAR2(4000)
8+
) LOGGING;
9+
10+
11+
--rollback DROP TABLE SCHEMA_B.TASKS;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
IDENTIFICATION=$1
4+
5+
while : ; do
6+
7+
# get status
8+
STATUS=$(kubectl get -n "feature-$IDENTIFICATION" singleinstancedatabase "db-$IDENTIFICATION" -o 'jsonpath={.status.status}')
9+
# if database is available, end while loop; else sleep
10+
if [[ "$STATUS" == "Healthy" ]]; then
11+
break;
12+
else
13+
echo "Checking for DB Availability..."
14+
sleep 5;
15+
fi;
16+
17+
done;
18+
19+
echo "Database now available."
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
# set token
4+
IDENTIFICATION=$(python get.py "$BRANCH_REF")
5+
DBNAME=$(python clean_dbname.py "$IDENTIFICATION")
6+
7+
# kustomize remove if exists
8+
if test -f "kustomization.yaml"; then
9+
rm kustomization.yaml
10+
fi
11+
12+
# create patch
13+
cat <<EOF > patch.yaml
14+
- op: add # action
15+
path: "/spec/details/compartmentOCID"
16+
value: ${COMPARTMENT_OCID}
17+
- op: add # action
18+
path: "/spec/details/dbName"
19+
value: db${DBNAME}
20+
- op: add # action
21+
path: "/spec/details/displayName"
22+
value: dbtest-${IDENTIFICATION}
23+
EOF
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sys
2+
import re
3+
import sys
4+
import os
5+
import json
6+
7+
8+
9+
def list_schemas(location):
10+
d = [ x.removeprefix(f'{location}/') for x in [x[0] for x in os.walk(location) if x[0] != location ] ]
11+
d.remove("admin")
12+
return json.dumps({"schemas": d})
13+
14+
15+
def clean(with_name):
16+
# used for database name
17+
return re.sub('[^a-z0-9_$#]', '', with_name)
18+
19+
20+
def get_feature_name(listing):
21+
# expects list of [x, feature/A102, y]
22+
23+
for x in listing:
24+
if x.startswith("feature"):
25+
return x
26+
27+
def get_identification(from_feature_name):
28+
# expects identification to be after feature
29+
30+
listing = from_feature_name.split("/")
31+
for i in range(len(listing)):
32+
if listing[i].lower().startswith("feature") and i+1 < len(listing):
33+
return listing[i+1].lower()
34+
35+
36+
# expects [filename] [command] [inputs]
37+
command = sys.argv[1]
38+
39+
if command == "dbname":
40+
print(clean(sys.argv[2]))
41+
elif command == "id":
42+
names = get_feature_name(sys.argv[2:])
43+
ident = get_identification(names)
44+
print(ident)
45+
elif command == "schemas":
46+
print(list_schemas(sys.argv[2]))

0 commit comments

Comments
 (0)