golang-migrate / migrate

Database migrations. CLI and Golang library.
Other
14.48k stars 1.34k forks source link

SQLite3 accepts file URL with prefix `file:///`, but golang-migrate requires prefix `sqlite3:///`. Not flexible. #1062

Open atljoseph opened 3 months ago

atljoseph commented 3 months ago

Describe the Bug SQLite3 accepts file URL with prefix file:///, but golang-migrate requires prefix sqlite3:///.

  1. Import github.com/golang-migrate/migrate/v4.
  2. Import github.com/golang-migrate/migrate/v4/database/sqlite3.
  3. Import github.com/mattn/go-sqlite3.

Steps to Reproduce Steps to reproduce the behavior:

  1. Open DB file with github.com/mattn/go-sqlite3, by using this URI: file:///path/to/sqlite3.db.
  2. Try to run migration on exact same filename: file:///path/to/sqlite3.db.
  3. Error is returned, citing forgotten import... But no... They were not forgotten. Both imports (2) and (3) from above are present.
  4. Try to run migration on exact same filename: sqlite3:///path/to/sqlite3.db.
  5. No error.

Expected Behavior I should be able to call this code in following way, with no error, and with positive functionality:

File migrations/embed.go:

package migrations

//go:embed *.sql
var FS embed.FS

File db.go:

...
iofsDriver, iofsErr := iofs.New(migrations.FS, ".")
if iofsErr != nil {
    return fmt.Errorf("cannot open database migration embedded directory: %w", iofsErr)
}
m, mErr := migrate.NewWithSourceInstance("iofs", iofsDriver, `sqlite3:///path/to/sqlite3.db`)
if mErr != nil {
    return fmt.Errorf("cannot open database migration helper: %w", mErr)
}
if migrateErr := m.Up(); migrateErr != nil {
    if !errors.Is(migrateErr, migrate.ErrNoChange) {
        return fmt.Errorf("cannot perform database migration: %w", migrateErr)
    }
}
...

Migrate Version From go.mod: github.com/golang-migrate/migrate/v4 v4.17.0 I don't have CLI installed. Calling it from Go code.

Loaded Source Drivers Already described above.

Loaded Database Drivers Already described above.

Go Version go version go1.22.1 darwin/arm64

Stacktrace None.

Additional context Running this in fyne on a mobile app. Incidentally, if i run the following similar code, there is no error:

...
config := &sqlite3.Config{}
driver, driverErr := sqlite3.WithInstance(db.dbx.DB, config)
if driverErr != nil {
    return fmt.Errorf("cannot open database migration driver: %w", driverErr)
}
m, mErr := migrate.NewWithInstance("iofs", iofsDriver, "", driver)
if mErr != nil {
    return fmt.Errorf("cannot open database migration helper: %w", mErr)
}
if migrateErr := m.Up(); migrateErr != nil {
    if !errors.Is(migrateErr, migrate.ErrNoChange) {
        return fmt.Errorf("cannot perform database migration: %w", migrateErr)
    }
}
...
atljoseph commented 3 months ago

Should accept file URI, and possible DB type string.