code.gitea.io/gitea@v1.21.7/models/migrations/v1_17/v222.go (about)

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