MCPcopy Create free account
hub / github.com/Permify/permify / ListSchemas

Method ListSchemas

internal/storage/postgres/schema_reader.go:221–281  ·  view source on GitHub ↗

ListSchemas - List all Schemas

(ctx context.Context, tenantID string, pagination database.Pagination)

Source from the content-addressed store, hash-verified

219
220// ListSchemas - List all Schemas
221func (r *SchemaReader) ListSchemas(ctx context.Context, tenantID string, pagination database.Pagination) (schemas []*base.SchemaList, ct database.EncodedContinuousToken, err error) {
222 ctx, span := internal.Tracer.Start(ctx, "tenant-reader.list-tenants")
223 defer span.End()
224
225 slog.DebugContext(ctx, "listing schemas with pagination", slog.Any("pagination", pagination))
226
227 builder := r.database.Builder.Select("DISTINCT version").From(SchemaDefinitionTable).Where(squirrel.Eq{"tenant_id": tenantID})
228 if pagination.Token() != "" {
229 var t database.ContinuousToken
230 t, err = utils.EncodedContinuousToken{Value: pagination.Token()}.Decode()
231 if err != nil {
232 return nil, nil, utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_INVALID_CONTINUOUS_TOKEN)
233 }
234 builder = builder.Where(squirrel.LtOrEq{"version": t.(utils.ContinuousToken).Value})
235 }
236
237 builder = builder.OrderBy("version DESC").Limit(uint64(pagination.PageSize() + 1))
238
239 var query string
240 var args []interface{}
241
242 query, args, err = builder.ToSql()
243 if err != nil {
244 return nil, nil, utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_SQL_BUILDER)
245 }
246
247 slog.DebugContext(ctx, "executing sql query", slog.Any("query", query), slog.Any("arguments", args))
248 var rows pgx.Rows
249 rows, err = r.database.ReadPool.Query(ctx, query, args...)
250 if err != nil {
251 return nil, nil, utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_EXECUTION)
252 }
253 defer rows.Close()
254
255 var lastVersion string
256 schemas = make([]*base.SchemaList, 0, pagination.PageSize()+1)
257 for rows.Next() {
258 sch := &base.SchemaList{}
259 err = rows.Scan(&sch.Version)
260 if err != nil {
261 return nil, nil, utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_SCAN)
262 }
263 id, err := xid.FromString(sch.Version)
264 if err != nil {
265 return nil, nil, utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_SCAN)
266 }
267 sch.CreatedAt = id.Time().String()
268 lastVersion = sch.Version
269 schemas = append(schemas, sch)
270 }
271 if err = rows.Err(); err != nil {
272 return nil, nil, utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_INTERNAL)
273 }
274
275 slog.DebugContext(ctx, "successfully listed schemas", slog.Any("number_of_schemas", len(schemas)))
276
277 if len(schemas) > int(pagination.PageSize()) {
278 return schemas[:pagination.PageSize()], utils.NewContinuousToken(lastVersion).Encode(), nil

Callers

nothing calls this directly

Implementers 7

NoopSchemaReaderinternal/storage/storage.go
SchemaReaderinternal/storage/proxies/cache/schema_
SchemaReaderinternal/storage/proxies/circuitbreake
SchemaReaderinternal/storage/proxies/singleflight/
SchemaReaderinternal/storage/postgres/schema_reade
SchemaReaderinternal/storage/memory/schema_reader.
mockSchemaReaderinternal/engines/lookup_test.go

Calls 12

HandleErrorFunction · 0.92
NewContinuousTokenFunction · 0.92
NewNoopContinuousTokenFunction · 0.92
StartMethod · 0.80
TokenMethod · 0.80
PageSizeMethod · 0.80
DecodeMethod · 0.65
CloseMethod · 0.65
StringMethod · 0.65
EncodeMethod · 0.65
LimitMethod · 0.45
ScanMethod · 0.45

Tested by

no test coverage detected