Memorandum of Understanding when migrating with GORM

Introduction

Make a note of the part that I investigated a little around, such as model definition with struct and migration with ʻAutoMigrate`.

To use UUID for ID

While leaving the automatic generation of UUID to the library, I wanted to be able to specify a fixed UUID when inputting test and seed data, so I corresponded as follows.

First, define the model in the struct

package model

import (
	"github.com/go-playground/validator/v10"
	"github.com/google/uuid"
)

type User struct {
  ID string `validate:"is_valid_uuid" gorm:"primaryKey;size:255;default:uuid_generate_v4()" json:"id"`
  ...
}

//If the ID does not come, do not validate it, and if it does, check if it is a UUID
validate := validator.New()
validate.RegisterValidation("is_valid_uuid", isValidUUID)

func isValidUUID(fl v.FieldLevel) bool {
	id := fl.Field().String()
	if len(id) == 0 {
		return true
	}
	_, err := uuid.Parse(id)
	return err == nil
}

Install the extension ʻuuid-ossp in postgres before ʻAutoMigrate (& User {}) to use ʻuuid_generate_v4 ()` above

db, err = gorm.Open("postgres", fmt.Sprintf(
  "host=%s port=%s user=%s password=%s dbname=%s", host, port, user, password, dbname,
))

if err != nil {
  panic(err)
}

db.Exec(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`)

result := db.AutoMigrate(&model.User{})
...

To use Postgres enum type

Specify type with struct tag

type User struct {
  Role string `validate:"is_valid_role" gorm:"type:role_enum;default:'user'" json:"role"`
  ...
}

ʻAdd type definition to postgres before AutoMigrate`

db.Exec(`
DO
$$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'role_enum') THEN
    create type role_enum AS ENUM ('admin', 'editor', 'user');
  END IF;
END
$$;
`)

result := db.AutoMigrate(&model.User{})
...

Summary

It was a memo about the migration of GORM. If there are others, I will add them.

Recommended Posts

Memorandum of Understanding when migrating with GORM
Memorandum of understanding when Python is run on EC2 with Apache
elasticsearch_dsl Memorandum of Understanding
A memorandum of understanding about django's QueryDict
Summary of snippets when developing with Go
A memorandum of trouble when formatting data
Memorandum of means when you want to make machine learning with 50 images
Memorandum of sed
A memorandum of method often used when analyzing data with pandas (for beginners)
[PyTorch] A little understanding of CrossEntropyLoss with mathematical formulas
Analysis of measurement data ①-Memorandum of understanding for scipy fitting-
Summary of problems when doing Semantic Segmentation with Pytorch
Memorandum of understanding for environment construction of AutoML library PyCaret
A memorandum when an error occurs with pip install
Things to watch out for when migrating with Django
A memorandum when making a surveillance camera with Raspberry Pi
Precautions when operating with string for TmeStampType of PySpark
Memorandum of fastText (editing)
memorandum of vi command
1. Statistics learned with Python 2. Probability distribution [Thorough understanding of scipy.stats]
A collection of methods used when aggregating data with pandas