go-gorm / gen

Gen: Friendly & Safer GORM powered by Code Generation
https://gorm.io/gen/
MIT License
2.25k stars 294 forks source link

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

Closed renchunxiao closed 1 month ago

jiekun commented 2 years ago

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 commented 2 years ago

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
tr1v3r commented 2 years ago

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

jiekun commented 2 years ago

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

tr1v3r commented 2 years ago
  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
techmotive commented 2 years ago

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

techmotive commented 2 years ago

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

lifegit commented 2 years ago

我觉得这个功能很棒!

li-jin-gou commented 1 year ago

Any progress? 💕

to2false commented 1 year ago

Any progress? +1

qqxhb commented 1 month ago

https://gorm.io/gen/database_to_structs.html#Generate-From-Sql