rubenv / rubenv/sql-migrate

Migration doesn't apply

Open
#109 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
3.4k
Forks
292
PR merge metrics
No merged PRs in 30d

Description

Hi, I'm trying to figure out why my migration doesn't apply.
I use bleeding edge version of gorp and postgres driver. To embed the migration files I use bindata. So first of all I run following command:

$ go-bindata -pkg repository -o repository/migrations.go migrations/...
package repository

import (
	"database/sql"

	"github.com/go-gorp/gorp"
	_ "github.com/lib/pq"
	migrate "github.com/rubenv/sql-migrate"
)

func InitStorage(storage string, dsn string) (*gorp.DbMap, error) {
	dbmap := createStorage(storage, dsn)
	migrations := &migrate.AssetMigrationSource{
		Asset:    Asset,
		AssetDir: AssetDir,
		Dir:      "migrations",
	}

	n, err := migrate.Exec(dbmap.Db, "postgres", migrations, migrate.Up)
	if err != nil {
		return nil, err
	}
	fmt.Printf("Applied %d migrations!\n", n)

	return dbmap, nil
}

func createStorage(storage string, dsn string) *gorp.DbMap {
	dialect, driver := dialectAndDriver(storage)
	dbmap := &gorp.DbMap{Db: connect(driver, dsn), Dialect: dialect}
	return dbmap
}

func connect(driver string, dsn string) *sql.DB {
	if dsn == "" {
		panic("DSN is no set.")
	}

	db, err := sql.Open(driver, dsn)
	if err != nil {
		panic("Error connecting to db: " + err.Error())
	}
	return db
}

func dialectAndDriver(storage string) (gorp.Dialect, string) {
	switch storage {
	case "postgres":
		return gorp.PostgresDialect{}, "postgres"
	case "sqlite":
		return gorp.SqliteDialect{}, "sqlite3"
	default:
		return gorp.PostgresDialect{}, "postgres"
	}
}

My migration file looks like:
migrations/1_init.sql

-- +migrate UP

CREATE TABLE IF NOT EXISTS "organization" (
  "id" VARCHAR(250) PRIMARY KEY,
  "name" VARCHAR(20) NOT NULL,
  "email" VARCHAR(25) NOT NULL,
  "description" VARCHAR(150),
  "location" VARCHAR(50)
);

-- +migrate Down
DROP TABLE IF EXISTS "organization";

But after running my application, I receive following console output:

Applied 0 migrations!

And my DB is empty, table organization was not created. What am I doing wrong?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with migrations/1_init.sql and the AssetMigrationSource passed to migrate.Exec, then inspect how embedded migration assets and migration directives are discovered. Confirm the migration is recognized and applied, with organization created in the PostgreSQL database, and run the relevant migration tests if available.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, postgres
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.