Class: ActiveRecord::ConnectionAdapters::SchemaCreation
Do not use. This class is for internal use only.
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Inherits: | Object |
Defined in: | activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb |
Class Method Summary
- .new(conn) ⇒ SchemaCreation constructor
Instance Attribute Summary
- #quote_column_name readonly
- #quote_default_expression readonly
- #quote_table_name readonly
- #quoted_columns_for_index readonly
- #type_to_sql readonly
- #supports_index_using? ⇒ Boolean readonly private
Instance Method Summary
- #accept(o)
- #options_include_default? ⇒ Boolean
- #supports_check_constraints? ⇒ Boolean
- #supports_exclusion_constraints? ⇒ Boolean
- #supports_index_include? ⇒ Boolean
- #supports_indexes_in_create? ⇒ Boolean
- #supports_nulls_not_distinct? ⇒ Boolean
- #supports_partial_index? ⇒ Boolean
- #supports_unique_constraints? ⇒ Boolean
- #use_foreign_keys? ⇒ Boolean
- #action_sql(action, dependency) private
- #add_column_options!(sql, options) private
- #add_table_options!(create_sql, o) private
- #column_options(o) private
- #quoted_columns(o) private
-
#table_modifier_in_create(o)
private
Returns any SQL string to go between CREATE and TABLE.
- #to_sql(sql) private
- #visit_AddCheckConstraint(o) private
- #visit_AddColumnDefinition(o) private
- #visit_AddForeignKey(o) private
- #visit_AlterTable(o) private
- #visit_CheckConstraintDefinition(o) private
- #visit_ColumnDefinition(o) private
- #visit_CreateIndexDefinition(o) private
-
#visit_DropCheckConstraint(name)
private
Alias for #visit_DropConstraint.
- #visit_DropConstraint(name) (also: #visit_DropForeignKey, #visit_DropCheckConstraint) private
-
#visit_DropForeignKey(name)
private
Alias for #visit_DropConstraint.
- #visit_ForeignKeyDefinition(o) private
- #visit_PrimaryKeyDefinition(o) private
- #visit_TableDefinition(o) private
Constructor Details
.new(conn) ⇒ SchemaCreation
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 6
def initialize(conn) @conn = conn @cache = {} end
Instance Attribute Details
#quote_column_name (readonly)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 16
delegate :quote_column_name, :quote_table_name, :quote_default_expression, :type_to_sql, :, :supports_indexes_in_create?, :use_foreign_keys?, :quoted_columns_for_index, :supports_partial_index?, :supports_check_constraints?, :supports_index_include?, :supports_exclusion_constraints?, :supports_unique_constraints?, :supports_nulls_not_distinct?, to: :@conn, private: true
#quote_default_expression (readonly)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 16
delegate :quote_column_name, :quote_table_name, :quote_default_expression, :type_to_sql, :, :supports_indexes_in_create?, :use_foreign_keys?, :quoted_columns_for_index, :supports_partial_index?, :supports_check_constraints?, :supports_index_include?, :supports_exclusion_constraints?, :supports_unique_constraints?, :supports_nulls_not_distinct?, to: :@conn, private: true
#quote_table_name (readonly)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 16
delegate :quote_column_name, :quote_table_name, :quote_default_expression, :type_to_sql, :, :supports_indexes_in_create?, :use_foreign_keys?, :quoted_columns_for_index, :supports_partial_index?, :supports_check_constraints?, :supports_index_include?, :supports_exclusion_constraints?, :supports_unique_constraints?, :supports_nulls_not_distinct?, to: :@conn, private: true
#quoted_columns_for_index (readonly)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 16
delegate :quote_column_name, :quote_table_name, :quote_default_expression, :type_to_sql, :, :supports_indexes_in_create?, :use_foreign_keys?, :quoted_columns_for_index, :supports_partial_index?, :supports_check_constraints?, :supports_index_include?, :supports_exclusion_constraints?, :supports_unique_constraints?, :supports_nulls_not_distinct?, to: :@conn, private: true
#supports_index_using? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 137
def supports_index_using? true end
#type_to_sql (readonly)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 16
delegate :quote_column_name, :quote_table_name, :quote_default_expression, :type_to_sql, :, :supports_indexes_in_create?, :use_foreign_keys?, :quoted_columns_for_index, :supports_partial_index?, :supports_check_constraints?, :supports_index_include?, :supports_exclusion_constraints?, :supports_unique_constraints?, :supports_nulls_not_distinct?, to: :@conn, private: true
Instance Method Details
#accept(o)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 11
def accept(o) m = @cache[o.class] ||= "visit_#{o.class.name.split('::').last}" send m, o end
#action_sql(action, dependency) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 175
def action_sql(action, dependency) case dependency when :nullify then "ON #{action} SET NULL" when :cascade then "ON #{action} CASCADE" when :restrict then "ON #{action} RESTRICT" else raise ArgumentError, <<~MSG '#{dependency}' is not supported for :on_update or :on_delete. Supported values are: :nullify, :cascade, :restrict MSG end end
#add_column_options!(sql, options) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 150
def (sql, ) sql << " DEFAULT #{quote_default_expression( [:default], [:column])}" if ( ) # must explicitly check for :null to allow change_column to work on migrations if [:null] == false sql << " NOT NULL" end if [:auto_increment] == true sql << " AUTO_INCREMENT" end if [:primary_key] == true sql << " PRIMARY KEY" end sql end
#add_table_options!(create_sql, o) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 141
def (create_sql, o) create_sql << " #{o.}" if o. create_sql end
#column_options(o) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 146
def (o) o. .merge(column: o) end
#options_include_default? ⇒ Boolean
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 16
delegate :quote_column_name, :quote_table_name, :quote_default_expression, :type_to_sql, :, :supports_indexes_in_create?, :use_foreign_keys?, :quoted_columns_for_index, :supports_partial_index?, :supports_check_constraints?, :supports_index_include?, :supports_exclusion_constraints?, :supports_unique_constraints?, :supports_nulls_not_distinct?, to: :@conn, private: true
#quoted_columns(o) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 133
def quoted_columns(o) String === o.columns ? o.columns : quoted_columns_for_index(o.columns, o. ) end
#supports_check_constraints? ⇒ Boolean
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 16
delegate :quote_column_name, :quote_table_name, :quote_default_expression, :type_to_sql, :, :supports_indexes_in_create?, :use_foreign_keys?, :quoted_columns_for_index, :supports_partial_index?, :supports_check_constraints?, :supports_index_include?, :supports_exclusion_constraints?, :supports_unique_constraints?, :supports_nulls_not_distinct?, to: :@conn, private: true
#supports_exclusion_constraints? ⇒ Boolean
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 16
delegate :quote_column_name, :quote_table_name, :quote_default_expression, :type_to_sql, :, :supports_indexes_in_create?, :use_foreign_keys?, :quoted_columns_for_index, :supports_partial_index?, :supports_check_constraints?, :supports_index_include?, :supports_exclusion_constraints?, :supports_unique_constraints?, :supports_nulls_not_distinct?, to: :@conn, private: true
#supports_index_include? ⇒ Boolean
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 16
delegate :quote_column_name, :quote_table_name, :quote_default_expression, :type_to_sql, :, :supports_indexes_in_create?, :use_foreign_keys?, :quoted_columns_for_index, :supports_partial_index?, :supports_check_constraints?, :supports_index_include?, :supports_exclusion_constraints?, :supports_unique_constraints?, :supports_nulls_not_distinct?, to: :@conn, private: true
#supports_indexes_in_create? ⇒ Boolean
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 16
delegate :quote_column_name, :quote_table_name, :quote_default_expression, :type_to_sql, :, :supports_indexes_in_create?, :use_foreign_keys?, :quoted_columns_for_index, :supports_partial_index?, :supports_check_constraints?, :supports_index_include?, :supports_exclusion_constraints?, :supports_unique_constraints?, :supports_nulls_not_distinct?, to: :@conn, private: true
#supports_nulls_not_distinct? ⇒ Boolean
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 16
delegate :quote_column_name, :quote_table_name, :quote_default_expression, :type_to_sql, :, :supports_indexes_in_create?, :use_foreign_keys?, :quoted_columns_for_index, :supports_partial_index?, :supports_check_constraints?, :supports_index_include?, :supports_exclusion_constraints?, :supports_unique_constraints?, :supports_nulls_not_distinct?, to: :@conn, private: true
#supports_partial_index? ⇒ Boolean
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 16
delegate :quote_column_name, :quote_table_name, :quote_default_expression, :type_to_sql, :, :supports_indexes_in_create?, :use_foreign_keys?, :quoted_columns_for_index, :supports_partial_index?, :supports_check_constraints?, :supports_index_include?, :supports_exclusion_constraints?, :supports_unique_constraints?, :supports_nulls_not_distinct?, to: :@conn, private: true
#supports_unique_constraints? ⇒ Boolean
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 16
delegate :quote_column_name, :quote_table_name, :quote_default_expression, :type_to_sql, :, :supports_indexes_in_create?, :use_foreign_keys?, :quoted_columns_for_index, :supports_partial_index?, :supports_check_constraints?, :supports_index_include?, :supports_exclusion_constraints?, :supports_unique_constraints?, :supports_nulls_not_distinct?, to: :@conn, private: true
#table_modifier_in_create(o) (private)
Returns any SQL string to go between CREATE and TABLE. May be nil.
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 171
def table_modifier_in_create(o) " TEMPORARY" if o.temporary end
#to_sql(sql) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 165
def to_sql(sql) sql = sql.to_sql if sql.respond_to?(:to_sql) sql end
#use_foreign_keys? ⇒ Boolean
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 16
delegate :quote_column_name, :quote_table_name, :quote_default_expression, :type_to_sql, :, :supports_indexes_in_create?, :use_foreign_keys?, :quoted_columns_for_index, :supports_partial_index?, :supports_check_constraints?, :supports_index_include?, :supports_exclusion_constraints?, :supports_unique_constraints?, :supports_nulls_not_distinct?, to: :@conn, private: true
#visit_AddCheckConstraint(o) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 129
def visit_AddCheckConstraint(o) "ADD #{accept(o)}" end
#visit_AddColumnDefinition(o) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 41
def visit_AddColumnDefinition(o) +"ADD #{accept(o.column)}" end
#visit_AddForeignKey(o) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 96
def visit_AddForeignKey(o) "ADD #{accept(o)}" end
#visit_AlterTable(o) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 24
def visit_AlterTable(o) sql = +"ALTER TABLE #{quote_table_name(o.name)} " sql << o.adds.map { |col| accept col }.join(" ") sql << o.foreign_key_adds.map { |fk| visit_AddForeignKey fk }.join(" ") sql << o.foreign_key_drops.map { |fk| visit_DropForeignKey fk }.join(" ") sql << o.check_constraint_adds.map { |con| visit_AddCheckConstraint con }.join(" ") sql << o.check_constraint_drops.map { |con| visit_DropCheckConstraint con }.join(" ") sql << o.constraint_drops.map { |con| visit_DropConstraint con }.join(" ") end
#visit_CheckConstraintDefinition(o) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 125
def visit_CheckConstraintDefinition(o) "CONSTRAINT #{o.name} CHECK (#{o.expression})" end
#visit_ColumnDefinition(o) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 34
def visit_ColumnDefinition(o) o.sql_type = type_to_sql(o.type, **o. ) column_sql = +"#{quote_column_name(o.name)} #{o.sql_type}" (column_sql, (o)) unless o.type == :primary_key column_sql end
#visit_CreateIndexDefinition(o) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 106
def visit_CreateIndexDefinition(o) index = o.index sql = ["CREATE"] sql << "UNIQUE" if index.unique sql << "INDEX" sql << o.algorithm if o.algorithm sql << "IF NOT EXISTS" if o.if_not_exists sql << index.type if index.type sql << "#{quote_column_name(index.name)} ON #{quote_table_name(index.table)}" sql << "USING #{index.using}" if supports_index_using? && index.using sql << "(#{quoted_columns(index)})" sql << "INCLUDE (#{quoted_include_columns(index.include)})" if supports_index_include? && index.include sql << "NULLS NOT DISTINCT" if supports_nulls_not_distinct? && index.nulls_not_distinct sql << "WHERE #{index.where}" if supports_partial_index? && index.where sql.join(" ") end
#visit_DropCheckConstraint(name) (private)
Alias for #visit_DropConstraint.
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 104
alias :visit_DropCheckConstraint :visit_DropConstraint
#visit_DropConstraint(name) (private) Also known as: #visit_DropForeignKey, #visit_DropCheckConstraint
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 100
def visit_DropConstraint(name) "DROP CONSTRAINT #{quote_column_name(name)}" end
#visit_DropForeignKey(name) (private)
Alias for #visit_DropConstraint.
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 103
alias :visit_DropForeignKey :visit_DropConstraint
#visit_ForeignKeyDefinition(o) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 83
def visit_ForeignKeyDefinition(o) quoted_columns = Array(o.column).map { |c| quote_column_name(c) } quoted_primary_keys = Array(o.primary_key).map { |c| quote_column_name(c) } sql = +<<~SQL CONSTRAINT #{quote_column_name(o.name)} FOREIGN KEY (#{quoted_columns.join(", ")}) REFERENCES #{quote_table_name(o.to_table)} (#{quoted_primary_keys.join(", ")}) SQL sql << " #{action_sql('DELETE', o.on_delete)}" if o.on_delete sql << " #{action_sql('UPDATE', o.on_update)}" if o.on_update sql end
#visit_PrimaryKeyDefinition(o) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 79
def visit_PrimaryKeyDefinition(o) "PRIMARY KEY (#{o.name.map { |name| quote_column_name(name) }.join(', ')})" end
#visit_TableDefinition(o) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb', line 45
def visit_TableDefinition(o) create_sql = +"CREATE#{table_modifier_in_create(o)} TABLE " create_sql << "IF NOT EXISTS " if o.if_not_exists create_sql << "#{quote_table_name(o.name)} " statements = o.columns.map { |c| accept c } statements << accept(o.primary_keys) if o.primary_keys if supports_indexes_in_create? statements.concat(o.indexes.map { |column_name, | index_in_create(o.name, column_name, ) }) end if use_foreign_keys? statements.concat(o.foreign_keys.map { |fk| accept fk }) end if supports_check_constraints? statements.concat(o.check_constraints.map { |chk| accept chk }) end if supports_exclusion_constraints? statements.concat(o.exclusion_constraints.map { |exc| accept exc }) end if supports_unique_constraints? statements.concat(o.unique_constraints.map { |exc| accept exc }) end create_sql << "(#{statements.join(', ')})" if statements.present? (create_sql, o) create_sql << " AS #{to_sql(o.as)}" if o.as create_sql end