Skip to content

Commit a9e3e43

Browse files
committed
implemented drop column if exists for postgres
Signed-off-by: involk-secure-1609 <[email protected]>
1 parent a79c773 commit a9e3e43

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

src/backend/mysql/table.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,11 @@ impl TableBuilder for MysqlQueryBuilder {
190190
sql.write_str(" TO ").unwrap();
191191
self.prepare_iden(to_name, sql);
192192
}
193-
TableAlterOption::DropColumn(column_name) => {
193+
TableAlterOption::DropColumn(DropColumnOption { column_name, if_exists }) => {
194194
sql.write_str("DROP COLUMN ").unwrap();
195+
if *if_exists{
196+
panic!("MySQL does not support drop column if exists");
197+
}
195198
self.prepare_iden(column_name, sql);
196199
}
197200
TableAlterOption::DropForeignKey(name) => {

src/backend/postgres/table.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,14 @@ impl TableBuilder for PostgresQueryBuilder {
170170
sql.write_str(" TO ").unwrap();
171171
self.prepare_iden(to_name, sql);
172172
}
173-
TableAlterOption::DropColumn(column_name) => {
173+
TableAlterOption::DropColumn(DropColumnOption{
174+
column_name,
175+
if_exists
176+
}) => {
174177
sql.write_str("DROP COLUMN ").unwrap();
178+
if *if_exists {
179+
sql.write_str("IF EXISTS ").unwrap();
180+
}
175181
self.prepare_iden(column_name, sql);
176182
}
177183
TableAlterOption::DropForeignKey(name) => {

src/backend/sqlite/table.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,15 @@ impl TableBuilder for SqliteQueryBuilder {
6262
sql.write_str(" TO ").unwrap();
6363
self.prepare_iden(to_name, sql);
6464
}
65-
TableAlterOption::DropColumn(col_name) => {
65+
TableAlterOption::DropColumn(DropColumnOption {
66+
column_name,
67+
if_exists,
68+
}) => {
6669
sql.write_str("DROP COLUMN ").unwrap();
67-
self.prepare_iden(col_name, sql);
70+
if *if_exists {
71+
panic!("Sqlite does not support drop column if exists");
72+
}
73+
self.prepare_iden(column_name, sql);
6874
}
6975
TableAlterOption::DropForeignKey(_) => {
7076
panic!(

src/table/alter.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ pub struct AddColumnOption {
4242
pub(crate) if_not_exists: bool,
4343
}
4444

45+
/// table alter drop column options
46+
#[derive(Debug, Clone)]
47+
pub struct DropColumnOption {
48+
pub(crate) column_name: DynIden,
49+
pub(crate) if_exists: bool,
50+
}
51+
4552
/// All available table alter options
4653
#[allow(clippy::large_enum_variant)]
4754
#[derive(Debug, Clone)]
@@ -50,7 +57,7 @@ pub enum TableAlterOption {
5057
AddColumn(AddColumnOption),
5158
ModifyColumn(ColumnDef),
5259
RenameColumn(DynIden, DynIden),
53-
DropColumn(DynIden),
60+
DropColumn(DropColumnOption),
5461
AddForeignKey(TableForeignKey),
5562
DropForeignKey(DynIden),
5663
}
@@ -234,7 +241,38 @@ impl TableAlterStatement {
234241
where
235242
T: IntoIden,
236243
{
237-
self.add_alter_option(TableAlterOption::DropColumn(col_name.into_iden()))
244+
self.add_alter_option(TableAlterOption::DropColumn(DropColumnOption {
245+
column_name: col_name.into(),
246+
if_exists: false,
247+
}))
248+
}
249+
250+
/// Drop a column from an existing table if it exists
251+
///
252+
/// # Examples
253+
///
254+
/// ```
255+
/// use sea_query::{tests_cfg::*, *};
256+
///
257+
/// let table = Table::alter()
258+
/// .table(Font::Table)
259+
/// .drop_column_if_exists("new_column")
260+
/// .to_owned();
261+
///
262+
/// assert_eq!(
263+
/// table.to_string(PostgresQueryBuilder),
264+
/// r#"ALTER TABLE "font" DROP COLUMN IF EXISTS "new_column""#
265+
/// );
266+
/// // MySQL and Sqlite do not support DROP COLUMN IF EXISTS
267+
/// ```
268+
pub fn drop_column_if_exists<T>(&mut self, col_name: T) -> &mut Self
269+
where
270+
T: IntoIden,
271+
{
272+
self.add_alter_option(TableAlterOption::DropColumn(DropColumnOption {
273+
column_name: col_name.into(),
274+
if_exists: true,
275+
}))
238276
}
239277

240278
/// Add a foreign key to existing table

0 commit comments

Comments
 (0)