Error "no change" when using golang-migrate and docker postgres

112 views Asked by At

I've a golang project.

Below is the main.go file -

package main

import (
    "database/sql"
    "log"

    _ "github.com/jackc/pgx/v4/stdlib"
    dbmigration "github.com/rajcspsg/grpc-ms/unary/server/db"
)

func main() {

    sqlDB, err := sql.Open("pgx", "postgres://postgres:postgres@localhost:5432/grpc?sslmode=disable")

    if err != nil {
        log.Fatalln("Can't connect to database: ", err)
    }

    dbmigration.Migrate(sqlDB)
}

dbmigration.go is -

package dbmigration

import (
    "database/sql"
    "log"

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

func Migrate(conn *sql.DB) {
    log.Println("Database migration start")

    m, err := migrate.New(
        "file://db/migrations",
        "postgres://user-name:strong-password@localhost:5432/postgres?sslmode=disable")
    if err != nil {
        log.Fatalln("Database migration failed: ", err)
    }

    if err := m.Down(); err != nil {
        log.Fatalln("Database migration(down) failed:", err)
    }

    if err := m.Up(); err != nil {
        log.Fatalln("Database migration(up) failed:", err)
    }

    log.Println("Database migration completed!!!")
}

db/migrations/000_create_table_dummy.up.sql -

CREATE TABLE IF NOT EXISTS DUMMY (
  user_id UUID PRIMARY KEY,
  user_name TEXT NOT NULL,
  created_at TIMESTAMPZ,
  updated_at TIMESTAMPZ
);

db/migrations/000_create_table_dummy.down.sql -

DROP TABLE IF EXISTS DUMMY;

docker-compose.yml for running postgres is -

version: "3.8"
services:
  db:
    image: postgres
    container_name: local_pgdb4grpc
    restart: always
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: user-name
      POSTGRES_PASSWORD: strong-password
    volumes:
      - local_pgdata:/var/lib/postgresql/data
  pgadmin:
    image: dpage/pgadmin4
    container_name: pgadmin4_container4grpc
    restart: always
    ports:
      - "8888:80"
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: strong-password
    volumes:
      - pgadmin-data:/var/lib/pgadmin

volumes:
  local_pgdata:
  pgadmin-data:

When I run the main program I get the below error -

./bin/server
18:41:36 Database migration start
18:41:37 Database migration(down) failed: no change
make: *** [Makefile:27: execute] Error 1

How can I fix this error?

0

There are 0 answers