Module: ActiveRecord::ConnectionAdapters::SchemaStatements
- Defined in:
- lib/ensured_schema/schema_statements.rb
Instance Method Summary collapse
-
#column_exists?(table_name, column_name, type = nil, options = {}) ⇒ Boolean
Checks to see if a column exists in a given table.
- #ensure_index(table_name, column_name, options = {}) ⇒ Object
- #ensure_table(table_name) {|EnsuredTable.new(table_name, self)| ... } ⇒ Object
-
#new_index_exists?(table_name, column_name, options = {}) ⇒ Boolean
Checks to see if an index exists on a table for a given index definition.
- #table(table_name, options = {}, &block) ⇒ Object
Instance Method Details
#column_exists?(table_name, column_name, type = nil, options = {}) ⇒ Boolean
Checks to see if a column exists in a given table.
Examples
# Check a column exists
column_exists?(:suppliers, :name)
# Check a column exists of a particular type
column_exists?(:suppliers, :name, :string)
# Check a column exists with a specific definition
column_exists?(:suppliers, :name, :string, :limit => 100)
39 40 41 42 43 44 45 46 |
# File 'lib/ensured_schema/schema_statements.rb', line 39 def column_exists?(table_name, column_name, type = nil, = {}) #debugger columns(table_name).any?{ |c| c.name == column_name.to_s && (!type || c.type.to_s == type.to_s) && (![:limit] || c.limit_exists?([:limit])) && (![:precision] || c.precision == [:precision]) && (![:scale] || c.scale == [:scale]) } end |
#ensure_index(table_name, column_name, options = {}) ⇒ Object
60 61 62 63 64 |
# File 'lib/ensured_schema/schema_statements.rb', line 60 def ensure_index(table_name, column_name, = {}) unless new_index_exists?(table_name, column_name, ) add_index(table_name, column_name, ) end end |
#ensure_table(table_name) {|EnsuredTable.new(table_name, self)| ... } ⇒ Object
56 57 58 |
# File 'lib/ensured_schema/schema_statements.rb', line 56 def ensure_table(table_name) yield EnsuredTable.new(table_name, self) end |
#new_index_exists?(table_name, column_name, options = {}) ⇒ Boolean
Checks to see if an index exists on a table for a given index definition
Examples
# Check an index exists
index_exists?(:suppliers, :company_id)
# Check an index on multiple columns exists
index_exists?(:suppliers, [:company_id, :company_type])
# Check a unique index exists
index_exists?(:suppliers, :company_id, :unique => true)
# Check an index with a custom name exists
index_exists?(:suppliers, :company_id, :name => "idx_company_id"
18 19 20 21 22 23 24 25 26 |
# File 'lib/ensured_schema/schema_statements.rb', line 18 def new_index_exists?(table_name, column_name, = {}) # Don't overwrite existing index_exists? column_names = Array.wrap(column_name) index_name = .key?(:name) ? [:name].to_s : index_name(table_name, :column => column_names) if [:unique] indexes(table_name).any?{ |i| i.unique && i.name == index_name } else indexes(table_name).any?{ |i| i.name == index_name } end end |
#table(table_name, options = {}, &block) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/ensured_schema/schema_statements.rb', line 48 def table(table_name, = {}, &block) if table_exists?(table_name) ensure_table(table_name, &block) # what to do about changing table options else create_table(table_name, , &block) end end |