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