common_meta/ddl/test_util/
alter_table.rs

1// Copyright 2023 Greptime Team
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 api::v1::alter_table_expr::Kind;
16use api::v1::{AddColumn, AddColumns, AlterTableExpr, ColumnDef, RenameTable};
17use common_catalog::consts::{DEFAULT_CATALOG_NAME, DEFAULT_SCHEMA_NAME};
18use derive_builder::Builder;
19
20#[derive(Default, Builder)]
21#[builder(default)]
22pub struct TestAlterTableExpr {
23    #[builder(setter(into), default = "DEFAULT_CATALOG_NAME.to_string()")]
24    catalog_name: String,
25    #[builder(setter(into), default = "DEFAULT_SCHEMA_NAME.to_string()")]
26    schema_name: String,
27    #[builder(setter(into))]
28    table_name: String,
29    #[builder(setter(into))]
30    add_columns: Vec<ColumnDef>,
31    #[builder(setter(into, strip_option))]
32    new_table_name: Option<String>,
33    #[builder(setter)]
34    add_if_not_exists: bool,
35}
36
37impl From<TestAlterTableExpr> for AlterTableExpr {
38    fn from(value: TestAlterTableExpr) -> Self {
39        if let Some(new_table_name) = value.new_table_name {
40            Self {
41                catalog_name: value.catalog_name,
42                schema_name: value.schema_name,
43                table_name: value.table_name,
44                kind: Some(Kind::RenameTable(RenameTable { new_table_name })),
45            }
46        } else {
47            Self {
48                catalog_name: value.catalog_name,
49                schema_name: value.schema_name,
50                table_name: value.table_name,
51                kind: Some(Kind::AddColumns(AddColumns {
52                    add_columns: value
53                        .add_columns
54                        .into_iter()
55                        .map(|col| AddColumn {
56                            column_def: Some(col),
57                            location: None,
58                            add_if_not_exists: value.add_if_not_exists,
59                        })
60                        .collect(),
61                })),
62            }
63        }
64    }
65}