Module: SchemaPlus::ActiveRecord::ConnectionAdapters::AbstractAdapter
- Defined in:
- lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb
Overview
SchemaPlus adds several methods to ActiveRecord::ConnectionAdapters::AbstractAdapter. In most cases you don’t call these directly, but rather the methods that define things are called by schema statements, and methods that query things are called by ActiveRecord::Base.
Defined Under Namespace
Modules: AddColumnOptions, VisitTableDefinition
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#_build_foreign_key(table_name, column_names, references_table_name, references_column_names, options = {}) ⇒ Object
:nodoc:.
-
#add_foreign_key(table_name, column_names, references_table_name, references_column_names, options = {}) ⇒ Object
Define a foreign key constraint.
-
#add_foreign_key_sql(table_name, column_names, references_table_name, references_column_names, options = {}) ⇒ Object
called directly by AT’s bulk_change_table, for migration change_table :name, :bulk => true { … }.
-
#create_view(view_name, definition, options = {}) ⇒ Object
Create a view given the SQL definition.
-
#drop_table(name, options = {}) ⇒ Object
Extends rails’ drop_table to include these options: :cascade :if_exists.
-
#drop_view(view_name) ⇒ Object
Drop the named view.
-
#foreign_keys(table_name, name = nil) ⇒ Object
(abstract) Return the ForeignKeyDefinition objects for foreign key constraints defined on this table.
-
#initialize_with_schema_plus(*args) ⇒ Object
:nodoc:.
-
#remove_foreign_key(table_name, *args) ⇒ Object
Remove a foreign key constraint.
- #remove_foreign_key_sql(table_name, *args) ⇒ Object
-
#remove_index_with_schema_plus(table_name, *args) ⇒ Object
Extends rails’ remove_index to include this options: :if_exists.
-
#rename_indexes_and_foreign_keys(oldname, newname) ⇒ Object
called from individual adpaters, after renaming table from old name to.
-
#reverse_foreign_keys(table_name, name = nil) ⇒ Object
(abstract) Return the ForeignKeyDefinition objects for foreign key constraints defined on other tables that reference this table.
-
#supports_partial_indexes? ⇒ Boolean
Returns true if the database supports parital indexes (abstract; only Postgresql returns true).
-
#view_definition(view_name, name = nil) ⇒ Object
(abstract) Returns the SQL definition of a given view.
-
#views(name = nil) ⇒ Object
(abstract) Returns the names of all views, as an array of strings.
Class Method Details
.included(base) ⇒ Object
:nodoc:
14 15 16 17 |
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 14 def self.included(base) #:nodoc: base.alias_method_chain :initialize, :schema_plus base.alias_method_chain :remove_index, :schema_plus end |
Instance Method Details
#_build_foreign_key(table_name, column_names, references_table_name, references_column_names, options = {}) ⇒ Object
:nodoc:
80 81 82 |
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 80 def _build_foreign_key(table_name, column_names, references_table_name, references_column_names, = {}) #:nodoc: ForeignKeyDefinition.new([:name] || ForeignKeyDefinition.default_name(table_name, column_names), table_name, column_names, ::ActiveRecord::Migrator.proper_table_name(references_table_name), references_column_names, [:on_update], [:on_delete], [:deferrable]) end |
#add_foreign_key(table_name, column_names, references_table_name, references_column_names, options = {}) ⇒ Object
Define a foreign key constraint. Valid options are :on_update, :on_delete, and :deferrable, with values as described at ConnectionAdapters::ForeignKeyDefinition
(NOTE: Sqlite3 does not support altering a table to add foreign-key constraints; they must be included in the table specification when it’s created. If you’re using Sqlite3, this method will raise an error.)
68 69 70 71 |
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 68 def add_foreign_key(table_name, column_names, references_table_name, references_column_names, = {}) foreign_key_sql = add_foreign_key_sql(table_name, column_names, references_table_name, references_column_names, ) execute "ALTER TABLE #{quote_table_name(table_name)} #{foreign_key_sql}" end |
#add_foreign_key_sql(table_name, column_names, references_table_name, references_column_names, options = {}) ⇒ Object
called directly by AT’s bulk_change_table, for migration change_table :name, :bulk => true { … }
75 76 77 78 |
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 75 def add_foreign_key_sql(table_name, column_names, references_table_name, references_column_names, = {}) #:nodoc: foreign_key = _build_foreign_key(table_name, column_names, references_table_name, references_column_names, ) "ADD #{foreign_key.to_sql}" end |
#create_view(view_name, definition, options = {}) ⇒ Object
Create a view given the SQL definition. Specify :force => true to first drop the view if it already exists.
48 49 50 51 52 |
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 48 def create_view(view_name, definition, ={}) definition = definition.to_sql if definition.respond_to? :to_sql execute "DROP VIEW IF EXISTS #{quote_table_name(view_name)}" if [:force] execute "CREATE VIEW #{quote_table_name(view_name)} AS #{definition}" end |
#drop_table(name, options = {}) ⇒ Object
Extends rails’ drop_table to include these options:
:cascade
:if_exists
125 126 127 128 129 130 131 |
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 125 def drop_table(name, = {}) sql = "DROP TABLE" sql += " IF EXISTS" if [:if_exists] sql += " #{quote_table_name(name)}" sql += " CASCADE" if [:cascade] execute sql end |
#drop_view(view_name) ⇒ Object
Drop the named view
55 56 57 |
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 55 def drop_view(view_name) execute "DROP VIEW #{quote_table_name(view_name)}" end |
#foreign_keys(table_name, name = nil) ⇒ Object
(abstract) Return the ForeignKeyDefinition objects for foreign key constraints defined on this table
261 |
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 261 def foreign_keys(table_name, name = nil) raise "Internal Error: Connection adapter didn't override abstract function"; [] end |
#initialize_with_schema_plus(*args) ⇒ Object
:nodoc:
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 19 def initialize_with_schema_plus(*args) #:nodoc: initialize_without_schema_plus(*args) adapter = case adapter_name # name of MySQL adapter depends on mysql gem # * with mysql gem adapter is named MySQL # * with mysql2 gem adapter is named Mysql2 # Here we handle this and hopefully futher adapter names when /^MySQL/i then 'MysqlAdapter' when 'PostgreSQL', 'PostGIS' then 'PostgresqlAdapter' when 'SQLite' then 'Sqlite3Adapter' end unless adapter ::ActiveRecord::Base.logger.warn "SchemaPlus: Unsupported adapter name #{adapter_name.inspect}. Leaving it alone." return end adapter_module = SchemaPlus::ActiveRecord::ConnectionAdapters.const_get(adapter) self.class.send(:include, adapter_module) unless self.class.include?(adapter_module) if "#{::ActiveRecord::VERSION::MAJOR}.#{::ActiveRecord::VERSION::MINOR}".to_r >= "4.1".to_r self.class.const_get(:SchemaCreation).send(:include, adapter_module.const_get(:AddColumnOptions)) else self.class.send(:include, adapter_module.const_get(:AddColumnOptions)) end extend(SchemaPlus::ActiveRecord::ForeignKeys) end |
#remove_foreign_key(table_name, *args) ⇒ Object
Remove a foreign key constraint
Arguments are the same as for add_foreign_key, or by name:
remove_foreign_key table_name, column_names, references_table_name, references_column_names
remove_foreign_key name: constraint_name
(NOTE: Sqlite3 does not support altering a table to remove foreign-key constraints. If you’re using Sqlite3, this method will raise an error.)
94 95 96 97 98 |
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 94 def remove_foreign_key(table_name, *args) case sql = remove_foreign_key_sql(table_name, *args) when String then execute "ALTER TABLE #{quote_table_name(table_name)} #{sql}" end end |
#remove_foreign_key_sql(table_name, *args) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 100 def remove_foreign_key_sql(table_name, *args) column_names, references_table_name, references_column_names, = args ||= {} foreign_key_name = case when args.length == 1 case args[0] when Hash then args[0][:name] else args[0] end else test_fk = _build_foreign_key(table_name, column_names, references_table_name, references_column_names, ) if foreign_keys(table_name).detect { |fk| fk == test_fk } test_fk.name else raise "SchemaPlus: no foreign key constraint found on #{table_name.inspect} matching #{args.inspect}" unless [:if_exists] nil end end foreign_key_name ? "DROP CONSTRAINT #{foreign_key_name}" : [] # hack -- return empty array rather than nil, so that result will disappear when caller flattens but doesn't compact end |
#remove_index_with_schema_plus(table_name, *args) ⇒ Object
Extends rails’ remove_index to include this options:
:if_exists
135 136 137 138 139 140 141 142 |
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 135 def remove_index_with_schema_plus(table_name, *args) = args. if_exists = .delete(:if_exists) .delete(:column) if [:name] and ::ActiveRecord::VERSION::MAJOR < 4 args << if .any? return if if_exists and not index_name_exists?(table_name, [:name] || index_name(table_name, *args), false) remove_index_without_schema_plus(table_name, *args) end |
#rename_indexes_and_foreign_keys(oldname, newname) ⇒ Object
called from individual adpaters, after renaming table from old name to
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 146 def rename_indexes_and_foreign_keys(oldname, newname) #:nodoc: indexes(newname).select{|index| index.name == index_name(oldname, index.columns)}.each do |index| rename_index(newname, index.name, index_name(newname, index.columns)) end foreign_keys(newname).each do |fk| index = indexes(newname).find{|index| index.name == ForeignKeyDefinition.auto_index_name(oldname, fk.column_names)} begin remove_foreign_key(newname, fk.name) rescue NotImplementedError # sqlite3 can't remove foreign keys, so just skip it end # rename the index only when the fk constraint doesn't exist. # mysql doesn't allow the rename (which is a delete & add) # if the index is on a foreign key constraint rename_index(newname, index.name, ForeignKeyDefinition.auto_index_name(newname, index.columns)) if index begin add_foreign_key(newname, fk.column_names, fk.references_table_name, fk.references_column_names, :name => fk.name.sub(/#{oldname}/, newname), :on_update => fk.on_update, :on_delete => fk.on_delete, :deferrable => fk.deferrable) rescue NotImplementedError # sqlite3 can't add foreign keys, so just skip it end end end |
#reverse_foreign_keys(table_name, name = nil) ⇒ Object
(abstract) Return the ForeignKeyDefinition objects for foreign key constraints defined on other tables that reference this table
265 |
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 265 def reverse_foreign_keys(table_name, name = nil) raise "Internal Error: Connection adapter didn't override abstract function"; [] end |
#supports_partial_indexes? ⇒ Boolean
Returns true if the database supports parital indexes (abstract; only Postgresql returns true)
171 172 173 |
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 171 def supports_partial_indexes? false end |
#view_definition(view_name, name = nil) ⇒ Object
(abstract) Returns the SQL definition of a given view. This is the literal SQL would come after ‘CREATVE VIEW viewname AS ’ in the SQL statement to create a view.
257 |
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 257 def view_definition(view_name, name = nil) raise "Internal Error: Connection adapter didn't override abstract function"; end |
#views(name = nil) ⇒ Object
(abstract) Returns the names of all views, as an array of strings
252 |
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 252 def views(name = nil) raise "Internal Error: Connection adapter didn't override abstract function"; [] end |