class ActiveRecord::ConnectionAdapters::PostgreSQLTableDefinition

Creates a PostgreSQL table definition. This class isn’t really meant to be used directly. Instead, see PostgreSQLAdapter#create_table for usage.

Beyond our various PostgreSQL-specific extensions, we’ve also added the post_processing member, which allows you to tack on some SQL statements to run after creating the table. This member should be an Array of SQL statements to run once the table has been created. See the source code for PostgreSQLAdapter#create_table and PostgreSQLTableDefinition#geometry for an example of its use.

Constants

LIKE_TYPES

Attributes

base[RW]
options[RW]
table_name[RW]

Public Instance Methods

check_constraint(expression, options = {}) click to toggle source

Add a CHECK constraint to the table. See PostgreSQLCheckConstraint for more details.

# File lib/active_record/postgresql_extensions/tables.rb, line 291
def check_constraint(expression, options = {})
  table_constraints << PostgreSQLCheckConstraint.new(@base, expression, options)
end
exclude(excludes, options = {}) click to toggle source

Add an EXCLUDE constraint to the table. See PostgreSQLExcludeConstraint for more details.

# File lib/active_record/postgresql_extensions/tables.rb, line 309
def exclude(excludes, options = {})
  table_constraints << PostgreSQLExcludeConstraint.new(@base, table_name, excludes, options)
end
foreign_key(columns, ref_table, *args) click to toggle source

Add a FOREIGN KEY constraint to the table. See PostgreSQLForeignKeyConstraint for more details.

# File lib/active_record/postgresql_extensions/tables.rb, line 303
def foreign_key(columns, ref_table, *args)
  table_constraints << PostgreSQLForeignKeyConstraint.new(@base, columns, ref_table, *args)
end
geography(column_name, opts = {}) click to toggle source
# File lib/active_record/postgresql_extensions/geometry.rb, line 328
def geography(column_name, opts = {})
  opts = {
    :srid => ActiveRecord::PostgreSQLExtensions::PostGIS.UNKNOWN_SRIDS[:geography]
  }.merge(opts)

  self.spatial(column_name, opts.merge(
    :spatial_column_type => :geography
  ))
end
geometry(column_name, opts = {})
Alias for: spatial
index(name, columns, options = {}) click to toggle source

Add an INDEX to the table. This INDEX will be added during post processing after the table has been created. See PostgreSQLIndexDefinition for more details.

# File lib/active_record/postgresql_extensions/tables.rb, line 377
def index(name, columns, options = {})
  post_processing << PostgreSQLIndexDefinition.new(@base, name, self.table_name, columns, options)
end
like(parent_table, options = {}) click to toggle source

Creates a LIKE statement for use in a table definition.

Options

  • :including and :excluding - set options for the INCLUDING and EXCLUDING clauses in a LIKE statement. Valid values are :constraints, :defaults and :indexes. You can set one or more by using an Array.

See the PostgreSQL documentation for details on how to use LIKE. Be sure to take note as to how it differs from INHERITS.

Also, be sure to note that, like, this LIKE isn’t, like, the LIKE you use in a WHERE condition. This is, PostgreSQL’s own special LIKE clause for table definitions. Like.

# File lib/active_record/postgresql_extensions/tables.rb, line 271
def like(parent_table, options = {})
  assert_valid_like_types(options[:includes])
  assert_valid_like_types(options[:excludes])

  # Huh? Whyfor I dun this?
  # @like = base.with_schema(@schema) { "LIKE #{base.quote_table_name(parent_table)}" }
  @like = "LIKE #{@base.quote_table_name(parent_table)}"

  if options[:including]
    @like << Array.wrap(options[:including]).collect { |l| " INCLUDING #{l.to_s.upcase}" }.join
  end

  if options[:excluding]
    @like << Array.wrap(options[:excluding]).collect { |l| " EXCLUDING #{l.to_s.upcase}" }.join
  end
  @like
end
post_processing() click to toggle source

Add statements to execute to after a table has been created.

# File lib/active_record/postgresql_extensions/tables.rb, line 382
def post_processing
  @post_processing ||= []
end
primary_key_constraint(columns, options = {}) click to toggle source
# File lib/active_record/postgresql_extensions/tables.rb, line 313
def primary_key_constraint(columns, options = {})
  table_constraints << PostgreSQLPrimaryKeyConstraint.new(@base, columns, options)
end
spatial(column_name, opts = {}) click to toggle source

This is a special spatial type for the PostGIS extension’s data types. It is used in a table definition to define a spatial column.

Depending on the version of PostGIS being used, we’ll try to create geometry columns in a post-2.0-ish, typmod-based way or a pre-2.0-ish AddGeometryColumn-based way. We can also add CHECK constraints and create a GiST index on the column all in one go.

In versions of PostGIS prior to 2.0, geometry columns are created using the AddGeometryColumn and will created with CHECK constraints where appropriate and entries to the geometry_columns will be updated accordingly.

In versions of PostGIS after 2.0, geometry columns are creating using typmod specifiers. CHECK constraints can still be created, but their creation must be forced using the :force_constraints option.

The geometry and geography methods are shortcuts to calling the spatial method with the :spatial_column_type option set accordingly.

Options

  • :spatial_column_type - the column type. This value can be one of :geometry or :geography. This value doesn’t refer to the spatial type used by the column, but rather by the actual column type itself.

  • :geometry_type - set the geometry type. The actual data type is either “geometry” or “geography”; this option refers to the spatial type being used, i.e. “POINT”, “POLYGON”, “”

  • :add_constraints - automatically creates the CHECK constraints used to enforce ndims, srid and geometry type. The default is true.

  • :force_constraints - forces the creation of CHECK constraints in versions of PostGIS post-2.0.

  • :add_geometry_columns_entry - automatically adds an entry to the geometry_columns table. We will try to delete any existing match in geometry_columns before inserting. The default is true. This value is ignored in versions of PostGIS post-2.0.

  • :create_gist_index - automatically creates a GiST index for the new geometry column. This option accepts either a true/false expression or a String. If the value is a String, we’ll use it as the index name. The default is true.

  • :ndims - the number of dimensions to allow in the geometry. This value is either 2 or 3 by default depending on the value of the :geometry_type option. If the :geometry_type ends in an “m” (for “measured geometries” the default is 3); for everything else, it is 2.

  • :srid - the SRID, a.k.a. the Spatial Reference Identifier. The default depends on the version of PostGIS being used and the spatial column type being used. Refer to the PostGIS docs for the specifics, but generally this means either a value of -1 for versions of PostGIS prior to 2.0 for geometry columns and a value of 0 for versions post-2.0 and for all geography columns.

# File lib/active_record/postgresql_extensions/geometry.rb, line 313
def spatial(column_name, opts = {})
  column = self[column_name] || PostgreSQLGeometryColumnDefinition.new(base, column_name, opts)

  unless @columns.include?(column)
    @columns << column
  end

  table_constraints.concat(column.table_constraints)
  post_processing.concat(column.geometry_columns_entry(table_name))
  post_processing.concat(column.geometry_column_index(table_name))

  self
end
Also aliased as: geometry
unique_constraint(columns, options = {}) click to toggle source

Add a UNIQUE constraint to the table. See PostgreSQLUniqueConstraint for more details.

# File lib/active_record/postgresql_extensions/tables.rb, line 297
def unique_constraint(columns, options = {})
  table_constraints << PostgreSQLUniqueConstraint.new(@base, columns, options)
end

Private Instance Methods

table_constraints() click to toggle source
# File lib/active_record/postgresql_extensions/tables.rb, line 389
def table_constraints
  @table_constraints ||= []
end