Skip to content

Commit

Permalink
added escaping the name of tables
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAkulov committed May 21, 2019
1 parent 1a10c79 commit ba996f1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
10 changes: 5 additions & 5 deletions clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ func (ch *ClickHouse) FreezeTable(table Table) error {
}
log.Printf(" partition '%v'", item.PartitionID)
query := fmt.Sprintf(
"ALTER TABLE %v.%v FREEZE PARTITION ID '%v';",
"ALTER TABLE `%v`.`%v` FREEZE PARTITION ID '%v';",
table.Database,
table.Name,
item.PartitionID)
if item.PartitionID == "all" {
query = fmt.Sprintf(
"ALTER TABLE %v.%v FREEZE PARTITION tuple();",
"ALTER TABLE `%v`.`%v` FREEZE PARTITION tuple();",
table.Database,
table.Name)
}
Expand Down Expand Up @@ -321,7 +321,7 @@ func (ch *ClickHouse) AttachPatritions(table BackupTable) error {
if _, ok := attachedParts[partName]; ok {
continue
}
query := fmt.Sprintf("ALTER TABLE %s.%s ATTACH PARTITION %s", table.Database, table.Name, partName)
query := fmt.Sprintf("ALTER TABLE `%s`.`%s` ATTACH PARTITION %s", table.Database, table.Name, partName)
attachedParts[partName] = struct{}{}
log.Println(query)
if ch.DryRun {
Expand All @@ -336,7 +336,7 @@ func (ch *ClickHouse) AttachPatritions(table BackupTable) error {

// CreateDatabase - create specific database from metadata in backup folder
func (ch *ClickHouse) CreateDatabase(database string) error {
createQuery := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", database)
createQuery := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", database)
if ch.DryRun {
log.Printf("DRY-RUN: %s", createQuery)
return nil
Expand All @@ -353,7 +353,7 @@ func (ch *ClickHouse) CreateTable(table RestoreTable) error {
log.Printf("DRY-RUN: Create table '%s.%s'", table.Database, table.Table)
return nil
}
if _, err := ch.conn.Exec(fmt.Sprintf("USE %s", table.Database)); err != nil {
if _, err := ch.conn.Exec(fmt.Sprintf("USE `%s`", table.Database)); err != nil {
return err
}
log.Printf("Create table '%s.%s'", table.Database, table.Table)
Expand Down
38 changes: 20 additions & 18 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/stretchr/testify/require"
)

const dbName = "testdb"

type TestDataStuct struct {
Database string
Table string
Expand All @@ -26,7 +28,7 @@ type TestDataStuct struct {

var testData = []TestDataStuct{
TestDataStuct{
Database: "testdb",
Database: dbName,
Table: "table1",
Schema: "(Date Date, TimeStamp DateTime, Log String) ENGINE = MergeTree(Date, (TimeStamp, Log), 8192)",
Rows: []map[string]interface{}{
Expand All @@ -41,7 +43,7 @@ var testData = []TestDataStuct{
OrderBy: "TimeStamp",
},
TestDataStuct{
Database: "testdb",
Database: dbName,
Table: "table2",
Schema: "(id UInt64, User String) ENGINE = MergeTree ORDER BY id SETTINGS index_granularity = 8192",
Rows: []map[string]interface{}{
Expand All @@ -56,7 +58,7 @@ var testData = []TestDataStuct{
OrderBy: "id",
},
TestDataStuct{
Database: "testdb",
Database: dbName,
Table: "table3",
Schema: "(TimeStamp DateTime, Item String, Date Date MATERIALIZED toDate(TimeStamp)) ENGINE = MergeTree() PARTITION BY Date ORDER BY TimeStamp SETTINGS index_granularity = 8192",
Rows: []map[string]interface{}{
Expand All @@ -71,7 +73,7 @@ var testData = []TestDataStuct{
OrderBy: "TimeStamp",
},
TestDataStuct{
Database: "testdb",
Database: dbName,
Table: "table4",
Schema: "(id UInt64, Col1 String, Col2 String, Col3 String, Col4 String, Col5 String) ENGINE = MergeTree PARTITION BY id ORDER BY (id, Col1, Col2, Col3, Col4, Col5) SETTINGS index_granularity = 8192",
Rows: func() []map[string]interface{} {
Expand All @@ -88,7 +90,7 @@ var testData = []TestDataStuct{

var incrementData = []TestDataStuct{
TestDataStuct{
Database: "testdb",
Database: dbName,
Table: "table1",
Schema: "(Date Date, TimeStamp DateTime, Log String) ENGINE = MergeTree(Date, (TimeStamp, Log), 8192)",
Rows: []map[string]interface{}{
Expand All @@ -98,7 +100,7 @@ var incrementData = []TestDataStuct{
OrderBy: "TimeStamp",
},
TestDataStuct{
Database: "testdb",
Database: dbName,
Table: "table2",
Schema: "(id UInt64, User String) ENGINE = MergeTree ORDER BY id SETTINGS index_granularity = 8192",
Rows: []map[string]interface{}{
Expand All @@ -111,7 +113,7 @@ var incrementData = []TestDataStuct{
OrderBy: "id",
},
TestDataStuct{
Database: "testdb",
Database: dbName,
Table: "table3",
Schema: "(TimeStamp DateTime, Item String, Date Date MATERIALIZED toDate(TimeStamp)) ENGINE = MergeTree() PARTITION BY Date ORDER BY TimeStamp SETTINGS index_granularity = 8192",
Rows: []map[string]interface{}{
Expand All @@ -122,7 +124,7 @@ var incrementData = []TestDataStuct{
OrderBy: "TimeStamp",
},
TestDataStuct{
Database: "testdb",
Database: dbName,
Table: "table4",
Schema: "(id UInt64, Col1 String, Col2 String, Col3 String, Col4 String, Col5 String) ENGINE = MergeTree PARTITION BY id ORDER BY (id, Col1, Col2, Col3, Col4, Col5) SETTINGS index_granularity = 8192",
Rows: func() []map[string]interface{} {
Expand All @@ -146,7 +148,7 @@ func TestRestoreLegacyBackupFormat(t *testing.T) {
}
r := require.New(t)
r.NoError(ch.Connect())
r.NoError(ch.dropDatabase("testdb"))
r.NoError(ch.dropDatabase(dbName))
fmt.Println("Generate test data")
for _, data := range testData {
r.NoError(ch.createTestData(data))
Expand All @@ -155,7 +157,7 @@ func TestRestoreLegacyBackupFormat(t *testing.T) {
fmt.Println("Create backup")
r.NoError(dockerExec("clickhouse-backup", "freeze"))
dockerExec("mkdir", "-p", "/var/lib/clickhouse/backup/old_format")
r.NoError(dockerExec("cp","-r", "/var/lib/clickhouse/metadata", "/var/lib/clickhouse/backup/old_format/"))
r.NoError(dockerExec("cp", "-r", "/var/lib/clickhouse/metadata", "/var/lib/clickhouse/backup/old_format/"))
r.NoError(dockerExec("mv", "/var/lib/clickhouse/shadow", "/var/lib/clickhouse/backup/old_format/"))
dockerExec("ls", "-lha", "/var/lib/clickhouse/backup/old_format/")

Expand All @@ -168,7 +170,7 @@ func TestRestoreLegacyBackupFormat(t *testing.T) {
r.Error(dockerExec("clickhouse-backup", "upload", "increment_old_format", "--diff-from", "old_format"))

fmt.Println("Drop database")
r.NoError(ch.dropDatabase("testdb"))
r.NoError(ch.dropDatabase(dbName))

dockerExec("ls", "-lha", "/var/lib/clickhouse/backup")
fmt.Println("Delete backup")
Expand Down Expand Up @@ -199,7 +201,7 @@ func TestIntegration(t *testing.T) {
}
r := require.New(t)
r.NoError(ch.Connect())
r.NoError(ch.dropDatabase("testdb"))
r.NoError(ch.dropDatabase(dbName))
fmt.Println("Generate test data")
for _, data := range testData {
r.NoError(ch.createTestData(data))
Expand All @@ -219,7 +221,7 @@ func TestIntegration(t *testing.T) {
r.NoError(dockerExec("clickhouse-backup", "upload", "increment", "--diff-from", "test_backup"))

fmt.Println("Drop database")
r.NoError(ch.dropDatabase("testdb"))
r.NoError(ch.dropDatabase(dbName))

dockerExec("ls", "-lha", "/var/lib/clickhouse/backup")
fmt.Println("Delete backup")
Expand All @@ -241,7 +243,7 @@ func TestIntegration(t *testing.T) {
}
// test increment
fmt.Println("Drop database")
r.NoError(ch.dropDatabase("testdb"))
r.NoError(ch.dropDatabase(dbName))

dockerExec("ls", "-lha", "/var/lib/clickhouse/backup")
fmt.Println("Delete backup")
Expand Down Expand Up @@ -272,7 +274,7 @@ func (ch *ClickHouse) createTestData(data TestDataStuct) error {
if err := ch.CreateTable(RestoreTable{
Database: data.Database,
Table: data.Table,
Query: fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s.%s %s", data.Database, data.Table, data.Schema),
Query: fmt.Sprintf("CREATE TABLE IF NOT EXISTS `%s`.`%s` %s", data.Database, data.Table, data.Schema),
}); err != nil {
return err
}
Expand All @@ -283,7 +285,7 @@ func (ch *ClickHouse) createTestData(data TestDataStuct) error {
return fmt.Errorf("can't begin transaction with: %v", err)
}
if _, err := tx.NamedExec(
fmt.Sprintf("INSERT INTO %s.%s (%s) VALUES (:%s)",
fmt.Sprintf("INSERT INTO `%s`.`%s` (%s) VALUES (:%s)",
data.Database,
data.Table,
strings.Join(data.Fields, ","),
Expand All @@ -300,13 +302,13 @@ func (ch *ClickHouse) createTestData(data TestDataStuct) error {

func (ch *ClickHouse) dropDatabase(database string) error {
fmt.Println("DROP DATABASE IF EXISTS ", database)
_, err := ch.conn.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", database))
_, err := ch.conn.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", database))
return err
}

func (ch *ClickHouse) checkData(t *testing.T, data TestDataStuct) error {
fmt.Printf("Check '%d' rows in '%s.%s'\n", len(data.Rows), data.Database, data.Table)
rows, err := ch.conn.Queryx(fmt.Sprintf("SELECT * FROM %s.%s ORDER BY %s", data.Database, data.Table, data.OrderBy))
rows, err := ch.conn.Queryx(fmt.Sprintf("SELECT * FROM `%s`.`%s` ORDER BY %s", data.Database, data.Table, data.OrderBy))
if err != nil {
return err
}
Expand Down

0 comments on commit ba996f1

Please sign in to comment.