-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89fd916
commit 8ede715
Showing
9 changed files
with
637 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package dbump_ch | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
) | ||
|
||
// MigratorClickHouse to migrate ClickHouse. | ||
type MigratorClickHouse struct { | ||
db *sql.DB | ||
versionTable string | ||
} | ||
|
||
// NewMigratorClickHouse instantiates new MigratorClickHouse. | ||
func NewMigratorClickHouse(db *sql.DB) *MigratorClickHouse { | ||
return &MigratorClickHouse{ | ||
db: db, | ||
versionTable: "_dbump_schema_version", | ||
} | ||
} | ||
|
||
// LockDB is a method for Migrator interface. | ||
func (ch *MigratorClickHouse) LockDB(ctx context.Context) error { | ||
// TODO: currently no-op | ||
return nil | ||
} | ||
|
||
// UnlockDB is a method for Migrator interface. | ||
func (ch *MigratorClickHouse) UnlockDB(ctx context.Context) error { | ||
// TODO: currently no-op | ||
return nil | ||
} | ||
|
||
// Version is a method for Migrator interface. | ||
func (ch *MigratorClickHouse) Version(ctx context.Context) (version int, err error) { | ||
row := ch.db.QueryRowContext(ctx, "SELECT version FROM "+ch.versionTable) | ||
err = row.Scan(&version) | ||
return version, err | ||
} | ||
|
||
// SetVersion is a method for Migrator interface. | ||
func (ch *MigratorClickHouse) SetVersion(ctx context.Context, version int) error { | ||
_, err := ch.db.ExecContext(ctx, "UPDATE "+ch.versionTable+" SET version = $1", version) | ||
return err | ||
} | ||
|
||
// Exec is a method for Migrator interface. | ||
func (ch *MigratorClickHouse) Exec(ctx context.Context, query string, args ...interface{}) error { | ||
_, err := ch.db.ExecContext(ctx, query) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module github.com/cristalhq/dbump/dbump_ch | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/cristalhq/dbump v0.1.1 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/cristalhq/dbump v0.1.1 h1:cRbnpydrF19ML0wNO2pGsY6qwEVH/rtwnN64VwZpy9M= | ||
github.com/cristalhq/dbump v0.1.1/go.mod h1:rAjULuStbuNPCLrJT62Eu7Sp/2gVt/4URUvsnPK1yFA= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/cristalhq/dbump/dbump_mysql | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/cristalhq/dbump v0.1.1 | ||
github.com/lib/pq v1.10.5 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
github.com/cristalhq/dbump v0.1.1 h1:cRbnpydrF19ML0wNO2pGsY6qwEVH/rtwnN64VwZpy9M= | ||
github.com/cristalhq/dbump v0.1.1/go.mod h1:rAjULuStbuNPCLrJT62Eu7Sp/2gVt/4URUvsnPK1yFA= | ||
github.com/lib/pq v1.10.5 h1:J+gdV2cUmX7ZqL2B0lFcW0m+egaHC2V3lpO8nWxyYiQ= | ||
github.com/lib/pq v1.10.5/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package dbump_mysql | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
) | ||
|
||
// to prevent multiple migrations running at the same time | ||
const lockNum int64 = 777_777_777 | ||
|
||
// Migrator to migrate MySQL. | ||
type Migrator struct { | ||
db *sql.DB | ||
versionTable string | ||
} | ||
|
||
// NewMigrator instantiates new Migrator. | ||
func NewMigrator(db *sql.DB) *Migrator { | ||
return &Migrator{ | ||
db: db, | ||
versionTable: "_dbump_schema_version", | ||
} | ||
} | ||
|
||
// Init migrator. | ||
func (pg *Migrator) Init(ctx context.Context) error { | ||
query := fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s ( | ||
version BIGINT NOT NULL PRIMARY KEY, | ||
created_at TIMESTAMP NOT NULL | ||
);`, pg.versionTable) | ||
_, err := pg.db.ExecContext(ctx, query) | ||
return err | ||
} | ||
|
||
// LockDB is a method for Migrator interface. | ||
func (my *Migrator) LockDB(ctx context.Context) error { | ||
_, err := my.db.ExecContext(ctx, `SELECT GET_LOCK(?, 10)`, lockNum) | ||
return err | ||
} | ||
|
||
// UnlockDB is a method for Migrator interface. | ||
func (my *Migrator) UnlockDB(ctx context.Context) error { | ||
_, err := my.db.ExecContext(ctx, "SELECT RELEASE_LOCK(?)", lockNum) | ||
return err | ||
} | ||
|
||
// Version is a method for Migrator interface. | ||
func (my *Migrator) Version(ctx context.Context) (version int, err error) { | ||
row := my.db.QueryRowContext(ctx, "SELECT version FROM "+my.versionTable) | ||
err = row.Scan(&version) | ||
return version, err | ||
} | ||
|
||
// SetVersion is a method for Migrator interface. | ||
func (my *Migrator) SetVersion(ctx context.Context, version int) error { | ||
_, err := my.db.ExecContext(ctx, "UPDATE "+my.versionTable+" SET version = $1", version) | ||
return err | ||
} | ||
|
||
// Exec is a method for Migrator interface. | ||
func (my *Migrator) Exec(ctx context.Context, query string, args ...interface{}) error { | ||
_, err := my.db.ExecContext(ctx, query) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package dbump_mysql_test | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"testing" | ||
) |
Oops, something went wrong.