1package dialectquery
2
3import "fmt"
4
5type Clickhouse struct{}
6
7var _ Querier = (*Clickhouse)(nil)
8
9func (c *Clickhouse) CreateTable(tableName string) string {
10 q := `CREATE TABLE IF NOT EXISTS %s (
11 version_id Int64,
12 is_applied UInt8,
13 date Date default now(),
14 tstamp DateTime default now()
15 )
16 ENGINE = MergeTree()
17 ORDER BY (date)`
18 return fmt.Sprintf(q, tableName)
19}
20
21func (c *Clickhouse) InsertVersion(tableName string) string {
22 q := `INSERT INTO %s (version_id, is_applied) VALUES ($1, $2)`
23 return fmt.Sprintf(q, tableName)
24}
25
26func (c *Clickhouse) DeleteVersion(tableName string) string {
27 q := `ALTER TABLE %s DELETE WHERE version_id = $1 SETTINGS mutations_sync = 2`
28 return fmt.Sprintf(q, tableName)
29}
30
31func (c *Clickhouse) GetMigrationByVersion(tableName string) string {
32 q := `SELECT tstamp, is_applied FROM %s WHERE version_id = $1 ORDER BY tstamp DESC LIMIT 1`
33 return fmt.Sprintf(q, tableName)
34}
35
36func (c *Clickhouse) ListMigrations(tableName string) string {
37 q := `SELECT version_id, is_applied FROM %s ORDER BY version_id DESC`
38 return fmt.Sprintf(q, tableName)
39}
40
41func (c *Clickhouse) GetLatestVersion(tableName string) string {
42 q := `SELECT max(version_id) FROM %s`
43 return fmt.Sprintf(q, tableName)
44}