risingwave_frontend/catalog/system_catalog/rw_catalog/
rw_subscriptions.rs

1// Copyright 2024 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use risingwave_common::id::{SchemaId, SubscriptionId, UserId};
16use risingwave_common::types::{Fields, Timestamptz};
17use risingwave_frontend_macro::system_catalog;
18
19use crate::catalog::system_catalog::{SysCatalogReaderImpl, get_acl_items};
20use crate::error::Result;
21
22#[derive(Fields)]
23struct RwSubscription {
24    #[primary_key]
25    id: SubscriptionId,
26    name: String,
27    schema_id: SchemaId,
28    schema_name: String,
29    owner: UserId,
30    definition: String,
31    retention_seconds: i64,
32    acl: Vec<String>,
33    initialized_at: Option<Timestamptz>,
34    created_at: Option<Timestamptz>,
35    initialized_at_cluster_version: Option<String>,
36    created_at_cluster_version: Option<String>,
37}
38
39#[system_catalog(table, "rw_catalog.rw_subscriptions")]
40fn read_rw_subscriptions_info(reader: &SysCatalogReaderImpl) -> Result<Vec<RwSubscription>> {
41    let catalog_reader = reader.catalog_reader.read_guard();
42    let schemas = catalog_reader.iter_schemas(&reader.auth_context.database)?;
43    let user_reader = reader.user_info_reader.read_guard();
44    let current_user = user_reader
45        .get_user_by_name(&reader.auth_context.user_name)
46        .expect("user not found");
47    let users = user_reader.get_all_users();
48    let username_map = user_reader.get_user_name_map();
49
50    Ok(schemas
51        .flat_map(|schema| {
52            schema
53                .iter_subscription_with_acl(current_user)
54                .map(|subscription| RwSubscription {
55                    id: subscription.id,
56                    name: subscription.name.clone(),
57                    schema_id: schema.id(),
58                    schema_name: schema.name(),
59                    owner: subscription.owner,
60                    definition: subscription.definition.clone(),
61                    retention_seconds: subscription.retention_seconds as i64,
62                    acl: get_acl_items(subscription.id, false, &users, username_map),
63                    initialized_at: subscription
64                        .initialized_at_epoch
65                        .map(|e| e.as_timestamptz()),
66                    created_at: subscription.created_at_epoch.map(|e| e.as_timestamptz()),
67                    initialized_at_cluster_version: subscription
68                        .initialized_at_cluster_version
69                        .clone(),
70                    created_at_cluster_version: subscription.created_at_cluster_version.clone(),
71                })
72        })
73        .collect())
74}