Skip to content

Commit e88fb14

Browse files
feat: use conduit for migrations; prefix all top-level modules with shield*
1 parent 9dc15ca commit e88fb14

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+473
-252
lines changed

.test.env

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
DATABASE_URI="postgresql://local:local@localhost:5432/local?sslmode=disable"
2-
2+
CONDUIT_DATABASE_URL="postgresql://local:local@localhost:5432/local?sslmode=disable"
3+
CONDUIT_MIGRATION_DIR="migrations"

cmd/shield/main.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
"os"
7+
"os/signal"
8+
"syscall"
9+
10+
dotenv "github.com/joho/godotenv"
11+
"go.inout.gg/conduit"
12+
"go.inout.gg/conduit/conduitcli"
13+
"go.inout.gg/shield/internal/migrations"
14+
)
15+
16+
func main() {
17+
_ = dotenv.Load()
18+
19+
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM)
20+
defer cancel()
21+
22+
migrator := conduit.NewMigrator(conduit.NewConfig(func(c *conduit.Config) {
23+
c.Registry = migrations.Registry
24+
}))
25+
26+
if err := conduitcli.Execute(ctx, migrator); err != nil {
27+
log.Fatal(err)
28+
os.Exit(1)
29+
return
30+
}
31+
}

csrf/middleware.go

+8-13
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,28 @@ import (
88
"net/http"
99
"slices"
1010

11-
httperror "go.inout.gg/foundations/http/error"
12-
"go.inout.gg/foundations/http/errorhandler"
13-
"go.inout.gg/foundations/http/middleware"
11+
"go.inout.gg/foundations/http/httperror"
12+
"go.inout.gg/foundations/http/httpmiddleware"
1413
)
1514

1615
type ctxKey struct{}
1716

1817
var kCtxKey = ctxKey{}
1918

20-
var (
21-
ErrNoChecksumSecret = errors.New("shield/csrf: checksum secret is not provided")
22-
)
19+
var ErrNoChecksumSecret = errors.New("shield/csrf: checksum secret is not provided")
2320

2421
var (
2522
DefaultFieldName = "csrf_token"
2623
DefaultHeaderName = "X-CSRF-Token"
2724
DefaultCookieName = "csrf_token"
2825
)
2926

30-
var (
31-
DefaultTokenLength = 32
32-
)
27+
var DefaultTokenLength = 32
3328

3429
// Config is the configuration for the CSRF middleware.
3530
type Config struct {
36-
IgnoredMethods []string // optional (default: [GET, HEAD, OPTIONS, TRACE])
37-
ErrorHandler errorhandler.ErrorHandler // optional (default: errorhandler.DefaultErrorHandler)
31+
IgnoredMethods []string // optional (default: [GET, HEAD, OPTIONS, TRACE])
32+
ErrorHandler httperror.ErrorHandler // optional (default: errorhandler.DefaultErrorHandler)
3833

3934
ChecksumSecret string
4035
TokenLength int // optional (default: 64)
@@ -47,7 +42,7 @@ type Config struct {
4742
}
4843

4944
// Middleware returns a middleware that adds CSRF token to the request context.
50-
func Middleware(secret string, config ...func(*Config)) (middleware.MiddlewareFunc, error) {
45+
func Middleware(secret string, config ...func(*Config)) (httpmiddleware.MiddlewareFunc, error) {
5146
cfg := Config{
5247
IgnoredMethods: []string{
5348
http.MethodGet,
@@ -66,7 +61,7 @@ func Middleware(secret string, config ...func(*Config)) (middleware.MiddlewareFu
6661
}
6762

6863
if cfg.ErrorHandler == nil {
69-
cfg.ErrorHandler = errorhandler.DefaultErrorHandler
64+
cfg.ErrorHandler = httperror.DefaultErrorHandler
7065
}
7166

7267
return func(next http.Handler) http.Handler {

docker-compose.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
db:
3+
image: postgres:16
4+
restart: unless-stopped
5+
environment:
6+
POSTGRES_PASSWORD: local
7+
POSTGRES_USER: local
8+
POSTGRES_DB: local
9+
ports:
10+
- "5432:5432"

flake.nix

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
description = "Shield is a comprehensive, opinionated authentication framework for Go built on PostgreSQL";
3+
4+
inputs = {
5+
nixpkgs.url = "nixpkgs/nixos-unstable";
6+
flake-utils.url = "github:numtide/flake-utils";
7+
};
8+
9+
outputs = { self, nixpkgs, flake-utils }:
10+
flake-utils.lib.eachDefaultSystem (system:
11+
let
12+
pkgs = import nixpkgs { inherit system; };
13+
in
14+
{
15+
devShells.default = pkgs.mkShell {
16+
buildInputs = with pkgs; [
17+
nodejs
18+
go_1_23
19+
sqlc
20+
golangci-lint
21+
gofumpt
22+
];
23+
};
24+
}
25+
);
26+
}

go.mod

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
module go.inout.gg/shield
22

3-
go 1.23
4-
5-
toolchain go1.23.0
3+
go 1.23.1
64

75
require (
86
github.com/coreos/go-oidc/v3 v3.11.0
97
github.com/go-playground/mold/v4 v4.5.0
108
github.com/go-playground/validator/v10 v10.22.0
119
github.com/go-webauthn/webauthn v0.11.2
1210
github.com/google/uuid v1.6.0
13-
github.com/jackc/pgx/v5 v5.6.0
14-
github.com/samber/lo v1.47.0
11+
github.com/jackc/pgx/v5 v5.7.1
12+
github.com/joho/godotenv v1.5.1
1513
github.com/stretchr/testify v1.9.0
16-
go.inout.gg/foundations v0.0.0-20240827213129-017c0a123b9e
17-
golang.org/x/crypto v0.26.0
14+
go.inout.gg/conduit v0.0.0-20241005222024-59593f2d1c1c
15+
go.inout.gg/foundations v0.0.0-20240922153347-0be999149d7c
16+
golang.org/x/crypto v0.27.0
1817
golang.org/x/oauth2 v0.22.0
19-
golang.org/x/text v0.17.0
18+
golang.org/x/text v0.18.0
2019
)
2120

2221
require (
2322
github.com/caarlos0/env/v11 v11.1.0 // indirect
23+
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
2424
github.com/davecgh/go-spew v1.1.1 // indirect
2525
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
2626
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
@@ -33,17 +33,21 @@ require (
3333
github.com/gosimple/slug v1.13.1 // indirect
3434
github.com/gosimple/unidecode v1.0.1 // indirect
3535
github.com/jackc/pgpassfile v1.0.0 // indirect
36-
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
37-
github.com/jackc/puddle/v2 v2.2.1 // indirect
38-
github.com/joho/godotenv v1.5.1 // indirect
36+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
37+
github.com/jackc/puddle/v2 v2.2.2 // indirect
3938
github.com/leodido/go-urn v1.4.0 // indirect
4039
github.com/mitchellh/mapstructure v1.5.0 // indirect
4140
github.com/pmezard/go-difflib v1.0.0 // indirect
41+
github.com/rogpeppe/go-internal v1.13.1 // indirect
42+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
43+
github.com/samber/lo v1.47.0 // indirect
4244
github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734 // indirect
4345
github.com/segmentio/go-snakecase v1.2.0 // indirect
46+
github.com/urfave/cli/v2 v2.27.4 // indirect
4447
github.com/x448/float16 v0.8.4 // indirect
48+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
4549
golang.org/x/net v0.27.0 // indirect
4650
golang.org/x/sync v0.8.0 // indirect
47-
golang.org/x/sys v0.23.0 // indirect
51+
golang.org/x/sys v0.25.0 // indirect
4852
gopkg.in/yaml.v3 v3.0.1 // indirect
4953
)

go.sum

+26-16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/caarlos0/env/v11 v11.1.0 h1:a5qZqieE9ZfzdvbbdhTalRrHT5vu/4V1/ad1Ka6fr
22
github.com/caarlos0/env/v11 v11.1.0/go.mod h1:LwgkYk1kDvfGpHthrWWLof3Ny7PezzFwS4QrsJdHTMo=
33
github.com/coreos/go-oidc/v3 v3.11.0 h1:Ia3MxdwpSw702YW0xgfmP1GVCMA9aEFWu12XUZ3/OtI=
44
github.com/coreos/go-oidc/v3 v3.11.0/go.mod h1:gE3LgjOgFoHi9a4ce4/tJczr0Ai2/BoDhf0r5lltWI0=
5+
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
6+
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
57
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
68
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
79
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -39,12 +41,12 @@ github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6
3941
github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc=
4042
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
4143
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
42-
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
43-
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
44-
github.com/jackc/pgx/v5 v5.6.0 h1:SWJzexBzPL5jb0GEsrPMLIsi/3jOo7RHlzTjcAeDrPY=
45-
github.com/jackc/pgx/v5 v5.6.0/go.mod h1:DNZ/vlrUnhWCoFGxHAG8U2ljioxukquj7utPDgtQdTw=
46-
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
47-
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
44+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
45+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
46+
github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs=
47+
github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA=
48+
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
49+
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
4850
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
4951
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
5052
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
@@ -57,8 +59,10 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
5759
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
5860
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5961
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
60-
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
61-
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
62+
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
63+
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
64+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
65+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
6266
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
6367
github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
6468
github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734 h1:Cpx2WLIv6fuPvaJAHNhYOgYzk/8RcJXu/8+mOrxf2KM=
@@ -70,22 +74,28 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
7074
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
7175
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
7276
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
77+
github.com/urfave/cli/v2 v2.27.4 h1:o1owoI+02Eb+K107p27wEX9Bb8eqIoZCfLXloLUSWJ8=
78+
github.com/urfave/cli/v2 v2.27.4/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ=
7379
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
7480
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
75-
go.inout.gg/foundations v0.0.0-20240827213129-017c0a123b9e h1:OYnYTAJHu/OC/SwREXgsyCrzGEr3Mj5KfGdJULOHrgo=
76-
go.inout.gg/foundations v0.0.0-20240827213129-017c0a123b9e/go.mod h1:GL+lTMK6SWIrnHsQ8VqIUzj+2mb0OdDZDF3pb+qfNr8=
77-
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
78-
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
81+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
82+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
83+
go.inout.gg/conduit v0.0.0-20241005222024-59593f2d1c1c h1:oGSiGChsI/gVGI/optS68IPRe55OsOefyPeq6ALTccQ=
84+
go.inout.gg/conduit v0.0.0-20241005222024-59593f2d1c1c/go.mod h1:WKBNQOuiROnR9hJ0oJpLkI7k4+gXS7tnk8Ma+8RRaWQ=
85+
go.inout.gg/foundations v0.0.0-20240922153347-0be999149d7c h1:xkhJcJgpXchg5mL4xxzxlmMtFepJLewCUVa81bv03Q4=
86+
go.inout.gg/foundations v0.0.0-20240922153347-0be999149d7c/go.mod h1:/6QZOnj4l6OJz8iogY0uArlkPeUsxz1PTA2EHWotD7Q=
87+
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
88+
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
7989
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
8090
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
8191
golang.org/x/oauth2 v0.22.0 h1:BzDx2FehcG7jJwgWLELCdmLuxk2i+x9UDpSiss2u0ZA=
8292
golang.org/x/oauth2 v0.22.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
8393
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
8494
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
85-
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
86-
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
87-
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
88-
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
95+
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
96+
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
97+
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
98+
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
8999
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
90100
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
91101
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

internal/dbsqlc/migrations.go

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
"log/slog"
6+
"os"
7+
8+
"github.com/jackc/pgx/v5"
9+
"go.inout.gg/conduit/conduitmigrate"
10+
)
11+
12+
var (
13+
logger = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
14+
m1726951089532 = conduitmigrate.New(&conduitmigrate.Config{
15+
Logger: logger,
16+
})
17+
)
18+
19+
func init() {
20+
Registry.Up(up1726951089532)
21+
Registry.Down(down1726951089532)
22+
}
23+
24+
func up1726951089532(ctx context.Context, conn *pgx.Conn) error {
25+
return m1726951089532.Up(ctx, conn)
26+
}
27+
28+
func down1726951089532(ctx context.Context, conn *pgx.Conn) error {
29+
return m1726951089532.Down(ctx, conn)
30+
}

internal/dbsqlc/migrations/0001_initial_schema.sql internal/migrations/1726951117573_initial_schema.sql

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
CREATE TABLE users (
1+
-- migration: 1726948730233_initial_schema.sql
2+
3+
CREATE TABLE IF NOT EXISTS users (
24
id UUID NOT NULL,
35
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
46
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
@@ -8,7 +10,7 @@ CREATE TABLE users (
810
UNIQUE (email)
911
);
1012

11-
CREATE TABLE user_email_verification_tokens (
13+
CREATE TABLE IF NOT EXISTS user_email_verification_tokens (
1214
id UUID NOT NULL,
1315
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
1416
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
@@ -23,7 +25,7 @@ CREATE TABLE user_email_verification_tokens (
2325
ON DELETE CASCADE
2426
);
2527

26-
CREATE TABLE user_credentials (
28+
CREATE TABLE IF NOT EXISTS user_credentials (
2729
id UUID NOT NULL,
2830
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
2931
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
@@ -38,7 +40,7 @@ CREATE TABLE user_credentials (
3840
ON DELETE CASCADE
3941
);
4042

41-
CREATE TABLE password_reset_tokens (
43+
CREATE TABLE IF NOT EXISTS password_reset_tokens (
4244
id UUID NOT NULL,
4345
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
4446
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
@@ -55,7 +57,7 @@ CREATE TABLE password_reset_tokens (
5557
CHECK (expires_at > created_at)
5658
);
5759

58-
CREATE TABLE user_sessions (
60+
CREATE TABLE IF NOT EXISTS user_sessions (
5961
id UUID NOT NULL,
6062
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
6163
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
@@ -73,8 +75,8 @@ CREATE TABLE user_sessions (
7375

7476
---- create above / drop below ----
7577

76-
DROP TABLE users;
77-
DROP TABLE user_email_verification_tokens;
78-
DROP TABLE user_credentials;
79-
DROP TABLE password_reset_tokens;
80-
DROP TABLE user_sessions;
78+
DROP TABLE IF EXISTS user_sessions;
79+
DROP TABLE IF EXISTS password_reset_tokens;
80+
DROP TABLE IF EXISTS user_credentials;
81+
DROP TABLE IF EXISTS user_email_verification_tokens;
82+
DROP TABLE IF EXISTS users;

internal/migrations/registry.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package migrations
2+
3+
import (
4+
"embed"
5+
6+
"go.inout.gg/conduit/conduitregistry"
7+
)
8+
9+
var Registry = conduitregistry.New("inout/shield")
10+
11+
//go:embed **.sql
12+
var migrationFS embed.FS
13+
14+
func init() {
15+
Registry.FromFS(migrationFS)
16+
}

0 commit comments

Comments
 (0)