Skip to content

Commit

Permalink
refactor: dialectquery (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Mar 21, 2023
1 parent e2ecb28 commit baaec13
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 136 deletions.
26 changes: 14 additions & 12 deletions internal/dialect/dialectquery/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package dialectquery

import "fmt"

type clickhouse struct {
table string
type Clickhouse struct {
Table string
}

func (c *clickhouse) CreateTable() string {
var _ Querier = (*Clickhouse)(nil)

func (c *Clickhouse) CreateTable() string {
q := `CREATE TABLE IF NOT EXISTS %s (
version_id Int64,
is_applied UInt8,
Expand All @@ -15,25 +17,25 @@ func (c *clickhouse) CreateTable() string {
)
ENGINE = MergeTree()
ORDER BY (date)`
return fmt.Sprintf(q, c.table)
return fmt.Sprintf(q, c.Table)
}

func (c *clickhouse) InsertVersion() string {
func (c *Clickhouse) InsertVersion() string {
q := `INSERT INTO %s (version_id, is_applied) VALUES ($1, $2)`
return fmt.Sprintf(q, c.table)
return fmt.Sprintf(q, c.Table)
}

func (c *clickhouse) DeleteVersion() string {
func (c *Clickhouse) DeleteVersion() string {
q := `ALTER TABLE %s DELETE WHERE version_id = $1 SETTINGS mutations_sync = 2`
return fmt.Sprintf(q, c.table)
return fmt.Sprintf(q, c.Table)
}

func (c *clickhouse) GetMigrationByVersion() string {
func (c *Clickhouse) GetMigrationByVersion() string {
q := `SELECT tstamp, is_applied FROM %s WHERE version_id = $1 ORDER BY tstamp DESC LIMIT 1`
return fmt.Sprintf(q, c.table)
return fmt.Sprintf(q, c.Table)
}

func (c *clickhouse) ListMigrations() string {
func (c *Clickhouse) ListMigrations() string {
q := `SELECT version_id, is_applied FROM %s ORDER BY version_id DESC`
return fmt.Sprintf(q, c.table)
return fmt.Sprintf(q, c.Table)
}
32 changes: 0 additions & 32 deletions internal/dialect/dialectquery/dialectquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,3 @@ type Querier interface {
// The query should return the version_id and is_applied columns.
ListMigrations() string
}

func NewPostgres(table string) Querier {
return &postgres{table: table}
}

func NewMysql(table string) Querier {
return &mysql{table: table}
}

func NewSqlite3(table string) Querier {
return &sqlite3{table: table}
}

func NewSqlserver(table string) Querier {
return &sqlserver{table: table}
}

func NewRedshift(table string) Querier {
return &redshift{table: table}
}

func NewTidb(table string) Querier {
return &tidb{table: table}
}

func NewClickhouse(table string) Querier {
return &clickhouse{table: table}
}

func NewVertica(table string) Querier {
return &vertica{table: table}
}
26 changes: 14 additions & 12 deletions internal/dialect/dialectquery/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,39 @@ package dialectquery

import "fmt"

type mysql struct {
table string
type Mysql struct {
Table string
}

func (m *mysql) CreateTable() string {
var _ Querier = (*Mysql)(nil)

func (m *Mysql) CreateTable() string {
q := `CREATE TABLE %s (
id serial NOT NULL,
version_id bigint NOT NULL,
is_applied boolean NOT NULL,
tstamp timestamp NULL default now(),
PRIMARY KEY(id)
)`
return fmt.Sprintf(q, m.table)
return fmt.Sprintf(q, m.Table)
}

func (m *mysql) InsertVersion() string {
func (m *Mysql) InsertVersion() string {
q := `INSERT INTO %s (version_id, is_applied) VALUES (?, ?)`
return fmt.Sprintf(q, m.table)
return fmt.Sprintf(q, m.Table)
}

func (m *mysql) DeleteVersion() string {
func (m *Mysql) DeleteVersion() string {
q := `DELETE FROM %s WHERE version_id=?`
return fmt.Sprintf(q, m.table)
return fmt.Sprintf(q, m.Table)
}

func (m *mysql) GetMigrationByVersion() string {
func (m *Mysql) GetMigrationByVersion() string {
q := `SELECT tstamp, is_applied FROM %s WHERE version_id=? ORDER BY tstamp DESC LIMIT 1`
return fmt.Sprintf(q, m.table)
return fmt.Sprintf(q, m.Table)
}

func (m *mysql) ListMigrations() string {
func (m *Mysql) ListMigrations() string {
q := `SELECT version_id, is_applied from %s ORDER BY id DESC`
return fmt.Sprintf(q, m.table)
return fmt.Sprintf(q, m.Table)
}
26 changes: 14 additions & 12 deletions internal/dialect/dialectquery/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,39 @@ package dialectquery

import "fmt"

type postgres struct {
table string
type Postgres struct {
Table string
}

func (p *postgres) CreateTable() string {
var _ Querier = (*Postgres)(nil)

func (p *Postgres) CreateTable() string {
q := `CREATE TABLE %s (
id serial NOT NULL,
version_id bigint NOT NULL,
is_applied boolean NOT NULL,
tstamp timestamp NULL default now(),
PRIMARY KEY(id)
)`
return fmt.Sprintf(q, p.table)
return fmt.Sprintf(q, p.Table)
}

func (p *postgres) InsertVersion() string {
func (p *Postgres) InsertVersion() string {
q := `INSERT INTO %s (version_id, is_applied) VALUES ($1, $2)`
return fmt.Sprintf(q, p.table)
return fmt.Sprintf(q, p.Table)
}

func (p *postgres) DeleteVersion() string {
func (p *Postgres) DeleteVersion() string {
q := `DELETE FROM %s WHERE version_id=$1`
return fmt.Sprintf(q, p.table)
return fmt.Sprintf(q, p.Table)
}

func (p *postgres) GetMigrationByVersion() string {
func (p *Postgres) GetMigrationByVersion() string {
q := `SELECT tstamp, is_applied FROM %s WHERE version_id=$1 ORDER BY tstamp DESC LIMIT 1`
return fmt.Sprintf(q, p.table)
return fmt.Sprintf(q, p.Table)
}

func (p *postgres) ListMigrations() string {
func (p *Postgres) ListMigrations() string {
q := `SELECT version_id, is_applied from %s ORDER BY id DESC`
return fmt.Sprintf(q, p.table)
return fmt.Sprintf(q, p.Table)
}
26 changes: 14 additions & 12 deletions internal/dialect/dialectquery/redshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,39 @@ package dialectquery

import "fmt"

type redshift struct {
table string
type Redshift struct {
Table string
}

func (r *redshift) CreateTable() string {
var _ Querier = (*Redshift)(nil)

func (r *Redshift) CreateTable() string {
q := `CREATE TABLE %s (
id integer NOT NULL identity(1, 1),
version_id bigint NOT NULL,
is_applied boolean NOT NULL,
tstamp timestamp NULL default sysdate,
PRIMARY KEY(id)
)`
return fmt.Sprintf(q, r.table)
return fmt.Sprintf(q, r.Table)
}

func (r *redshift) InsertVersion() string {
func (r *Redshift) InsertVersion() string {
q := `INSERT INTO %s (version_id, is_applied) VALUES ($1, $2)`
return fmt.Sprintf(q, r.table)
return fmt.Sprintf(q, r.Table)
}

func (r *redshift) DeleteVersion() string {
func (r *Redshift) DeleteVersion() string {
q := `DELETE FROM %s WHERE version_id=$1`
return fmt.Sprintf(q, r.table)
return fmt.Sprintf(q, r.Table)
}

func (r *redshift) GetMigrationByVersion() string {
func (r *Redshift) GetMigrationByVersion() string {
q := `SELECT tstamp, is_applied FROM %s WHERE version_id=$1 ORDER BY tstamp DESC LIMIT 1`
return fmt.Sprintf(q, r.table)
return fmt.Sprintf(q, r.Table)
}

func (r *redshift) ListMigrations() string {
func (r *Redshift) ListMigrations() string {
q := `SELECT version_id, is_applied from %s ORDER BY id DESC`
return fmt.Sprintf(q, r.table)
return fmt.Sprintf(q, r.Table)
}
26 changes: 14 additions & 12 deletions internal/dialect/dialectquery/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,38 @@ package dialectquery

import "fmt"

type sqlite3 struct {
table string
type Sqlite3 struct {
Table string
}

func (s *sqlite3) CreateTable() string {
var _ Querier = (*Sqlite3)(nil)

func (s *Sqlite3) CreateTable() string {
q := `CREATE TABLE %s (
id INTEGER PRIMARY KEY AUTOINCREMENT,
version_id INTEGER NOT NULL,
is_applied INTEGER NOT NULL,
tstamp TIMESTAMP DEFAULT (datetime('now'))
)`
return fmt.Sprintf(q, s.table)
return fmt.Sprintf(q, s.Table)
}

func (s *sqlite3) InsertVersion() string {
func (s *Sqlite3) InsertVersion() string {
q := `INSERT INTO %s (version_id, is_applied) VALUES (?, ?)`
return fmt.Sprintf(q, s.table)
return fmt.Sprintf(q, s.Table)
}

func (s *sqlite3) DeleteVersion() string {
func (s *Sqlite3) DeleteVersion() string {
q := `DELETE FROM %s WHERE version_id=?`
return fmt.Sprintf(q, s.table)
return fmt.Sprintf(q, s.Table)
}

func (s *sqlite3) GetMigrationByVersion() string {
func (s *Sqlite3) GetMigrationByVersion() string {
q := `SELECT tstamp, is_applied FROM %s WHERE version_id=? ORDER BY tstamp DESC LIMIT 1`
return fmt.Sprintf(q, s.table)
return fmt.Sprintf(q, s.Table)
}

func (s *sqlite3) ListMigrations() string {
func (s *Sqlite3) ListMigrations() string {
q := `SELECT version_id, is_applied from %s ORDER BY id DESC`
return fmt.Sprintf(q, s.table)
return fmt.Sprintf(q, s.Table)
}
26 changes: 14 additions & 12 deletions internal/dialect/dialectquery/sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,33 @@ package dialectquery

import "fmt"

type sqlserver struct {
table string
type Sqlserver struct {
Table string
}

func (s *sqlserver) CreateTable() string {
var _ Querier = (*Sqlserver)(nil)

func (s *Sqlserver) CreateTable() string {
q := `CREATE TABLE %s (
id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
version_id BIGINT NOT NULL,
is_applied BIT NOT NULL,
tstamp DATETIME NULL DEFAULT CURRENT_TIMESTAMP
)`
return fmt.Sprintf(q, s.table)
return fmt.Sprintf(q, s.Table)
}

func (s *sqlserver) InsertVersion() string {
func (s *Sqlserver) InsertVersion() string {
q := `INSERT INTO %s (version_id, is_applied) VALUES (@p1, @p2)`
return fmt.Sprintf(q, s.table)
return fmt.Sprintf(q, s.Table)
}

func (s *sqlserver) DeleteVersion() string {
func (s *Sqlserver) DeleteVersion() string {
q := `DELETE FROM %s WHERE version_id=@p1`
return fmt.Sprintf(q, s.table)
return fmt.Sprintf(q, s.Table)
}

func (s *sqlserver) GetMigrationByVersion() string {
func (s *Sqlserver) GetMigrationByVersion() string {
q := `
WITH Migrations AS
(
Expand All @@ -40,10 +42,10 @@ FROM Migrations
WHERE RowNumber BETWEEN 1 AND 2
ORDER BY tstamp DESC
`
return fmt.Sprintf(q, s.table)
return fmt.Sprintf(q, s.Table)
}

func (s *sqlserver) ListMigrations() string {
func (s *Sqlserver) ListMigrations() string {
q := `SELECT version_id, is_applied FROM %s ORDER BY id DESC`
return fmt.Sprintf(q, s.table)
return fmt.Sprintf(q, s.Table)
}
Loading

0 comments on commit baaec13

Please sign in to comment.