更改字段类型

This commit is contained in:
xmdhs 2023-09-04 15:07:39 +08:00
parent 8603e31163
commit 48f05b4e95
No known key found for this signature in database
GPG Key ID: E809D6D43DEFCC95
7 changed files with 49 additions and 15 deletions

View File

@ -11,7 +11,7 @@ var (
// SkinsColumns holds the columns for the "skins" table. // SkinsColumns holds the columns for the "skins" table.
SkinsColumns = []*schema.Column{ SkinsColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt, Increment: true}, {Name: "id", Type: field.TypeInt, Increment: true},
{Name: "skin_hash", Type: field.TypeString}, {Name: "skin_hash", Type: field.TypeString, SchemaType: map[string]string{"mysql": "VARCHAR(100)"}},
{Name: "type", Type: field.TypeUint8}, {Name: "type", Type: field.TypeUint8},
{Name: "variant", Type: field.TypeString}, {Name: "variant", Type: field.TypeString},
{Name: "skin_created_user", Type: field.TypeInt}, {Name: "skin_created_user", Type: field.TypeInt},
@ -40,9 +40,9 @@ var (
// UsersColumns holds the columns for the "users" table. // UsersColumns holds the columns for the "users" table.
UsersColumns = []*schema.Column{ UsersColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt, Increment: true}, {Name: "id", Type: field.TypeInt, Increment: true},
{Name: "email", Type: field.TypeString, Unique: true}, {Name: "email", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"mysql": "VARCHAR(30)"}},
{Name: "password", Type: field.TypeString}, {Name: "password", Type: field.TypeString, SchemaType: map[string]string{"mysql": "VARCHAR(80)"}},
{Name: "salt", Type: field.TypeString}, {Name: "salt", Type: field.TypeString, SchemaType: map[string]string{"mysql": "VARCHAR(50)"}},
{Name: "state", Type: field.TypeInt}, {Name: "state", Type: field.TypeInt},
{Name: "reg_time", Type: field.TypeInt64}, {Name: "reg_time", Type: field.TypeInt64},
{Name: "user_token", Type: field.TypeInt, Nullable: true}, {Name: "user_token", Type: field.TypeInt, Nullable: true},
@ -78,8 +78,8 @@ var (
// UserProfilesColumns holds the columns for the "user_profiles" table. // UserProfilesColumns holds the columns for the "user_profiles" table.
UserProfilesColumns = []*schema.Column{ UserProfilesColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt, Increment: true}, {Name: "id", Type: field.TypeInt, Increment: true},
{Name: "name", Type: field.TypeString, Unique: true}, {Name: "name", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"mysql": "VARCHAR(20)"}},
{Name: "uuid", Type: field.TypeString}, {Name: "uuid", Type: field.TypeString, SchemaType: map[string]string{"mysql": "VARCHAR(32)"}},
{Name: "user_profile", Type: field.TypeInt, Unique: true}, {Name: "user_profile", Type: field.TypeInt, Unique: true},
} }
// UserProfilesTable holds the schema information for the "user_profiles" table. // UserProfilesTable holds the schema information for the "user_profiles" table.
@ -107,13 +107,20 @@ var (
UserTokensColumns = []*schema.Column{ UserTokensColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt, Increment: true}, {Name: "id", Type: field.TypeInt, Increment: true},
{Name: "token_id", Type: field.TypeUint64}, {Name: "token_id", Type: field.TypeUint64},
{Name: "uuid", Type: field.TypeString}, {Name: "uuid", Type: field.TypeString, SchemaType: map[string]string{"mysql": "VARCHAR(32)"}},
} }
// UserTokensTable holds the schema information for the "user_tokens" table. // UserTokensTable holds the schema information for the "user_tokens" table.
UserTokensTable = &schema.Table{ UserTokensTable = &schema.Table{
Name: "user_tokens", Name: "user_tokens",
Columns: UserTokensColumns, Columns: UserTokensColumns,
PrimaryKey: []*schema.Column{UserTokensColumns[0]}, PrimaryKey: []*schema.Column{UserTokensColumns[0]},
Indexes: []*schema.Index{
{
Name: "usertoken_uuid",
Unique: false,
Columns: []*schema.Column{UserTokensColumns[2]},
},
},
} }
// Tables holds all the tables in the schema. // Tables holds all the tables in the schema.
Tables = []*schema.Table{ Tables = []*schema.Table{

View File

@ -2,6 +2,7 @@ package schema
import ( import (
"entgo.io/ent" "entgo.io/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/schema/edge" "entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field" "entgo.io/ent/schema/field"
"entgo.io/ent/schema/index" "entgo.io/ent/schema/index"
@ -15,7 +16,9 @@ type Skin struct {
// Fields of the Skin. // Fields of the Skin.
func (Skin) Fields() []ent.Field { func (Skin) Fields() []ent.Field {
return []ent.Field{ return []ent.Field{
field.String("skin_hash"), field.String("skin_hash").SchemaType(map[string]string{
dialect.MySQL: "VARCHAR(100)",
}),
field.Uint8("type"), field.Uint8("type"),
field.String("variant"), field.String("variant"),
} }

View File

@ -2,6 +2,7 @@ package schema
import ( import (
"entgo.io/ent" "entgo.io/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/entsql" "entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema/edge" "entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field" "entgo.io/ent/schema/field"
@ -16,9 +17,15 @@ type User struct {
// Fields of the User. // Fields of the User.
func (User) Fields() []ent.Field { func (User) Fields() []ent.Field {
return []ent.Field{ return []ent.Field{
field.String("email").Unique(), field.String("email").Unique().SchemaType(map[string]string{
field.String("password"), dialect.MySQL: "VARCHAR(30)",
field.String("salt"), }),
field.String("password").SchemaType(map[string]string{
dialect.MySQL: "VARCHAR(80)",
}),
field.String("salt").SchemaType(map[string]string{
dialect.MySQL: "VARCHAR(50)",
}),
// 二进制状态位,保留 // 二进制状态位,保留
field.Int("state"), field.Int("state"),
field.Int64("reg_time"), field.Int64("reg_time"),

View File

@ -2,6 +2,7 @@ package schema
import ( import (
"entgo.io/ent" "entgo.io/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/schema/edge" "entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field" "entgo.io/ent/schema/field"
"entgo.io/ent/schema/index" "entgo.io/ent/schema/index"
@ -15,8 +16,12 @@ type UserProfile struct {
// Fields of the UserProfile. // Fields of the UserProfile.
func (UserProfile) Fields() []ent.Field { func (UserProfile) Fields() []ent.Field {
return []ent.Field{ return []ent.Field{
field.String("name").Unique(), field.String("name").Unique().SchemaType(map[string]string{
field.String("uuid"), dialect.MySQL: "VARCHAR(20)",
}),
field.String("uuid").SchemaType(map[string]string{
dialect.MySQL: "VARCHAR(32)",
}),
} }
} }

View File

@ -2,7 +2,9 @@ package schema
import ( import (
"entgo.io/ent" "entgo.io/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/schema/field" "entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
) )
// UserToken holds the schema definition for the UserToken entity. // UserToken holds the schema definition for the UserToken entity.
@ -15,7 +17,9 @@ func (UserToken) Fields() []ent.Field {
return []ent.Field{ return []ent.Field{
// 用于验证 jwt token 是否被注销,若相同则有效 // 用于验证 jwt token 是否被注销,若相同则有效
field.Uint64("token_id"), field.Uint64("token_id"),
field.String("uuid"), field.String("uuid").SchemaType(map[string]string{
dialect.MySQL: "VARCHAR(32)",
}),
} }
} }
@ -23,3 +27,9 @@ func (UserToken) Fields() []ent.Field {
func (UserToken) Edges() []ent.Edge { func (UserToken) Edges() []ent.Edge {
return nil return nil
} }
func (UserToken) Indexes() []ent.Index {
return []ent.Index{
index.Fields("uuid"),
}
}

View File

@ -16,5 +16,6 @@ type User struct {
type TokenClaims struct { type TokenClaims struct {
Tid string `json:"tid"` Tid string `json:"tid"`
CID string `json:"cid"`
jwt.RegisteredClaims jwt.RegisteredClaims
} }

View File

@ -56,7 +56,7 @@ func (y *Yggdrasil) Authenticate(cxt context.Context, auth yggdrasil.Authenticat
} }
} }
if utoken == nil { if utoken == nil {
ut, err := tx.UserToken.Create().SetTokenID(1).Save(cxt) ut, err := tx.UserToken.Create().SetTokenID(1).SetUUID(u.Edges.Profile.UUID).Save(cxt)
if err != nil { if err != nil {
return err return err
} }
@ -74,6 +74,7 @@ func (y *Yggdrasil) Authenticate(cxt context.Context, auth yggdrasil.Authenticat
claims := model.TokenClaims{ claims := model.TokenClaims{
Tid: strconv.FormatUint(utoken.TokenID, 10), Tid: strconv.FormatUint(utoken.TokenID, 10),
CID: clientToken,
RegisteredClaims: jwt.RegisteredClaims{ RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(15 * 24 * time.Hour)), ExpiresAt: jwt.NewNumericDate(time.Now().Add(15 * 24 * time.Hour)),
Issuer: "authlib-skin", Issuer: "authlib-skin",