ent 基本使用十三 debug 模式

ent 生成的代码client 包中包含了一个方便的方法Debug(), 记得昨天我为了查看生成的sql 查询通过配置mysql
启用慢查询,同时设置记录慢查询为0,实际上client Debug 方法使用了builder 的模式,我们通过Debug 方法
可以直接就方便的输出生成的sql

参考代码

    if err := client.Debug().Schema.Create(ctx); err != nil {
        log.Fatalf("failed printing schema changes: %v", err)
    }

日志效果

go run cmd/migration/main.go 
2019/10/15 11:03:07 driver.Tx(661af82b-dd17-4c45-b721-88118838ca8e): started
2019/10/15 11:03:07 Tx(661af82b-dd17-4c45-b721-88118838ca8e).Query: query=SHOW VARIABLES LIKE 'version' args=[]
2019/10/15 11:03:07 Tx(661af82b-dd17-4c45-b721-88118838ca8e).Query: query=SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE `TABLE_SCHEMA` = (SEL
ECT DATABASE()) AND `TABLE_NAME` = ? args=[groups]
2019/10/15 11:03:07 Tx(661af82b-dd17-4c45-b721-88118838ca8e).Exec: query=CREATE TABLE IF NOT EXISTS `groups`(`id` bigint AUTO_INCREMENT NOT NULL, `n
ame` varchar(255) NOT NULL, `admin_id` bigint NULL, PRIMARY KEY(`id`)) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin args=[]
2019/10/15 11:03:07 Tx(661af82b-dd17-4c45-b721-88118838ca8e).Query: query=SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE `TABLE_SCHEMA` = (SEL
ECT DATABASE()) AND `TABLE_NAME` = ? args=[pets]
2019/10/15 11:03:07 Tx(661af82b-dd17-4c45-b721-88118838ca8e).Exec: query=CREATE TABLE IF NOT EXISTS `pets`(`id` bigint AUTO_INCREMENT NOT NULL, `nam
e` varchar(255) NOT NULL, `owner_id` bigint NULL, PRIMARY KEY(`id`)) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin args=[]
2019/10/15 11:03:07 Tx(661af82b-dd17-4c45-b721-88118838ca8e).Query: query=SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE `TABLE_SCHEMA` = (SEL
......

参考资料

https://entgo.io/docs/getting-started/

posted on 2019-10-15 11:21  荣锋亮  阅读(1041)  评论(0编辑  收藏  举报

导航