I'm trying to use golang-migrate
to migrate a sql file into my postgresql database. I'm likely doing this wrong, but when I run the command to migrate it says that no scheme is found:
$ go run ./cmd/ migrate
2022/04/05 16:20:29 no scheme
exit status 1
Here is the code:
// package dbschema contains the database schema, migrations, and seeding data.
package dbschema
import (
"context"
_ "embed" // Calls init function.
"fmt"
"log"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/jmoiron/sqlx"
"github.com/jonleopard/bootstrap/pkg/sys/database"
_ "github.com/lib/pq"
)
var (
//go:embed sql/000001_schema.up.sql
schemaDoc string
//go:embed sql/seed.sql
seedDoc string
)
// Migrate attempts to bring the schema for db up to date with the migrations
// defined in this package.
func Migrate(ctx context.Context, db *sqlx.DB) error {
if err := database.StatusCheck(ctx, db); err != nil {
return fmt.Errorf("status check database: %w", err)
}
driver, err := postgres.WithInstance(db.DB, &postgres.Config{})
if err != nil {
return fmt.Errorf("Construct Migrate driver: %w", err)
}
m, err := migrate.NewWithDatabaseInstance(schemaDoc, "postgres", driver)
if err != nil {
log.Fatal(err)
}
return m.Up()
}
The definition of
NewWithDatabaseInstance
is:So the first parameter is a URL and you are passing in the script itself.
NewWithDatabaseInstance
callsSchemeFromURL
which is what generates the error you are seeing (because the url you are passing does not contain a:
). A URL consists of a "scheme" followed by:
and then other info.To use golang-migrate with
embed
see the example in the docs:You will note that this passes in a folder (as an
embed.FS
) rather than a single file. This is becausegolang-migrate
is designed to apply multiple migrations rather than just running a single script against the database. You should be able to use something like: