Skip to content

Commit fb92764

Browse files
committed
Add only option to add_index in PostgreSQL
1 parent 15a7a92 commit fb92764

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

lib/sequel/adapters/shared/postgres.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1757,7 +1757,7 @@ def index_definition_sql(table_name, index)
17571757
index_type = :gist
17581758
end
17591759

1760-
"CREATE #{unique}INDEX#{' CONCURRENTLY' if index[:concurrently]}#{if_not_exists} #{quote_identifier(index_name)} ON #{quote_schema_table(table_name)} #{"USING #{index_type} " if index_type}#{expr}#{" INCLUDE #{literal(Array(index[:include]))}" if index[:include]}#{nulls_distinct}#{" TABLESPACE #{quote_identifier(index[:tablespace])}" if index[:tablespace]}#{filter}"
1760+
"CREATE #{unique}INDEX#{' CONCURRENTLY' if index[:concurrently]}#{if_not_exists} #{quote_identifier(index_name)} ON#{' ONLY' if index[:only]} #{quote_schema_table(table_name)} #{"USING #{index_type} " if index_type}#{expr}#{" INCLUDE #{literal(Array(index[:include]))}" if index[:include]}#{nulls_distinct}#{" TABLESPACE #{quote_identifier(index[:tablespace])}" if index[:tablespace]}#{filter}"
17611761
end
17621762

17631763
# Setup datastructures shared by all postgres adapters.

lib/sequel/database/schema_generator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ def has_column?(name)
299299
# :opclass :: Set an opclass to use for all columns (per-column opclasses require
300300
# custom SQL).
301301
# :tablespace :: Specify tablespace for index.
302+
# :only :: Create index only for given table, not for any child tables (PostgreSQL 11+)
302303
#
303304
# Microsoft SQL Server specific options:
304305
#

spec/adapters/postgres_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,15 @@ def before_create
16411641
@db.add_index :atest, :a, :tablespace=>:pg_default
16421642
end
16431643

1644+
it "should support creating indexes with ONLY option" do
1645+
@db.create_table(:atest, partition_by: :id, :partition_type=>:range){Integer :id}
1646+
@db.create_table(:btest, partition_of: :atest){from 1; to 3}
1647+
@db.add_index :atest, :id, :only=>true
1648+
1649+
@db.indexes(:atest, :include_invalid=>true).size.must_equal 1
1650+
@db.indexes(:btest, :include_invalid=>true).must_be_empty
1651+
end if DB.server_version >= 110000
1652+
16441653
it "#lock should lock table if inside a transaction" do
16451654
@db.transaction{@d.lock('EXCLUSIVE'); @d.insert(:name=>'a')}
16461655
end

0 commit comments

Comments
 (0)