|
| 1 | +// Copyright 2022 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package migrations |
| 6 | + |
| 7 | +import ( |
| 8 | + "code.gitea.io/gitea/modules/timeutil" |
| 9 | + |
| 10 | + "xorm.io/xorm" |
| 11 | + "xorm.io/xorm/schemas" |
| 12 | +) |
| 13 | + |
| 14 | +func increaseCredentialIDTo1640(x *xorm.Engine) error { |
| 15 | + // Create webauthnCredential table |
| 16 | + type webauthnCredential struct { |
| 17 | + ID int64 `xorm:"pk autoincr"` |
| 18 | + Name string |
| 19 | + LowerName string `xorm:"unique(s)"` |
| 20 | + UserID int64 `xorm:"INDEX unique(s)"` |
| 21 | + CredentialID string `xorm:"INDEX VARCHAR(1640)"` // CredentialID is at most 1023 bytes as per spec released 20 July 2022 -> 1640 base32 encoding |
| 22 | + PublicKey []byte |
| 23 | + AttestationType string |
| 24 | + AAGUID []byte |
| 25 | + SignCount uint32 `xorm:"BIGINT"` |
| 26 | + CloneWarning bool |
| 27 | + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` |
| 28 | + UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` |
| 29 | + } |
| 30 | + if err := x.Sync2(&webauthnCredential{}); err != nil { |
| 31 | + return err |
| 32 | + } |
| 33 | + |
| 34 | + switch x.Dialect().URI().DBType { |
| 35 | + case schemas.MYSQL: |
| 36 | + _, err := x.Exec("ALTER TABLE webauthn_credential MODIFY COLUMN credential_id VARCHAR(1640)") |
| 37 | + if err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + case schemas.ORACLE: |
| 41 | + _, err := x.Exec("ALTER TABLE webauthn_credential MODIFY credential_id VARCHAR(1640)") |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + case schemas.MSSQL: |
| 46 | + // This column has an index on it. I could write all of the code to attempt to change the index OR |
| 47 | + // I could just use recreate table. |
| 48 | + sess := x.NewSession() |
| 49 | + if err := sess.Begin(); err != nil { |
| 50 | + _ = sess.Close() |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + if err := recreateTable(sess, new(webauthnCredential)); err != nil { |
| 55 | + _ = sess.Close() |
| 56 | + return err |
| 57 | + } |
| 58 | + if err := sess.Commit(); err != nil { |
| 59 | + _ = sess.Close() |
| 60 | + return err |
| 61 | + } |
| 62 | + if err := sess.Close(); err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + case schemas.POSTGRES: |
| 66 | + _, err := x.Exec("ALTER TABLE webauthn_credential ALTER COLUMN credential_id TYPE VARCHAR(1640)") |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + default: |
| 71 | + // SQLite doesn't support ALTER COLUMN, and it already makes String _TEXT_ by default so no migration needed |
| 72 | + // nor is there any need to re-migrate |
| 73 | + } |
| 74 | + return nil |
| 75 | +} |
0 commit comments