Skip to content

Commit

Permalink
Create binary for starting test clickhouse container
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJoiner committed Apr 19, 2024
1 parent 84e4eb7 commit a1dd442
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
59 changes: 59 additions & 0 deletions cmd/clickhouse-container/main.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions pkg/clickhouseinfra/clickhouseinfra.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/migrations/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a1dd442

Please sign in to comment.