From 14155f8d7a9a36fea2fc3062093a3fabb8802a77 Mon Sep 17 00:00:00 2001 From: Taufan Adhitya Date: Thu, 24 Oct 2024 16:23:44 +0700 Subject: [PATCH 1/5] fix: skip index recreation --- pkg/resource/tables/compare.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/resource/tables/compare.go b/pkg/resource/tables/compare.go index ad7a43db..cf1eda46 100644 --- a/pkg/resource/tables/compare.go +++ b/pkg/resource/tables/compare.go @@ -238,13 +238,13 @@ func compareRelations(table *objects.Table, source, target []objects.TablesRelat continue } - if t.Index == nil && sc.Index == nil { - updateItems = append(updateItems, objects.UpdateRelationItem{ - Data: sc, - Type: objects.UpdateRelationCreateIndex, - }) - Logger.Debug("create new index", "constrain-name", sc.ConstraintName) - } + // if t.Index == nil && sc.Index == nil { + // updateItems = append(updateItems, objects.UpdateRelationItem{ + // Data: sc, + // Type: objects.UpdateRelationCreateIndex, + // }) + // Logger.Debug("create new index", "constrain-name", sc.ConstraintName) + // } if t.Action != nil && sc.Action != nil { if t.Action.UpdateAction != sc.Action.UpdateAction { From bfbb805f4ca9e9a7a26115199991cfc9d189e52c Mon Sep 17 00:00:00 2001 From: Taufan Adhitya Date: Fri, 25 Oct 2024 18:49:35 +0700 Subject: [PATCH 2/5] feat: introduce indexed tag --- pkg/generator/model.go | 4 ++++ pkg/generator/model_test.go | 1 + 2 files changed, 5 insertions(+) diff --git a/pkg/generator/model.go b/pkg/generator/model.go index 60391662..11de391a 100644 --- a/pkg/generator/model.go +++ b/pkg/generator/model.go @@ -274,6 +274,10 @@ func BuildRelationTag(r *state.Relation) string { jsonTag := fmt.Sprintf("json:%q", utils.ToSnakeCase(r.Table)+",omitempty") tags = append(tags, jsonTag) + if r.Index != nil { + tags = append(tags, "indexed") + } + if r.Action != nil { onUpdate, onDelete := objects.RelationActionDefaultLabel, objects.RelationActionDefaultLabel diff --git a/pkg/generator/model_test.go b/pkg/generator/model_test.go index fcd9374e..fd1384ca 100644 --- a/pkg/generator/model_test.go +++ b/pkg/generator/model_test.go @@ -48,6 +48,7 @@ func TestGenerateModels(t *testing.T) { ForeignKey: "test_table_id", PrimaryKey: "id", Action: &relationshipAction, + Index: &objects.Index{Schema: "public", Table: "related_table", Name: "test_table_id", Definition: "CREATE INDEX test_table_id ON related_table(test_table_id);"}, }, }, Policies: objects.Policies{}, From 3e9f8c3a202f859a558691d5096df6f61d578e1b Mon Sep 17 00:00:00 2001 From: Taufan Adhitya Date: Fri, 25 Oct 2024 20:05:14 +0700 Subject: [PATCH 3/5] feat: add index on state --- pkg/generator/model.go | 2 +- pkg/state/table.go | 11 +++++++++++ pkg/state/table_test.go | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/generator/model.go b/pkg/generator/model.go index 11de391a..47fd0da9 100644 --- a/pkg/generator/model.go +++ b/pkg/generator/model.go @@ -275,7 +275,7 @@ func BuildRelationTag(r *state.Relation) string { tags = append(tags, jsonTag) if r.Index != nil { - tags = append(tags, "indexed") + tags = append(tags, "indexed:true") } if r.Action != nil { diff --git a/pkg/state/table.go b/pkg/state/table.go index e295d55f..db6d30fb 100644 --- a/pkg/state/table.go +++ b/pkg/state/table.go @@ -172,6 +172,17 @@ func buildTableFromModel(model any) (ei ExtractTableItem) { } } + // check index field + indexed := field.Tag.Get("indexed") + if len(indexed) > 0 { + rel.Index = &objects.Index{ + Schema: ei.Table.Schema, + Table: rel.TargetTableName, + Name: fmt.Sprintf("ix_%s_%s", rel.TargetTableName, rel.TargetColumnName), + Definition: fmt.Sprintf("CREATE INDEX ix_%s_%s ON %s(%s);", rel.TargetTableName, rel.TargetColumnName, rel.TargetTableName, rel.TargetColumnName), + } + } + ei.Table.Relationships = append(ei.Table.Relationships, rel) } } diff --git a/pkg/state/table_test.go b/pkg/state/table_test.go index 5f12e7be..aeeb9c7d 100644 --- a/pkg/state/table_test.go +++ b/pkg/state/table_test.go @@ -24,7 +24,7 @@ type Submission struct { Acl string `json:"-" read:"anon" write:"anon"` // Relations - Candidate *Candidate `json:"candidate,omitempty" join:"joinType:hasOne;primaryKey:id;foreignKey:candidate_id" onUpdate:"cascade" onDelete:"cascade"` + Candidate *Candidate `json:"candidate,omitempty" join:"joinType:hasOne;primaryKey:id;foreignKey:candidate_id" onUpdate:"cascade" onDelete:"cascade" indexed:"true"` } type Candidate struct { From 23d6e872516c987659cc3f84ef3955f71b26123d Mon Sep 17 00:00:00 2001 From: Taufan Adhitya Date: Fri, 25 Oct 2024 20:25:05 +0700 Subject: [PATCH 4/5] feat: debugging index --- pkg/resource/import.go | 1 + pkg/resource/tables/compare.go | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/resource/import.go b/pkg/resource/import.go index 47b06b8b..b8a95896 100644 --- a/pkg/resource/import.go +++ b/pkg/resource/import.go @@ -44,6 +44,7 @@ func Import(flags *Flags, config *raiden.Config) error { if err != nil { return err } + ImportLogger.Trace("indexes", spResource.Indexes) spResource.Tables = tables.AttachIndexAndAction(spResource.Tables, spResource.Indexes, spResource.RelationActions) // create import state diff --git a/pkg/resource/tables/compare.go b/pkg/resource/tables/compare.go index cf1eda46..f223e55a 100644 --- a/pkg/resource/tables/compare.go +++ b/pkg/resource/tables/compare.go @@ -238,13 +238,17 @@ func compareRelations(table *objects.Table, source, target []objects.TablesRelat continue } - // if t.Index == nil && sc.Index == nil { + if t.Index == nil && sc.Index == nil { // updateItems = append(updateItems, objects.UpdateRelationItem{ // Data: sc, // Type: objects.UpdateRelationCreateIndex, // }) - // Logger.Debug("create new index", "constrain-name", sc.ConstraintName) - // } + Logger.Debug("create new index", "constrain-name", sc.ConstraintName) + } else { + Logger.Debug("check-index", "t-index", t.Index) + Logger.Debug("check-index", "sc-index", sc.Index) + Logger.Debug("updating index", "constrain-name", sc.ConstraintName) + } if t.Action != nil && sc.Action != nil { if t.Action.UpdateAction != sc.Action.UpdateAction { From 7966df91d10e34c1a37b8405c9c39635b82919a1 Mon Sep 17 00:00:00 2001 From: Taufan Adhitya Date: Fri, 25 Oct 2024 21:30:38 +0700 Subject: [PATCH 5/5] fix: linter --- pkg/resource/tables/compare.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/resource/tables/compare.go b/pkg/resource/tables/compare.go index f223e55a..bea5946b 100644 --- a/pkg/resource/tables/compare.go +++ b/pkg/resource/tables/compare.go @@ -239,10 +239,10 @@ func compareRelations(table *objects.Table, source, target []objects.TablesRelat } if t.Index == nil && sc.Index == nil { - // updateItems = append(updateItems, objects.UpdateRelationItem{ - // Data: sc, - // Type: objects.UpdateRelationCreateIndex, - // }) + // updateItems = append(updateItems, objects.UpdateRelationItem{ + // Data: sc, + // Type: objects.UpdateRelationCreateIndex, + // }) Logger.Debug("create new index", "constrain-name", sc.ConstraintName) } else { Logger.Debug("check-index", "t-index", t.Index)