Fixed indexing on m:n table, as well as added seeding function

This commit is contained in:
Kasper Juul Hermansen 2022-02-15 00:00:48 +01:00
parent 66f9df86d8
commit 205adeb118
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
6 changed files with 81 additions and 0 deletions

View File

@ -48,6 +48,7 @@ services:
DATABASE_URL: "postgresql://serverctl:serverctlsecret@db/serverctl" DATABASE_URL: "postgresql://serverctl:serverctlsecret@db/serverctl"
ports: ports:
- "8080:8080" - "8080:8080"
- "6060:6060" # Profiler optional
logging: *loki-logging logging: *loki-logging
depends_on: depends_on:
- db_migrator - db_migrator

View File

@ -0,0 +1,10 @@
-- Write your migrate up statements here
create index member_id_project_member_idx on sctl_project_member (member_id);
---- create above / drop below ----
drop index member_id_project_member_idx;
-- Write your migrate down statements here. If this migration is irreversible
-- Then delete the separator line above.

View File

@ -0,0 +1,9 @@
-- Write your migrate up statements here
create index project_id_project_member_idx on sctl_project_member (project_id);
---- create above / drop below ----
drop index project_id_project_member_idx;
-- Write your migrate down statements here. If this migration is irreversible
-- Then delete the separator line above.

View File

@ -29,6 +29,7 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/protobuf v1.5.0 // indirect github.com/golang/protobuf v1.5.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.11.0 // indirect github.com/jackc/pgconn v1.11.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect github.com/jackc/pgio v1.0.0 // indirect

View File

@ -437,6 +437,8 @@ github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY= github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=

View File

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"fmt"
"github.com/dgraph-io/ristretto" "github.com/dgraph-io/ristretto"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/client" "github.com/docker/docker/client"
@ -10,10 +11,13 @@ import (
"github.com/eko/gocache/store" "github.com/eko/gocache/store"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-co-op/gocron" "github.com/go-co-op/gocron"
"github.com/google/uuid"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"os" "os"
"serverctl/pkg/application/projects" "serverctl/pkg/application/projects"
@ -23,6 +27,8 @@ import (
"time" "time"
) )
import _ "net/http/pprof"
func setupLogger() *zap.Logger { func setupLogger() *zap.Logger {
highPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { highPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.ErrorLevel return lvl >= zapcore.ErrorLevel
@ -269,5 +275,57 @@ func main() {
projectsRepository := postgres.NewProjectsRepository(database) projectsRepository := postgres.NewProjectsRepository(database)
projectsService := projects.NewService(logger, projectsRepository, cacheM) projectsService := projects.NewService(logger, projectsRepository, cacheM)
setupProfiler()
addSeedData(database, usersRepository, projectsRepository)
setupApi(logger, cacheM, usersService, projectsService) setupApi(logger, cacheM, usersService, projectsService)
} }
func setupProfiler() {
go func() {
log.Println(http.ListenAndServe(":6060", nil))
}()
}
func addSeedData(database *db.Client, ur users.Repository, pr projects.Repository) {
conn := database.GetConn(context.Background())
defer conn.Release()
var numRows int
err := conn.QueryRow(context.Background(), "select count(id) from sctl_user").Scan(&numRows)
if err != nil {
panic(err)
}
if numRows == 0 {
addTestData(conn, ur, pr)
}
}
func addTestData(conn *pgxpool.Conn, ur users.Repository, pr projects.Repository) {
ctx := context.Background()
for jobs := 0; jobs < 100; jobs++ {
go func() {
for i := 0; i < 1_000; i++ {
var (
user *users.CreateUser
err error
userId int
)
user, err = users.NewCreateUser(fmt.Sprintf("%s@test.com", uuid.New().String()), "password", users.NewPlainTextPasswordHasher())
if err != nil {
panic(err)
}
userId, err = ur.Create(ctx, user)
if err != nil {
panic(err)
}
_, err = pr.Create(ctx, projects.NewCreateProject(uuid.New().String()[:20], userId))
if err != nil {
panic(err)
}
}
}()
}
}