diff --git a/README.md b/README.md index d7695e1..531c992 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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.``|String||Name of the index| +|indexes.``.Unique|boolean|false|flag of the uniqueness of the Index| +|indexes.``.fields|Array of string||List of fields for the index| ## Materializations diff --git a/internal/application/templates/scaffold.tar.gz b/internal/application/templates/scaffold.tar.gz index 9b011a4..04411cd 100644 Binary files a/internal/application/templates/scaffold.tar.gz and b/internal/application/templates/scaffold.tar.gz differ diff --git a/internal/domain/generators/templates/dwh_sql_model_asset.tmpl b/internal/domain/generators/templates/dwh_sql_model_asset.tmpl index 58eb01f..e26032b 100644 --- a/internal/domain/generators/templates/dwh_sql_model_asset.tmpl +++ b/internal/domain/generators/templates/dwh_sql_model_asset.tmpl @@ -1,3 +1,5 @@ +{{- $mp := .ModelProfile }} +{{- $modelName := .ModelName }} package assets import ( @@ -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 }}) diff --git a/internal/domain/internal_models/model_config.go b/internal/domain/internal_models/model_config.go index b8e8f19..0a5ac62 100644 --- a/internal/domain/internal_models/model_config.go +++ b/internal/domain/internal_models/model_config.go @@ -30,4 +30,11 @@ type ModelConfig struct { ModelType ModelType ModelFieldsFunc string PrimaryKeyExpression string + Indexes []*IndexConfig +} + +type IndexConfig struct { + IndexName string + Unique bool + IndexFields string } diff --git a/internal/domain/services/sql_model_profiles.go b/internal/domain/services/sql_model_profiles.go index cd6c18b..9bd60a1 100644 --- a/internal/domain/services/sql_model_profiles.go +++ b/internal/domain/services/sql_model_profiles.go @@ -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 diff --git a/internal/domain/services/sql_models_factory.go b/internal/domain/services/sql_models_factory.go index f8d78ea..d1f8e87 100644 --- a/internal/domain/services/sql_models_factory.go +++ b/internal/domain/services/sql_models_factory.go @@ -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) } } diff --git a/pkg/configs/config_service.go b/pkg/configs/config_service.go index 9dca56b..e5d9bf0 100644 --- a/pkg/configs/config_service.go +++ b/pkg/configs/config_service.go @@ -7,7 +7,7 @@ import ( "gopkg.in/yaml.v2" ) -const TEAL_VERSION = "v0.1.6" +const TEAL_VERSION = "v0.1.10" type ConfigService struct { } diff --git a/pkg/configs/profile.go b/pkg/configs/profile.go index 919550c..bd7a2d5 100644 --- a/pkg/configs/profile.go +++ b/pkg/configs/profile.go @@ -40,6 +40,7 @@ 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:"-"` @@ -47,6 +48,12 @@ type ModelProfile struct { 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"`