ddlgen

module
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2023 License: Apache-2.0

README

ddlgen

license pkg goreportcard workflow workflow workflow codecov sourcegraph

Overview

ddlgen is a tool for generating DDL from annotated Go struct.

Example

$ # == 1. Prepare your annotated 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:      index: IndexUsersUsername ON Users(Username)
type User struct {
    UserID   int64  `db:"UserId"   spanddl:"STRING(36)  NOT NULL" pk:"true"`
    Username string `db:"Username" 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:  index: CREATE UNIQUE INDEX IndexGroupsGroupName ON Groups(GroupName)
type Group struct {
    GroupID     int64  `db:"GroupId"     spanddl:"STRING(36)   NOT NULL" pk:"true"`
    GroupName   string `db:"GroupName"   spanddl:"STRING(255)  NOT NULL"`
    Description string `db:"Description" spanddl:"STRING(2048) NOT NULL"`
}
EOF

$ # == 2. generate DDL ================================
$ ddlgen --dialect spanner --column-tag-go db --ddl-tag-go spanddl --pk-tag-go pk --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.
--

-- source: tmp/sample.go:5
-- User is a user model struct.
--
-- spanddl:      table: Users
-- spanddl: constraint: CONSTRAINT AgeGTEZero CHECK(Age >= 0)
CREATE TABLE Users (
    `UserId`   STRING(36)  NOT NULL,
    `Username` STRING(255) NOT NULL,
    `Age`      INT64       NOT NULL,
    CONSTRAINT AgeGTEZero CHECK(Age >= 0)
)
PRIMARY KEY (`UserId`);

-- source: tmp/sample.go:7
-- spanddl:      index: IndexUsersUsername ON Users(Username)
CREATE INDEX IndexUsersUsername ON Users(Username);

-- source: tmp/sample.go:16
-- Group is a group model struct.
--
-- spanddl:  table: CREATE TABLE IF NOT EXISTS Groups
CREATE TABLE IF NOT EXISTS Groups (
    `GroupId`     STRING(36)   NOT NULL,
    `GroupName`   STRING(255)  NOT NULL,
    `Description` STRING(2048) NOT NULL
)
PRIMARY KEY (`GroupId`);

-- source: tmp/sample.go:17
-- spanddl:  index: CREATE UNIQUE INDEX IndexGroupsGroupName ON Groups(GroupName)
CREATE UNIQUE INDEX IndexGroupsGroupName ON Groups(GroupName);

Installation

pre-built binary
VERSION=v0.0.6

# 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
go install github.com/kunitsucom/ddlgen/cmd/ddlgen@latest

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
    --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
    --help (default: false)
        show usage

TODO

  • dialect
    • Support spanner
    • Support postgres
    • Support mysql
    • Support sqlite3
  • lang
    • Support go

Directories

Path Synopsis
cmd
ddlgen command
internal
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL