• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Permify / permify / 6428700044

06 Oct 2023 07:20AM UTC coverage: 73.003%. Remained the same
6428700044

Pull #741

github

tolgaOzen
feat: implement GitHub Action for docs deployment
Pull Request #741: feat: implement GitHub Action for docs deployment

4605 of 6308 relevant lines covered (73.0%)

70.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.26
/internal/storage/postgres/utils/common.go
1
package utils
2

3
import (
4
        "database/sql"
5
        "fmt"
6

7
        "github.com/pkg/errors"
8

9
        "github.com/Masterminds/squirrel"
10

11
        "github.com/Permify/permify/pkg/logger"
12
)
13

14
const (
15
        BulkEntityFilterTemplate = `
16
    WITH entities AS (
17
        (SELECT id, entity_id, entity_type, tenant_id, created_tx_id, expired_tx_id FROM relation_tuples)
18
        UNION ALL
19
        (SELECT id, entity_id, entity_type, tenant_id, created_tx_id, expired_tx_id FROM attributes)
20
    ), filtered_entities AS (
21
        SELECT DISTINCT ON (entity_id) id, entity_id
22
        FROM entities
23
        WHERE tenant_id = '%s'
24
        AND entity_type = '%s'
25
        AND %s
26
        AND %s
27
    )
28
    SELECT id, entity_id
29
    FROM filtered_entities`
30
)
31

32
// SnapshotQuery adds conditions to a SELECT query for checking transaction visibility based on created and expired transaction IDs.
33
// The query checks if transactions are visible in a snapshot associated with the provided value.
34
func SnapshotQuery(sl squirrel.SelectBuilder, value uint64) squirrel.SelectBuilder {
1✔
35
        // Convert the value to a string once to reduce redundant calls to fmt.Sprintf.
1✔
36
        valStr := fmt.Sprintf("'%v'::xid8", value)
1✔
37

1✔
38
        // Create a subquery for the snapshot associated with the provided value.
1✔
39
        snapshotQuery := fmt.Sprintf("(select snapshot from transactions where id = %s)", valStr)
1✔
40

1✔
41
        // Create an expression to check if a transaction with a specific created_tx_id is visible in the snapshot.
1✔
42
        visibilityExpr := squirrel.Expr(fmt.Sprintf("pg_visible_in_snapshot(created_tx_id, %s) = true", snapshotQuery))
1✔
43
        // Create an expression to check if the created_tx_id is equal to the provided value.
1✔
44
        createdExpr := squirrel.Expr(fmt.Sprintf("created_tx_id = %s", valStr))
1✔
45
        // Use OR condition for the created expressions.
1✔
46
        createdWhere := squirrel.Or{visibilityExpr, createdExpr}
1✔
47

1✔
48
        // Create an expression to check if a transaction with a specific expired_tx_id is not visible in the snapshot.
1✔
49
        expiredVisibilityExpr := squirrel.Expr(fmt.Sprintf("pg_visible_in_snapshot(expired_tx_id, %s) = false", snapshotQuery))
1✔
50
        // Create an expression to check if the expired_tx_id is equal to zero.
1✔
51
        expiredZeroExpr := squirrel.Expr("expired_tx_id = '0'::xid8")
1✔
52
        // Create an expression to check if the expired_tx_id is not equal to the provided value.
1✔
53
        expiredNotExpr := squirrel.Expr(fmt.Sprintf("expired_tx_id <> %s", valStr))
1✔
54
        // Use AND condition for the expired expressions, checking both visibility and non-equality with value.
1✔
55
        expiredWhere := squirrel.And{squirrel.Or{expiredVisibilityExpr, expiredZeroExpr}, expiredNotExpr}
1✔
56

1✔
57
        // Add the created and expired conditions to the SELECT query.
1✔
58
        return sl.Where(createdWhere).Where(expiredWhere)
1✔
59
}
1✔
60

