updates
This commit is contained in:
18
.gitea/workflows/build.yaml
Executable file
18
.gitea/workflows/build.yaml
Executable file
@@ -0,0 +1,18 @@
|
||||
name: Go
|
||||
on: [push]
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
go-version: [ '1.19' ]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Setup Go
|
||||
uses: actions/setup-go@v4
|
||||
with:
|
||||
go-version: ${{ matrix.go-version }}
|
||||
- name: Build
|
||||
run: go build -v ./...
|
||||
- name: Test
|
||||
run: go test -v ./...
|
||||
10
README.md
Normal file → Executable file
10
README.md
Normal file → Executable file
@@ -5,15 +5,15 @@
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
go get github.com/jchenry/migrate
|
||||
go get git.sdf.org/jchenry/migrate
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
...
|
||||
records :=
|
||||
[]Record{
|
||||
changes :=
|
||||
[]Change{
|
||||
{
|
||||
Description: "create people table",
|
||||
F: func(ctx Context) (err error) {
|
||||
@@ -36,7 +36,7 @@ records :=
|
||||
},
|
||||
}
|
||||
|
||||
err = migrate.Apply(db, migrate.Sqlite3(), records)
|
||||
err = migrate.Apply(db, migrate.Sqlite3(), changes)
|
||||
...
|
||||
|
||||
```
|
||||
@@ -48,5 +48,3 @@ Please make sure to update tests as appropriate.
|
||||
|
||||
## License
|
||||
[MIT](https://choosealicense.com/licenses/mit/)
|
||||
|
||||
courtesey of https://www.makeareadme.com
|
||||
|
||||
0
dialect.go
Normal file → Executable file
0
dialect.go
Normal file → Executable file
4
go.mod
Normal file → Executable file
4
go.mod
Normal file → Executable file
@@ -1,6 +1,6 @@
|
||||
module github.com/jchenry/migrate
|
||||
module git.sdf.org/jchenry/migrate
|
||||
|
||||
go 1.16
|
||||
go 1.19
|
||||
|
||||
require github.com/mattn/go-sqlite3 v1.14.7
|
||||
|
||||
|
||||
16
migrate.go
Normal file → Executable file
16
migrate.go
Normal file → Executable file
@@ -21,9 +21,9 @@ func (e Error) Unwrap() error {
|
||||
return e.wrapped
|
||||
}
|
||||
|
||||
type Record struct {
|
||||
type Change struct {
|
||||
Description string
|
||||
F func(ctx Context) error
|
||||
Apply func(ctx Context) error
|
||||
}
|
||||
|
||||
type Context interface {
|
||||
@@ -31,7 +31,7 @@ type Context interface {
|
||||
Query(query string, args ...interface{}) (*sql.Rows, error)
|
||||
}
|
||||
|
||||
func Apply(ctx Context, d Dialect, migrations []Record) (err error) {
|
||||
func Apply(ctx Context, d Dialect, migrations []Change) (err error) {
|
||||
if err = initialize(ctx, d); err == nil {
|
||||
var currentVersion int64
|
||||
if currentVersion, err = dbVersion(ctx, d); err == nil {
|
||||
@@ -51,22 +51,22 @@ func Apply(ctx Context, d Dialect, migrations []Record) (err error) {
|
||||
}
|
||||
|
||||
func initialize(ctx Context, d Dialect) (err error) {
|
||||
if noVersionTable(ctx, d) {
|
||||
if !versionTableExists(ctx, d) {
|
||||
return createVersionTable(ctx, d)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func noVersionTable(ctx Context, d Dialect) bool {
|
||||
func versionTableExists(ctx Context, d Dialect) bool {
|
||||
rows, table_check := ctx.Query(d.TableExists(table))
|
||||
if rows != nil {
|
||||
defer rows.Close()
|
||||
}
|
||||
return table_check != nil
|
||||
return table_check == nil
|
||||
}
|
||||
|
||||
func apply(ctx Context, d Dialect, r Record) (err error) {
|
||||
if err = r.F(ctx); err == nil {
|
||||
func apply(ctx Context, d Dialect, r Change) (err error) {
|
||||
if err = r.Apply(ctx); err == nil {
|
||||
err = incrementVersion(ctx, d, r.Description)
|
||||
}
|
||||
return
|
||||
|
||||
10
migrate_test.go
Normal file → Executable file
10
migrate_test.go
Normal file → Executable file
@@ -127,10 +127,10 @@ func TestApply(t *testing.T) {
|
||||
sl3 := Sqlite3()
|
||||
|
||||
records :=
|
||||
[]Record{
|
||||
[]Change{
|
||||
{
|
||||
Description: "create people table",
|
||||
F: func(ctx Context) (err error) {
|
||||
Apply: func(ctx Context) (err error) {
|
||||
_, err = ctx.Exec(`
|
||||
CREATE TABLE people (
|
||||
given_name VARCHAR(20),
|
||||
@@ -143,7 +143,7 @@ func TestApply(t *testing.T) {
|
||||
},
|
||||
{
|
||||
Description: "Insert a person into people",
|
||||
F: func(ctx Context) (err error) {
|
||||
Apply: func(ctx Context) (err error) {
|
||||
_, err = ctx.Exec(`INSERT INTO people VALUES('Henry','Colin','M', 42)`)
|
||||
return
|
||||
},
|
||||
@@ -179,9 +179,9 @@ func TestApply(t *testing.T) {
|
||||
|
||||
ishouldntHideUserErrors := errors.New("I should fail")
|
||||
|
||||
records = append(records, Record{
|
||||
records = append(records, Change{
|
||||
Description: "Insert a person into people",
|
||||
F: func(ctx Context) (err error) {
|
||||
Apply: func(ctx Context) (err error) {
|
||||
return ishouldntHideUserErrors
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user