Skip to content

Commit e0c99f6

Browse files
committed
Support :no_inherit option for NOT NULL and CHECK constraints on PostgreSQL
1 parent 917b1d2 commit e0c99f6

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

CHANGELOG

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

3+
* Support :no_inherit option for CHECK constraints on PostgreSQL 9.2+ (jeremyevans)
4+
5+
* Support :no_inherit option for NOT NULL constraints on PostgreSQL 18+ (jeremyevans)
6+
37
* Support not_null: {name: "constaint_name"} column option for naming NOT NULL constraints on PostgreSQL 18+ (jeremyevans)
48

59
* Support :period foreign_key option for PERIOD on PostgreSQL 18+ (jeremyevans)

lib/sequel/adapters/shared/postgres.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,9 @@ def constraint_definition_sql(constraint)
13881388
sql
13891389
else # when :foreign_key, :check
13901390
sql = super
1391+
if constraint[:no_inherit]
1392+
sql << " NO INHERIT"
1393+
end
13911394
if constraint[:not_enforced]
13921395
sql << " NOT ENFORCED"
13931396
end
@@ -1406,10 +1409,15 @@ def column_definition_add_references_sql(sql, column)
14061409
end
14071410

14081411
def column_definition_null_sql(sql, column)
1409-
if column[:not_null].is_a?(Hash) && (name = column[:not_null][:name])
1412+
constraint = column[:not_null]
1413+
constraint = nil unless constraint.is_a?(Hash)
1414+
if constraint && (name = constraint[:name])
14101415
sql << " CONSTRAINT #{quote_identifier(name)}"
14111416
end
14121417
super
1418+
if constraint && constraint[:no_inherit]
1419+
sql << " NO INHERIT"
1420+
end
14131421
end
14141422

14151423
# Handle :period option

lib/sequel/database/schema_generator.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ def check(*args, &block)
121121
# :not_null :: Similar to setting <tt>null: false</tt>, but you can provide a hash value
122122
# to specify options for the NOT NULL constraint on PostgreSQL 18+:
123123
# :name :: The name of the NOT NULL constraint
124+
# :no_inherit :: Set NO INHERIT on the constraint, so it will not propogate to
125+
# child tables.
124126
# :null :: Mark the column as allowing NULL values (if true),
125127
# or not allowing NULL values (if false). The default is to allow NULL values.
126128
# :on_delete :: Specify the behavior of this column when being deleted
@@ -191,6 +193,8 @@ def column(name, type, opts = OPTS)
191193
# :deferrable :: Whether the CHECK constraint should be marked DEFERRABLE.
192194
#
193195
# PostgreSQL specific options:
196+
# :no_inherit :: Set NO INHERIT on the constraint, so it will not propogate to
197+
# child tables.
194198
# :not_enforced :: Whether the CHECK constraint should be marked NOT ENFORCED.
195199
# :not_valid :: Whether the CHECK constraint should be marked NOT VALID.
196200
def constraint(name, *args, &block)

spec/adapters/postgres_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,30 @@ def before_create
13241324
@db[:atest].select_map(:a).must_equal [nil]
13251325
end if DB.server_version >= 180000
13261326

1327+
it 'supports :not_null column option with :no_inherit option' do
1328+
begin
1329+
@db.create_table(:atest){Integer :a, :not_null => {:no_inherit => true}}
1330+
proc{@db[:atest].insert}.must_raise Sequel::NotNullConstraintViolation
1331+
@db.create_table(:atest2, :inherits=>:atest)
1332+
@db[:atest2].insert
1333+
@db[:atest2].select_map(:a).must_equal [nil]
1334+
ensure
1335+
@db.drop_table?(:atest2)
1336+
end
1337+
end if DB.server_version >= 180000
1338+
1339+
it 'supports check constraint :no_inherit option' do
1340+
begin
1341+
@db.create_table(:atest){Integer :a; constraint(:no_inherit => true){a > 10}}
1342+
proc{@db[:atest].insert(9)}.must_raise Sequel::CheckConstraintViolation
1343+
@db.create_table(:atest2, :inherits=>:atest)
1344+
@db[:atest2].insert(9)
1345+
@db[:atest2].select_map(:a).must_equal [9]
1346+
ensure
1347+
@db.drop_table?(:atest2)
1348+
end
1349+
end if DB.server_version >= 90200
1350+
13271351
it 'supports using PERIOD in composite foreign key constraints' do
13281352
@db.create_table!(:atest) do
13291353
int4range :id

0 commit comments

Comments
 (0)