Skip to content

Commit

Permalink
feat(service.user) config db & scaffold models (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
darrensemusemu authored Jul 15, 2022
1 parent 3cde71c commit 4d6be4c
Show file tree
Hide file tree
Showing 22 changed files with 6,170 additions and 383 deletions.
11 changes: 6 additions & 5 deletions common/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ require (
)

require (
github.com/golang/protobuf v1.4.2 // indirect
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect
golang.org/x/oauth2 v0.0.0-20210323180902-22b0adad7558 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/protobuf v1.25.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.6 // indirect
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420 // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
)
381 changes: 375 additions & 6 deletions common/go.sum

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions conf/db/dbconfig.yml → conf/db/migrations/dbconfig.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
development:
dialect: postgres
datasource: host=localhost port=5433 dbname=certify_d user=postgres password=postgress sslmode=disable
dir: migrations/postgres
datasource: host=localhost port=5432 dbname=certify_d user=postgres sslmode=disable
dir: postgres
table: migrations

production:
dialect: postgres
datasource: host=prodhost dbname=proddb user=${DB_USER} password=${DB_PASSWORD} sslmode=require
dir: migrations/postgres
dir: postgres
table: migrations
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

-- +migrate Up
-- add kratos customer user and schema
CREATE USER kratos_customer WITH PASSWORD 'kratos_customer';
Expand Down
11 changes: 11 additions & 0 deletions conf/db/migrations/postgres/20220701230935-user-service-schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

-- +migrate Up
-- add user_service user and schema
CREATE USER user_service WITH PASSWORD 'user_service';
CREATE SCHEMA user_service AUTHORIZATION user_service;
alter user user_service set search_path to 'user_service';

-- +migrate Down
-- remove user_service customer user and schema
DROP SCHEMA IF EXISTS user_service;
DROP USER IF EXISTS user_service;
6 changes: 6 additions & 0 deletions conf/db/migrations/postgres/20220715205846-uuid-ossp.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

-- +migrate Up
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- +migrate Down
DROP EXTENSION IF EXISTS "uuid-ossp";
438 changes: 84 additions & 354 deletions go.work.sum

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions service.user/cmd/restapi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ import (

"github.com/darrensemusemu/certify-d-api/common/pkg/middleware/jwt"
"github.com/go-chi/chi/v5"
_ "github.com/jackc/pgx/v4/stdlib"
)

func main() {
// ctx := context.Background()

os.Setenv("XDG_CONFIG_HOME", "../../sqlbioler.toml")
// connString := "postgres://user_service:user_service@localhost:5432/certify_d"
// db, err := sql.Open("pgx", connString)
// if err != nil {
// panic(err)
// }
r := chi.NewRouter()
os.Setenv(jwt.EnvVarJWKSUrl, "http://oathkeeper-api:4456/.well-known/jwks.json")

Expand Down
14 changes: 14 additions & 0 deletions service.user/config/sqlboiler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
add-enum-types = true
add-soft-deletes = true
no-tests = true
output = "pkg/models"
wipe = true

[psql]
dbname = "certify_d"
host = "localhost"
port = 5432
user = "user_service"
pass = "user_service"
schema = "user_service"
blacklist = ["migrations"]
11 changes: 11 additions & 0 deletions service.user/db/migrations/dbconfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
development:
dialect: postgres
datasource: host=localhost port=5432 dbname=certify_d user=user_service password=user_service sslmode=disable
dir: postgres
table: migrations

production:
dialect: postgres
datasource: host=prodhost dbname=proddb user=${DB_USER} password=${DB_PASSWORD} sslmode=require
dir: postgres
table: migrations
62 changes: 62 additions & 0 deletions service.user/db/migrations/postgres/20220714222311-init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
-- +migrate Up
-- Create tables and relationships

CREATE TYPE "user_service"."permission_ref" AS ENUM (
'super_admin',
'admin'
);

CREATE TABLE "user_service"."user" (
"id" uuid PRIMARY KEY NOT NULL DEFAULT (public.uuid_generate_v4()),
"role_id" uuid NOT NULL,
"created_at" timestamp DEFAULT 'now()',
"updated_at" timestamp,
"deleted_at" timestamp
);

CREATE TABLE "user_service"."role" (
"id" uuid PRIMARY KEY NOT NULL DEFAULT (public.uuid_generate_v4()),
"slug" text UNIQUE NOT NULL,
"created_at" timestamp DEFAULT 'now()',
"updated_at" timestamp,
"deleted_at" timestamp
);

CREATE TABLE "user_service"."role_permission" (
"id" uuid NOT NULL DEFAULT (public.uuid_generate_v4()),
"role_id" uuid NOT NULL,
"permission_id" uuid NOT NULL,
"created_at" timestamp DEFAULT 'now()',
"updated_at" timestamp,
"deleted_at" timestamp,
PRIMARY KEY ("id", "role_id", "permission_id")
);

CREATE TABLE "user_service"."permission" (
"id" uuid PRIMARY KEY NOT NULL DEFAULT (public.uuid_generate_v4()),
"slug" user_service.permission_ref UNIQUE NOT NULL,
"created_at" timestamp DEFAULT 'now()',
"updated_at" timestamp,
"deleted_at" timestamp
);

ALTER TABLE "user_service"."user" ADD FOREIGN KEY ("role_id") REFERENCES "user_service"."role" ("id");

ALTER TABLE "user_service"."role_permission" ADD FOREIGN KEY ("role_id") REFERENCES "user_service"."role" ("id");

ALTER TABLE "user_service"."role_permission" ADD FOREIGN KEY ("permission_id") REFERENCES "user_service"."permission" ("id");

-- Seed values
INSERT INTO "user_service"."role" ("slug") VALUES('customer');

-- +migrate Down
-- Drop table & relationships
DROP TABLE "user_service"."role_permission" CASCADE;

DROP TABLE "user_service"."permission" CASCADE;

DROP TABLE "user_service"."role" CASCADE;

DROP TABLE "user_service"."user" CASCADE;

DROP TYPE "user_service"."permission_ref" CASCADE;
35 changes: 28 additions & 7 deletions service.user/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,37 @@ module github.com/darrensemusemu/certify-d-api/service.user

go 1.18

require github.com/go-chi/chi/v5 v5.0.7
require (
github.com/darrensemusemu/certify-d-api/common v0.0.0-20220713212301-3cde71cd118c
github.com/friendsofgo/errors v0.9.2
github.com/go-chi/chi/v5 v5.0.7
github.com/jackc/pgx/v4 v4.16.1
github.com/volatiletech/null/v8 v8.1.2
github.com/volatiletech/sqlboiler/v4 v4.11.0
github.com/volatiletech/strmangle v0.0.4
)

require (
github.com/MicahParks/keyfunc v1.1.0 // indirect
github.com/darrensemusemu/certify-d-api/common v0.0.0-20220712234417-7a84ec43d456
github.com/gofrs/uuid v4.0.0+incompatible // indirect
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
github.com/golang/protobuf v1.4.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.12.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.11.0 // indirect
github.com/ory/kratos-client-go v0.8.0-alpha.2 // indirect
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect
golang.org/x/oauth2 v0.0.0-20210323180902-22b0adad7558 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/protobuf v1.25.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/volatiletech/inflect v0.0.1 // indirect
github.com/volatiletech/randomize v0.0.1 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420 // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
)
Loading

0 comments on commit 4d6be4c

Please sign in to comment.