How to use database migration in Golang?

WBOY
Release: 2024-06-01 17:48:00
Original
399 people have browsed it

Using database migration in Golang ensures that the database is synchronized with the code. Migration can be performed using libraries such as Ent or Gormigrate: Using Ent: Install Ent. Generate migration files. Run the migration. Using Gormigrate: Install Gormigrate. Create migration files (including up and down migration functions). Run the migration.

如何在 Golang 中使用数据库迁移?

#How to use database migration in Golang?

Database migration is a technique for managing database schema changes to ensure that the database is always in sync with the code. In Golang, database migrations can be performed using libraries such as [ent](https://entgo.io/docs/migrate/) or [gormigrate](https://github.com/go-gormigrate/gormigrate).

Use Ent for database migration

Ent is a Golang ORM that provides a built-in migration system. To use Ent for database migration, follow these steps:

  1. Install Ent:

    go get github.com/ent/ent/cmd/ent
    Copy after login
  2. Generate migration file:

    ent generate migration AddUser
    Copy after login
  3. Run migration:

    ent migrate
    Copy after login

Use Gormigrate for database migration

Gormigrate is a third-party library that can be used Perform database migration in Golang. To use Gormigrate, follow these steps:

  1. Install Gormigrate:

    go get github.com/go-gormigrate/gormigrate/v2
    Copy after login
  2. Create migration files:

    touch migrations/001_add_user.go
    Copy after login
  3. Write code in the migration file:

    import (
     "github.com/go-gormigrate/gormigrate/v2/gormigrate"
     "gorm.io/gorm"
    )
    
    func Up(db *gorm.DB) error {
     type User struct {
         ID   uint `gormigrate:"primary_key"`
         Name string
     }
     return db.AutoMigrate(&User{})
    }
    
    func Down(db *gorm.DB) error {
     return db.Migrator().DropTable("users")
    }
    Copy after login
  4. Run the migration:

    gormigrate -up
    Copy after login

Practical case

Suppose we have a user table and its schema has changed. Using Gormigrate, we can create a migration file like the following:

import (
    "github.com/go-gormigrate/gormigrate/v2/gormigrate"
    "gorm.io/gorm"
)

func Up(db *gorm.DB) error {
    type User struct {
        ID       uint  `gormigrate:"primary_key"`
        Name     string
        Password string
    }
    return db.AutoMigrate(&User{})
}

func Down(db *gorm.DB) error {
    return db.Migrator().DropColumn("users", "password")
}
Copy after login

Running this migration will add a new "password" column to the "users" table.

The above is the detailed content of How to use database migration in Golang?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template