DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Building and Integrating REST APIs With AWS RDS Databases: A Node.js Example
  • Sample Data Generation With Built-In Database Capabilities
  • Kubernetes Evolution: Transitioning from etcd to Distributed SQL
  • Adding a Gas Station Map to a React and Go/Gin/Gorm Application

Trending

  • Navigating the LLM Landscape: A Comparative Analysis of Leading Large Language Models
  • Agentic AI and Generative AI: Revolutionizing Decision Making and Automation
  • Building AI-Driven Intelligent Applications: A Hands-On Development Guide for Integrating GenAI Into Your Applications
  • How to Format Articles for DZone
  1. DZone
  2. Data Engineering
  3. Databases
  4. Building Database Connections and Migrations in Go With GORM and Goose

Building Database Connections and Migrations in Go With GORM and Goose

This article walks you through setting up a robust database client, handling migrations, preloading associations, and querying data efficiently.

By 
Bhanuprakash Jirra user avatar
Bhanuprakash Jirra
·
Jul. 20, 24 · Code Snippet
Likes (2)
Comment
Save
Tweet
Share
6.2K Views

Join the DZone community and get the full member experience.

Join For Free

Managing database connections and migrations is crucial for any application. In Go, we can leverage powerful libraries such as GORM for ORM functionality and Goose for database migrations. This article walks you through setting up a robust database client, handling migrations, preloading associations, and querying data efficiently.

Tutorial

Prerequisites

Before we dive into the implementation, ensure you have the following installed:

  • Go (version 1.16 or higher)
  • MySQL or PostgreSQL database

Project Structure

Here’s a brief overview of the project structure:

myapp/
├── main.go
├── config.go
├── client.go
├── migrations/
│   └── 202106010001_create_users_table.sql
├── go.mod
└── go.sum


Step 1: Define Configuration

First, we define the configuration structure to hold database details.

// config.go
package main
Go
 
type Config struct {
    Dialect       string
    Host          string
    Name          string
    Schema        string
    Port          int
    Replicas      string
    User          string
    Password      string
    DbScriptsPath string
    Timeout       int
}


Step 2: Implement Database Client

Next, we implement the Client struct with functions to initialize the connection, handle migrations, and query data. This implementation supports both MySQL and PostgreSQL.

// client.go
package main
Go
 
import (
    "context"
    "fmt"
    "github.com/pressly/goose/v3"
    "gorm.io/driver/mysql"
    "gorm.io/driver/postgres"
    "gorm.io/gorm"
    "gorm.io/gorm/clause"
    "gorm.io/plugin/dbresolver"
    "strings"
    "time"
)

type Client struct {
    db     *gorm.DB
    Config *Config
}

var databaseMigrationPath = "migrations"func (client *Client) Initialize(dbConfig Config) error {
    var err error
    client.Config = &dbConfig
    client.db, err = gorm.Open(
        getDialector(dbConfig),
        &gorm.Config{
            Logger: gormLogger(),
        },
    )
    if err != nil {
        return fmt.Errorf("database connection could not be established: %w", err)
    }
    err = client.DbMigration(dbConfig.Dialect)
    if err != nil {
        return err
    }
    replicaDSNs := strings.Split(dbConfig.Replicas, ",")
    if len(replicaDSNs) > 0 {
        replicas := make([]gorm.Dialector, 0)
        for _, replica := range replicaDSNs {
            replicas = append(replicas, getDialector(dbConfig, replica))
        }
        err = client.db.Use(dbresolver.Register(dbresolver.Config{
            Replicas: replicas,
            Policy:   dbresolver.RandomPolicy{},
        }))
    }
    if err != nil {
        return fmt.Errorf("database replica connection could not be established: %w", err)
    }
    return nil
}

func (client *Client) DbMigration(dialect string) error {
    err := goose.SetDialect(dialect)
    if err != nil {
        return fmt.Errorf("failed to set dialect: %w", err)
    }
    sqlDb, sqlDbErr := client.db.DB()
    if sqlDbErr != nil {
        return fmt.Errorf("an error occurred receiving the db instance: %w", sqlDbErr)
    }
    if client.Config.DbScriptsPath == "" {
        client.Config.DbScriptsPath = databaseMigrationPath
    }
    err = goose.Up(sqlDb, client.Config.DbScriptsPath)
    if err != nil {
        return fmt.Errorf("an error occurred during migration: %w", err)
    }
    return nil
}

func (client *Client) Db() *gorm.DB {
    return client.db
}

// Preloads associations because GORM does not preload all child associations by default.
func (client *Client) FindById(ctx context.Context, model interface{}, id interface{}) error {
    return PreloadAssociationsFromRead(client.db, model).Preload(clause.Associations).WithContext(ctx).First(model, "id = ?", id).Error
}

