Caution
This project has been moved to ddlctl
ddlgen
is a tool for generating DDL from annotated Go struct.
$ # == 1. Prepare your annotated model source code ================================
$ cat <<"EOF" > /tmp/sample.go
package sample
// User is a user model struct.
//
//pgddl:table "users"
//pgddl:constraint UNIQUE ("username")
//pgddl:index "index_users_username" ON "users" ("username")
type User struct {
UserID string `db:"user_id" pgddl:"TEXT NOT NULL" pk:"true"`
Username string `db:"username" pgddl:"TEXT NOT NULL"`
Age int `db:"age" pgddl:"INT NOT NULL"`
}
// Group is a group model struct.
//
//pgddl:table CREATE TABLE IF NOT EXISTS "groups"
//pgddl:index CREATE UNIQUE INDEX "index_groups_group_name" ON "groups" ("group_name")
type Group struct {
GroupID string `db:"group_id" pgddl:"TEXT NOT NULL" pk:"true"`
GroupName string `db:"group_name" pgddl:"TEXT NOT NULL"`
Description string `db:"description" pgddl:"TEXT NOT NULL"`
}
EOF
$ # == 2. generate DDL ================================
$ ddlgen --dialect postgres --column-tag-go db --ddl-tag-go pgddl --pk-tag-go pk --src /tmp/sample.go --dst /tmp/sample.sql
INFO: 2023/11/16 16:10:39 ddlgen.go:44: source: /tmp/sample.go
INFO: 2023/11/16 16:10:39 ddlgen.go:73: destination: /tmp/sample.sql
$ # == 3. Check generated DDL ================================
$ cat /tmp/sample.sql
-- Code generated by ddlgen. DO NOT EDIT.
--
-- source: tmp/sample.go:5
-- User is a user model struct.
--
-- pgddl:table "users"
-- pgddl:constraint UNIQUE ("username")
CREATE TABLE "users" (
"user_id" TEXT NOT NULL,
"username" TEXT NOT NULL,
"age" INT NOT NULL,
PRIMARY KEY ("user_id"),
UNIQUE ("username")
);
-- source: tmp/sample.go:7
-- pgddl:index "index_users_username" ON "users" ("username")
CREATE INDEX "index_users_username" ON "users" ("username");
-- source: tmp/sample.go:16
-- Group is a group model struct.
--
-- pgddl:table CREATE TABLE IF NOT EXISTS "groups"
CREATE TABLE IF NOT EXISTS "groups" (
"group_id" TEXT NOT NULL,
"group_name" TEXT NOT NULL,
"description" TEXT NOT NULL,
PRIMARY KEY ("group_id")
);
-- source: tmp/sample.go:17
-- pgddl:index CREATE UNIQUE INDEX "index_groups_group_name" ON "groups" ("group_name")
CREATE UNIQUE INDEX "index_groups_group_name" ON "groups" ("group_name");
VERSION=v0.0.13
# download
curl -fLROSs https://github.com/kunitsucom/ddlgen/releases/download/${VERSION}/ddlgen_${VERSION}_darwin_arm64.zip
# unzip
unzip -j ddlgen_${VERSION}_darwin_arm64.zip '*/ddlgen'
go install github.com/kunitsucom/ddlgen/cmd/[email protected]
$ ddlgen --help
Usage:
ddlgen [options]
Description:
Generate DDL from annotated source code.
options:
--version (default: false)
show version information and exit
--trace (env: DDLGEN_TRACE, default: false)
trace mode enabled
--debug (env: DDLGEN_DEBUG, default: false)
debug mode
--lang (env: DDLGEN_LANGUAGE, default: go)
programming language to generate DDL
--dialect (env: DDLGEN_DIALECT, default: )
SQL dialect to generate DDL
--src (env: DDLGEN_SOURCE, default: /dev/stdin)
source file or directory
--dst (env: DDLGEN_DESTINATION, default: /dev/stdout)
destination file or directory
--column-tag-go (env: DDLGEN_COLUMN_TAG_GO, default: db)
column annotation key for Go struct tag
--ddl-tag-go (env: DDLGEN_DDL_TAG_GO, default: ddlgen)
DDL annotation key for Go struct tag
--pk-tag-go (env: DDLGEN_PK_TAG_GO, default: pk)
primary key annotation key for Go struct tag
--help (default: false)
show usage
- dialect
- Support
mysql
- Support
postgres
- Support
spanner
- Support
sqlite3
- Support
- lang
- Support
go
- Support