Skip to content

Commit

Permalink
test/e2e: apply manual changes on the database and trigger controller…
Browse files Browse the repository at this point in the history
… recognize
  • Loading branch information
giautm committed Oct 14, 2024
1 parent e9ce6a4 commit 460f7d4
Showing 1 changed file with 180 additions and 0 deletions.
180 changes: 180 additions & 0 deletions test/e2e/testscript/schema-manual-changes.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
env DB_URL=postgres://root:pass@postgres.${NAMESPACE}:5432/postgres?sslmode=disable
kubectl apply -f database.yaml
kubectl create secret generic postgres-credentials --from-literal=url=${DB_URL}
# Wait for the DB ready before creating the schema
kubectl wait --for=condition=ready --timeout=60s -l app=postgres pods

# Create the secret to store ATLAS_TOKEN
kubectl create secret generic atlas-token --from-literal=ATLAS_TOKEN=${ATLAS_TOKEN}

# Sync the $WORK directory to the controller pod
kubectl cp -n ${CONTROLLER_NS} ${WORK} ${CONTROLLER}:/tmp/${NAMESPACE}/
env DEV_URL=postgres://root:pass@postgres.${NAMESPACE}:5433/postgres?sslmode=disable
# List all schema plans and remove them, it may come from previous failure runs
atlas schema plan list --format="{{range .}}{{println .URL}}{{end}}" --dev-url=${DEV_URL} --repo=atlas://atlas-operator --from=file:///tmp/${NAMESPACE}/manual.sql --to=file:///tmp/${NAMESPACE}/schema-v1.hcl
plans-rm stdout

# Create the schema
kubectl apply -f schema.yaml
kubectl wait --for=condition=ready --timeout=120s AtlasSchema/postgres

# Inspect the schema to ensure it's correct
atlas schema inspect -u ${DB_URL}
cmp stdout schema-v1.hcl

# Make a manual change to the database
atlas schema apply --auto-approve --url=${DB_URL} --dev-url=${DEV_URL} --to=file:///tmp/${NAMESPACE}/manual.sql

kubectl patch -f schema.yaml --type merge --patch-file patch-comment.yaml
# Ensure the controller is aware of the change
kubectl wait --for=jsonpath='{.status.conditions[*].reason}'=ApprovalPending --timeout=60s AtlasSchemas/postgres

# Get the plan URL from resource then approve it
kubectl get AtlasSchemas/postgres -o go-template --template='{{ .status.planURL }}'
envfile PLAN_URL=stdout
atlas schema plan approve --url=${PLAN_URL}

# The schema should be updated now
kubectl wait --for=condition=ready --timeout=120s AtlasSchemas/postgres
atlas schema inspect -u ${DB_URL}
cmp stdout schema-v1.hcl

# Cleanup schema plan for the next run
atlas schema plan rm --url=${PLAN_URL}
-- schema-v1.hcl --
table "t1" {
schema = schema.public
column "id" {
null = false
type = integer
}
column "c1" {
null = true
type = integer
}
primary_key {
columns = [column.id]
}
}
schema "public" {
comment = "standard public schema"
}
-- patch-comment.yaml --
spec:
schema:
sql: |
-- comment
create table t1 (
id int not null,
c1 int,
primary key (id)
);
-- manual.sql --
create table t1 (
id int not null,
c1 int,
c2 int,
primary key (id)
);
-- schema.yaml --
apiVersion: db.atlasgo.io/v1alpha1
kind: AtlasSchema
metadata:
name: postgres
spec:
urlFrom:
secretKeyRef:
name: postgres-credentials
key: url
cloud:
repo: atlas-operator
tokenFrom:
secretKeyRef:
name: atlas-token
key: ATLAS_TOKEN
schema:
sql: |
create table t1 (
id int not null,
c1 int,
primary key (id)
);
-- database.yaml --
apiVersion: v1
kind: Service
metadata:
name: postgres
spec:
selector:
app: postgres
ports:
- name: postgres
port: 5432
targetPort: postgres
- name: postgres-dev
port: 5433
targetPort: postgres-dev
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres
spec:
selector:
matchLabels:
app: postgres
replicas: 1
template:
metadata:
labels:
app: postgres
spec:
securityContext:
runAsNonRoot: true
runAsUser: 999
containers:
- name: postgres
image: postgres:15.4
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- all
env:
- name: POSTGRES_PASSWORD
value: pass
- name: POSTGRES_USER
value: root
ports:
- containerPort: 5432
name: postgres
readinessProbe:
initialDelaySeconds: 5
periodSeconds: 2
timeoutSeconds: 1
exec:
command: [ "pg_isready" ]
- name: postgres-dev
image: postgres:15.4
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- all
env:
- name: POSTGRES_PASSWORD
value: pass
- name: POSTGRES_USER
value: root
- name: PGPORT
value: "5433"
ports:
- containerPort: 5433
name: postgres-dev
readinessProbe:
initialDelaySeconds: 5
periodSeconds: 2
timeoutSeconds: 1
exec:
command: [ "pg_isready" ]

0 comments on commit 460f7d4

Please sign in to comment.