Skip to content

Commit

Permalink
Merge pull request #11 from go-teal/feature/indexes
Browse files Browse the repository at this point in the history
feat(indexes): indexes feature
  • Loading branch information
WWTLF authored Dec 5, 2024
2 parents 6064e1d + 0e323bd commit a6d5724
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ The asset profile can be specified via the `profile.yaml` file or via a GO templ
connection: 'default'
materialization: 'table'
is_data_framed: true
primary_key_fields:
- "id"
indexes:
- name: "wallet"
unique: false
fields:
- "wallet_id"
{{ end }}
select
Expand All @@ -325,6 +332,11 @@ select
|materialization|String|table|See [Materializations](#materializations).|
|is_data_framed|boolean|false|See [Cross-database references](#cross-database-references).|
|persist_inputs|boolean|false|See [Cross-database references](#cross-database-references).|
|primary_key_fields|Array of string||List of fields for the primary unique index|
|indexes|Array of Indexes||List of indexes for the asset (only for the table and incremental materializations)|
|indexes.`<name: IndexName>`|String||Name of the index|
|indexes.`<name: IndexName>`.Unique|boolean|false|flag of the uniqueness of the Index|
|indexes.`<name: IndexName>`.fields|Array of string||List of fields for the index|

## Materializations

Expand Down
Binary file modified internal/application/templates/scaffold.tar.gz
Binary file not shown.
11 changes: 11 additions & 0 deletions internal/domain/generators/templates/dwh_sql_model_asset.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{{- $mp := .ModelProfile }}
{{- $modelName := .ModelName }}
package assets

import (
Expand All @@ -18,6 +20,15 @@ as ({{ .SqlByteBuffer }});
{{- if ne .PrimaryKeyExpression "" }}
create unique index {{ .ModelProfile.Name }}_pkey on {{ .ModelName }} ({{ .PrimaryKeyExpression }});
{{- end }}

{{- range .Indexes }}
{{- if .Unique}}
create unique index {{ $mp.Name }}_{{ .IndexName }}_idx on {{ $modelName }} ({{ .IndexFields }});
{{- else}}
create index {{ $mp.Name }}_{{ .IndexName }}_idx on {{ $modelName }} ({{ .IndexFields }});
{{- end }}
{{- end }}

`
const SQL_{{ .NameUpperCase }}_INSERT = `
insert into {{ .ModelName }} ({{ .ModelFieldsFunc }}) ({{ .SqlByteBuffer }})
Expand Down
7 changes: 7 additions & 0 deletions internal/domain/internal_models/model_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ type ModelConfig struct {
ModelType ModelType
ModelFieldsFunc string
PrimaryKeyExpression string
Indexes []*IndexConfig
}

type IndexConfig struct {
IndexName string
Unique bool
IndexFields string
}
3 changes: 3 additions & 0 deletions internal/domain/services/sql_model_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func CombineProfiles(config *configs.Config, profiles *configs.ProjectProfile) {
if len(newModelProfile.PrimaryKeyFields) > 0 {
profile.PrimaryKeyFields = newModelProfile.PrimaryKeyFields
}
if len(newModelProfile.Indexes) > 0 {
profile.Indexes = newModelProfile.Indexes
}

if len(newModelProfile.Tests) > 0 {
profile.Tests = newModelProfile.Tests
Expand Down
11 changes: 11 additions & 0 deletions internal/domain/services/sql_models_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ func InitSQLModelConfigs(config *configs.Config, profiles *configs.ProjectProfil
if len(modelProfile.PrimaryKeyFields) > 0 {
data.PrimaryKeyExpression = strings.Join(defaultModelProfile.PrimaryKeyFields, ", ")
}

if len(modelProfile.Indexes) > 0 {
for _, index := range modelProfile.Indexes {
data.Indexes = append(data.Indexes, &internalmodels.IndexConfig{
IndexName: index.Name,
Unique: index.Unique,
IndexFields: strings.Join(index.Fields, ", "),
})
}

}
modelsConfigs = append(modelsConfigs, data)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/configs/config_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"gopkg.in/yaml.v2"
)

const TEAL_VERSION = "v0.1.6"
const TEAL_VERSION = "v0.1.10"

type ConfigService struct {
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/configs/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,20 @@ type ModelProfile struct {
Connection string `yaml:"connection"`
Materialization MatType `yaml:"materialization"`
PrimaryKeyFields []string `yaml:"primary_key_fields"`
Indexes []*DBIndex `yaml:"indexes"`
IsDataFramed bool `yaml:"is_data_framed"`
PersistInputs bool `yaml:"persist_inputs"`
Stage string `yaml:"-"`
Tests []*TestProfile `yaml:"tests"`
RawUpstreams []string `yaml:"raw_upstreams"`
}

type DBIndex struct {
Name string `yaml:"name"`
Unique bool `yaml:"unique"`
Fields []string `yaml:"fields"`
}

type TestProfile struct {
Name string `yaml:"name"`
Connection string `yaml:"connection"`
Expand Down

0 comments on commit a6d5724

Please sign in to comment.