Musa Abdulhameed
Musa Abdulhameed

Reputation: 49

Database Migration using library

I was trying to migrate database schema to POSTGRESQL using the code below BUT it does not work, it returned an error

"source driver: unknown driver file (forgotten import?)"

And it worked well using CLI

Up.sql and down.sql files are in dbMigration folder

package main

import (
    "log"
    "github.com/golang-migrate/migrate"
)

func main() {
    mg, err := migrate.New(
        "file://dbMigration",
        "postgres://username:localhost:5432/databasename?sslmode=disable",
    )
    if err != nil {
        log.Fatal(err)
    }
    if err = mg.Up; err != nil {
        log.Fatal(err)
    }
}

Upvotes: 2

Views: 524

Answers (1)

The Fool
The Fool

Reputation: 20430

If you use database packages in go, you usually need to import the driver separately.

That's also what the error is trying to tell you.

source driver: unknown driver file (forgotten import?)

In golang-migrate, you can import the driver from their repository.

import (
    "github.com/golang-migrate/migrate/v4"
    _ "github.com/golang-migrate/migrate/v4/database/postgres"
    _ "github.com/golang-migrate/migrate/v4/source/github"
)

This is also documented in their readme.

Upvotes: 3

Related Questions