
Overview
ddlgen is a tool for generating DDL from annotated Go struct.
Example
$ # == 1. Prepare your model source code ================================
$ cat <<"EOF" > /tmp/sample.go
package sample
// User is a user model struct.
//
// spanddl: table: Users
// spanddl: constraint: CONSTRAINT AgeGTEZero CHECK(Age >= 0)
// spanddl: option: PRIMARY KEY (Id)
// spanddl: index: IndexUsersName ON Users(Name)
type User struct {
ID int64 `db:"Id" spanddl:"STRING(36) NOT NULL"`
Name string `db:"Name" spanddl:"STRING(255) NOT NULL"`
Age int64 `db:"Age" spanddl:"INT64 NOT NULL"`
}
// Group is a group model struct.
//
// spanddl: table: CREATE TABLE IF NOT EXISTS Groups
// spanddl: option: PRIMARY KEY (Id)
// spanddl: index: CREATE UNIQUE INDEX IndexGroupsName ON Groups(Name)
type Group struct {
ID int64 `db:"Id" spanddl:"STRING(36) NOT NULL"`
Name string `db:"Name" spanddl:"STRING(255) NOT NULL"`
Description string `db:"Description" spanddl:"STRING(2048) NOT NULL"`
}
EOF
$ # == 2. generate DDL ================================
$ ddlgen --dialect spanner --column-key-go db --ddl-key-go spanddl --src /tmp/sample.go --dst /tmp/sample.sql
INFO: 2023/11/07 20:49:39 ddlgen.go:44: source: /tmp/sample.go
INFO: 2023/11/07 20:49:39 ddlgen.go:73: destination: /tmp/sample.sql
$ # == 3. Check generated DDL ================================
$ cat /tmp/sample.sql
-- Code generated by ddlgen. DO NOT EDIT.
--
-- Date: 2023-11-07T21:17:57+09:00
--
-- source: tmp/sample.go:3
-- User is a user model struct.
--
-- spanddl: table: Users
-- spanddl: constraint: CONSTRAINT AgeGTEZero CHECK(Age >= 0)
-- spanddl: option: PRIMARY KEY (Id)
CREATE TABLE Users (
`Id` STRING(36) NOT NULL,
`Name` STRING(255) NOT NULL,
`Age` INT64 NOT NULL,
CONSTRAINT AgeGTEZero CHECK(Age >= 0)
)
PRIMARY KEY (Id);
-- source: tmp/sample.go:8
-- spanddl: index: IndexUsersName ON Users(Name)
CREATE INDEX IndexUsersName ON Users(Name);
-- source: tmp/sample.go:15
-- Group is a group model struct.
--
-- spanddl: table: CREATE TABLE IF NOT EXISTS Groups
-- spanddl: option: PRIMARY KEY (Id)
CREATE TABLE IF NOT EXISTS Groups (
`Id` STRING(36) NOT NULL,
`Name` STRING(255) NOT NULL,
`Description` STRING(2048) NOT NULL
)
PRIMARY KEY (Id);
-- source: tmp/sample.go:19
-- spanddl: index: CREATE UNIQUE INDEX IndexGroupsName ON Groups(Name)
CREATE UNIQUE INDEX IndexGroupsName ON Groups(Name);
Installation
go install
go install github.com/kunitsucom/ddlgen/cmd/ddlgen@latest
pre-built binary
VERSION=v0.0.2
# 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'
Usage
$ 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
--timestamp (env: DDLGEN_TIMESTAMP, default: 2023-11-07T21:09:43+09:00)
code generation timestamp
--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-key-go (env: DDLGEN_COLUMN_KEY_GO, default: db)
column annotation key for Go struct tag
--ddl-key-go (env: DDLGEN_DDL_KEY_GO, default: ddlgen)
DDL annotation key for Go struct tag
--help (default: false)
show usage
TODO
- dialect
- Support
spanner
- Support
postgres
- Support
mysql
- Support
sqlite3
- lang