github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/schema/repository.go (about)

     1  package schema
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/pkg/resource"
     7  
     8  	"github.com/kyma-incubator/compass/components/director/internal/repo"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  const (
    13  	schemaVersionColumn = "version"
    14  	schemaDirtyColumn   = "dirty"
    15  	tableName           = `"public"."schema_migrations"`
    16  )
    17  
    18  // PgRepository represents a repository for schema migration operations
    19  type PgRepository struct {
    20  	singleGetter repo.SingleGetterGlobal
    21  }
    22  
    23  // NewRepository creates a new instance of PgRepository
    24  func NewRepository() *PgRepository {
    25  	return &PgRepository{
    26  		singleGetter: repo.NewSingleGetterGlobal(resource.Schema, tableName, []string{schemaVersionColumn, schemaDirtyColumn}),
    27  	}
    28  }
    29  
    30  type schemaVersion struct {
    31  	Version string `db:"version"`
    32  	Dirty   bool   `db:"dirty"`
    33  }
    34  
    35  // GetVersion returns the current schema version
    36  func (r *PgRepository) GetVersion(ctx context.Context) (string, bool, error) {
    37  	var version schemaVersion
    38  	err := r.singleGetter.GetGlobal(ctx, repo.Conditions{}, repo.NoOrderBy, &version)
    39  	if err != nil {
    40  		return "", false, errors.Wrap(err, "while getting schema version")
    41  	}
    42  
    43  	return version.Version, version.Dirty, nil
    44  }