github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/sqlstore/license_store.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package sqlstore
     5  
     6  import (
     7  	sq "github.com/Masterminds/squirrel"
     8  	"github.com/pkg/errors"
     9  
    10  	"github.com/mattermost/mattermost-server/v5/model"
    11  	"github.com/mattermost/mattermost-server/v5/store"
    12  )
    13  
    14  // SqlLicenseStore encapsulates the database writes and reads for
    15  // model.LicenseRecord objects.
    16  type SqlLicenseStore struct {
    17  	*SqlStore
    18  }
    19  
    20  func newSqlLicenseStore(sqlStore *SqlStore) store.LicenseStore {
    21  	ls := &SqlLicenseStore{sqlStore}
    22  
    23  	for _, db := range sqlStore.GetAllConns() {
    24  		table := db.AddTableWithName(model.LicenseRecord{}, "Licenses").SetKeys(false, "Id")
    25  		table.ColMap("Id").SetMaxSize(26)
    26  		table.ColMap("Bytes").SetMaxSize(10000)
    27  	}
    28  
    29  	return ls
    30  }
    31  
    32  func (ls SqlLicenseStore) createIndexesIfNotExists() {
    33  }
    34  
    35  // Save validates and stores the license instance in the database. The Id
    36  // and Bytes fields are mandatory. The Bytes field is limited to a maximum
    37  // of 10000 bytes. If the license ID matches an existing license in the
    38  // database it returns the license stored in the database. If not, it saves the
    39  // new database and returns the created license with the CreateAt field
    40  // updated.
    41  func (ls SqlLicenseStore) Save(license *model.LicenseRecord) (*model.LicenseRecord, error) {
    42  	license.PreSave()
    43  	if err := license.IsValid(); err != nil {
    44  		return nil, err
    45  	}
    46  	query := ls.getQueryBuilder().
    47  		Select("*").
    48  		From("Licenses").
    49  		Where(sq.Eq{"Id": license.Id})
    50  	queryString, args, err := query.ToSql()
    51  	if err != nil {
    52  		return nil, errors.Wrap(err, "license_tosql")
    53  	}
    54  	var storedLicense model.LicenseRecord
    55  	if err := ls.GetReplica().SelectOne(&storedLicense, queryString, args...); err != nil {
    56  		// Only insert if not exists
    57  		if err := ls.GetMaster().Insert(license); err != nil {
    58  			return nil, errors.Wrapf(err, "failed to get License with licenseId=%s", license.Id)
    59  		}
    60  		return license, nil
    61  	}
    62  	return &storedLicense, nil
    63  }
    64  
    65  // Get obtains the license with the provided id parameter from the database.
    66  // If the license doesn't exist it returns a model.AppError with
    67  // http.StatusNotFound in the StatusCode field.
    68  func (ls SqlLicenseStore) Get(id string) (*model.LicenseRecord, error) {
    69  	obj, err := ls.GetReplica().Get(model.LicenseRecord{}, id)
    70  	if err != nil {
    71  		return nil, errors.Wrapf(err, "failed to get License with licenseId=%s", id)
    72  	}
    73  	if obj == nil {
    74  		return nil, store.NewErrNotFound("License", id)
    75  	}
    76  	return obj.(*model.LicenseRecord), nil
    77  }