61
// snapshotQuery function generates two strings representing conditions to be applied in a SQL query to filter data based on visibility of transactions.
62
func snapshotQuery(value uint64) (string, string) {
×
63
        // Convert the provided value into a string format suitable for our SQL query, formatted as a transaction ID.
×
64
        valStr := fmt.Sprintf("'%v'::xid8", value)
×
65

×
66
        // Create a subquery that fetches the snapshot associated with the transaction ID.
×
67
        snapshotQ := fmt.Sprintf("(SELECT snapshot FROM transactions WHERE id = %s)", valStr)
×
68

×
69
        // Create an expression that checks whether a transaction (represented by 'created_tx_id') is visible in the snapshot.
×
70
        visibilityExpr := fmt.Sprintf("pg_visible_in_snapshot(created_tx_id, %s) = true", snapshotQ)
×
71
        // Create an expression that checks if the 'created_tx_id' is the same as our transaction ID.
×
72
        createdExpr := fmt.Sprintf("created_tx_id = %s", valStr)
×
73
        // Combine these expressions to form a condition. A row will satisfy this condition if either of the expressions are true.
×
74
        createdWhere := fmt.Sprintf("(%s OR %s)", visibilityExpr, createdExpr)
×
75

×
76
        // Create an expression that checks whether a transaction (represented by 'expired_tx_id') is not visible in the snapshot.
×
77
        expiredVisibilityExpr := fmt.Sprintf("pg_visible_in_snapshot(expired_tx_id, %s) = false", snapshotQ)
×
78
        // Create an expression that checks if the 'expired_tx_id' is zero. This handles cases where the transaction hasn't expired.
×
79
        expiredZeroExpr := "expired_tx_id = '0'::xid8"
×
80
        // Create an expression that checks if the 'expired_tx_id' is not the same as our transaction ID.
×
81
        expiredNotExpr := fmt.Sprintf("expired_tx_id <> %s", valStr)
×
82
        // Combine these expressions to form a condition. A row will satisfy this condition if the first set of expressions are true (either the transaction hasn't expired, or if it has, it's not visible in the snapshot) and the second expression is also true (the 'expired_tx_id' is not the same as our transaction ID).
×
83
        expiredWhere := fmt.Sprintf("(%s AND %s)", fmt.Sprintf("(%s OR %s)", expiredVisibilityExpr, expiredZeroExpr), expiredNotExpr)
×
84

×
85
        // Return the conditions for both 'created' and 'expired' transactions. These can be used in a WHERE clause of a SQL query to filter results.
×
86
        return createdWhere, expiredWhere
×
87
}
×
88

89
// BulkEntityFilterQuery -
90
func BulkEntityFilterQuery(tenantID, entityType string, snap uint64) string {
×
91
        createdWhere, expiredWhere := snapshotQuery(snap)
×
92
        return fmt.Sprintf(BulkEntityFilterTemplate, tenantID, entityType, createdWhere, expiredWhere)
×
93
}
×
94

95
// GenerateGCQuery generates a Squirrel DELETE query builder for garbage collection.
96
// It constructs a query to delete expired records from the specified table
97
// based on the provided value, which represents a transaction ID.
98
func GenerateGCQuery(table string, value uint64) squirrel.DeleteBuilder {
1✔
99
        // Convert the provided value into a string format suitable for our SQL query, formatted as a transaction ID.
1✔
100
        valStr := fmt.Sprintf("'%v'::xid8", value)
1✔
101

1✔
102
        // Create a Squirrel DELETE builder for the specified table.
1✔
103
        deleteBuilder := squirrel.Delete(table)
1✔
104

1✔
105
        // Create an expression to check if 'expired_tx_id' is not equal to '0' (not expired).
1✔
106
        expiredZeroExpr := squirrel.Expr("expired_tx_id <> '0'::xid8")
1✔
107

1✔
108
        // Create an expression to check if 'expired_tx_id' is less than the provided value (before the cutoff).
1✔
109
        beforeExpr := squirrel.Expr(fmt.Sprintf("expired_tx_id < %s", valStr))
1✔
110

1✔
111
        // Add the WHERE clauses to the DELETE query builder to filter and delete expired data.
1✔
112
        return deleteBuilder.Where(expiredZeroExpr).Where(beforeExpr)
1✔
113
}
1✔
114

115
// Rollback - Rollbacks a transaction and logs the error
116
func Rollback(tx *sql.Tx, logger logger.Interface) {
×
117
        if err := tx.Rollback(); !errors.Is(err, sql.ErrTxDone) && err != nil {
×
118
                logger.Error("failed to rollback transaction", err)
×
119
        }
×
120
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc