Skip to content
Permalink
Browse files

Fix missing namespace in table name (#222)

* Tidy modules and ensure latest version

* Add test for table names of models with namespaces

* Fix table names missing namespace

* Move tests to always use a db namespace
  • Loading branch information
mraerino committed Nov 18, 2019
1 parent 06a3401 commit c42b3e93febe5c4686532cb6779033fa7c95ec78
Showing with 49 additions and 346 deletions.
  1. +2 −7 go.mod
  2. +4 −323 go.sum
  3. +3 −2 hack/migrate.sh
  4. +1 −0 hack/test.env
  5. +2 −2 models/audit_log_entry.go
  6. +26 −0 models/db_test.go
  7. +4 −4 models/instance.go
  8. +3 −4 models/refresh_token.go
  9. +3 −3 models/user.go
  10. +1 −1 models/user_test.go
9 go.mod
@@ -7,24 +7,20 @@ require (
github.com/beevik/etree v0.0.0-20180609182452-90dafc1e1f11
github.com/cockroachdb/cockroach-go v0.0.0-20190925194419-606b3d062051 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/fatih/color v1.7.0 // indirect
github.com/go-chi/chi v3.1.4+incompatible
github.com/go-sql-driver/mysql v1.4.1
github.com/gobuffalo/depgen v0.0.0-20190329151759-d478694a28d3 // indirect
github.com/gobuffalo/fizz v1.9.5 // indirect
github.com/gobuffalo/genny v0.4.1 // indirect
github.com/gobuffalo/gitgen v0.0.0-20190315122116-cc086187d211 // indirect
github.com/gobuffalo/nulls v0.1.0 // indirect
github.com/gobuffalo/packr/v2 v2.7.1 // indirect
github.com/gobuffalo/pop v4.12.2+incompatible
github.com/gobuffalo/tags v2.1.6+incompatible // indirect
github.com/gobuffalo/uuid v2.0.5+incompatible
github.com/gobuffalo/x v0.0.0-20181110221217-14085ca3e1a9 // indirect
github.com/imdario/mergo v0.0.0-20160216103600-3e95a51e0639
github.com/jackc/pgconn v1.1.0 // indirect
github.com/jackc/pgx v3.6.0+incompatible // indirect
github.com/joho/godotenv v1.3.0
github.com/jonboulle/clockwork v0.0.0-20180716110948-e7c6d408fd5c // indirect
github.com/karrick/godirwalk v1.13.0 // indirect
github.com/kelseyhightower/envconfig v1.3.0
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.10 // indirect
@@ -40,14 +36,13 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.4.0
golang.org/x/crypto v0.0.0-20191107222254-f4817d981bb6
golang.org/x/lint v0.0.0-20190930215403-16217165b5de // indirect
golang.org/x/net v0.0.0-20191108063844-7e6e90b9ea88 // indirect
golang.org/x/oauth2 v0.0.0-20170807180024-9a379c6b3e95
golang.org/x/sys v0.0.0-20191105231009-c1f44814a5cd // indirect
golang.org/x/tools v0.0.0-20191107235519-f7ea15e60b12 // indirect
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 // indirect
google.golang.org/api v0.0.0-20170821230356-dd6bdadc5852 // indirect
google.golang.org/appengine v1.6.5 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 // indirect
gopkg.in/yaml.v2 v2.2.5 // indirect
327 go.sum

Large diffs are not rendered by default.

@@ -6,9 +6,10 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
DATABASE="$DIR/database.yml"

export GOTRUE_DB_DRIVER="mysql"
export GOTRUE_DB_DATABASE_URL="root@tcp(127.0.0.1:3306)/gotrue_$DB_ENV?parseTime=true"
export GOTRUE_DB_DATABASE_URL="root@tcp(127.0.0.1:3306)/gotrue_$DB_ENV?parseTime=true&multiStatements=true"
export GOTRUE_DB_MIGRATIONS_PATH=$DIR/../migrations

echo soda -v
soda drop -d -e $DB_ENV -c $DATABASE
soda create -d -e $DB_ENV -c $DATABASE
soda migrate -d -e $DB_ENV -c $DATABASE
go run main.go migrate -c $DIR/test.env
@@ -3,6 +3,7 @@ GOTRUE_JWT_EXP=3600
GOTRUE_JWT_AUD=api.netlify.com
GOTRUE_DB_DRIVER=mysql
GOTRUE_DB_AUTOMIGRATE=true
GOTRUE_DB_NAMESPACE=test
DATABASE_URL="root@tcp(127.0.0.1:3306)/gotrue_test?parseTime=true&sql_mode=TRADITIONAL"
GOTRUE_API_HOST=localhost
PORT=9999
@@ -5,10 +5,10 @@ import (
"fmt"
"time"

"github.com/gobuffalo/uuid"
"github.com/netlify/gotrue/storage"
"github.com/netlify/gotrue/storage/namespace"
"github.com/pkg/errors"
"github.com/gobuffalo/uuid"
)

type AuditAction string
@@ -55,7 +55,7 @@ type AuditLogEntry struct {
CreatedAt time.Time `json:"created_at" db:"created_at"`
}

func (a *AuditLogEntry) TableName() string {
func (AuditLogEntry) TableName() string {
tableName := "audit_log_entries"

if namespace.GetNamespace() != "" {
@@ -0,0 +1,26 @@
package models_test

import (
"testing"

"github.com/gobuffalo/pop"
"github.com/netlify/gotrue/models"
"github.com/stretchr/testify/assert"
)

func TestTableNameNamespacing(t *testing.T) {
cases := []struct {
expected string
value interface{}
}{
{expected: "test_audit_log_entries", value: []*models.AuditLogEntry{}},
{expected: "test_instances", value: []*models.Instance{}},
{expected: "test_refresh_tokens", value: []*models.RefreshToken{}},
{expected: "test_users", value: []*models.User{}},
}

for _, tc := range cases {
m := &pop.Model{Value: tc.value}
assert.Equal(t, tc.expected, m.TableName())
}
}
@@ -1,15 +1,15 @@
package models

import (
"github.com/netlify/gotrue/storage/namespace"
"database/sql"
"time"

"github.com/gobuffalo/pop"
"github.com/gobuffalo/uuid"
"github.com/netlify/gotrue/conf"
"github.com/netlify/gotrue/storage"
"github.com/netlify/gotrue/storage/namespace"
"github.com/pkg/errors"
"github.com/gobuffalo/uuid"
)

const baseConfigKey = ""
@@ -25,13 +25,13 @@ type Instance struct {
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}

func (i *Instance) TableName() string {
func (Instance) TableName() string {
tableName := "instances"

if namespace.GetNamespace() != "" {
return namespace.GetNamespace() + "_" + tableName
}

return tableName
}

@@ -5,10 +5,10 @@ import (
"time"

"github.com/gobuffalo/pop"
"github.com/gobuffalo/uuid"
"github.com/netlify/gotrue/crypto"
"github.com/netlify/gotrue/storage"
"github.com/pkg/errors"
"github.com/gobuffalo/uuid"
)

// RefreshToken is the database model for refresh tokens.
@@ -25,14 +25,13 @@ type RefreshToken struct {
UpdatedAt time.Time `db:"updated_at"`
}


func (rt *RefreshToken) TableName() string {
func (RefreshToken) TableName() string {
tableName := "refresh_tokens"

if namespace.GetNamespace() != "" {
return namespace.GetNamespace() + "_" + tableName
}

return tableName
}

@@ -6,10 +6,10 @@ import (
"time"

"github.com/gobuffalo/pop"
"github.com/gobuffalo/uuid"
"github.com/netlify/gotrue/storage"
"github.com/netlify/gotrue/storage/namespace"
"github.com/pkg/errors"
"github.com/gobuffalo/uuid"
"golang.org/x/crypto/bcrypt"
)

@@ -81,13 +81,13 @@ func NewSystemUser(instanceID uuid.UUID, aud string) *User {
}
}

func (u *User) TableName() string {
func (User) TableName() string {
tableName := "users"

if namespace.GetNamespace() != "" {
return namespace.GetNamespace() + "_" + tableName
}

return tableName
}

@@ -3,10 +3,10 @@ package models
import (
"testing"

"github.com/gobuffalo/uuid"
"github.com/netlify/gotrue/conf"
"github.com/netlify/gotrue/storage"
"github.com/netlify/gotrue/storage/test"
"github.com/gobuffalo/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

0 comments on commit c42b3e9

Please sign in to comment.
You can’t perform that action at this time.