Skip to main content

surrealdb_expr/expr/statements/alter/
table.rs

1use surrealdb_types::{SqlFormat, ToSql};
2
3use super::AlterKind;
4use crate::expr::permission::Permissions;
5use crate::expr::table_type::TableType;
6use crate::expr::{ChangeFeed, Expr, Literal};
7
8#[derive(Clone, Debug, Eq, PartialEq, Hash)]
9/// Executes `ALTER TABLE` operations against an existing table.
10///
11/// Supported operations include:
12/// - toggle `SCHEMAFULL`/`SCHEMALESS`
13/// - update `PERMISSIONS`
14/// - set/drop `CHANGEFEED`
15/// - set/drop table `COMMENT`
16/// - change table `TYPE` (`NORMAL`/`RELATION`/`ANY`)
17/// - request a table-level storage `COMPACT`
18///
19/// Notes:
20/// - When switching to a `RELATION` table type, in/out fields are created as needed via
21///   `DefineTableStatement::add_in_out_fields`.
22/// - When `compact` is true, underlying storage for this table is compacted.
23pub struct AlterTableStatement {
24	/// Table name.
25	pub name: Expr,
26	/// If true, do nothing (and succeed) when the table does not exist.
27	pub if_exists: bool,
28	/// Switch `SCHEMAFULL` on (`Set`) or switch to `SCHEMALESS` (`Drop`).
29	pub schemafull: AlterKind<()>,
30	/// New table permissions, if provided.
31	pub permissions: Option<Permissions>,
32	/// Set/drop changefeed definition.
33	pub changefeed: AlterKind<ChangeFeed>,
34	/// Set/drop human‑readable comment.
35	pub comment: AlterKind<String>,
36	/// Request a compaction of the table’s keyspace.
37	pub compact: bool,
38	/// Change the table type (`NORMAL` / `RELATION` / `ANY`).
39	pub kind: Option<TableType>,
40	/// Set/drop the inline adjacency cache cap.
41	pub inline_edges_cap: AlterKind<u32>,
42	/// Set/drop the inline reference cache cap.
43	pub inline_refs_cap: AlterKind<u32>,
44}
45
46impl Default for AlterTableStatement {
47	fn default() -> Self {
48		Self {
49			name: Expr::Literal(Literal::None),
50			if_exists: false,
51			schemafull: AlterKind::None,
52			permissions: None,
53			changefeed: AlterKind::None,
54			comment: AlterKind::None,
55			compact: false,
56			kind: None,
57			inline_edges_cap: AlterKind::None,
58			inline_refs_cap: AlterKind::None,
59		}
60	}
61}
62
63impl ToSql for AlterTableStatement {
64	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
65		let stmt: crate::sql::statements::alter::AlterTableStatement = self.clone().into();
66		stmt.fmt_sql(f, fmt);
67	}
68}