risingwave_meta_model/
connection.rs

1// Copyright 2025 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::PbConnection;
16use risingwave_pb::catalog::connection::PbInfo;
17use sea_orm::ActiveValue::Set;
18use sea_orm::entity::prelude::*;
19use serde::{Deserialize, Serialize};
20
21use crate::{ConnectionId, ConnectionParams, PrivateLinkService};
22
23#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
24#[sea_orm(table_name = "connection")]
25pub struct Model {
26    #[sea_orm(primary_key, auto_increment = false)]
27    pub connection_id: ConnectionId,
28    pub name: String,
29
30    // Private link service has been deprecated
31    pub info: PrivateLinkService,
32    pub params: ConnectionParams,
33}
34
35#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
36pub enum Relation {
37    #[sea_orm(
38        belongs_to = "super::object::Entity",
39        from = "Column::ConnectionId",
40        to = "super::object::Column::Oid",
41        on_update = "NoAction",
42        on_delete = "Cascade"
43    )]
44    Object,
45    #[sea_orm(has_many = "super::sink::Entity")]
46    Sink,
47    #[sea_orm(has_many = "super::source::Entity")]
48    Source,
49}
50
51impl Related<super::object::Entity> for Entity {
52    fn to() -> RelationDef {
53        Relation::Object.def()
54    }
55}
56
57impl Related<super::sink::Entity> for Entity {
58    fn to() -> RelationDef {
59        Relation::Sink.def()
60    }
61}
62
63impl Related<super::source::Entity> for Entity {
64    fn to() -> RelationDef {
65        Relation::Source.def()
66    }
67}
68
69impl ActiveModelBehavior for ActiveModel {}
70
71impl From<PbConnection> for ActiveModel {
72    fn from(conn: PbConnection) -> Self {
73        let Some(PbInfo::ConnectionParams(connection_params)) = conn.info else {
74            unreachable!("private link has been deprecated.")
75        };
76
77        Self {
78            connection_id: Set(conn.id as _),
79            name: Set(conn.name),
80            info: Set(PrivateLinkService::default()),
81            params: Set(ConnectionParams::from(&connection_params)),
82        }
83    }
84}