github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/datastore/spanner/revisions.go (about)

     1  package spanner
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	"cloud.google.com/go/spanner"
     9  
    10  	"github.com/authzed/spicedb/internal/datastore/revisions"
    11  	"github.com/authzed/spicedb/pkg/datastore"
    12  )
    13  
    14  var ParseRevisionString = revisions.RevisionParser(revisions.Timestamp)
    15  
    16  func (sd *spannerDatastore) headRevisionInternal(ctx context.Context) (datastore.Revision, error) {
    17  	now, err := sd.now(ctx)
    18  	if err != nil {
    19  		return datastore.NoRevision, fmt.Errorf(errRevision, err)
    20  	}
    21  
    22  	return revisions.NewForTime(now), nil
    23  }
    24  
    25  func (sd *spannerDatastore) HeadRevision(ctx context.Context) (datastore.Revision, error) {
    26  	return sd.headRevisionInternal(ctx)
    27  }
    28  
    29  func (sd *spannerDatastore) now(ctx context.Context) (time.Time, error) {
    30  	var timestamp time.Time
    31  	if err := sd.client.Single().Query(ctx, spanner.NewStatement("SELECT CURRENT_TIMESTAMP()")).Do(func(r *spanner.Row) error {
    32  		return r.Columns(&timestamp)
    33  	}); err != nil {
    34  		return time.Time{}, err
    35  	}
    36  
    37  	return timestamp, nil
    38  }