Skip to content

Commit

Permalink
feat(custom prefixes): Option to customize staging model prefix
Browse files Browse the repository at this point in the history
Some users prefer different prefixes, 'base' is a popular alternative or option alongside 'stg', this allows for that.
  • Loading branch information
gwenwindflower committed Apr 16, 2024
1 parent 19efe71 commit 2bc0be5
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
5 changes: 5 additions & 0 deletions forms.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type FormResponse struct {
CreateProfile bool
ScaffoldProject bool
ProjectName string
Prefix string
}

func Forms() (formResponse FormResponse) {
Expand Down Expand Up @@ -81,6 +82,10 @@ You'll need:
huh.NewConfirm().Affirmative("Yeah!").Negative("Nope").
Title("Would you like to scaffold a basic dbt project into the output directory?").
Value(&formResponse.ScaffoldProject),
huh.NewInput().
Title("What prefix do you want to use for your staging files?").
Value(&formResponse.Prefix).
Placeholder("stg"),
),
)
project_name_form := huh.NewForm(
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func main() {
}
bd = s
}
err = WriteFiles(ts, bd)
err = WriteFiles(ts, bd, formResponse.Prefix)
if err != nil {
log.Fatalf("Error writing files: %v\n", err)
}
Expand Down
4 changes: 2 additions & 2 deletions write_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/gwenwindflower/tbd/shared"
)

func WriteFiles(ts shared.SourceTables, bd string) error {
func WriteFiles(ts shared.SourceTables, bd string, prefix string) error {
if len(ts.SourceTables) == 0 {
return errors.New("no tables to write")
}
Expand All @@ -19,7 +19,7 @@ func WriteFiles(ts shared.SourceTables, bd string) error {
}()
go func() {
defer wg.Done()
WriteStagingModels(ts, bd)
WriteStagingModels(ts, bd, prefix)
}()
wg.Wait()
return nil
Expand Down
4 changes: 2 additions & 2 deletions write_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestWriteFiles(t *testing.T) {
},
}
bd := t.TempDir()
WriteFiles(ts, bd)
WriteFiles(ts, bd, "prefix")
}

func TestWriteFilesError(t *testing.T) {
Expand All @@ -31,7 +31,7 @@ func TestWriteFilesError(t *testing.T) {
}
bd := t.TempDir()

err := WriteFiles(ts, bd)
err := WriteFiles(ts, bd, "prefix")
if err == nil {
t.Error("expected error, got nil")
} else {
Expand Down
4 changes: 2 additions & 2 deletions write_staging_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
//go:embed *.sql
var stagingTemplate embed.FS

func WriteStagingModels(tables shared.SourceTables, buildDir string) {
func WriteStagingModels(tables shared.SourceTables, buildDir string, prefix string) {
var wg sync.WaitGroup

for _, table := range tables.SourceTables {
Expand All @@ -29,7 +29,7 @@ func WriteStagingModels(tables shared.SourceTables, buildDir string) {
log.Fatalf("Failed to parse template %v\n", err)
}

filename := fmt.Sprintf(buildDir + "/stg_" + strings.ToLower(table.Name) + ".sql")
filename := fmt.Sprintf(buildDir + "/" + prefix + "_" + strings.ToLower(table.Name) + ".sql")
outputFile, err := os.Create(filename)
if err != nil {
log.Fatalf("Failed to create file %v\n", err)
Expand Down

0 comments on commit 2bc0be5

Please sign in to comment.