risingwave_meta_model/
database.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::PbDatabase;
16use sea_orm::ActiveValue::Set;
17use sea_orm::entity::prelude::*;
18use serde::{Deserialize, Serialize};
19
20use crate::DatabaseId;
21
22#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
23#[sea_orm(table_name = "database")]
24pub struct Model {
25    #[sea_orm(primary_key, auto_increment = false)]
26    pub database_id: DatabaseId,
27    #[sea_orm(unique)]
28    pub name: String,
29    pub resource_group: String,
30    pub barrier_interval_ms: Option<i32>,
31    pub checkpoint_frequency: Option<i64>,
32}
33
34#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
35pub enum Relation {
36    #[sea_orm(
37        belongs_to = "super::object::Entity",
38        from = "Column::DatabaseId",
39        to = "super::object::Column::Oid",
40        on_update = "NoAction",
41        on_delete = "Cascade"
42    )]
43    Object,
44}
45
46impl Related<super::object::Entity> for Entity {
47    fn to() -> RelationDef {
48        Relation::Object.def()
49    }
50}
51
52impl ActiveModelBehavior for ActiveModel {}
53
54impl From<PbDatabase> for ActiveModel {
55    fn from(db: PbDatabase) -> Self {
56        Self {
57            database_id: Set(db.id as _),
58            name: Set(db.name),
59            resource_group: Set(db.resource_group),
60            barrier_interval_ms: Set(db.barrier_interval_ms.map(|v| v as i32)),
61            checkpoint_frequency: Set(db.checkpoint_frequency.map(|v| v as i64)),
62        }
63    }
64}