added plugins subpackage and added basic support for js migrations
This commit is contained in:
+12
-12
@@ -8,24 +8,24 @@ import (
|
||||
"github.com/pocketbase/dbx"
|
||||
)
|
||||
|
||||
type migration struct {
|
||||
file string
|
||||
up func(db dbx.Builder) error
|
||||
down func(db dbx.Builder) error
|
||||
type Migration struct {
|
||||
File string
|
||||
Up func(db dbx.Builder) error
|
||||
Down func(db dbx.Builder) error
|
||||
}
|
||||
|
||||
// MigrationsList defines a list with migration definitions
|
||||
type MigrationsList struct {
|
||||
list []*migration
|
||||
list []*Migration
|
||||
}
|
||||
|
||||
// Item returns a single migration from the list by its index.
|
||||
func (l *MigrationsList) Item(index int) *migration {
|
||||
func (l *MigrationsList) Item(index int) *Migration {
|
||||
return l.list[index]
|
||||
}
|
||||
|
||||
// Items returns the internal migrations list slice.
|
||||
func (l *MigrationsList) Items() []*migration {
|
||||
func (l *MigrationsList) Items() []*Migration {
|
||||
return l.list
|
||||
}
|
||||
|
||||
@@ -47,13 +47,13 @@ func (l *MigrationsList) Register(
|
||||
file = filepath.Base(path)
|
||||
}
|
||||
|
||||
l.list = append(l.list, &migration{
|
||||
file: file,
|
||||
up: up,
|
||||
down: down,
|
||||
l.list = append(l.list, &Migration{
|
||||
File: file,
|
||||
Up: up,
|
||||
Down: down,
|
||||
})
|
||||
|
||||
sort.Slice(l.list, func(i int, j int) bool {
|
||||
return l.list[i].file < l.list[j].file
|
||||
return l.list[i].File < l.list[j].File
|
||||
})
|
||||
}
|
||||
|
||||
@@ -26,8 +26,8 @@ func TestMigrationsList(t *testing.T) {
|
||||
|
||||
for i, name := range expected {
|
||||
item := l.Item(i)
|
||||
if item.file != name {
|
||||
t.Fatalf("Expected name %s for index %d, got %s", name, i, item.file)
|
||||
if item.File != name {
|
||||
t.Fatalf("Expected name %s for index %d, got %s", name, i, item.File)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+12
-12
@@ -111,19 +111,19 @@ func (r *Runner) Up() ([]string, error) {
|
||||
err := r.db.Transactional(func(tx *dbx.Tx) error {
|
||||
for _, m := range r.migrationsList.Items() {
|
||||
// skip applied
|
||||
if r.isMigrationApplied(tx, m.file) {
|
||||
if r.isMigrationApplied(tx, m.File) {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := m.up(tx); err != nil {
|
||||
return fmt.Errorf("Failed to apply migration %s: %w", m.file, err)
|
||||
if err := m.Up(tx); err != nil {
|
||||
return fmt.Errorf("Failed to apply migration %s: %w", m.File, err)
|
||||
}
|
||||
|
||||
if err := r.saveAppliedMigration(tx, m.file); err != nil {
|
||||
return fmt.Errorf("Failed to save applied migration info for %s: %w", m.file, err)
|
||||
if err := r.saveAppliedMigration(tx, m.File); err != nil {
|
||||
return fmt.Errorf("Failed to save applied migration info for %s: %w", m.File, err)
|
||||
}
|
||||
|
||||
applied = append(applied, m.file)
|
||||
applied = append(applied, m.File)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -146,7 +146,7 @@ func (r *Runner) Down(toRevertCount int) ([]string, error) {
|
||||
m := r.migrationsList.Item(i)
|
||||
|
||||
// skip unapplied
|
||||
if !r.isMigrationApplied(tx, m.file) {
|
||||
if !r.isMigrationApplied(tx, m.File) {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -155,15 +155,15 @@ func (r *Runner) Down(toRevertCount int) ([]string, error) {
|
||||
break
|
||||
}
|
||||
|
||||
if err := m.down(tx); err != nil {
|
||||
return fmt.Errorf("Failed to revert migration %s: %w", m.file, err)
|
||||
if err := m.Down(tx); err != nil {
|
||||
return fmt.Errorf("Failed to revert migration %s: %w", m.File, err)
|
||||
}
|
||||
|
||||
if err := r.saveRevertedMigration(tx, m.file); err != nil {
|
||||
return fmt.Errorf("Failed to save reverted migration info for %s: %w", m.file, err)
|
||||
if err := r.saveRevertedMigration(tx, m.File); err != nil {
|
||||
return fmt.Errorf("Failed to save reverted migration info for %s: %w", m.File, err)
|
||||
}
|
||||
|
||||
reverted = append(reverted, m.file)
|
||||
reverted = append(reverted, m.File)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -79,7 +79,7 @@ func TestRunnerUpAndDown(t *testing.T) {
|
||||
}
|
||||
|
||||
// simulate partially run migration
|
||||
r.saveAppliedMigration(testDB, r.migrationsList.Item(0).file)
|
||||
r.saveAppliedMigration(testDB, r.migrationsList.Item(0).File)
|
||||
|
||||
// Up()
|
||||
// ---
|
||||
|
||||
Reference in New Issue
Block a user