// Helper function to preload child associations.
func PreloadAssociationsFromRead(db *gorm.DB, model interface{}) *gorm.DB {
    associations := FindChildAssociations(model) // Assume FindChildAssociations is a utility function to find child associations.
    for _, association := range associations {
        db = db.Clauses(dbresolver.Read).Preload(association)
    }
    return db
}

func getDialector(dbConfig Config, host ...string) gorm.Dialector {
    dbHost := fmt.Sprintf("%s:%d", dbConfig.Host, dbConfig.Port)
    if len(host) > 0 && host[0] != "" {
        dbHost = host[0]
    }
    if dbConfig.Timeout == 0 {
        dbConfig.Timeout = 6000
    }
    switch dbConfig.Dialect {
    case "mysql":
        dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&net_write_timeout=%d",
            dbConfig.User, dbConfig.Password, dbHost, dbConfig.Name, dbConfig.Timeout)
        return mysql.Open(dsn)
    case "postgres":
        dsn := fmt.Sprintf("postgres://%s:%s@%s/%s?search_path=%s",
            dbConfig.User, dbConfig.Password, dbHost, dbConfig.Name, dbConfig.Schema)
        return postgres.Open(dsn)
    default:
        panic(fmt.Sprintf("unsupported dialect: %s", dbConfig.Dialect))
    }
}

func gormLogger() gorm.Logger {
    return gorm.Logger{
        LogLevel: gorm.Warn,
    }
}


Preloading Associations

GORM does not preload all child associations by default. To address this, we implemented the PreloadAssociationsFromRead function. This function preloads all necessary associations for a given model, ensuring that related data is loaded efficiently.

Step 3: Migration Scripts

Create your migration scripts under the migrations directory. For example:

-- 202106010001_create_users_table.sql
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100) UNIQUE NOT NULL
);


Step 4: Main Function

Finally, we use the Client struct in our main function to initialize the database connection, run migrations, and query a user by ID.

Using MySQL

To use MySQL, you would configure the Config struct as follows:

// main.go
package main
Go
 
import (
    "context"
    "fmt"
)

func main() {
    dbConfig := Config{
        Dialect:       "mysql",
        Host:          "localhost",
        Name:          "mydatabase",
        Port:          3306,
        User:          "myuser",
        Password:      "mypassword",
        DbScriptsPath: "migrations",
    }    client := &Client{}
    err := client.Initialize(dbConfig)
    if err != nil {
        fmt.Printf("Failed to initialize database client: %v\n", err)
        return
    }    fmt.Println("Database connection established and migrations applied.")    // Example of finding a user by ID
    type User struct {
        ID    uint
        Name  string
        Email string
    }    user := &User{}
    err = client.FindById(context.Background(), user, 1)
    if err != nil {
        fmt.Printf("Failed to find user: %v\n", err)
    } else {
        fmt.Printf("User found: %+v\n", user)
    }
}


Using PostgreSQL

To use PostgreSQL, you would configure the Config struct as follows:

// main.go
package main
Go
 
import (
    "context"
    "fmt"
)

func main() {
    dbConfig := Config{
        Dialect:       "postgres",
        Host:          "localhost",
        Name:          "mydatabase",
        Schema:        "public",
        Port:          5432,
        User:          "myuser",
        Password:      "mypassword",
        DbScriptsPath: "migrations",
    }    
    client := &Client{}
    err := client.Initialize(dbConfig)
    if err != nil {
        fmt.Printf("Failed to initialize database client: %v\n", err)
        return
    }    
  	
  fmt.Println("Database connection established and migrations applied.")    // Example of finding a user by ID
    
  type User struct {
        ID    uint
        Name  string
        Email string
   }    
  	user := &User{}
    err = client.FindById(context.Background(), user, 1)
    if err != nil {
        fmt.Printf("Failed to find user: %v\n", err)
    } else {
        fmt.Printf("User found: %+v\n", user)
    }
}


Conclusion

In this article, we’ve demonstrated how to set up a robust database client using GORM, handle database migrations with Goose, and query data efficiently in a Go application. We also addressed the importance of preloading associations in GORM, as it does not do so by default, ensuring that related data is loaded as needed. Importantly, the provided solution works seamlessly with both MySQL and PostgreSQL databases.

Database connection MySQL Go (programming language) Gorm (computing) PostgreSQL

Published at DZone with permission of Bhanuprakash Jirra. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building and Integrating REST APIs With AWS RDS Databases: A Node.js Example
  • Sample Data Generation With Built-In Database Capabilities
  • Kubernetes Evolution: Transitioning from etcd to Distributed SQL
  • Adding a Gas Station Map to a React and Go/Gin/Gorm Application

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!