Skip to main content

risingwave_meta_model/
source.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_pb::catalog::PbSource;
16use sea_orm::ActiveValue::Set;
17use sea_orm::entity::prelude::*;
18use serde::{Deserialize, Serialize};
19
20use crate::{
21    ColumnCatalogArray, ConnectionId, I32Array, Property, SecretRef, SourceId, SourceIngestMode,
22    StreamSourceInfo, TableId, WatermarkDescArray,
23};
24
25#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
26#[sea_orm(table_name = "source")]
27pub struct Model {
28    #[sea_orm(primary_key, auto_increment = false)]
29    pub source_id: SourceId,
30    pub name: String,
31    pub row_id_index: Option<i32>,
32    pub columns: ColumnCatalogArray,
33    pub pk_column_ids: I32Array,
34    pub with_properties: Property,
35    pub definition: String,
36    pub source_info: Option<StreamSourceInfo>,
37    pub watermark_descs: WatermarkDescArray,
38    pub optional_associated_table_id: Option<TableId>,
39    pub connection_id: Option<ConnectionId>,
40    pub version: i64,
41    // `secret_ref` stores the mapping info mapping from property name to secret id and type.
42    pub secret_ref: Option<SecretRef>,
43    pub rate_limit: Option<i32>,
44    #[sea_orm(column_name = "refresh_mode")]
45    #[serde(rename = "refresh_mode")]
46    pub ingest_mode: Option<SourceIngestMode>,
47}
48
49#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
50pub enum Relation {
51    #[sea_orm(
52        belongs_to = "super::connection::Entity",
53        from = "Column::ConnectionId",
54        to = "super::connection::Column::ConnectionId",
55        on_update = "NoAction",
56        on_delete = "NoAction"
57    )]
58    Connection,
59    #[sea_orm(
60        belongs_to = "super::object::Entity",
61        from = "Column::SourceId",
62        to = "super::object::Column::Oid",
63        on_update = "NoAction",
64        on_delete = "Cascade"
65    )]
66    Object,
67    #[sea_orm(has_many = "super::table::Entity")]
68    Table,
69}
70
71impl Related<super::connection::Entity> for Entity {
72    fn to() -> RelationDef {
73        Relation::Connection.def()
74    }
75}
76
77impl Related<super::object::Entity> for Entity {
78    fn to() -> RelationDef {
79        Relation::Object.def()
80    }
81}
82
83impl Related<super::table::Entity> for Entity {
84    fn to() -> RelationDef {
85        Relation::Table.def()
86    }
87}
88
89impl ActiveModelBehavior for ActiveModel {}
90
91impl From<PbSource> for ActiveModel {
92    fn from(source: PbSource) -> Self {
93        let optional_associated_table_id = source.optional_associated_table_id.map(Into::into);
94        Self {
95            source_id: Set(source.id),
96            name: Set(source.name),
97            row_id_index: Set(source.row_id_index.map(|x| x as _)),
98            columns: Set(ColumnCatalogArray::from(source.columns)),
99            pk_column_ids: Set(I32Array(source.pk_column_ids)),
100            with_properties: Set(Property(source.with_properties)),
101            definition: Set(source.definition),
102            source_info: Set(source.info.as_ref().map(StreamSourceInfo::from)),
103            watermark_descs: Set(WatermarkDescArray::from(source.watermark_descs)),
104            optional_associated_table_id: Set(optional_associated_table_id),
105            connection_id: Set(source.connection_id),
106            version: Set(source.version as _),
107            secret_ref: Set(Some(SecretRef::from(source.secret_refs))),
108            rate_limit: Set(source.rate_limit.map(|id| id as _)),
109            ingest_mode: Set(source.ingest_mode.as_ref().map(SourceIngestMode::from)),
110        }
111    }
112}
113
114impl Model {
115    pub fn is_shared(&self) -> bool {
116        self.source_info
117            .as_ref()
118            .is_some_and(|s| s.to_protobuf().is_shared())
119    }
120}