Skip to content

Commit 46a8bc9

Browse files
committed
Update mds examples
1 parent 314e6ca commit 46a8bc9

File tree

6 files changed

+177
-5
lines changed

6 files changed

+177
-5
lines changed

examples/mds/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ mds means MyBatis Dynamic SQL.
33

44
## Run
55

6+
start databases:
7+
8+
```shell
9+
cd e2e/mds
10+
bash db.sh
11+
```
12+
613
```shell
714
cd e2e/mds
815
go run ../../cmd/gencoder/main.go generate

examples/mds/db.sh

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env bash
2+
3+
# create MySQL
4+
docker rm -f test_mysql && docker run --name test_mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=testdb -p 3306:3306 -p 33060:33060 -id mysql:latest && sleep 10 && docker exec -i test_mysql mysql -uroot -proot -e "\
5+
CREATE TABLE testdb.user ( \
6+
id INT AUTO_INCREMENT PRIMARY KEY, \
7+
username VARCHAR(64) NOT NULL COMMENT 'Username, required', \
8+
password VARCHAR(128) NOT NULL, \
9+
email VARCHAR(128) NOT NULL DEFAULT '' COMMENT 'User email, required', \
10+
first_name VARCHAR(64) COMMENT 'First name of the user', \
11+
last_name VARCHAR(64) COMMENT 'Last name of the user', \
12+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT 'Record creation timestamp', \
13+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Record update timestamp', \
14+
status ENUM('active', 'inactive', 'suspended') DEFAULT 'active' COMMENT 'Account status', \
15+
INDEX idx_name (username), \
16+
UNIQUE INDEX idx_email (email), \
17+
INDEX idx_status_created (status, created_at), \
18+
INDEX idx_full_name (first_name, last_name) \
19+
) COMMENT='User account information';"
20+
21+
# create PostgreSQL
22+
docker rm -f test_postgres && docker run --name test_postgres -e POSTGRES_USER=root -e POSTGRES_PASSWORD=root -e POSTGRES_DB=testdb -p 5432:5432 -id postgres:latest && sleep 5 && docker exec -i test_postgres psql -U root -d testdb -c "\
23+
CREATE TABLE \"user\" ( \
24+
id SERIAL PRIMARY KEY, \
25+
username VARCHAR(64) NOT NULL, \
26+
password VARCHAR(128) NOT NULL, \
27+
email VARCHAR(128) NOT NULL DEFAULT '', \
28+
first_name VARCHAR(64), \
29+
last_name VARCHAR(64), \
30+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, \
31+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, \
32+
status VARCHAR(9) DEFAULT 'active' CHECK (status IN ('active', 'inactive', 'suspended')), \
33+
CONSTRAINT unique_email UNIQUE (email) \
34+
); \
35+
CREATE INDEX idx_name ON \"user\" (username); \
36+
CREATE INDEX idx_status_created ON \"user\" (status, created_at); \
37+
CREATE INDEX idx_full_name ON \"user\" (first_name, last_name); \
38+
COMMENT ON COLUMN \"user\".username IS 'Username, required'; \
39+
COMMENT ON COLUMN \"user\".email IS 'User email, required'; \
40+
COMMENT ON COLUMN \"user\".first_name IS 'First name of the user'; \
41+
COMMENT ON COLUMN \"user\".last_name IS 'Last name of the user'; \
42+
COMMENT ON COLUMN \"user\".created_at IS 'Record creation timestamp'; \
43+
COMMENT ON COLUMN \"user\".updated_at IS 'Record update timestamp'; \
44+
COMMENT ON COLUMN \"user\".status IS 'Account status'; \
45+
COMMENT ON TABLE \"user\" IS 'User account information';"
46+
47+
# create MSSQL
48+
docker rm -f test_sqlserver && \
49+
docker run --name test_sqlserver -e ACCEPT_EULA=Y -e MSSQL_SA_PASSWORD=Sa123456.. -p 1433:1433 -id mcr.microsoft.com/mssql/server:2022-latest && sleep 10 && \
50+
docker exec -i test_sqlserver /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P 'Sa123456..' -No -Q "CREATE DATABASE testdb;" && \
51+
docker exec -i test_sqlserver /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P 'Sa123456..' -No -d 'testdb' -Q "\
52+
CREATE TABLE [user] (
53+
id INT IDENTITY (1,1) PRIMARY KEY,
54+
username NVARCHAR(64) NOT NULL,
55+
password NVARCHAR(128) NOT NULL,
56+
email NVARCHAR(128) NOT NULL DEFAULT '',
57+
first_name NVARCHAR(64),
58+
last_name NVARCHAR(64),
59+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
60+
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
61+
status NVARCHAR(9) DEFAULT 'active' CHECK (status IN ('active', 'inactive', 'suspended')),
62+
deleted_at DATETIME NULL,
63+
CONSTRAINT unique_email UNIQUE (email)
64+
); \
65+
CREATE INDEX idx_name ON [user] (username); \
66+
CREATE INDEX idx_status_created ON [user] (status, created_at); \
67+
CREATE INDEX idx_full_name ON [user] (first_name, last_name); \
68+
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'User account information', @level0type = N'SCHEMA', @level0name = 'dbo', @level1type = N'TABLE', @level1name = 'user'; \
69+
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'Username, required', @level0type = N'SCHEMA', @level0name = 'dbo', @level1type = N'TABLE', @level1name = 'user', @level2type = N'COLUMN', @level2name = 'username'; \
70+
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'User email, required', @level0type = N'SCHEMA', @level0name = 'dbo', @level1type = N'TABLE', @level1name = 'user', @level2type = N'COLUMN', @level2name = 'email'; \
71+
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'First name of the user', @level0type = N'SCHEMA', @level0name = 'dbo', @level1type = N'TABLE', @level1name = 'user', @level2type = N'COLUMN', @level2name = 'first_name'; \
72+
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'Last name of the user', @level0type = N'SCHEMA', @level0name = 'dbo', @level1type = N'TABLE', @level1name = 'user', @level2type = N'COLUMN', @level2name = 'last_name'; \
73+
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'Record creation timestamp', @level0type = N'SCHEMA', @level0name = 'dbo', @level1type = N'TABLE', @level1name = 'user', @level2type = N'COLUMN', @level2name = 'created_at'; \
74+
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'Record update timestamp', @level0type = N'SCHEMA', @level0name = 'dbo', @level1type = N'TABLE', @level1name = 'user', @level2type = N'COLUMN', @level2name = 'updated_at'; \
75+
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'Account status', @level0type = N'SCHEMA', @level0name = 'dbo', @level1type = N'TABLE', @level1name = 'user', @level2type = N'COLUMN', @level2name = 'status';"

examples/mds/gencoder.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,14 @@ databases:
2525
- name: 'user'
2626
ignoreColumns:
2727
- deleted_at
28+
- name: mssql
29+
dsn: 'mssql://sa:Sa123456..@localhost:1433/testdb?encrypt=disable'
30+
schema: dbo
31+
properties:
32+
entityPkg: com.example.entity.mssql
33+
mapperPkg: com.example.mapper.mssql
34+
dynamicSQLPkg: com.example.mapper.mssql
35+
tables:
36+
- name: 'user'
37+
ignoreColumns:
38+
- deleted_at

pkg/db/mssql_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestGenMssqlTable(t *testing.T) {
2121

2222
ctx := context.Background()
2323
mssqlContainer, err := mssql.Run(ctx,
24-
"mcr.microsoft.com/mssql/server:latest",
24+
"mcr.microsoft.com/mssql/server:2022-latest",
2525
mssql.WithAcceptEULA(),
2626
mssql.WithPassword("Sa123456.."),
2727
)

pkg/util/util.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,14 @@ func getSchema(tbCfg *model.TableConfig, dbCfg *model.DatabaseConfig, u *dburl.U
218218
if u.Driver == "mysql" {
219219
arr := strings.Split(u.Path, "/")
220220
if len(arr) > 1 {
221-
return arr[1]
221+
return arr[1] // database name as schema in MySQL
222222
}
223223
}
224224
if u.Driver == "postgres" {
225-
return "public"
225+
return "public" // default schema in PostgreSQL
226+
}
227+
if u.Driver == "sqlserver" {
228+
return "dbo" // default schema in SQL Server
226229
}
227230
return ""
228231
}
@@ -233,6 +236,8 @@ func generateTable(conn *sql.DB, driver, schema string, tbCfg *model.TableConfig
233236
return db.GenMySQLTable(context.Background(), conn, schema, tbCfg.Name, tbCfg.IgnoreColumns)
234237
case "postgres":
235238
return db.GenPostgresTable(context.Background(), conn, schema, tbCfg.Name, tbCfg.IgnoreColumns)
239+
case "sqlserver":
240+
return db.GenMssqlTable(context.Background(), conn, schema, tbCfg.Name, tbCfg.IgnoreColumns)
236241
default:
237242
return nil, fmt.Errorf("unsupported driver: %s", driver)
238243
}

pkg/util/util_test.go

+76-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"github.com/DanielLiu1123/gencoder/pkg/model"
7+
_ "github.com/go-sql-driver/mysql"
78
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
910
"github.com/testcontainers/testcontainers-go/modules/mysql"
@@ -12,8 +13,6 @@ import (
1213
"os/exec"
1314
"path/filepath"
1415
"testing"
15-
16-
_ "github.com/go-sql-driver/mysql"
1716
)
1817

1918
func TestReadConfig(t *testing.T) {
@@ -151,3 +150,78 @@ func TestCollectRenderContexts(t *testing.T) {
151150
assert.Equal(t, "v01", contexts[0].Properties["k1"])
152151
assert.Equal(t, "v2", contexts[0].Properties["k2"])
153152
}
153+
154+
func Test_getSchema_whenDBUsingMySQL(t *testing.T) {
155+
d := &model.DatabaseConfig{
156+
Name: "mysql",
157+
Dsn: "mysql://user:password@localhost:3306/dbname",
158+
Properties: map[string]string{},
159+
Tables: []*model.TableConfig{
160+
{
161+
Name: "user1",
162+
},
163+
{
164+
Schema: "schema1",
165+
Name: "user2",
166+
},
167+
},
168+
}
169+
u, err := dburl.Parse(d.Dsn)
170+
require.NoError(t, err)
171+
172+
schema := getSchema(d.Tables[0], d, u)
173+
assert.Equal(t, "dbname", schema) // MySQL uses the database name as the schema.
174+
175+
schema = getSchema(d.Tables[1], d, u)
176+
assert.Equal(t, "schema1", schema)
177+
}
178+
179+
func Test_getSchema_whenDBUsingPostgres(t *testing.T) {
180+
d := &model.DatabaseConfig{
181+
Name: "postgres",
182+
Dsn: "postgres://user:password@localhost:5432/dbname",
183+
Properties: map[string]string{},
184+
Tables: []*model.TableConfig{
185+
{
186+
Name: "user1",
187+
},
188+
{
189+
Schema: "schema1",
190+
Name: "user2",
191+
},
192+
},
193+
}
194+
u, err := dburl.Parse(d.Dsn)
195+
require.NoError(t, err)
196+
197+
schema := getSchema(d.Tables[0], d, u)
198+
assert.Equal(t, "public", schema) // Default schema in PostgreSQL is "public" if not specified.
199+
200+
schema = getSchema(d.Tables[1], d, u)
201+
assert.Equal(t, "schema1", schema)
202+
}
203+
204+
func Test_getSchema_whenDBUsingMSSQL(t *testing.T) {
205+
d := &model.DatabaseConfig{
206+
Name: "mssql",
207+
Dsn: "mssql://user:password@localhost:1433/dbname",
208+
Properties: map[string]string{},
209+
Tables: []*model.TableConfig{
210+
{
211+
Name: "user1",
212+
},
213+
{
214+
Schema: "schema1",
215+
Name: "user2",
216+
},
217+
},
218+
}
219+
u, err := dburl.Parse(d.Dsn)
220+
require.NoError(t, err)
221+
222+
schema := getSchema(d.Tables[0], d, u)
223+
assert.Equal(t, "dbo", schema) // Default schema in MSSQL is "dbo" if not specified.
224+
225+
schema = getSchema(d.Tables[1], d, u)
226+
assert.Equal(t, "schema1", schema)
227+
}

0 commit comments

Comments
 (0)