Skip to content

Commit 92b81f4

Browse files
authored
upgrade sql generate (#148)
1 parent 7aec948 commit 92b81f4

File tree

38 files changed

+2189
-2081
lines changed

38 files changed

+2189
-2081
lines changed
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
-- SEQUENCE
2-
CREATE SEQUENCE IF NOT EXISTS "users_id_seq" INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;
2+
CREATE SEQUENCE IF NOT EXISTS "users__id__seq" INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;
33

44
-- TABLE
5-
CREATE TABLE IF NOT EXISTS "users" (
6-
"id" BIGINT DEFAULT nextval('users_id_seq') NOT NULL ,
7-
CONSTRAINT "users_id_pk" PRIMARY KEY ("id"),
8-
"name" VARCHAR(100) NOT NULL,
9-
"value" TEXT[] NOT NULL
5+
CREATE TABLE IF NOT EXISTS "users"
6+
(
7+
"id" BIGINT DEFAULT nextval('users__id__seq') NOT NULL,
8+
CONSTRAINT "users__id__pk" PRIMARY KEY ( "id" ),
9+
"name" VARCHAR( 100 ) NOT NULL,
10+
"value" TEXT[] NOT NULL,
11+
"meta0" JSONB NOT NULL
1012
);
1113

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1+
-- SEQUENCE
2+
CREATE SEQUENCE IF NOT EXISTS "meta__id__seq" INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;
3+
14
-- TABLE
2-
CREATE TABLE IF NOT EXISTS "meta" (
3-
"id" UUID NOT NULL ,
4-
CONSTRAINT "meta_id_pk" PRIMARY KEY ("id"),
5-
"user_id" BIGINT NOT NULL ,
6-
CONSTRAINT "meta_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE NOT DEFERRABLE,
7-
"roles" TEXT[] NOT NULL ,
8-
CONSTRAINT "meta_roles_unq" UNIQUE ("roles"),
9-
"fail" BOOLEAN NOT NULL,
10-
"created_at" TIMESTAMPTZ NOT NULL,
11-
"updated_at" TIMESTAMPTZ NOT NULL,
12-
"deleted_at" TIMESTAMPTZ NULL,
13-
CONSTRAINT "meta__id_user_id__uniq" UNIQUE ("id","user_id"),
14-
CONSTRAINT "meta__user_id_id__uniq" UNIQUE ("user_id","id")
5+
CREATE TABLE IF NOT EXISTS "meta"
6+
(
7+
"id" BIGINT DEFAULT nextval('meta__id__seq') NOT NULL,
8+
CONSTRAINT "meta__id__pk" PRIMARY KEY ( "id" ),
9+
"uid" UUID NOT NULL,
10+
"user_id" BIGINT NOT NULL,
11+
"roles" TEXT[] NOT NULL,
12+
CONSTRAINT "meta__roles__unq" UNIQUE ( "roles" ),
13+
"fail" BOOLEAN NOT NULL,
14+
"created_at" TIMESTAMPTZ NOT NULL,
15+
"updated_at" TIMESTAMPTZ NOT NULL,
16+
"deleted_at" TIMESTAMPTZ NULL
1517
);
1618

19+
-- INDEX
20+
CREATE INDEX "meta__uid__idx" ON "meta" USING btree ( "uid" );
21+
ALTER TABLE "meta" ADD CONSTRAINT "meta__id_user_id__unq" UNIQUE ("id", "user_id");
22+
ALTER TABLE "meta" ADD CONSTRAINT "meta__user_id_id__unq" UNIQUE ("user_id", "id");
23+

_example/go-gen/model.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,22 @@ import (
1111
"github.com/google/uuid"
1212
)
1313

14-
//go:generate goppy gen --type=orm:pgsql --db-read=slave --db-write=master --index=1000
14+
//go:generate goppy gen-orm --dialect=pgsql --db-read=slave --db-write=master --index=1000
1515

16-
//gen:orm table=users action=ro
16+
//gen:orm table=users crud=crud
1717
type User struct {
1818
Id int64 // col=id index=pk
1919
Name string // col=name len=100
2020
Value []string // col=value
21-
Meta0 []*Meta // link=id:meta.user_id
22-
//Meta1 []Meta // link=id
23-
//Meta2 *[]Meta // link=id
24-
//Meta3 Meta // link=id
25-
//Meta4 *Meta // link=id
21+
Meta0 []*Meta // col=meta0
2622
}
2723

28-
//gen:orm table=meta index=uniq:id,user_id index=uniq:user_id,id
24+
//gen:orm table=meta index=unq:id,user_id index=unq:user_id,id
2925
type Meta struct {
30-
Id uuid.UUID // col=id index=pk auto=c:uuid.New()
26+
Id int64 // col=id index=pk
27+
UID uuid.UUID // col=uid index=idx auto=c:uuid.New()
3128
UserId int64 // col=user_id index=fk:users.id
32-
Roles []string // col=roles index=uniq
29+
Roles []string // col=roles index=unq
3330
Fail bool // col=fail
3431
CreatedAt time.Time // col=created_at auto=c:time.Now()
3532
UpdatedAt time.Time // col=updated_at auto=time.Now()

0 commit comments

Comments
 (0)