|
| 1 | +// Licensed to ClickHouse, Inc. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. ClickHouse, Inc. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package issues |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "database/sql" |
| 23 | + "database/sql/driver" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/ClickHouse/clickhouse-go/v2" |
| 27 | + clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests" |
| 28 | + "github.com/pkg/errors" |
| 29 | + "github.com/stretchr/testify/require" |
| 30 | +) |
| 31 | + |
| 32 | +func Test1395(t *testing.T) { |
| 33 | + testEnv, err := clickhouse_tests.GetTestEnvironment("issues") |
| 34 | + require.NoError(t, err) |
| 35 | + opts := clickhouse_tests.ClientOptionsFromEnv(testEnv, clickhouse.Settings{}, false) |
| 36 | + conn, err := sql.Open("clickhouse", clickhouse_tests.OptionsToDSN(&opts)) |
| 37 | + require.NoError(t, err) |
| 38 | + |
| 39 | + ctx := context.Background() |
| 40 | + |
| 41 | + singleConn, err := conn.Conn(ctx) |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("Get single conn from pool: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + tx1 := func(c *sql.Conn) error { |
| 47 | + tx, err := c.BeginTx(ctx, nil) |
| 48 | + if err != nil { |
| 49 | + return errors.Wrap(err, "begin tx") |
| 50 | + } |
| 51 | + defer tx.Rollback() |
| 52 | + |
| 53 | + _, err = tx.ExecContext(ctx, ` |
| 54 | +CREATE TABLE IF NOT EXISTS test_table |
| 55 | +ON CLUSTER my |
| 56 | +(id UInt32, name String) |
| 57 | +ENGINE = MergeTree() |
| 58 | +ORDER BY id`) |
| 59 | + if err != nil { |
| 60 | + return errors.Wrap(err, "create table") |
| 61 | + } |
| 62 | + |
| 63 | + err = tx.Commit() |
| 64 | + if err != nil { |
| 65 | + return errors.Wrap(err, "commit tx") |
| 66 | + } |
| 67 | + |
| 68 | + return nil |
| 69 | + } |
| 70 | + |
| 71 | + err = tx1(singleConn) |
| 72 | + require.Error(t, err, "expected error due to cluster is not configured") |
| 73 | + |
| 74 | + tx2 := func(c *sql.Conn) error { |
| 75 | + tx, err := c.BeginTx(ctx, nil) |
| 76 | + if err != nil { |
| 77 | + return errors.Wrap(err, "begin tx") |
| 78 | + } |
| 79 | + defer tx.Rollback() |
| 80 | + |
| 81 | + _, err = tx.ExecContext(ctx, "INSERT INTO test_table (id, name) VALUES (?, ?)", 1, "test_name") |
| 82 | + if err != nil { |
| 83 | + return errors.Wrap(err, "failed to insert record") |
| 84 | + } |
| 85 | + err = tx.Commit() |
| 86 | + if err != nil { |
| 87 | + return errors.Wrap(err, "commit tx") |
| 88 | + } |
| 89 | + |
| 90 | + return nil |
| 91 | + } |
| 92 | + require.NotPanics( |
| 93 | + t, |
| 94 | + func() { |
| 95 | + err := tx2(singleConn) |
| 96 | + require.ErrorIs(t, err, driver.ErrBadConn) |
| 97 | + }, |
| 98 | + "must not panics", |
| 99 | + ) |
| 100 | +} |
0 commit comments