risingwave_meta_model/
refresh_job.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 sea_orm::entity::prelude::*;
16use serde::{Deserialize, Serialize};
17
18use crate::TableId;
19
20#[derive(Clone, Copy, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
21#[sea_orm(rs_type = "String", db_type = "string(None)")]
22pub enum RefreshState {
23    /// The table is refreshable and the current state is `Pending`.
24    #[sea_orm(string_value = "IDLE")]
25    Idle,
26    /// The table is refreshable and the current state is `Refreshing` (`RefreshStart` barrier passed).
27    #[sea_orm(string_value = "REFRESHING")]
28    Refreshing,
29    /// The table is refreshable and the current state is `Finishing`. (`LoadFinish` barrier passed).
30    #[sea_orm(string_value = "FINISHING")]
31    Finishing,
32}
33
34impl From<RefreshState> for risingwave_pb::catalog::RefreshState {
35    fn from(refresh_state: RefreshState) -> Self {
36        match refresh_state {
37            RefreshState::Idle => Self::Idle,
38            RefreshState::Refreshing => Self::Refreshing,
39            RefreshState::Finishing => Self::Finishing,
40        }
41    }
42}
43
44impl From<risingwave_pb::catalog::RefreshState> for RefreshState {
45    fn from(refresh_state: risingwave_pb::catalog::RefreshState) -> Self {
46        match refresh_state {
47            risingwave_pb::catalog::RefreshState::Idle => Self::Idle,
48            risingwave_pb::catalog::RefreshState::Refreshing => Self::Refreshing,
49            risingwave_pb::catalog::RefreshState::Finishing => Self::Finishing,
50            risingwave_pb::catalog::RefreshState::Unspecified => {
51                unreachable!("Unspecified refresh state")
52            }
53        }
54    }
55}
56
57impl std::fmt::Display for RefreshState {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        match self {
60            RefreshState::Idle => write!(f, "IDLE"),
61            RefreshState::Refreshing => write!(f, "REFRESHING"),
62            RefreshState::Finishing => write!(f, "FINISHING"),
63        }
64    }
65}
66
67#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
68#[sea_orm(table_name = "refresh_job")]
69pub struct Model {
70    #[sea_orm(primary_key, auto_increment = false)]
71    pub table_id: TableId,
72    pub last_trigger_time: Option<i64>, // timestamp in millis
73    pub trigger_interval_secs: Option<i64>,
74    pub current_status: RefreshState,
75    pub last_success_time: Option<i64>, // timestamp in millis
76}
77
78#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
79pub enum Relation {
80    #[sea_orm(
81        belongs_to = "super::table::Entity",
82        from = "Column::TableId",
83        to = "super::table::Column::TableId",
84        on_update = "Cascade",
85        on_delete = "Cascade"
86    )]
87    Table,
88}
89
90impl Related<super::table::Entity> for Entity {
91    fn to() -> RelationDef {
92        Relation::Table.def()
93    }
94}
95
96impl ActiveModelBehavior for ActiveModel {}