From a1dd44280e77260ed6fd361ae1647d6eab242174 Mon Sep 17 00:00:00 2001 From: Kevin Joiner <10265309+KevinJoiner@users.noreply.github.com> Date: Fri, 19 Apr 2024 15:47:27 -0400 Subject: [PATCH] Create binary for starting test clickhouse container --- cmd/clickhouse-container/main.go | 59 ++++++++++++++++++++++++++ pkg/clickhouseinfra/clickhouseinfra.go | 4 +- pkg/migrations/migrations_test.go | 2 +- 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 cmd/clickhouse-container/main.go diff --git a/cmd/clickhouse-container/main.go b/cmd/clickhouse-container/main.go new file mode 100644 index 0000000..7280ef1 --- /dev/null +++ b/cmd/clickhouse-container/main.go @@ -0,0 +1,59 @@ +// Package main is a binary for creating a test ClickHouse container. +package main + +import ( + "context" + "flag" + "fmt" + "log" + "os" + "os/signal" + + "github.com/DIMO-Network/model-garage/pkg/clickhouseinfra" + "github.com/DIMO-Network/model-garage/pkg/migrations" +) + +func main() { + err := run(context.Background()) + if err != nil { + log.Fatal(err) + } +} + +func run(ctx context.Context) error { + // Create flags for clickhouse user, password, and port + user := flag.String("user", "default", "ClickHouse user") + password := flag.String("password", "default", "ClickHouse password") + migrate := flag.Bool("migrate", true, "Run migrations") + flag.Parse() + + chcontainer, err := clickhouseinfra.CreateClickHouseContainer(ctx, *user, *password) + if err != nil { + return fmt.Errorf("failed to create clickhouse container: %w", err) + } + defer chcontainer.Terminate(ctx) + + if *migrate { + db, err := clickhouseinfra.GetClickhouseAsDB(ctx, chcontainer.ClickHouseContainer) + if err != nil { + return fmt.Errorf("failed to get clickhouse db: %w", err) + } + if err := migrations.RunGoose(ctx, []string{"up", "-v"}, db); err != nil { + return fmt.Errorf("failed to run migration: %w", err) + } + } + + host, err := chcontainer.ClickHouseContainer.ConnectionString(ctx) + if err != nil { + return fmt.Errorf("failed to get clickhouse host: %w", err) + } + fmt.Printf("ClickHouse container is running at: %s\n", host) + fmt.Println("Waiting for ctrl+c") + // wait for exit signal to terminate the containers + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + <-c + fmt.Println(" Cya Later!") + + return nil +} diff --git a/pkg/clickhouseinfra/clickhouseinfra.go b/pkg/clickhouseinfra/clickhouseinfra.go index edfd94b..26f5767 100644 --- a/pkg/clickhouseinfra/clickhouseinfra.go +++ b/pkg/clickhouseinfra/clickhouseinfra.go @@ -75,8 +75,8 @@ func GetClickHouseAsConn(container *chmodule.ClickHouseContainer) (clickhouse.Co } // GetClickhouseAsDB function returns a sql.DB connection which allows interfaceing with the stdlib database/sql package. -func GetClickhouseAsDB(container *chmodule.ClickHouseContainer) (*sql.DB, error) { - host, err := container.ConnectionHost(context.TODO()) +func GetClickhouseAsDB(ctx context.Context, container *chmodule.ClickHouseContainer) (*sql.DB, error) { + host, err := container.ConnectionHost(ctx) if err != nil { return nil, fmt.Errorf("failed to get clickhouse host: %w", err) } diff --git a/pkg/migrations/migrations_test.go b/pkg/migrations/migrations_test.go index ade534c..fa0d96a 100644 --- a/pkg/migrations/migrations_test.go +++ b/pkg/migrations/migrations_test.go @@ -17,7 +17,7 @@ func TestMigration(t *testing.T) { defer chcontainer.Terminate(ctx) - db, err := clickhouseinfra.GetClickhouseAsDB(chcontainer.ClickHouseContainer) + db, err := clickhouseinfra.GetClickhouseAsDB(ctx, chcontainer.ClickHouseContainer) require.NoError(t, err, "Failed to get clickhouse db") err = migrations.RunGoose(ctx, []string{"up", "-v"}, db)