Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow a USING clause for alter table column type in postgres #848

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/backend/postgres/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ impl TableBuilder for PostgresQueryBuilder {
if !first
&& !matches!(
column_spec,
ColumnSpec::AutoIncrement | ColumnSpec::Generated { .. }
ColumnSpec::AutoIncrement
| ColumnSpec::Generated { .. }
| ColumnSpec::Using(_)
)
{
write!(sql, ", ").unwrap();
Expand Down Expand Up @@ -180,6 +182,10 @@ impl TableBuilder for PostgresQueryBuilder {
ColumnSpec::Generated { .. } => {}
ColumnSpec::Extra(string) => write!(sql, "{string}").unwrap(),
ColumnSpec::Comment(_) => {}
ColumnSpec::Using(expr) => {
write!(sql, " USING ").unwrap();
QueryBuilder::prepare_simple_expr(self, expr, sql);
}
}
false
});
Expand Down
1 change: 1 addition & 0 deletions src/backend/table_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub trait TableBuilder:
}
ColumnSpec::Extra(string) => write!(sql, "{string}").unwrap(),
ColumnSpec::Comment(comment) => self.column_comment(comment, sql),
ColumnSpec::Using(_) => {}
}
}

Expand Down
29 changes: 29 additions & 0 deletions src/table/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ pub enum ColumnSpec {
Generated { expr: SimpleExpr, stored: bool },
Extra(String),
Comment(String),
Using(SimpleExpr),
}

// All interval fields
Expand Down Expand Up @@ -721,6 +722,34 @@ impl ColumnDef {
self
}

/// Some extra options in custom string
/// ```
/// use sea_query::{tests_cfg::*, *};
/// let table = Table::alter()
/// .table(Char::Table)
/// .modify_column(
/// ColumnDef::new(Char::Id)
/// .integer()
/// .using(Expr::col(Char::Id).cast_as(Alias::new("integer"))),
/// )
/// .to_owned();
/// assert_eq!(
/// table.to_string(PostgresQueryBuilder),
/// [
/// r#"ALTER TABLE "character""#,
/// r#"ALTER COLUMN "id" TYPE integer USING CAST("id" AS integer)"#,
/// ]
/// .join(" ")
/// );
/// ```
pub fn using<T>(&mut self, value: T) -> &mut Self
where
T: Into<SimpleExpr>,
{
self.spec.push(ColumnSpec::Using(value.into()));
self
}

/// MySQL only.
pub fn comment<T>(&mut self, string: T) -> &mut Self
where
Expand Down
Loading