Skip to content

Commit

Permalink
Merge pull request #922 from Permify/next
Browse files Browse the repository at this point in the history
refactor(datawriter): enhance modularity and readability of RunBundle…
  • Loading branch information
tolgaOzen committed Dec 5, 2023
2 parents 39bc67f + 5597b7c commit 636f33f
Show file tree
Hide file tree
Showing 10 changed files with 566 additions and 519 deletions.
2 changes: 1 addition & 1 deletion docs/apidocs.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"info": {
"title": "Permify API",
"description": "Permify is an open source authorization service for creating fine-grained and scalable authorization systems.",
"version": "v0.6.1",
"version": "v0.6.2",
"contact": {
"name": "API Support",
"url": "https://github.com/Permify/permify/issues",
Expand Down
2 changes: 1 addition & 1 deletion internal/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var Identifier = xid.New().String()
*/
const (
// Version is the last release of the Permify (e.g. v0.1.0)
Version = "v0.6.1"
Version = "v0.6.2"

// Banner is the view for terminal.
Banner = `
Expand Down
44 changes: 25 additions & 19 deletions internal/storage/postgres/dataReader.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ func (r *DataReader) QueryRelationships(ctx context.Context, tenantID string, fi
return nil, utils.HandleError(span, err, base.ErrorCode_ERROR_CODE_INTERNAL)
}

// Rollback the transaction in case of any error.
defer utils.Rollback(tx)
defer func() {
_ = tx.Rollback()
}()

// Build the relationships query based on the provided filter and snapshot value.
var args []interface{}
Expand Down Expand Up @@ -107,7 +108,7 @@ func (r *DataReader) QueryRelationships(ctx context.Context, tenantID string, fi
return nil, utils.HandleError(span, err, base.ErrorCode_ERROR_CODE_EXECUTION)
}

slog.Info("Successfully retrieved relationship tuples from the database.")
slog.Info("Successfully retrieved relation tuples from the database.")

// Return a TupleIterator created from the TupleCollection.
return collection.CreateTupleIterator(), nil
Expand Down Expand Up @@ -135,8 +136,9 @@ func (r *DataReader) ReadRelationships(ctx context.Context, tenantID string, fil
return nil, nil, utils.HandleError(span, err, base.ErrorCode_ERROR_CODE_INTERNAL)
}

// Rollback the transaction in case of any error.
defer utils.Rollback(tx)
defer func() {
_ = tx.Rollback()
}()

// Build the relationships query based on the provided filter, snapshot value, and pagination settings.
builder := r.database.Builder.Select("id, entity_type, entity_id, relation, subject_type, subject_id, subject_relation").From(RelationTuplesTable).Where(squirrel.Eq{"tenant_id": tenantID})
Expand Down Expand Up @@ -202,7 +204,7 @@ func (r *DataReader) ReadRelationships(ctx context.Context, tenantID string, fil
return nil, nil, utils.HandleError(span, err, base.ErrorCode_ERROR_CODE_EXECUTION)
}

slog.Info("Successfully read relationships from database.")
slog.Info("Successfully read relation tuples from database.")

// Return the results and encoded continuous token for pagination.
if len(tuples) > int(pagination.PageSize()) {
Expand Down Expand Up @@ -233,8 +235,9 @@ func (r *DataReader) QuerySingleAttribute(ctx context.Context, tenantID string,
return nil, utils.HandleError(span, err, base.ErrorCode_ERROR_CODE_INTERNAL)
}

// Rollback the transaction in case of any error.
defer utils.Rollback(tx)
defer func() {
_ = tx.Rollback()
}()

// Build the relationships query based on the provided filter and snapshot value.
var args []interface{}
Expand Down Expand Up @@ -309,8 +312,9 @@ func (r *DataReader) QueryAttributes(ctx context.Context, tenantID string, filte
return nil, utils.HandleError(span, err, base.ErrorCode_ERROR_CODE_INTERNAL)
}

// Rollback the transaction in case of any error.
defer utils.Rollback(tx)
defer func() {
_ = tx.Rollback()
}()

// Build the relationships query based on the provided filter and snapshot value.
var args []interface{}
Expand Down Expand Up @@ -397,8 +401,9 @@ func (r *DataReader) ReadAttributes(ctx context.Context, tenantID string, filter
return nil, nil, utils.HandleError(span, err, base.ErrorCode_ERROR_CODE_INTERNAL)
}

// Rollback the transaction in case of any error.
defer utils.Rollback(tx)
defer func() {
_ = tx.Rollback()
}()

// Build the relationships query based on the provided filter, snapshot value, and pagination settings.
builder := r.database.Builder.Select("id, entity_type, entity_id, attribute, value").From(AttributesTable).Where(squirrel.Eq{"tenant_id": tenantID})
Expand Down Expand Up @@ -508,8 +513,9 @@ func (r *DataReader) QueryUniqueEntities(ctx context.Context, tenantID, name, sn
return nil, nil, utils.HandleError(span, err, base.ErrorCode_ERROR_CODE_INTERNAL)
}

// Rollback the transaction in case of any error.
defer utils.Rollback(tx)
defer func() {
_ = tx.Rollback()
}()

query := utils.BulkEntityFilterQuery(tenantID, name, st.(snapshot.Token).Value.Uint)

Expand Down Expand Up @@ -595,8 +601,9 @@ func (r *DataReader) QueryUniqueSubjectReferences(ctx context.Context, tenantID
return nil, nil, utils.HandleError(span, err, base.ErrorCode_ERROR_CODE_INTERNAL)
}

// Rollback the transaction in case of any error.
defer utils.Rollback(tx)
defer func() {
_ = tx.Rollback()
}()

// Build the relationships query based on the provided filter, snapshot value, and pagination settings.
builder := r.database.Builder.
Expand Down Expand Up @@ -693,14 +700,13 @@ func (r *DataReader) HeadSnapshot(ctx context.Context, tenantID string) (token.S
}

// Execute the query and retrieve the highest transaction ID.
row := r.database.DB.QueryRowContext(ctx, query, args...)
err = row.Scan(&xid)
err = r.database.DB.QueryRowContext(ctx, query, args...).Scan(&xid)
if err != nil {
// If no rows are found, return a snapshot token with a value of 0.
if errors.Is(err, sql.ErrNoRows) {
return snapshot.Token{Value: types.XID8{Uint: 0}}, nil
}
return nil, utils.HandleError(span, err, base.ErrorCode_ERROR_CODE_EXECUTION)
return nil, utils.HandleError(span, err, base.ErrorCode_ERROR_CODE_SCAN)
}

slog.Info("Successfully retrieved latest snapshot token")
Expand Down

0 comments on commit 636f33f

Please sign in to comment.