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