Adding migrations
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
62f9656e19
commit
8522dbae20
@ -0,0 +1,37 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log/slog"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.runcible.io/learning/pulley/internal/config"
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
// https://medium.com/@neelkanthsingh.jr/understanding-database-connection-pools-and-the-pgx-library-in-go-3087f3c5a0c
|
||||||
|
|
||||||
|
// pgxpool.New implicitly creates a dbConfig from the database uri params which can include connection params
|
||||||
|
|
||||||
|
// newPgxConfig creates a pgxpool Config from the application service config
|
||||||
|
// func newPgxConfig(cfg config.ServiceConfig) (pgxpool.Config, error) {
|
||||||
|
// dbconfig, err := pgxpool.ParseConfig(cfg.DatabaseUri)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// OpenPgPool creates a db pool and tests connectivity
|
||||||
|
func OpenPgPool(ctx context.Context, cfg config.ServiceConfig) (*pgxpool.Pool, error) {
|
||||||
|
pool, err := pgxpool.New(ctx, cfg.DatabaseUri)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
timoutCtx, close := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer close()
|
||||||
|
|
||||||
|
if err := pool.Ping(timoutCtx); err != nil {
|
||||||
|
slog.Error("Error in attempting first postgres database connection")
|
||||||
|
pool.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return pool, nil
|
||||||
|
}
|
@ -0,0 +1,2 @@
|
|||||||
|
DROP TABLE IF EXISTS movies;
|
||||||
|
|
@ -0,0 +1,10 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS movies (
|
||||||
|
id bigserial PRIMARY KEY,
|
||||||
|
created_at timestamp(0) with time zone NOT NULL DEFAULT NOW(),
|
||||||
|
title text NOT NULL,
|
||||||
|
year integer NOT NULL,
|
||||||
|
runtime integer NOT NULL,
|
||||||
|
genres text[] NOT NULL,
|
||||||
|
version integer NOT NULL DEFAULT 1
|
||||||
|
);
|
||||||
|
|
@ -0,0 +1,41 @@
|
|||||||
|
package migrations
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"embed"
|
||||||
|
|
||||||
|
"github.com/golang-migrate/migrate/v4"
|
||||||
|
"github.com/golang-migrate/migrate/v4/database/pgx/v5"
|
||||||
|
"github.com/golang-migrate/migrate/v4/source/iofs"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed *.sql
|
||||||
|
var migrationFiles embed.FS
|
||||||
|
|
||||||
|
func Migrate(db *sql.DB) error {
|
||||||
|
|
||||||
|
//create database driver
|
||||||
|
driver, err := pgx.WithInstance(db, &pgx.Config{})
|
||||||
|
if err != nil {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create IFS source from embedded files
|
||||||
|
source, err := iofs.New(migrationFiles, ".")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//creat new migrations instance
|
||||||
|
m, err := migrate.NewWithInstance("iofs", source, "pgx", driver)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run Migrations
|
||||||
|
if err := m.Up(); err != nil && err != migrate.ErrNoChange {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in New Issue