gen icon indicating copy to clipboard operation
gen copied to clipboard

是否可以支持通过 .sql 文件来生成代码

Open renchunxiao opened this issue 3 years ago • 10 comments

renchunxiao avatar Dec 16 '21 01:12 renchunxiao

I would like to vote for this feature. Currently, we use docker to bypass this problem, which is heavy.

@riverchu Could you suggest if we could save time or not after embedding this feature to gen? Because if we want to generate files for a lot of tables, the program will run for a long time, which means the setup time of docker is not that important, actually.

jiekun avatar Mar 04 '22 10:03 jiekun

This is a simple script for people who want to bypass the problem.

$PROJECT_ROOT/docs/example.sql

create table `auth_tab` (
    `id` bigint(20) not null auto_increment comment 'id',
    `app_id` bigint(20) not null comment 'app_id',
    `create_time` bigint(20) unsigned not null comment 'create time',
    `update_time` bigint(20) unsigned not null comment 'update time',
    primary key (`id`),
    unique key `uk_appid` (`app_id`)
) engine=innodb default charset=utf8mb4 collate=utf8mb4_unicode_ci;

$PROJECT_ROOT/cmd/gen/main.go

// @Author: Jiekun
// @Date: 2022/2/26

package main

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gen"
	"gorm.io/gorm"
)

func main() {
	g := gen.NewGenerator(gen.Config{
		OutPath: "internal/dao/query",
		ModelPkgPath: "internal/dao/entity",
		WithUnitTest: true,
	})

	db, _ := gorm.Open(
		mysql.Open(
			fmt.Sprintf(
				"%s:%s@(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
				"root",
				"dao_gen_password",
				"127.0.0.1:13306",
				"dao_gen_db",
			),
		),
	)
	g.UseDB(db)
	g.ApplyBasic(g.GenerateAllTableWithSharding(`_\d{8}`)...)
	g.Execute()
}

$PROJECT_ROOT/gen_dao.sh

# create database container
docker rm dao_gen
docker run --name dao_gen -p 13306:3306 -e MYSQL_ROOT_PASSWORD=dao_gen_password -d mysql:5.7

while ! mysqladmin ping -h 127.0.0.1 -P13306 --silent; do
    sleep 1
done

# init databases and tables
mysql -h 127.0.0.1 -uroot -P13306 -pdao_gen_password -e "show databases;"
mysql -h 127.0.0.1 -uroot -P13306 -pdao_gen_password -e "drop database if exists dao_gen_db;"
mysql -h 127.0.0.1 -uroot -P13306 -pdao_gen_password -e "create database dao_gen_db;"
mysql -h 127.0.0.1 -uroot -P13306 -pdao_gen_password dao_gen_db < "$PWD"/docs/example.sql

# gen DAO codes
go run ./cmd/gen/main.go

# destroy container
docker stop dao_gen
docker rm dao_gen

So just edit the main.go to fit your project setting, then execute the shell script, which is rough but work.

sh ./gen_dao.sh

jiekun avatar Mar 16 '22 11:03 jiekun

We prepare to implement this feature by parsing SQL files, using docker is too rough for me

tr1v3r avatar Mar 16 '22 12:03 tr1v3r

When we have thousands of different tables, the performance/time cost of setting up a docker can be omitted. It just run for a very long time.

Of course, it's nice to have this feature inside Gen without any dependency. But I guess it's not worth putting a lot of effort.

BTW for enterprise usage, I think sharding is a lot more important. I don't know if there is some tricky way to use Gen with sharded tables in ByteDance. We implemented this in our way, which is quite rough tho. Would like to have some ideas from the author team if possible. :P

jiekun avatar Mar 16 '22 12:03 jiekun

  1. it's ok to import this script for some scene.
  2. yes, there is another project in ByteDance for sharding database, which implement by hook gorm.DB

tr1v3r avatar Mar 16 '22 12:03 tr1v3r

https://github.com/zeromicro/go-zero/blob/master/tools/goctl/model/sql/parser/parser_test.go#L35

techmotive avatar Apr 12 '22 03:04 techmotive

could refer to goctl, it would be nice if gen supports DDL parsing

techmotive avatar Apr 12 '22 03:04 techmotive

我觉得这个功能很棒!

lifegit avatar Apr 17 '22 17:04 lifegit

Any progress? 💕

li-jin-gou avatar Jan 02 '23 16:01 li-jin-gou

Any progress? +1

to2false avatar Jan 06 '23 08:01 to2false