mirror of
https://github.com/netbirdio/netbird.git
synced 2025-01-10 07:58:32 +01:00
Refactor and handle null time
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
parent
3f30eb7692
commit
0207a326dc
@ -18,11 +18,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func GetColumnName(db *gorm.DB, column string) string {
|
func GetColumnName(db *gorm.DB, column string) string {
|
||||||
|
|
||||||
if db.Name() == "mysql" {
|
if db.Name() == "mysql" {
|
||||||
return fmt.Sprintf("`%s`", column)
|
return fmt.Sprintf("`%s`", column)
|
||||||
}
|
}
|
||||||
|
|
||||||
return column
|
return column
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,7 +218,6 @@ func MigrateNetIPFieldFromBlobToJSON[T any](ctx context.Context, db *gorm.DB, fi
|
|||||||
}
|
}
|
||||||
|
|
||||||
func MigrateSetupKeyToHashedSetupKey[T any](ctx context.Context, db *gorm.DB) error {
|
func MigrateSetupKeyToHashedSetupKey[T any](ctx context.Context, db *gorm.DB) error {
|
||||||
|
|
||||||
orgColumnName := "key"
|
orgColumnName := "key"
|
||||||
oldColumnName := GetColumnName(db, orgColumnName)
|
oldColumnName := GetColumnName(db, orgColumnName)
|
||||||
newColumnName := "key_secret"
|
newColumnName := "key_secret"
|
||||||
|
@ -11,18 +11,31 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"github.com/testcontainers/testcontainers-go"
|
"github.com/testcontainers/testcontainers-go"
|
||||||
"github.com/testcontainers/testcontainers-go/modules/mysql"
|
"github.com/testcontainers/testcontainers-go/modules/mysql"
|
||||||
"github.com/testcontainers/testcontainers-go/modules/postgres"
|
"github.com/testcontainers/testcontainers-go/modules/postgres"
|
||||||
"github.com/testcontainers/testcontainers-go/wait"
|
"github.com/testcontainers/testcontainers-go/wait"
|
||||||
)
|
)
|
||||||
|
|
||||||
var mysqlContainerConfigPath = "../testdata/mysql.cnf"
|
var (
|
||||||
|
mysqlContainerConfigPath = "../testdata/mysql.cnf"
|
||||||
|
mysqlContainer = (*mysql.MySQLContainer)(nil)
|
||||||
|
postgresContainer = (*postgres.PostgresContainer)(nil)
|
||||||
|
mysqlConnStr = ""
|
||||||
|
postgresConnStr = ""
|
||||||
|
)
|
||||||
|
|
||||||
|
func emptyCleanup() { return }
|
||||||
|
|
||||||
|
// CreateMysqlTestContainer creates a new MySQL container for testing.
|
||||||
func CreateMysqlTestContainer() (func(), error) {
|
func CreateMysqlTestContainer() (func(), error) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
if mysqlContainer != nil && mysqlContainer.IsRunning() {
|
||||||
|
refreshMysqlDatabase(ctx)
|
||||||
|
return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_MYSQL_DSN", mysqlConnStr)
|
||||||
|
}
|
||||||
|
|
||||||
_, caller, _, ok := runtime.Caller(0)
|
_, caller, _, ok := runtime.Caller(0)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("failed to get caller information")
|
return nil, fmt.Errorf("failed to get caller information")
|
||||||
@ -39,23 +52,25 @@ func CreateMysqlTestContainer() (func(), error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup := func() {
|
|
||||||
if err = container.Terminate(ctx); err != nil {
|
|
||||||
log.WithContext(ctx).Warnf("failed to terminate container: %s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
talksConn, err := container.ConnectionString(ctx)
|
talksConn, err := container.ConnectionString(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cleanup, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
mysqlContainer = container
|
||||||
|
mysqlConnStr = talksConn
|
||||||
|
|
||||||
return cleanup, os.Setenv("NETBIRD_STORE_ENGINE_MYSQL_DSN", talksConn)
|
return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_MYSQL_DSN", talksConn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreatePostgresTestContainer creates a new PostgreSQL container for testing.
|
||||||
func CreatePostgresTestContainer() (func(), error) {
|
func CreatePostgresTestContainer() (func(), error) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
if postgresContainer != nil && postgresContainer.IsRunning() {
|
||||||
|
refreshPostgresDatabase(ctx)
|
||||||
|
return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_POSTGRES_DSN", postgresConnStr)
|
||||||
|
}
|
||||||
|
|
||||||
container, err := postgres.RunContainer(ctx,
|
container, err := postgres.RunContainer(ctx,
|
||||||
testcontainers.WithImage("postgres:16-alpine"),
|
testcontainers.WithImage("postgres:16-alpine"),
|
||||||
postgres.WithDatabase("netbird"),
|
postgres.WithDatabase("netbird"),
|
||||||
@ -70,16 +85,22 @@ func CreatePostgresTestContainer() (func(), error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup := func() {
|
|
||||||
if err = container.Terminate(ctx); err != nil {
|
|
||||||
log.WithContext(ctx).Warnf("failed to terminate container: %s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
talksConn, err := container.ConnectionString(ctx)
|
talksConn, err := container.ConnectionString(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cleanup, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
postgresContainer = container
|
||||||
|
postgresConnStr = talksConn
|
||||||
|
|
||||||
return cleanup, os.Setenv("NETBIRD_STORE_ENGINE_POSTGRES_DSN", talksConn)
|
return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_POSTGRES_DSN", talksConn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func refreshMysqlDatabase(ctx context.Context) {
|
||||||
|
_, _, _ = mysqlContainer.Exec(ctx, []string{"mysqladmin", "--user=root", "drop", "netbird", "-f"})
|
||||||
|
_, _, _ = mysqlContainer.Exec(ctx, []string{"mysqladmin", "--user=root", "create", "netbird"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func refreshPostgresDatabase(ctx context.Context) {
|
||||||
|
_, _, _ = postgresContainer.Exec(ctx, []string{"dropdb", "-f", "netbird"})
|
||||||
|
_, _, _ = postgresContainer.Exec(ctx, []string{"createdb", "netbird"})
|
||||||
}
|
}
|
||||||
|
@ -84,7 +84,7 @@ type User struct {
|
|||||||
// Blocked indicates whether the user is blocked. Blocked users can't use the system.
|
// Blocked indicates whether the user is blocked. Blocked users can't use the system.
|
||||||
Blocked bool
|
Blocked bool
|
||||||
// LastLogin is the last time the user logged in to IdP
|
// LastLogin is the last time the user logged in to IdP
|
||||||
LastLogin time.Time
|
LastLogin time.Time `gorm:"type:TIMESTAMP;null;default:null"`
|
||||||
// CreatedAt records the time the user was created
|
// CreatedAt records the time the user was created
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user