Skip to content

Commit 1c3305d

Browse files
committed
Support alter_table rename_constraint on PostgreSQL 9.2+
1 parent e370b68 commit 1c3305d

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
=== master
22

3+
* Support alter_table rename_constraint on PostgreSQL 9.2+ (jeremyevans) (#2274)
4+
35
* Do not auto parameterize PostgreSQL range and multirange types containing expressions (jeremyevans)
46

57
* Support sort and reverse methods in pg_array_ops extension on PostgreSQL 18+ (jeremyevans)

lib/sequel/adapters/shared/postgres.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ def alter_constraint(name, opts=OPTS)
154154
@operations << {:op => :alter_constraint, :name => name}.merge!(opts)
155155
end
156156

157+
# :inherit :: Set true to use INHERIT, or false to use NO INHERIT (PostgreSQL 18+)
158+
def rename_constraint(name, new_name)
159+
@operations << {:op => :rename_constraint, :name => name, :new_name => new_name}
160+
end
161+
157162
# Validate the constraint with the given name, which should have
158163
# been added previously with NOT VALID.
159164
def validate_constraint(name)
@@ -1210,6 +1215,10 @@ def alter_table_generator_class
12101215
Postgres::AlterTableGenerator
12111216
end
12121217

1218+
def alter_table_rename_constraint_sql(table, op)
1219+
"RENAME CONSTRAINT #{quote_identifier(op[:name])} TO #{quote_identifier(op[:new_name])}"
1220+
end
1221+
12131222
def alter_table_set_column_type_sql(table, op)
12141223
s = super
12151224
if using = op[:using]

spec/adapters/postgres_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,13 @@ def before_create
15141514
@db.alter_table(:atest){alter_constraint(:atest_nn, :inherit=>false)}
15151515
end if DB.server_version >= 180000
15161516

1517+
it "should support renaming constraints" do
1518+
@db.create_table!(:atest){Integer :i; constraint(:foo){i > 0}}
1519+
@db.check_constraints(:atest).keys.must_equal [:foo]
1520+
@db.alter_table(:atest){rename_constraint(:foo, :bar)}
1521+
@db.check_constraints(:atest).keys.must_equal [:bar]
1522+
end if DB.server_version >= 90200
1523+
15171524
it "should support :using when altering a column's type" do
15181525
@db.create_table!(:atest){Integer :t}
15191526
@db[:atest].insert(1262404000)

0 commit comments

Comments
 (0)