# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb', line 439defadd_column_options!(sql,options)#:nodoc:
sql<<" DEFAULT #{quote(options[:default],options[:column])}"ifoptions_include_default?(options)# must explicitly check for :null to allow change_column to work on migrations
ifoptions[:null]==falsesql<<" NOT NULL"endend

Adds a new index to the table. column_name can be a single
Symbol, or an Array of Symbols.

The index will be named after the table and the first column name, unless
you pass :name as an option.

When creating an index on multiple columns, the first column is used as a
name for the index. For example, when you specify an index on two columns
[:first, :last], the DBMS creates an index for both
columns as well as an index for the first column :first. Using
just the first name for this index makes sense, because you will never have
to create a singular index with this name.

Examples

# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb', line 209defchange_column(table_name,column_name,type,options={})raiseNotImplementedError,"change_column is not implemented"end

Examples

# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb', line 219defchange_column_default(table_name,column_name,default)raiseNotImplementedError,"change_column_default is not implemented"end

Creates a new table with the name table_name. table_name
may either be a String or a Symbol.

There are two ways to work with create_table. You can use the
block form or the regular form, like this:

Block form

# create_table() passes a TableDefinition object to the block.
# This form will not only create the table, but also columns for the
# table.
create_table(:suppliers)do|t|t.column:name,:string,:limit=>60# Other fields here
end

Regular form

# Creates a table called 'suppliers' with no columns.
create_table(:suppliers)# Add a column to 'suppliers'.
add_column(:suppliers,:name,:string,{:limit=>60})

The options hash can include the following keys:

:id

Whether to automatically add a primary key column. Defaults to true. Join
tables for has_and_belongs_to_many should set :id =>
false.

:primary_key

The name of the primary key, if one is to be added automatically. Defaults
to id.

# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb', line 365definitialize_schema_migrations_tablesm_table=ActiveRecord::Migrator.schema_migrations_table_nameunlesstables.detect{|t|t==sm_table}create_table(sm_table,:id=>false)do|schema_migrations_table|schema_migrations_table.column:version,:string,:null=>falseendadd_indexsm_table,:version,:unique=>true,:name=>"#{Base.table_name_prefix}unique_schema_migrations#{Base.table_name_suffix}"# Backwards-compatibility: if we find schema_info, assume we've
# migrated up to that point:
si_table=Base.table_name_prefix+'schema_info'+Base.table_name_suffixiftables.detect{|t|t==si_table}old_version=select_value("SELECT version FROM #{quote_table_name(si_table)}").to_iassume_migrated_upto_version(old_version)drop_table(si_table)endendend

Remove the index named accounts_branch_id_index in the accounts table.

remove_index:accounts,:column=>:branch_id

Remove the index named accounts_branch_id_party_id_index in the accounts
table.

remove_index:accounts,:column=>[:branch_id,:party_id]

Remove the index named by_branch_party in the accounts table.

remove_index:accounts,:name=>:by_branch_party

304
305
306
307
308
309
310
311

# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb', line 304defremove_index(table_name,options={})index_name=index_name(table_name,options)unlessindex_exists?(table_name,index_name,true)@logger.warn("Index name '#{index_name}' on table '#{table_name}' does not exist. Skipping.")returnendremove_index!(table_name,index_name)end

Example

rename_column(:suppliers,:description,:name)

Raises:

(NotImplementedError)

226
227
228

# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb', line 226defrename_column(table_name,column_name,new_column_name)raiseNotImplementedError,"rename_column is not implemented"end

# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb', line 321defrename_index(table_name,old_name,new_name)# this is a naive implementation; some DBs may support this more efficiently (Postgres, for instance)
old_index_def=indexes(table_name).detect{|i|i.name==old_name}returnunlessold_index_defremove_index(table_name,:name=>old_name)add_index(table_name,old_index_def.columns,:name=>new_name,:unique=>old_index_def.unique)end