From 91233dd58264eee2d0629f5f14bd2afd0499ae7e Mon Sep 17 00:00:00 2001 From: "Giau. Tran Minh" Date: Mon, 27 Nov 2023 15:45:58 +0700 Subject: [PATCH] chore: change to pointer --- api/v1alpha1/atlasschema_types.go | 12 ++++---- api/v1alpha1/zz_generated.deepcopy.go | 36 ++++++++++++++++++---- controllers/atlasschema_controller.go | 2 +- controllers/atlasschema_controller_test.go | 30 ++++++++++++------ controllers/templates/atlas_schema.tmpl | 4 ++- 5 files changed, 61 insertions(+), 23 deletions(-) diff --git a/api/v1alpha1/atlasschema_types.go b/api/v1alpha1/atlasschema_types.go index af801c13..9442479b 100644 --- a/api/v1alpha1/atlasschema_types.go +++ b/api/v1alpha1/atlasschema_types.go @@ -78,7 +78,7 @@ type ( // +kubebuilder:default=file TxMode TransactionMode `json:"txMode,omitempty"` // Policy defines the policies to apply when managing the schema change lifecycle. - Policy Policy `json:"policy,omitempty"` + Policy *Policy `json:"policy,omitempty"` // The names of the schemas (named databases) on the target database to be managed. Schemas []string `json:"schemas,omitempty"` } @@ -90,12 +90,12 @@ type ( } // Policy defines the policies to apply when managing the schema change lifecycle. Policy struct { - Lint Lint `json:"lint,omitempty"` - Diff Diff `json:"diff,omitempty"` + Lint *Lint `json:"lint,omitempty"` + Diff *Diff `json:"diff,omitempty"` } // Lint defines the linting policies to apply before applying the schema. Lint struct { - Destructive CheckConfig `json:"destructive,omitempty"` + Destructive *CheckConfig `json:"destructive,omitempty"` } // CheckConfig defines the configuration of a linting check. CheckConfig struct { @@ -103,8 +103,8 @@ type ( } // Diff defines the diff policies to apply when planning schema changes. Diff struct { - ConcurrentIndex ConcurrentIndex `json:"concurrent_index,omitempty"` - Skip SkipChanges `json:"skip,omitempty"` + ConcurrentIndex *ConcurrentIndex `json:"concurrent_index,omitempty"` + Skip *SkipChanges `json:"skip,omitempty"` } // SkipChanges represents the skip changes policy. SkipChanges struct { diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 113481ca..7b613176 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -193,7 +193,11 @@ func (in *AtlasSchemaSpec) DeepCopyInto(out *AtlasSchemaSpec) { *out = make([]string, len(*in)) copy(*out, *in) } - out.Policy = in.Policy + if in.Policy != nil { + in, out := &in.Policy, &out.Policy + *out = new(Policy) + (*in).DeepCopyInto(*out) + } if in.Schemas != nil { in, out := &in.Schemas, &out.Schemas *out = make([]string, len(*in)) @@ -307,8 +311,16 @@ func (in *Credentials) DeepCopy() *Credentials { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Diff) DeepCopyInto(out *Diff) { *out = *in - out.ConcurrentIndex = in.ConcurrentIndex - out.Skip = in.Skip + if in.ConcurrentIndex != nil { + in, out := &in.ConcurrentIndex, &out.ConcurrentIndex + *out = new(ConcurrentIndex) + **out = **in + } + if in.Skip != nil { + in, out := &in.Skip, &out.Skip + *out = new(SkipChanges) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Diff. @@ -352,7 +364,11 @@ func (in *Dir) DeepCopy() *Dir { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Lint) DeepCopyInto(out *Lint) { *out = *in - out.Destructive = in.Destructive + if in.Destructive != nil { + in, out := &in.Destructive, &out.Destructive + *out = new(CheckConfig) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Lint. @@ -368,8 +384,16 @@ func (in *Lint) DeepCopy() *Lint { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Policy) DeepCopyInto(out *Policy) { *out = *in - out.Lint = in.Lint - out.Diff = in.Diff + if in.Lint != nil { + in, out := &in.Lint, &out.Lint + *out = new(Lint) + (*in).DeepCopyInto(*out) + } + if in.Diff != nil { + in, out := &in.Diff, &out.Diff + *out = new(Diff) + (*in).DeepCopyInto(*out) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Policy. diff --git a/controllers/atlasschema_controller.go b/controllers/atlasschema_controller.go index afe79a23..1281a85d 100644 --- a/controllers/atlasschema_controller.go +++ b/controllers/atlasschema_controller.go @@ -67,7 +67,7 @@ type ( DevURL string Schemas []string Exclude []string - Policy dbv1alpha1.Policy + Policy *dbv1alpha1.Policy desired []byte ext string diff --git a/controllers/atlasschema_controller_test.go b/controllers/atlasschema_controller_test.go index 092fe556..8caa2d22 100644 --- a/controllers/atlasschema_controller_test.go +++ b/controllers/atlasschema_controller_test.go @@ -311,7 +311,11 @@ func TestReconcile_Lint(t *testing.T) { tt := cliTest(t) sc := conditionReconciling() sc.Spec.URL = tt.dburl - sc.Spec.Policy.Lint.Destructive.Error = true + sc.Spec.Policy = &dbv1alpha1.Policy{ + Lint: &dbv1alpha1.Lint{ + Destructive: &dbv1alpha1.CheckConfig{Error: true}, + }, + } sc.Status.LastApplied = 1 tt.k8s.put(sc) tt.initDB("create table x (c int);") @@ -359,7 +363,11 @@ func TestBadSQL(t *testing.T) { sc := conditionReconciling() sc.Spec.Schema.SQL = "bad sql;" sc.Spec.URL = tt.dburl - sc.Spec.Policy.Lint.Destructive.Error = true + sc.Spec.Policy = &dbv1alpha1.Policy{ + Lint: &dbv1alpha1.Lint{ + Destructive: &dbv1alpha1.CheckConfig{Error: true}, + }, + } sc.Status.LastApplied = 1 tt.k8s.put(sc) resp, err := tt.r.Reconcile(context.Background(), req()) @@ -377,8 +385,12 @@ func TestDiffPolicy(t *testing.T) { sc := conditionReconciling() sc.Spec.URL = tt.dburl sc.Spec.Schema.SQL = "create table y (c int);" - sc.Spec.Policy.Diff.Skip = dbv1alpha1.SkipChanges{ - DropTable: true, + sc.Spec.Policy = &dbv1alpha1.Policy{ + Diff: &dbv1alpha1.Diff{ + Skip: &dbv1alpha1.SkipChanges{ + DropTable: true, + }, + }, } sc.Status.LastApplied = 1 tt.k8s.put(sc) @@ -401,12 +413,12 @@ func TestConfigTemplate(t *testing.T) { EnvName: defaultEnvName, URL: must(url.Parse("mysql://root:password@localhost:3306/test")), DevURL: "mysql://root:password@localhost:3306/dev", - Policy: dbv1alpha1.Policy{ - Lint: dbv1alpha1.Lint{ - Destructive: dbv1alpha1.CheckConfig{Error: true}, + Policy: &dbv1alpha1.Policy{ + Lint: &dbv1alpha1.Lint{ + Destructive: &dbv1alpha1.CheckConfig{Error: true}, }, - Diff: dbv1alpha1.Diff{ - Skip: dbv1alpha1.SkipChanges{ + Diff: &dbv1alpha1.Diff{ + Skip: &dbv1alpha1.SkipChanges{ DropSchema: true, DropTable: true, }, diff --git a/controllers/templates/atlas_schema.tmpl b/controllers/templates/atlas_schema.tmpl index 66213a0c..8ed8500f 100644 --- a/controllers/templates/atlas_schema.tmpl +++ b/controllers/templates/atlas_schema.tmpl @@ -7,8 +7,9 @@ variable "lint_destructive" { {{- end }} } {{- with .Policy }} - {{- with .Diff.Skip }} + {{- with .Diff }} diff { + {{- with .Skip }} skip { {{- if .AddSchema }} add_schema = true @@ -56,6 +57,7 @@ diff { modify_foreign_key = true {{- end }} } + {{- end }} } {{- end }} {{- with .Lint }}