use crate::catalog::id::DbObjectId;
use crate::render::Safety;
pub use crate::render::SqlRenderer;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OperationKind {
Create,
Drop,
Alter,
}
pub use aggregate::*;
pub use cast::*;
pub use comments::*;
pub use constraint::*;
pub use domain::*;
pub use extension::*;
pub use function::*;
pub use grant::*;
pub use index::*;
pub use operator::*;
pub use policy::*;
pub use schema::*;
pub use sequence::*;
pub use table::*;
pub use trigger::*;
pub use types::*;
pub use view::*;
pub mod aggregate;
pub mod cast;
pub mod comments;
pub mod constraint;
pub mod domain;
pub mod extension;
pub mod function;
pub mod grant;
pub mod index;
pub mod operator;
pub mod policy;
pub mod schema;
pub mod sequence;
pub mod table;
pub mod trigger;
pub mod types;
pub mod view;
#[derive(Debug, Clone)]
pub enum MigrationStep {
Schema(SchemaOperation),
Table(TableOperation),
View(ViewOperation),
Type(TypeOperation),
Domain(DomainOperation),
Sequence(SequenceOperation),
Function(FunctionOperation),
Aggregate(AggregateOperation),
Operator(OperatorOperation),
Cast(CastOperation),
Index(IndexOperation),
Constraint(ConstraintOperation),
Trigger(TriggerOperation),
Policy(PolicyOperation),
Extension(ExtensionOperation),
Grant(GrantOperation),
Comment(CommentOperation),
}
impl MigrationStep {
pub fn id(&self) -> DbObjectId {
self.db_object_id()
}
pub fn operation_kind(&self) -> OperationKind {
match self {
Self::Schema(op) => op.operation_kind(),
Self::Table(op) => op.operation_kind(),
Self::View(op) => op.operation_kind(),
Self::Type(op) => op.operation_kind(),
Self::Domain(op) => op.operation_kind(),
Self::Sequence(op) => op.operation_kind(),
Self::Function(op) => op.operation_kind(),
Self::Aggregate(op) => op.operation_kind(),
Self::Operator(op) => op.operation_kind(),
Self::Cast(op) => op.operation_kind(),
Self::Index(op) => op.operation_kind(),
Self::Constraint(op) => op.operation_kind(),
Self::Trigger(op) => op.operation_kind(),
Self::Policy(op) => op.operation_kind(),
Self::Extension(op) => op.operation_kind(),
Self::Grant(op) => op.operation_kind(),
Self::Comment(op) => op.operation_kind(),
}
}
pub fn has_destructive_sql(&self) -> bool {
self.to_sql()
.iter()
.any(|s| s.safety == Safety::Destructive)
}
pub fn is_relationship(&self) -> bool {
match self {
MigrationStep::Sequence(SequenceOperation::AlterOwnership { .. }) => true,
MigrationStep::Constraint(ConstraintOperation::Create(constraint)) => {
matches!(
constraint.constraint_type,
crate::catalog::constraint::ConstraintType::ForeignKey { .. }
)
}
_ => false,
}
}
pub fn summary(&self) -> String {
let verb = match self.operation_kind() {
OperationKind::Create => "Create",
OperationKind::Drop => "Drop",
OperationKind::Alter => "Update",
};
format!("{} {}", verb, self.id())
}
pub fn is_grant(&self) -> bool {
matches!(self, MigrationStep::Grant(_))
}
pub fn dependencies(&self) -> Vec<DbObjectId> {
match self {
MigrationStep::Grant(GrantOperation::Grant { grant }) => grant.depends_on.clone(),
MigrationStep::Grant(GrantOperation::Revoke { grant }) => grant.depends_on.clone(),
MigrationStep::Grant(GrantOperation::GrantColumns(cg)) => cg.depends_on.clone(),
MigrationStep::Grant(GrantOperation::RevokeColumns(cg)) => cg.depends_on.clone(),
_ => vec![],
}
}
}