]> git.openstreetmap.org Git - rails.git/blob - lib/migrate.rb
Refactor some more amf_controller tests to use factories.
[rails.git] / lib / migrate.rb
1 module ActiveRecord
2   module ConnectionAdapters
3     module SchemaStatements
4       def add_index_options_with_columns(table_name, column_name, options = {})
5         columns = options.delete(:columns)
6         index_name, index_type, index_columns, index_options, algorithm, using = add_index_options_without_columns(table_name, column_name, options)
7         [index_name, index_type, columns || index_columns, index_options, algorithm, using]
8       end
9
10       alias_method_chain :add_index_options, :columns
11     end
12
13     module PostgreSQL
14       module Quoting
15         def quote_column_name_with_arrays(name)
16           Array(name).map { |n| quote_column_name_without_arrays(n) }.join(", ")
17         end
18
19         alias_method_chain :quote_column_name, :arrays
20       end
21
22       module SchemaStatements
23         def add_primary_key(table_name, column_name, _options = {})
24           execute "ALTER TABLE #{quote_table_name(table_name)} ADD PRIMARY KEY (#{quote_column_name(column_name)})"
25         end
26
27         def remove_primary_key(table_name)
28           execute "ALTER TABLE #{quote_table_name(table_name)} DROP PRIMARY KEY"
29         end
30
31         def alter_primary_key(table_name, new_columns)
32           execute "ALTER TABLE #{quote_table_name(table_name)} DROP CONSTRAINT #{quote_table_name(table_name + '_pkey')}"
33           execute "ALTER TABLE #{quote_table_name(table_name)} ADD PRIMARY KEY (#{quote_column_name(new_columns)})"
34         end
35
36         def create_enumeration(enumeration_name, values)
37           execute "CREATE TYPE #{enumeration_name} AS ENUM ('#{values.join '\',\''}')"
38         end
39
40         def drop_enumeration(enumeration_name)
41           execute "DROP TYPE #{enumeration_name}"
42         end
43
44         def rename_enumeration(old_name, new_name)
45           execute "ALTER TYPE #{quote_table_name(old_name)} RENAME TO #{quote_table_name(new_name)}"
46         end
47       end
48     end
49   end
50 end