Skip to content

Commit

Permalink
refactor: move to atlas (#84)
Browse files Browse the repository at this point in the history
* feat: refactor to atlas
chore: switch to devbox

* chore: add generated file test workflow

* chore: add devbox script go generation

* chore: rename step

* chore: update atlas install script

* chore: fix issues from staging

* chore: lint

* chore: do not fail fast seeded workflow

* fix: tag descriptions CAN be null

* fix: game_version can be null

* fix: apparently even downloads can be null

* fix: normalize database to match code model

* fix: version dependencies should resolve mod ids

* chore: lint

* chore: remove flaky test

* refactor: remove unused indexes
fix: return mod_reference in mod_id field again
feat: add mod_reference field on version dependency

* chore: hash migrations

* fix: return correct dependencies on REST endpoint

* feat: add mod_reference field to REST endpoint

* chore: generate files
  • Loading branch information
Vilsol authored Aug 19, 2024
1 parent e726005 commit a0847a4
Show file tree
Hide file tree
Showing 80 changed files with 2,048 additions and 430 deletions.
8 changes: 7 additions & 1 deletion .envrc
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
use flake
# Automatically sets up your devbox environment whenever you cd into this
# directory via our direnv integration:

eval "$(devbox generate direnv --print-envrc)"

# check out https://www.jetpack.io/devbox/docs/ide_configuration/direnv/
# for more details
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ jobs:
name: Seeded Test
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- download_url: "S3_URL_PROD"
Expand Down
35 changes: 35 additions & 0 deletions .github/workflows/generated.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: generated

on:
- push
- pull_request

jobs:
atlas:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install atlas CLI
run: curl -sSf https://atlasgo.sh | sh

- name: Hash migrations
run: atlas migrate hash --dir "file://migrations/sql?format=golang-migrate"

- name: Ensure no changes
run: git diff --exit-code

go:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: jetify-com/[email protected]
with:
enable-cache: true

- name: Run generation tasks
run: devbox run generate

- name: Ensure no changes
run: git diff --exit-code
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22.4-alpine AS builder
FROM golang:1.22.3-alpine AS builder

RUN apk add --no-cache git build-base libpng-dev protoc
RUN go install google.golang.org/protobuf/cmd/[email protected]
Expand All @@ -18,7 +18,7 @@ RUN go generate -tags tools -x ./...
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -v -a -installsuffix cgo -o /go/bin/api cmd/api/serve.go


FROM golang:1.22.4-alpine
FROM golang:1.22.3-alpine
RUN apk add --no-cache libstdc++ libpng
COPY --from=builder /go/bin/api /api
WORKDIR /app
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func InitializeConfig(baseCtx context.Context) context.Context {

err := viper.ReadInConfig() //nolint:ifshort

if err := logging.SetupLogger(); err != nil {
if err := logging.SetupLogger(nil); err != nil {
panic(err)
}

Expand Down
1 change: 1 addition & 0 deletions conversion/ent_to_graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type Version interface {
// goverter:extend TimeToString UIntToInt Int64ToInt
type VersionDependency interface {
// goverter:ignore Mod Version
// goverter:map ModID ModReference
Convert(source *ent.VersionDependency) *generated.VersionDependency
ConvertSlice(source []*ent.VersionDependency) []*generated.VersionDependency
}
Expand Down
3 changes: 3 additions & 0 deletions conversion/ent_to_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type ModAllVersions interface {

// goverter:map . Link | TargetLink
ConvertTarget(source *ent.VersionTarget) *types.ModAllVersionsVersionTarget

// goverter:map ModID ModReference
ConvertDependency(source *ent.VersionDependency) *types.ModAllVersionsVersionDependency
}

func TargetLink(source *ent.VersionTarget) string {
Expand Down
2 changes: 1 addition & 1 deletion dataloader/loaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Middleware() func(handlerFunc echo.HandlerFunc) echo.HandlerFunc {
ctx := context.WithValue(c.Request().Context(), loadersKey{}, &Loaders{
VersionDependenciesByVersionID: dataloader.NewBatchedLoader(func(ctx context.Context, ids []string) []*dataloader.Result[[]*ent.VersionDependency] {
// TODO Query only selected fields from context
entities, err := db.From(ctx).VersionDependency.Query().Where(versiondependency.VersionIDIn(ids...)).All(ctx)
entities, err := db.From(ctx).VersionDependency.Query().Where(versiondependency.VersionIDIn(ids...)).WithMod().All(ctx)
if err != nil {
return nil
}
Expand Down
5 changes: 2 additions & 3 deletions db/ent.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
"github.com/spf13/viper"

"github.com/satisfactorymodding/smr-api/db/entlogger"
"github.com/satisfactorymodding/smr-api/generated/ent"

// Required PGX driver
Expand Down Expand Up @@ -60,9 +61,7 @@ func WithDB(ctx context.Context) (context.Context, error) {

realClient := ent.NewClient(
ent.Driver(cacheDriver),
ent.Log(func(v ...interface{}) {
slox.Info(ctx, fmt.Sprint(v...))
}),
ent.Log(entlogger.EntLogger(ctx)),
)

if debugEnabled {
Expand Down
18 changes: 18 additions & 0 deletions db/entlogger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package entlogger

import (
"context"
"fmt"

"github.com/Vilsol/slox"
)

/*
Placed here so our [logging.StackRewriter] can exclude it
*/

func EntLogger(ctx context.Context) func(v ...interface{}) {
return func(v ...interface{}) {
slox.Info(ctx, fmt.Sprint(v...))
}
}
7 changes: 0 additions & 7 deletions db/schema/mixin_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package schema
import (
"entgo.io/ent"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
"entgo.io/ent/schema/mixin"

"github.com/satisfactorymodding/smr-api/util"
Expand All @@ -18,9 +17,3 @@ func (IDMixin) Fields() []ent.Field {
field.String("id").DefaultFunc(util.GenerateUniqueID),
}
}

func (IDMixin) Indexes() []ent.Index {
return []ent.Index{
index.Fields("id"),
}
}
3 changes: 2 additions & 1 deletion db/schema/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (Mod) Fields() []ent.Field {
field.Uint("downloads").Default(0),
field.Bool("denied").Default(false),
field.Time("last_version_date").Optional(),
field.String("mod_reference").MaxLen(32).Unique(),
field.String("mod_reference").MaxLen(32),
field.Bool("hidden").Default(false),
field.JSON("compatibility", &util.CompatibilityInfo{}).Optional(),
}
Expand All @@ -45,6 +45,7 @@ func (Mod) Fields() []ent.Field {
func (Mod) Indexes() []ent.Index {
return []ent.Index{
index.Fields("last_version_date"),
index.Fields("mod_reference").Unique(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion db/schema/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (Tag) Mixin() []ent.Mixin {
func (Tag) Fields() []ent.Field {
return []ent.Field{
field.String("name").MaxLen(24).Unique(),
field.String("description").MaxLen(512).Unique(),
field.String("description").MaxLen(512).Optional(),
}
}

Expand Down
16 changes: 8 additions & 8 deletions db/schema/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ func (User) Mixin() []ent.Mixin {

func (User) Fields() []ent.Field {
return []ent.Field{
field.String("email").MaxLen(256).Unique(),
field.String("email").MaxLen(256),
field.String("username").MaxLen(32),
field.String("avatar").Optional(),
field.String("joined_from").Optional(),
field.Bool("banned").Default(false),
field.Int("rank").Default(1),
field.String("github_id").MaxLen(16).Unique().Optional(),
field.String("google_id").MaxLen(16).Unique().Optional(),
field.String("facebook_id").MaxLen(16).Unique().Optional(),
field.String("github_id").MaxLen(16).Optional(),
field.String("google_id").MaxLen(16).Optional(),
field.String("facebook_id").MaxLen(16).Optional(),
}
}

func (User) Indexes() []ent.Index {
return []ent.Index{
index.Fields("email"),
index.Fields("github_id"),
index.Fields("google_id"),
index.Fields("facebook_id"),
index.Fields("email").Unique(),
index.Fields("github_id").Unique(),
index.Fields("google_id").Unique(),
index.Fields("facebook_id").Unique(),
}
}

Expand Down
9 changes: 8 additions & 1 deletion db/schema/user_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
)

type UserSession struct {
Expand All @@ -20,11 +21,17 @@ func (UserSession) Mixin() []ent.Mixin {

func (UserSession) Fields() []ent.Field {
return []ent.Field{
field.String("token").MaxLen(512).Unique(),
field.String("token").MaxLen(512),
field.String("user_agent").Optional(),
}
}

func (UserSession) Indexes() []ent.Index {
return []ent.Index{
index.Fields("token").Unique(),
}
}

func (UserSession) Edges() []ent.Edge {
return []ent.Edge{
edge.From("user", User.Type).
Expand Down
11 changes: 10 additions & 1 deletion db/schema/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"

"github.com/satisfactorymodding/smr-api/util"
)
Expand All @@ -28,7 +29,7 @@ func (Version) Fields() []ent.Field {
field.String("changelog").Optional(),
field.Uint("downloads").Default(0),
field.String("key").Optional(),
field.Enum("stability").GoType(util.Stability("")),
field.Enum("stability").GoType(util.Stability("")).Default("release"),
field.Bool("approved").Default(false),
field.Uint("hotness").Default(0),
field.Bool("denied").Default(false),
Expand All @@ -42,6 +43,14 @@ func (Version) Fields() []ent.Field {
}
}

func (Version) Indexes() []ent.Index {
return []ent.Index{
index.Fields("approved"),
index.Fields("denied"),
index.Fields("mod_id"),
}
}

func (Version) Edges() []ent.Edge {
return []ent.Edge{
edge.From("mod", Mod.Type).
Expand Down
2 changes: 1 addition & 1 deletion db/schema/version_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (VersionTarget) Fields() []ent.Field {

func (VersionTarget) Edges() []ent.Edge {
return []ent.Edge{
edge.From("sml_version", Version.Type).
edge.From("version", Version.Type).
Ref("targets").
Field("version_id").
Unique().
Expand Down
44 changes: 44 additions & 0 deletions devbox.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"$schema": "https://raw.githubusercontent.com/jetify-com/devbox/0.11.0/.schema/devbox.schema.json",
"packages": [
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"minio-client@2024-06-10T16-44-15Z",
"[email protected]",
"[email protected]",
"[email protected]",
".#atlas"
],
"shell": {
"scripts": {
"api": "go run cmd/api/serve.go",
"test": "go test -v ./...",
"migrate:diff": [
"echo -n 'Migration Name: '",
"read migration_name",
"atlas migrate diff \"$migration_name\" --dir \"file://migrations/sql?format=golang-migrate\" --to \"ent://db/schema\" --dev-url \"docker://postgres/16/test?search_path=public\""
],
"migrate:new": [
"echo -n 'Migration Name: '",
"read migration_name",
"atlas migrate new \"$migration_name\" --dir \"file://migrations/sql?format=golang-migrate\""
],
"migrate:hash": [
"atlas migrate hash --dir \"file://migrations/sql?format=golang-migrate\""
],
"generate": "go generate -tags tools -x ./...",
"setup": [
"docker compose up -d && sleep 5",
"mc alias set local http://localhost:9000 minio minio123",
"mc admin user svcacct add local minio --access-key REPLACE_ME_KEY --secret-key REPLACE_ME_SECRET",
"mc anonymous set public local/smr"
],
"teardown": [
"docker compose down"
]
}
}
}
Loading

0 comments on commit a0847a4

Please sign in to comment.