Skip to content

Commit

Permalink
chore: change to pointer
Browse files Browse the repository at this point in the history
atlas/schema: added concurrent_index support

update template for .ConcurrentIndex

schema: allow control transaction mode

chore: update codegen

chore: fixed error message
  • Loading branch information
giautm committed Jan 15, 2024
1 parent e573477 commit fde341c
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 25 deletions.
23 changes: 18 additions & 5 deletions api/v1alpha1/atlasschema_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ type (
DevURLFrom Secret `json:"devURLFrom,omitempty"`
// Exclude a list of glob patterns used to filter existing resources being taken into account.
Exclude []string `json:"exclude,omitempty"`
// TxMode defines the transaction mode to use when applying the schema.
// +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"`
}
Expand All @@ -87,20 +90,21 @@ 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 {
Error bool `json:"error,omitempty"`
}
// Diff defines the diff policies to apply when planning schema changes.
Diff struct {
Skip SkipChanges `json:"skip,omitempty"`
ConcurrentIndex *ConcurrentIndex `json:"concurrent_index,omitempty"`
Skip *SkipChanges `json:"skip,omitempty"`
}
// SkipChanges represents the skip changes policy.
SkipChanges struct {
Expand Down Expand Up @@ -135,6 +139,15 @@ type (
// +optional
ModifyForeignKey bool `json:"modify_foreign_key,omitempty"`
}
ConcurrentIndex struct {
// +optional
Create bool `json:"create,omitempty"`
// +optional
Drop bool `json:"drop,omitempty"`
}
// TransactionMode
// +kubebuilder:validation:Enum=file;all;none
TransactionMode string
)

func init() {
Expand Down
50 changes: 45 additions & 5 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions charts/atlas-operator/templates/crds/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,13 @@ spec:
description: Diff defines the diff policies to apply when planning
schema changes.
properties:
concurrent_index:
properties:
create:
type: boolean
drop:
type: boolean
type: object
skip:
description: SkipChanges represents the skip changes policy.
properties:
Expand Down Expand Up @@ -603,6 +610,15 @@ spec:
items:
type: string
type: array
txMode:
default: file
description: TxMode defines the transaction mode to use when applying
the schema.
enum:
- file
- all
- none
type: string
url:
description: URL of the target database schema.
type: string
Expand Down
16 changes: 16 additions & 0 deletions config/crd/bases/db.atlasgo.io_atlasschemas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ spec:
description: Diff defines the diff policies to apply when planning
schema changes.
properties:
concurrent_index:
properties:
create:
type: boolean
drop:
type: boolean
type: object
skip:
description: SkipChanges represents the skip changes policy.
properties:
Expand Down Expand Up @@ -272,6 +279,15 @@ spec:
items:
type: string
type: array
txMode:
default: file
description: TxMode defines the transaction mode to use when applying
the schema.
enum:
- file
- all
- none
type: string
url:
description: URL of the target database schema.
type: string
Expand Down
17 changes: 12 additions & 5 deletions controllers/atlasschema_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ type (
DevURL string
Schemas []string
Exclude []string
Policy dbv1alpha1.Policy
Policy *dbv1alpha1.Policy
TxMode dbv1alpha1.TransactionMode

desired []byte
ext string
Expand Down Expand Up @@ -199,7 +200,7 @@ func (r *AtlasSchemaReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return result(err)
}
}
report, err := r.apply(ctx, wd.Path(), data.EnvName)
report, err := r.apply(ctx, wd.Path(), data.EnvName, string(data.TxMode))
if err != nil {
res.SetNotReady("ApplyingSchema", err.Error())
r.recorder.Event(res, corev1.EventTypeWarning, "ApplyingSchema", err.Error())
Expand Down Expand Up @@ -255,13 +256,14 @@ func (r *AtlasSchemaReconciler) watchRefs(res *dbv1alpha1.AtlasSchema) {
}
}

func (r *AtlasSchemaReconciler) apply(ctx context.Context, dir, envName string) (*atlas.SchemaApply, error) {
func (r *AtlasSchemaReconciler) apply(ctx context.Context, dir, envName, txMode string) (*atlas.SchemaApply, error) {
cli, err := atlas.NewClient(dir, r.execPath)
if err != nil {
return nil, err
}
return cli.SchemaApply(ctx, &atlas.SchemaApplyParams{
Env: envName,
Env: envName,
TxMode: txMode,
})
}

Expand All @@ -275,6 +277,7 @@ func (r *AtlasSchemaReconciler) extractData(ctx context.Context, res *dbv1alpha1
Schemas: s.Schemas,
Exclude: s.Exclude,
Policy: s.Policy,
TxMode: s.TxMode,
}
)
data.URL, err = s.DatabaseURL(ctx, r, res.Namespace)
Expand Down Expand Up @@ -311,7 +314,11 @@ func (d *managedData) Source() string {

// ShouldLint returns true if the linting policy is set to error.
func (d *managedData) shouldLint() bool {
return d.Policy.Lint.Destructive.Error
p := d.Policy
if p == nil || p.Lint == nil || p.Lint.Destructive == nil {
return false
}
return p.Lint.Destructive.Error
}

// hash returns the sha256 hash of the desired.
Expand Down
30 changes: 21 additions & 9 deletions controllers/atlasschema_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,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);")
Expand Down Expand Up @@ -360,7 +364,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())
Expand All @@ -378,8 +386,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)
Expand All @@ -402,12 +414,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,
},
Expand Down
16 changes: 15 additions & 1 deletion controllers/templates/atlas_schema.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@ variable "lint_destructive" {
{{- end }}
}
{{- with .Policy }}
{{- with .Diff.Skip }}
{{- with .Diff }}
{{- if or .ConcurrentIndex .Skip }}
diff {
{{- with .ConcurrentIndex }}
concurrent_index {
{{- if .Create }}
create = true
{{- end }}
{{- if .Drop }}
drop = true
{{- end }}
}
{{- end }}
{{- with .Skip }}
skip {
{{- if .AddSchema }}
add_schema = true
Expand Down Expand Up @@ -56,7 +68,9 @@ diff {
modify_foreign_key = true
{{- end }}
}
{{- end }}
}
{{- end }}
{{- end }}
{{- with .Lint }}
lint {
Expand Down

0 comments on commit fde341c

Please sign in to comment.