Skip to content

Commit

Permalink
docs: Update README.md (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent authored Apr 29, 2024
2 parents 3c56a41 + e86c564 commit 44aa564
Show file tree
Hide file tree
Showing 6 changed files with 371 additions and 341 deletions.
543 changes: 272 additions & 271 deletions CREDITS

Large diffs are not rendered by default.

97 changes: 34 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

## Demo

![ddlctl_demo](https://github.com/kunitsucom/ddlctl/assets/29125616/c7a59aaf-4c13-4c8e-998b-cd42a1f956d5)
![ddlctl_demo](https://github.com/kunitsucom/ddlctl/assets/29125616/cdb603ad-bb44-4e18-8af0-ac5bce46e319)

## Overview

Expand Down Expand Up @@ -71,9 +71,9 @@ package sample

// User is a user model struct.
//
//pgddl:table public.users
//pgddl:table public.users
//pgddl:constraint UNIQUE ("username")
//pgddl:index "index_users_username" ON public.users ("username")
//pgddl:index "index_users_username" ON public.users ("username")
type User struct {
UserID string `db:"user_id" pgddl:"TEXT NOT NULL" pk:"true"`
Username string `db:"username" pgddl:"TEXT NOT NULL"`
Expand All @@ -91,51 +91,22 @@ type Group struct {
}
```

Write it to an appropriate file:

```sh
cat <<"EOF" > /tmp/sample.go
package sample
// User is a user model struct.
//
//pgddl:table public.users
//pgddl:constraint UNIQUE ("username")
//pgddl:index "index_users_username" ON public.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 public.groups
//pgddl:index CREATE UNIQUE INDEX "index_groups_group_name" ON public.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

Please execute the ddlctl command as follows:

```console
$ ddlctl generate --dialect postgres --go-column-tag db --go-ddl-tag pgddl --go-pk-tag pk --src /tmp/sample.go --dst /tmp/sample.sql
INFO: 2023/11/16 16:10:39 ddlctl.go:44: source: /tmp/sample.go
INFO: 2023/11/16 16:10:39 ddlctl.go:73: destination: /tmp/sample.sql
$ ddlctl generate --dialect postgres --go-column-tag db --go-ddl-tag pgddl --go-pk-tag pk sample.go sample.sql
INFO: 2023/11/16 16:10:39 ddlctl.go:44: source: sample.go
INFO: 2023/11/16 16:10:39 ddlctl.go:73: destination: sample.sql
```

### 3. Check generated DDL file

Please check the contents of the outputted DDL:

```sh
cat /tmp/sample.sql
cat sample.sql
```

content:
Expand All @@ -144,10 +115,10 @@ content:
-- Code generated by ddlctl. DO NOT EDIT.
--

-- source: tmp/sample.go:5
-- source: docs/sample.go:5
-- User is a user model struct.
--
-- pgddl:table public.users
-- pgddl:table public.users
-- pgddl:constraint UNIQUE ("username")
CREATE TABLE public.users (
"user_id" TEXT NOT NULL,
Expand All @@ -157,11 +128,11 @@ CREATE TABLE public.users (
UNIQUE ("username")
);

-- source: tmp/sample.go:7
-- pgddl:index "index_users_username" ON public.users ("username")
-- source: docs/sample.go:7
-- pgddl:index "index_users_username" ON public.users ("username")
CREATE INDEX "index_users_username" ON public.users ("username");

-- source: tmp/sample.go:16
-- source: docs/sample.go:16
-- Group is a group model struct.
--
-- pgddl:table CREATE TABLE IF NOT EXISTS public.groups
Expand All @@ -172,7 +143,7 @@ CREATE TABLE IF NOT EXISTS public.groups (
PRIMARY KEY ("group_id")
);

-- source: tmp/sample.go:17
-- source: docs/sample.go:17
-- pgddl:index CREATE UNIQUE INDEX "index_groups_group_name" ON public.groups ("group_name")
CREATE UNIQUE INDEX "index_groups_group_name" ON public.groups ("group_name");
```
Expand All @@ -182,7 +153,7 @@ CREATE UNIQUE INDEX "index_groups_group_name" ON public.groups ("group_name");
### 1. Prepare your DDL

```sh
cat /tmp/sample.sql
cat sample.sql
```

content:
Expand All @@ -191,10 +162,10 @@ content:
-- Code generated by ddlctl. DO NOT EDIT.
--

-- source: tmp/sample.go:5
-- source: docs/sample.go:5
-- User is a user model struct.
--
-- pgddl:table public.users
-- pgddl:table public.users
-- pgddl:constraint UNIQUE ("username")
CREATE TABLE public.users (
"user_id" TEXT NOT NULL,
Expand All @@ -204,11 +175,11 @@ CREATE TABLE public.users (
UNIQUE ("username")
);

-- source: tmp/sample.go:7
-- pgddl:index "index_users_username" ON public.users ("username")
-- source: docs/sample.go:7
-- pgddl:index "index_users_username" ON public.users ("username")
CREATE INDEX "index_users_username" ON public.users ("username");

-- source: tmp/sample.go:16
-- source: docs/sample.go:16
-- Group is a group model struct.
--
-- pgddl:table CREATE TABLE IF NOT EXISTS public.groups
Expand All @@ -219,7 +190,7 @@ CREATE TABLE IF NOT EXISTS public.groups (
PRIMARY KEY ("group_id")
);

-- source: tmp/sample.go:17
-- source: docs/sample.go:17
-- pgddl:index CREATE UNIQUE INDEX "index_groups_group_name" ON public.groups ("group_name")
CREATE UNIQUE INDEX "index_groups_group_name" ON public.groups ("group_name");
```
Expand All @@ -229,7 +200,7 @@ CREATE UNIQUE INDEX "index_groups_group_name" ON public.groups ("group_name");
Please check the differences between the local DDL file and the destination database:

```console
$ ddlctl diff --dialect postgres "postgres://postgres:password@localhost/testdb?sslmode=disable" /tmp/sample.sql
$ ddlctl diff --dialect postgres "postgres://postgres:password@localhost/testdb?sslmode=disable" sample.sql
CREATE TABLE public.users (
"user_id" TEXT NOT NULL,
"username" TEXT NOT NULL,
Expand All @@ -250,7 +221,7 @@ CREATE UNIQUE INDEX "index_groups_group_name" ON public.groups ("group_name");
### 3. Apply DDL

```console
$ ddlctl apply --dialect postgres "postgres://postgres:password@localhost/testdb?sslmode=disable" /tmp/sample.sql --auto-approve
$ ddlctl apply --dialect postgres "postgres://postgres:password@localhost/testdb?sslmode=disable" sample.sql --auto-approve

ddlctl will exec the following DDL queries:

Expand Down Expand Up @@ -303,7 +274,7 @@ done
apply:

```console
$ ddlctl apply --dialect postgres "postgres://postgres:password@localhost/testdb?sslmode=disable" /tmp/sample.sql --auto-approve
$ ddlctl apply --dialect postgres "postgres://postgres:password@localhost/testdb?sslmode=disable" sample.sql --auto-approve

ddlctl will exec the following DDL queries:

Expand Down Expand Up @@ -331,14 +302,18 @@ done
### pre-built binary

```bash
LATEST_VERSION=$(curl -ILSs https://github.com/kunitsucom/ddlctl/releases/latest | tr -d '\r' | awk -F'/tag/' '/location/ { print $2 }') && echo "${LATEST_VERSION}"
OS=$(uname | tr '[:upper:]' '[:lower:]') && echo "${OS}"
ARCH=$(uname -m) && echo "${ARCH}"
LATEST_VERSION=$(curl -ISs https://github.com/kunitsucom/ddlctl/releases/latest | tr -d '\r' | awk -F/ '/location:/{print $NF}')
OS=$(uname | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
URL="https://github.com/kunitsucom/ddlctl/releases/download/${LATEST_VERSION}/ddlctl_${LATEST_VERSION}_${OS}_${ARCH}.zip"

# Check URL
echo "${URL}"

# download
curl -fLROSs "https://github.com/kunitsucom/ddlctl/releases/download/${LATEST_VERSION}/ddlctl_${LATEST_VERSION}_${OS}_${ARCH}.zip"
# Download
curl -fLROSs "${URL}"

# unzip
# Unzip
unzip -j ddlctl_${LATEST_VERSION}_darwin_arm64.zip '*/ddlctl'
```

Expand Down Expand Up @@ -381,7 +356,7 @@ options:
```console
$ ddlctl generate --help
Usage:
ddlctl generate [options] --dialect <DDL dialect> --src <source> --dst <destination>
ddlctl generate [options] --dialect <DDL dialect> <source> <destination>

Description:
generate DDL from source (file or directory) to destination (file or directory).
Expand All @@ -397,10 +372,6 @@ options:
DDL annotation key for Go struct tag
--go-pk-tag (env: DDLCTL_PK_TAG_GO, default: pk)
primary key annotation key for Go struct tag
--src (env: DDLCTL_SOURCE, default: /dev/stdin)
source file or directory
--dst (env: DDLCTL_DESTINATION, default: /dev/stdout)
destination file or directory
--help (default: false)
show usage
```
Expand Down
1 change: 1 addition & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.cast
*.gif
sample.sql
35 changes: 35 additions & 0 deletions docs/diff.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
-- Code generated by ddlctl. DO NOT EDIT.
--

-- source: docs/sample.go:5
-- User is a user model struct.
--
-- pgddl:table public.users
-- pgddl:constraint UNIQUE ("username")
CREATE TABLE public.users (
"user_id" TEXT NOT NULL,
"username" TEXT NOT NULL,
"age" INT NOT NULL,
"description" TEXT NOT NULL,
PRIMARY KEY ("user_id"),
UNIQUE ("username")
);

-- source: docs/sample.go:7
-- pgddl:index "index_users_username" ON public.users ("username")
CREATE INDEX "index_users_username" ON public.users ("username");

-- source: docs/sample.go:16
-- Group is a group model struct.
--
-- pgddl:table CREATE TABLE IF NOT EXISTS public.groups
CREATE TABLE IF NOT EXISTS public.groups (
"group_id" TEXT NOT NULL,
"group_name" TEXT NOT NULL,
"description" TEXT NOT NULL,
PRIMARY KEY ("group_id")
);

-- source: docs/sample.go:17
-- pgddl:index CREATE UNIQUE INDEX "index_groups_group_name" ON public.groups ("group_name")
CREATE UNIQUE INDEX "index_groups_group_name" ON public.groups ("group_name");
14 changes: 7 additions & 7 deletions docs/ghostplay.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ghostplay_custom_prompt


# Prepare your annotated model source code
bat /tmp/sample.go
bat sample.go

#ghostplay silent
sleep 2
Expand All @@ -35,7 +35,7 @@ ghostplay_custom_prompt


# Generate DDL
ddlctl generate --dialect postgres --go-column-tag db --go-ddl-tag pgddl --go-pk-tag pk --src /tmp/sample.go --dst /tmp/sample.sql
ddlctl generate --dialect postgres --go-column-tag db --go-ddl-tag pgddl --go-pk-tag pk sample.go sample.sql

#ghostplay silent
sleep 2
Expand All @@ -48,7 +48,7 @@ ghostplay_custom_prompt


# Check generated DDL file
bat /tmp/sample.sql
bat sample.sql

#ghostplay silent
sleep 2
Expand All @@ -71,7 +71,7 @@ ghostplay_custom_prompt


# Apply DDL
ddlctl apply --dialect postgres "${DATABASE_DSN}" /tmp/sample.sql --auto-approve
ddlctl apply --dialect postgres "${DATABASE_DSN}" sample.sql --auto-approve

#ghostplay silent
sleep 2
Expand All @@ -84,7 +84,7 @@ ghostplay_custom_prompt


# Edit DDL and check diff
diff -uw /tmp/sample.sql /tmp/diff.sql | bat --language diff
diff -uw sample.sql diff.sql | bat --language diff

#ghostplay silent
sleep 2
Expand All @@ -97,7 +97,7 @@ ghostplay_custom_prompt


# Diff current database schema and DDL file
ddlctl diff --dialect postgres "${DATABASE_DSN}" /tmp/diff.sql | bat --language sql
ddlctl diff --dialect postgres "${DATABASE_DSN}" diff.sql | bat --language sql

#ghostplay silent
sleep 2
Expand All @@ -110,7 +110,7 @@ ghostplay_custom_prompt


# Apply DDL
ddlctl apply --dialect postgres "${DATABASE_DSN}" /tmp/diff.sql --auto-approve
ddlctl apply --dialect postgres "${DATABASE_DSN}" diff.sql --auto-approve

#ghostplay silent
sleep 4
Expand Down
22 changes: 22 additions & 0 deletions docs/sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sample

// User is a user model struct.
//
//pgddl:table public.users
//pgddl:constraint UNIQUE ("username")
//pgddl:index "index_users_username" ON public.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 public.groups
//pgddl:index CREATE UNIQUE INDEX "index_groups_group_name" ON public.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"`
}

0 comments on commit 44aa564

Please sign in to comment.