Skip to content

Commit

Permalink
atlas/schema: handle no plans case (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm authored Nov 24, 2024
1 parent 2fb4209 commit fbf3475
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
10 changes: 7 additions & 3 deletions api/v1alpha1/atlasschema_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,14 @@ func (sc *AtlasSchema) IsHashModified(hash string) bool {
// SetReady sets the Ready condition to true
func (sc *AtlasSchema) SetReady(status AtlasSchemaStatus, report any) {
var msg string
if j, err := json.Marshal(report); err != nil {
msg = fmt.Sprintf("Error marshalling apply response: %v", err)
if report != nil {
if j, err := json.Marshal(report); err != nil {
msg = fmt.Sprintf("Error marshalling apply response: %v", err)
} else {
msg = fmt.Sprintf("The schema has been applied successfully. Apply response: %s", j)
}
} else {
msg = fmt.Sprintf("The schema has been applied successfully. Apply response: %s", j)
msg = "The schema has been applied successfully."
}
sc.Status = status
meta.SetStatusCondition(&sc.Status.Conditions, metav1.Condition{
Expand Down
25 changes: 17 additions & 8 deletions internal/controller/atlasschema_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,15 @@ func (r *AtlasSchemaReconciler) Reconcile(ctx context.Context, req ctrl.Request)
To: []string{desiredURL},
Pending: true,
})
if err != nil {
switch {
case err != nil && strings.Contains(err.Error(), "no changes to be made"):
res.SetReady(dbv1alpha1.AtlasSchemaStatus{
LastApplied: time.Now().Unix(),
ObservedHash: hash,
}, nil)
r.recorder.Event(res, corev1.EventTypeNormal, "Applied", "Applied schema")
return ctrl.Result{}, nil
case err != nil:
reason, msg := "SchemaPlan", err.Error()
res.SetNotReady(reason, msg)
r.recorder.Event(res, corev1.EventTypeWarning, reason, msg)
Expand All @@ -281,14 +289,15 @@ func (r *AtlasSchemaReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}
r.recordErrEvent(res, err)
return result(err)
default:
log.Info("created a new schema plan", "plan", plan.File.URL, "desiredURL", desiredURL)
res.Status.PlanURL = plan.File.URL
res.Status.PlanLink = plan.File.Link
reason, msg := "ApprovalPending", "Schema plan is waiting for approval"
res.SetNotReady(reason, msg)
r.recorder.Event(res, corev1.EventTypeNormal, reason, msg)
return ctrl.Result{RequeueAfter: time.Second * 5}, nil
}
log.Info("created a new schema plan", "plan", plan.File.URL, "desiredURL", desiredURL)
res.Status.PlanURL = plan.File.URL
res.Status.PlanLink = plan.File.Link
reason, msg := "ApprovalPending", "Schema plan is waiting for approval"
res.SetNotReady(reason, msg)
r.recorder.Event(res, corev1.EventTypeNormal, reason, msg)
return ctrl.Result{RequeueAfter: time.Second * 5}, nil
}
// List the schema plans to check if there are any plans.
switch plans, err := cli.SchemaPlanList(ctx, &atlasexec.SchemaPlanListParams{
Expand Down
23 changes: 23 additions & 0 deletions test/e2e/testscript/schema-review-always.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ kubectl-wait-ready AtlasSchema/postgres
# Inspect the schema to ensure it's correct
atlas schema inspect -u ${DB_URL}
cmp stdout schema-v1.hcl
kubectl get -o jsonpath --template='{.status.observed_hash}' AtlasSchema/postgres
stdout 2a59599f3d4f2f07d4fabb87bf872030020fd7410e10a1042ace7a0865953cac

# Apply again without any change in the schema, it should be a no-op
kubectl patch -f schema.yaml --type merge --patch-file patch-comment.yaml
# Ensure the controller is aware of the change
exec sleep 10
kubectl-wait-ready AtlasSchema/postgres
# Inspect the schema to ensure it's correct
atlas schema inspect -u ${DB_URL}
cmp stdout schema-v1.hcl
kubectl get -o jsonpath --template='{.status.observed_hash}' AtlasSchema/postgres
stdout 6a60e051a482fb40392f768e0a080bc101bd9d87efa21ff40472b1dbd7d4e7a8
-- empty.hcl --
schema "public" {
comment = "standard public schema"
Expand All @@ -51,6 +64,16 @@ table "t1" {
schema "public" {
comment = "standard public schema"
}
-- patch-comment.yaml --
spec:
schema:
sql: |
-- added some comment to cause the hash change
create table t1 (
id int not null,
c1 int,
primary key (id)
);
-- schema.yaml --
apiVersion: db.atlasgo.io/v1alpha1
kind: AtlasSchema
Expand Down

0 comments on commit fbf3475

Please sign in to comment.