Skip to content

auto generate sql from gorm model struct

Notifications You must be signed in to change notification settings

testap92/gorm2sql

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gorm2sql: auto generate sql from gorm model struct

A Swiss Army Knife helps you generate sql from gorm model struct.

Installation

go get github.com/liudanking/gorm2sql

Usage

user_email.go:

type UserBase struct {
	UserId string `sql:"index:idx_ub"`
	Ip     string `sql:"unique_index:uniq_ip"`
}

type UserEmail struct {
	Id       int64    `gorm:"primary_key"`
	UserBase
	Email      string
	Sex        bool
	Age        int
	Score      float64
	UpdateTime time.Time `sql:"default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"`
	CreateTime time.Time `sql:"default:CURRENT_TIMESTAMP"`
}
gorm2sql sql -f user_email.go -s UserEmail -o db.sql

Result:

CREATE TABLE `user_email`
(
  `id` bigint AUTO_INCREMENT NOT NULL ,
  `user_id` varchar(128) NOT NULL ,
  `ip` varchar(128) NOT NULL ,
  `email` varchar(128) NOT NULL ,
  `sex` boolean NOT NULL ,
  `age` int NOT NULL ,
  `score` double NOT NULL ,
  `update_time` datetime NOT NULL  DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `create_time` datetime NOT NULL  DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_ub (`user_id`),
  UNIQUE INDEX uniq_ip (`ip`),
  PRIMARY KEY (`id`)
) engine=innodb DEFAULT charset=utf8mb4;

How it works

gorm2sql loads go source file to golang AST, then generate sql according to tag of gorm struct field.

About

auto generate sql from gorm model struct

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 95.2%
  • Smarty 4.8%