migrations using clickhouse and testcontainers

49 Views Asked by At

everyone!
I have some problems to up my migration files inside clickhouse container

package tests

import (
    "context"
    "fmt"
    _ "github.com/ClickHouse/clickhouse-go"
    "github.com/docker/go-connections/nat"
    tc "github.com/testcontainers/testcontainers-go"
    "github.com/testcontainers/testcontainers-go/modules/clickhouse"
    "github.com/testcontainers/testcontainers-go/wait"
    "io"
    "strings"
)

func StartContainerClickhouse() error {
    ctx := context.Background()
    zkPort := nat.Port("2181/tcp")

    zkcontainer, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{
       ContainerRequest: tc.ContainerRequest{
          ExposedPorts: []string{zkPort.Port()},
          Image:        "zookeeper",
          WaitingFor:   wait.ForListeningPort(zkPort),
       },
       Started: true,
    })
    if err != nil {
       return err
    }

    ipaddr, err := zkcontainer.ContainerIP(ctx)
    if err != nil {
       return err
    }
    port, err := zkcontainer.MappedPort(ctx, nat.Port("2181/tcp"))
    if err != nil {
       return err
    }

    fmt.Println(port)

    container, err := clickhouse.RunContainer(ctx,
       tc.CustomizeRequest(tc.GenericContainerRequest{
          ContainerRequest: tc.ContainerRequest{
             ExposedPorts: []string{"8123/tcp", "9000/tcp"},
             Files: []tc.ContainerFile{
                {
                   HostFilePath:      "../migrations/",
                   ContainerFilePath: "/",
                   FileMode:          0o700,
                },
             },
          },
       }),
       clickhouse.WithUsername("heisenberg"),
       clickhouse.WithPassword("password"),
       clickhouse.WithZookeeper(ipaddr, zkPort.Port()),
    )

    _, reader, err := container.Exec(ctx, []string{
       "sh",
       "-c",
       "cd migrations && clickhouse-client -u heisenberg --multiline --multiquery --query \"$(cat *.up.sql)\"",
    })

    buf := new(strings.Builder)
    _, err = io.Copy(buf, reader)
    fmt.Println(buf)

    fmt.Println(container.Host(ctx))
    mappedPort, err := container.MappedPort(ctx, nat.Port("8123/tcp"))
    if err != nil {
       return err
    }
    fmt.Println("Mapped port 8123", mappedPort.Port())
    fmt.Println(container.ConnectionHost(ctx))
    fmt.Println(container.ConnectionString(ctx))
    if err != nil {
       return err
    }

    return nil
}

Migrations file:

CREATE TABLE IF NOT EXISTS user ON CLUSTER 'default' (
    id UInt32,
    name String
) ENGINE = MergeTree
    ORDER BY id;

But every time when i want to execute this command clickhouse-client -u heisenberg --multiline --multiquery --query "$(cat *.up.sql)", i recieve this error:

Received exception from server (version 23.3.8):
Code: 999. DB::Exception: Received from localhost:9000. Coordination::Exception. Coordination::Exception: Connection loss, path: All connection tries failed while connecting to ZooKeeper. nodes: 172.17.0.29:2181
Code: 999. Coordination::Exception: Unexpected handshake length received: 37 (Marshalling error): while receiving handshake from ZooKeeper. (KEEPER_EXCEPTION) (version 23.3.8.21 (official build)), 172.17.0.29:2181
Code: 999. Coordination::Exception: Unexpected handshake length received: 37 (Marshalling error): while receiving handshake from ZooKeeper. (KEEPER_EXCEPTION) (version 23.3.8.21 (official build)), 172.17.0.29:2181
Code: 999. Coordination::Exception: Unexpected handshake length received: 37 (Marshalling error): while receiving handshake from ZooKeeper. (KEEPER_EXCEPTION) (version 23.3.8.21 (official build)), 172.17.0.29:2181
. (KEEPER_EXCEPTION)
(query: CREATE TABLE IF NOT EXISTS user ON CLUSTER 'default' (
    id UInt32,
    name String
) ENGINE = MergeTree
    ORDER BY id;)

I dont know what am i doing wrong.

I try to find any answers in internet but dont find anyone.

I try to use sed command to uncomennt zookepeer section in config.xml.

0

There are 0 best solutions below