Initial version
This commit is contained in:
8
migrations/data/001_init.sql
Normal file
8
migrations/data/001_init.sql
Normal file
@@ -0,0 +1,8 @@
|
||||
CREATE TABLE jokes (
|
||||
id SERIAL NOT NULL PRIMARY KEY,
|
||||
joke_text TEXT NOT NULL
|
||||
);
|
||||
|
||||
---- create above / drop below ----
|
||||
|
||||
DROP TABLE jokes;
|
||||
5
migrations/data/002_ts.sql
Normal file
5
migrations/data/002_ts.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
ALTER TABLE jokes ADD COLUMN ts TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW();
|
||||
|
||||
---- create above / drop below ----
|
||||
|
||||
ALTER TABLE jokes DROP COLUMN ts;
|
||||
5
migrations/data/003_nsfw.sql
Normal file
5
migrations/data/003_nsfw.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
ALTER TABLE jokes ADD COLUMN nsfw BOOLEAN NOT NULL DEFAULT 'f';
|
||||
|
||||
---- create above / drop below ----
|
||||
|
||||
ALTER TABLE jokes DROP COLUMN nsfw;
|
||||
7
migrations/data/004_votes.sql
Normal file
7
migrations/data/004_votes.sql
Normal file
@@ -0,0 +1,7 @@
|
||||
ALTER TABLE jokes ADD COLUMN up_votes INT NOT NULL DEFAULT 0;
|
||||
ALTER TABLE jokes ADD COLUMN down_votes INT NOT NULL DEFAULT 0;
|
||||
|
||||
---- create above / drop below ----
|
||||
|
||||
ALTER TABLE jokes DROP COLUMN up_votes;
|
||||
ALTER TABLE jokes DROP COLUMN down_votes;
|
||||
82
migrations/migrations.go
Normal file
82
migrations/migrations.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/tern/v2/migrate"
|
||||
)
|
||||
|
||||
const versionTable = "db_version"
|
||||
|
||||
type Migrator struct {
|
||||
migrator *migrate.Migrator
|
||||
}
|
||||
|
||||
//go:embed data/*.sql
|
||||
var migrationFiles embed.FS
|
||||
|
||||
func NewMigrator(dbDNS string) (Migrator, error) {
|
||||
|
||||
conn, err := pgx.Connect(context.Background(), dbDNS)
|
||||
if err != nil {
|
||||
return Migrator{}, err
|
||||
}
|
||||
migrator, err := migrate.NewMigratorEx(context.Background(), conn, versionTable, &migrate.MigratorOptions{
|
||||
DisableTx: false,
|
||||
})
|
||||
if err != nil {
|
||||
return Migrator{}, err
|
||||
}
|
||||
|
||||
migrationRoot, _ := fs.Sub(migrationFiles, "data")
|
||||
|
||||
err = migrator.LoadMigrations(migrationRoot)
|
||||
if err != nil {
|
||||
return Migrator{}, err
|
||||
}
|
||||
|
||||
return Migrator{
|
||||
migrator: migrator,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Info the current migration version and the embedded maximum migration, and a textual
|
||||
// representation of the migration state for informational purposes.
|
||||
func (m Migrator) Info() (int32, int32, string, error) {
|
||||
|
||||
version, err := m.migrator.GetCurrentVersion(context.Background())
|
||||
if err != nil {
|
||||
return 0, 0, "", err
|
||||
}
|
||||
info := ""
|
||||
|
||||
var last int32
|
||||
for _, thisMigration := range m.migrator.Migrations {
|
||||
last = thisMigration.Sequence
|
||||
|
||||
cur := version == thisMigration.Sequence
|
||||
indicator := " "
|
||||
if cur {
|
||||
indicator = "->"
|
||||
}
|
||||
info = info + fmt.Sprintf("%2s %3d %s\n", indicator, thisMigration.Sequence, thisMigration.Name)
|
||||
}
|
||||
|
||||
return version, last, info, nil
|
||||
}
|
||||
|
||||
// Migrate migrates the DB to the most recent version of the schema.
|
||||
func (m Migrator) Migrate() error {
|
||||
err := m.migrator.Migrate(context.Background())
|
||||
return err
|
||||
}
|
||||
|
||||
// MigrateTo migrates to a specific version of the schema. Use '0' to undo all migrations.
|
||||
func (m Migrator) MigrateTo(ver int32) error {
|
||||
err := m.migrator.MigrateTo(context.Background(), ver)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user