risingwave_meta_model/
object.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::common::PbObjectType;
16use sea_orm::entity::prelude::*;
17use serde::{Deserialize, Serialize};
18
19use crate::{DatabaseId, ObjectId, SchemaId, UserId};
20
21#[derive(
22    Clone, Debug, Hash, PartialEq, Eq, Copy, EnumIter, DeriveActiveEnum, Serialize, Deserialize,
23)]
24#[sea_orm(rs_type = "String", db_type = "string(None)")]
25pub enum ObjectType {
26    #[sea_orm(string_value = "DATABASE")]
27    Database,
28    #[sea_orm(string_value = "SCHEMA")]
29    Schema,
30    #[sea_orm(string_value = "TABLE")]
31    Table,
32    #[sea_orm(string_value = "SOURCE")]
33    Source,
34    #[sea_orm(string_value = "SINK")]
35    Sink,
36    #[sea_orm(string_value = "VIEW")]
37    View,
38    #[sea_orm(string_value = "INDEX")]
39    Index,
40    #[sea_orm(string_value = "FUNCTION")]
41    Function,
42    #[sea_orm(string_value = "CONNECTION")]
43    Connection,
44    #[sea_orm(string_value = "SUBSCRIPTION")]
45    Subscription,
46    #[sea_orm(string_value = "SECRET")]
47    Secret,
48}
49
50impl ObjectType {
51    pub fn as_str(&self) -> &'static str {
52        match self {
53            ObjectType::Database => "database",
54            ObjectType::Schema => "schema",
55            ObjectType::Table => "table",
56            ObjectType::Source => "source",
57            ObjectType::Sink => "sink",
58            ObjectType::View => "view",
59            ObjectType::Index => "index",
60            ObjectType::Function => "function",
61            ObjectType::Connection => "connection",
62            ObjectType::Subscription => "subscription",
63            ObjectType::Secret => "secret",
64        }
65    }
66}
67
68impl From<PbObjectType> for ObjectType {
69    fn from(pb_object_type: PbObjectType) -> Self {
70        match pb_object_type {
71            PbObjectType::Database => ObjectType::Database,
72            PbObjectType::Schema => ObjectType::Schema,
73            PbObjectType::Table | PbObjectType::Mview => ObjectType::Table,
74            PbObjectType::Source => ObjectType::Source,
75            PbObjectType::Sink => ObjectType::Sink,
76            PbObjectType::View => ObjectType::View,
77            PbObjectType::Index => ObjectType::Index,
78            PbObjectType::Function => ObjectType::Function,
79            PbObjectType::Connection => ObjectType::Connection,
80            PbObjectType::Subscription => ObjectType::Subscription,
81            PbObjectType::Secret => ObjectType::Secret,
82            PbObjectType::Unspecified => {
83                unreachable!("Unspecified object type")
84            }
85        }
86    }
87}
88
89#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
90#[sea_orm(table_name = "object")]
91pub struct Model {
92    #[sea_orm(primary_key)]
93    pub oid: ObjectId,
94    pub obj_type: ObjectType,
95    pub owner_id: UserId,
96    pub schema_id: Option<SchemaId>,
97    pub database_id: Option<DatabaseId>,
98    pub initialized_at: DateTime,
99    pub created_at: DateTime,
100    pub initialized_at_cluster_version: Option<String>,
101    pub created_at_cluster_version: Option<String>,
102}
103
104#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
105pub enum Relation {
106    #[sea_orm(has_many = "super::connection::Entity")]
107    Connection,
108    #[sea_orm(has_many = "super::database::Entity")]
109    Database,
110    #[sea_orm(has_many = "super::fragment::Entity")]
111    Fragment,
112    #[sea_orm(has_many = "super::function::Entity")]
113    Function,
114    #[sea_orm(has_many = "super::index::Entity")]
115    Index,
116    #[sea_orm(
117        belongs_to = "Entity",
118        from = "Column::DatabaseId",
119        to = "Column::Oid",
120        on_update = "NoAction",
121        on_delete = "Cascade"
122    )]
123    SelfRef2,
124    #[sea_orm(
125        belongs_to = "Entity",
126        from = "Column::SchemaId",
127        to = "Column::Oid",
128        on_update = "NoAction",
129        on_delete = "Cascade"
130    )]
131    SelfRef1,
132    #[sea_orm(
133        belongs_to = "super::database::Entity",
134        from = "Column::DatabaseId",
135        to = "super::database::Column::DatabaseId",
136        on_update = "NoAction",
137        on_delete = "NoAction"
138    )]
139    Database2,
140    #[sea_orm(has_many = "super::schema::Entity")]
141    Schema,
142    #[sea_orm(has_many = "super::sink::Entity")]
143    Sink,
144    #[sea_orm(has_many = "super::subscription::Entity")]
145    Subscription,
146    #[sea_orm(has_many = "super::source::Entity")]
147    Source,
148    #[sea_orm(has_many = "super::table::Entity")]
149    Table,
150    #[sea_orm(has_many = "super::streaming_job::Entity")]
151    StreamingJob,
152    #[sea_orm(
153        belongs_to = "super::user::Entity",
154        from = "Column::OwnerId",
155        to = "super::user::Column::UserId",
156        on_update = "NoAction",
157        on_delete = "Cascade"
158    )]
159    User,
160    #[sea_orm(has_many = "super::user_privilege::Entity")]
161    UserPrivilege,
162    #[sea_orm(has_many = "super::view::Entity")]
163    View,
164    #[sea_orm(
165        belongs_to = "super::schema::Entity",
166        from = "Column::SchemaId",
167        to = "super::schema::Column::SchemaId",
168        on_update = "NoAction",
169        on_delete = "NoAction"
170    )]
171    Schema2,
172}
173
174impl Related<super::connection::Entity> for Entity {
175    fn to() -> RelationDef {
176        Relation::Connection.def()
177    }
178}
179
180impl Related<super::database::Entity> for Entity {
181    fn to() -> RelationDef {
182        Relation::Database.def()
183    }
184}
185
186impl Related<super::fragment::Entity> for Entity {
187    fn to() -> RelationDef {
188        Relation::Fragment.def()
189    }
190}
191
192impl Related<super::function::Entity> for Entity {
193    fn to() -> RelationDef {
194        Relation::Function.def()
195    }
196}
197
198impl Related<super::index::Entity> for Entity {
199    fn to() -> RelationDef {
200        Relation::Index.def()
201    }
202}
203
204impl Related<super::schema::Entity> for Entity {
205    fn to() -> RelationDef {
206        Relation::Schema.def()
207    }
208}
209
210impl Related<super::sink::Entity> for Entity {
211    fn to() -> RelationDef {
212        Relation::Sink.def()
213    }
214}
215
216impl Related<super::subscription::Entity> for Entity {
217    fn to() -> RelationDef {
218        Relation::Subscription.def()
219    }
220}
221
222impl Related<super::source::Entity> for Entity {
223    fn to() -> RelationDef {
224        Relation::Source.def()
225    }
226}
227
228impl Related<super::table::Entity> for Entity {
229    fn to() -> RelationDef {
230        Relation::Table.def()
231    }
232}
233
234impl Related<super::streaming_job::Entity> for Entity {
235    fn to() -> RelationDef {
236        Relation::StreamingJob.def()
237    }
238}
239
240impl Related<super::user::Entity> for Entity {
241    fn to() -> RelationDef {
242        Relation::User.def()
243    }
244}
245
246impl Related<super::user_privilege::Entity> for Entity {
247    fn to() -> RelationDef {
248        Relation::UserPrivilege.def()
249    }
250}
251
252impl Related<super::view::Entity> for Entity {
253    fn to() -> RelationDef {
254        Relation::View.def()
255    }
256}
257
258impl ActiveModelBehavior for ActiveModel {}