gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package v1_17
  4. import (
  5. "context"
  6. "errors"
  7. "fmt"
  8. "code.gitea.io/gitea/models/migrations/base"
  9. "code.gitea.io/gitea/modules/timeutil"
  10. "xorm.io/xorm"
  11. )
  12. func DropOldCredentialIDColumn(x *xorm.Engine) error {
  13. // This migration maybe rerun so that we should check if it has been run
  14. credentialIDExist, err := x.Dialect().IsColumnExist(x.DB(), context.Background(), "webauthn_credential", "credential_id")
  15. if err != nil {
  16. return err
  17. }
  18. if !credentialIDExist {
  19. // Column is already non-extant
  20. return nil
  21. }
  22. credentialIDBytesExists, err := x.Dialect().IsColumnExist(x.DB(), context.Background(), "webauthn_credential", "credential_id_bytes")
  23. if err != nil {
  24. return err
  25. }
  26. if !credentialIDBytesExists {
  27. // looks like 221 hasn't properly run
  28. return errors.New("webauthn_credential does not have a credential_id_bytes column... it is not safe to run this migration")
  29. }
  30. // Create webauthnCredential table
  31. type webauthnCredential struct {
  32. ID int64 `xorm:"pk autoincr"`
  33. Name string
  34. LowerName string `xorm:"unique(s)"`
  35. UserID int64 `xorm:"INDEX unique(s)"`
  36. CredentialID string `xorm:"INDEX VARCHAR(410)"`
  37. // Note the lack of the INDEX on CredentialIDBytes - we will add this in v223.go
  38. CredentialIDBytes []byte `xorm:"VARBINARY(1024)"` // CredentialID is at most 1023 bytes as per spec released 20 July 2022
  39. PublicKey []byte
  40. AttestationType string
  41. AAGUID []byte
  42. SignCount uint32 `xorm:"BIGINT"`
  43. CloneWarning bool
  44. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  45. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  46. }
  47. if err := x.Sync(&webauthnCredential{}); err != nil {
  48. return err
  49. }
  50. // Drop the old credential ID
  51. sess := x.NewSession()
  52. defer sess.Close()
  53. if err := base.DropTableColumns(sess, "webauthn_credential", "credential_id"); err != nil {
  54. return fmt.Errorf("unable to drop old credentialID column: %w", err)
  55. }
  56. return sess.Commit()
  57. }