Skip to content

Commit 917b1d2

Browse files
committed
Support not_null: {name: "constaint_name"} column option for naming NOT NULL constraints on PostgreSQL 18+
This allows for altering the constraint later. It also allows for dropping the constraint by name.
1 parent 9631a0b commit 917b1d2

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
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 not_null: {name: "constaint_name"} column option for naming NOT NULL constraints on PostgreSQL 18+ (jeremyevans)
4+
35
* Support :period foreign_key option for PERIOD on PostgreSQL 18+ (jeremyevans)
46

57
* Support :include unique/primary_key/exclude option for INCLUDE on PostgreSQL 11+ (jeremyevans)

lib/sequel/adapters/shared/postgres.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,13 @@ def column_definition_add_references_sql(sql, column)
14051405
end
14061406
end
14071407

1408+
def column_definition_null_sql(sql, column)
1409+
if column[:not_null].is_a?(Hash) && (name = column[:not_null][:name])
1410+
sql << " CONSTRAINT #{quote_identifier(name)}"
1411+
end
1412+
super
1413+
end
1414+
14081415
# Handle :period option
14091416
def column_references_table_constraint_sql(constraint)
14101417
sql = String.new

lib/sequel/database/schema_generator.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ def check(*args, &block)
118118
# that this column references. Unnecessary if this column
119119
# references the primary key of the associated table, except if you are
120120
# using MySQL.
121+
# :not_null :: Similar to setting <tt>null: false</tt>, but you can provide a hash value
122+
# to specify options for the NOT NULL constraint on PostgreSQL 18+:
123+
# :name :: The name of the NOT NULL constraint
121124
# :null :: Mark the column as allowing NULL values (if true),
122125
# or not allowing NULL values (if false). The default is to allow NULL values.
123126
# :on_delete :: Specify the behavior of this column when being deleted

lib/sequel/database/schema_methods.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,12 @@ def column_definition_default_sql(sql, column)
592592

593593
# Add null/not null SQL fragment to column creation SQL.
594594
def column_definition_null_sql(sql, column)
595-
null = column.fetch(:null, column[:allow_null])
595+
null = if column[:not_null]
596+
false
597+
else
598+
column.fetch(:null, column[:allow_null])
599+
end
600+
596601
if null.nil? && !can_add_primary_key_constraint_on_nullable_columns? && column[:primary_key]
597602
null = false
598603
end

spec/adapters/postgres_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,14 @@ def before_create
13161316
proc{@db[:atest].insert(Sequel.pg_range(1..1), Sequel.pg_range(9..11))}.must_raise Sequel::Postgres::ExclusionConstraintViolation
13171317
end if DB.server_version >= 180000
13181318

1319+
it 'supports :not_null column option with hash value to name constraint' do
1320+
@db.create_table(:atest){Integer :a, :not_null => {name: :atest_a_nn}}
1321+
proc{@db[:atest].insert}.must_raise Sequel::NotNullConstraintViolation
1322+
@db.alter_table(:atest){drop_constraint(:atest_a_nn)}
1323+
@db[:atest].insert
1324+
@db[:atest].select_map(:a).must_equal [nil]
1325+
end if DB.server_version >= 180000
1326+
13191327
it 'supports using PERIOD in composite foreign key constraints' do
13201328
@db.create_table!(:atest) do
13211329
int4range :id
@@ -1471,7 +1479,7 @@ def before_create
14711479
end if DB.server_version >= 180000
14721480

14731481
it "should support altering inheritability of NOT NULL constraints" do
1474-
@db.run "CREATE TABLE atest (fk INTEGER CONSTRAINT atest_nn NOT NULL)"
1482+
@db.create_table(:atest){Integer :fk, :not_null => {:name => :atest_nn}}
14751483
@db.alter_table(:atest){alter_constraint(:atest_nn, :inherit=>true)}
14761484
@db.alter_table(:atest){alter_constraint(:atest_nn, :inherit=>false)}
14771485
end if DB.server_version >= 180000

0 commit comments

Comments
 (0)