feat(07-02): add user session schema, throttle table, and jwt config

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Jakub Zych
2026-09-22 13:53:16 +02:00
parent 79e8373560
commit 1311bd6cad
8 changed files with 235 additions and 11 deletions

View File

@@ -0,0 +1,67 @@
package updates
import (
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)
var extendUsersMigrations = []*gormigrate.Migration{
{
ID: "202609220005_extend_users",
Migrate: func(tx *gorm.DB) error {
return execStmts(tx, []string{
`ALTER TABLE users ADD COLUMN name TEXT`,
`ALTER TABLE users ADD COLUMN surname TEXT`,
`ALTER TABLE users ADD COLUMN is_activated BOOLEAN NOT NULL DEFAULT FALSE`,
`ALTER TABLE users ADD COLUMN activated_at TIMESTAMPTZ`,
`ALTER TABLE users ADD COLUMN activation_code TEXT`,
`ALTER TABLE users ADD COLUMN activation_code_issued_at TIMESTAMPTZ`,
`ALTER TABLE users ADD COLUMN reset_password_code TEXT`,
`ALTER TABLE users ADD COLUMN reset_password_code_issued_at TIMESTAMPTZ`,
`ALTER TABLE users ADD COLUMN has_self_set_password BOOLEAN NOT NULL DEFAULT TRUE`,
`ALTER TABLE users ADD COLUMN marketing_consent BOOLEAN NOT NULL DEFAULT FALSE`,
`ALTER TABLE users ADD COLUMN is_onboarded BOOLEAN NOT NULL DEFAULT FALSE`,
`ALTER TABLE users ADD COLUMN organisation_id INTEGER REFERENCES golem15_user_organisations(id)`,
`ALTER TABLE users ADD COLUMN organisation_role TEXT`,
`ALTER TABLE users ADD COLUMN preferred_locale TEXT`,
`ALTER TABLE users ADD COLUMN tokens_valid_after TIMESTAMPTZ`,
`ALTER TABLE users ADD COLUMN created_ip_address TEXT`,
`ALTER TABLE users ADD COLUMN last_ip_address TEXT`,
})
},
Rollback: func(tx *gorm.DB) error {
return execStmts(tx, []string{
`ALTER TABLE users DROP COLUMN IF EXISTS last_ip_address`,
`ALTER TABLE users DROP COLUMN IF EXISTS created_ip_address`,
`ALTER TABLE users DROP COLUMN IF EXISTS tokens_valid_after`,
`ALTER TABLE users DROP COLUMN IF EXISTS preferred_locale`,
`ALTER TABLE users DROP COLUMN IF EXISTS organisation_role`,
`ALTER TABLE users DROP COLUMN IF EXISTS organisation_id`,
`ALTER TABLE users DROP COLUMN IF EXISTS is_onboarded`,
`ALTER TABLE users DROP COLUMN IF EXISTS marketing_consent`,
`ALTER TABLE users DROP COLUMN IF EXISTS has_self_set_password`,
`ALTER TABLE users DROP COLUMN IF EXISTS reset_password_code_issued_at`,
`ALTER TABLE users DROP COLUMN IF EXISTS reset_password_code`,
`ALTER TABLE users DROP COLUMN IF EXISTS activation_code_issued_at`,
`ALTER TABLE users DROP COLUMN IF EXISTS activation_code`,
`ALTER TABLE users DROP COLUMN IF EXISTS activated_at`,
`ALTER TABLE users DROP COLUMN IF EXISTS is_activated`,
`ALTER TABLE users DROP COLUMN IF EXISTS surname`,
`ALTER TABLE users DROP COLUMN IF EXISTS name`,
})
},
},
}
func execStmts(tx *gorm.DB, stmts []string) error {
for _, stmt := range stmts {
if err := tx.Exec(stmt).Error; err != nil {
return err
}
}
return nil
}
func init() {
Register(extendUsersMigrations...)
}

View File

@@ -0,0 +1,35 @@
package updates
import (
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)
var userThrottleMigrations = []*gormigrate.Migration{
{
ID: "202609220006_create_user_throttle",
Migrate: func(tx *gorm.DB) error {
return execStmts(tx, []string{
`CREATE TABLE user_throttle (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id),
ip_address TEXT,
attempts INTEGER NOT NULL DEFAULT 0,
is_suspended BOOLEAN NOT NULL DEFAULT FALSE,
suspended_at TIMESTAMPTZ,
is_banned BOOLEAN NOT NULL DEFAULT FALSE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
)`,
`CREATE INDEX user_throttle_user_ip ON user_throttle (user_id, ip_address)`,
})
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec(`DROP TABLE IF EXISTS user_throttle`).Error
},
},
}
func init() {
Register(userThrottleMigrations...)
}

View File

@@ -0,0 +1,26 @@
package updates
import (
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)
var jwtBlacklistMigrations = []*gormigrate.Migration{
{
ID: "202609220007_create_jwt_blacklist",
Migrate: func(tx *gorm.DB) error {
return tx.Exec(`CREATE TABLE jwt_blacklist (
jti TEXT PRIMARY KEY,
expires_at TIMESTAMPTZ NOT NULL,
valid_until TIMESTAMPTZ NOT NULL
)`).Error
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec(`DROP TABLE IF EXISTS jwt_blacklist`).Error
},
},
}
func init() {
Register(jwtBlacklistMigrations...)
}

View File

@@ -38,12 +38,27 @@ func TestCreateOrganisationsMigration(t *testing.T) {
if err := gdb.Raw(`SELECT id FROM summer_migrations_golem15_user ORDER BY id`).Scan(&ids).Error; err != nil {
t.Fatal(err)
}
if len(ids) != 2 || ids[1] != "202609180002_create_organisations" {
want := []string{
"202609170001_create_users",
"202609180002_create_organisations",
"202609220005_extend_users",
"202609220006_create_user_throttle",
"202609220007_create_jwt_blacklist",
}
if len(ids) != len(want) {
t.Fatalf("history = %v", ids)
}
for i := range want {
if ids[i] != want[i] {
t.Fatalf("history = %v", ids)
}
}
if err := m.RollbackLast(); err != nil {
t.Fatal(err)
// jwt blacklist, throttle, extend-users, then organisations.
for range 4 {
if err := m.RollbackLast(); err != nil {
t.Fatal(err)
}
}
if gdb.Migrator().HasTable("golem15_user_organisations") {
t.Fatal("rollback must drop golem15_user_organisations")