Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
atljoseph opened this issue Mar 23, 2024 · 1 comment

Comments

@atljoseph
Copy link

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
Copy link
Author

Should accept file URI, and possible DB type string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant