lib/arql/concerns/table_data_definition.rb in arql-0.3.31 vs lib/arql/concerns/table_data_definition.rb in arql-0.4.0

- old
+ new

@@ -89,21 +89,21 @@ # # # Defines a column with a database-specific type. # Shape.add_column(:triangle, 'polygon') # # ALTER TABLE "shapes" ADD "triangle" polygon def add_column(column_name, type, **options) - ActiveRecord::Base.connection.add_column(table_name, column_name, type, **options) + connection.add_column(table_name, column_name, type, **options) end # Changes the column's definition according to the new options. # See TableDefinition#column for details of the options you can use. # # Supplier.change_column(:name, :string, limit: 80) # Post.change_column(:description, :text) # def change_column(column_name, type, options = {}) - ActiveRecord::Base.connection.change_column(table_name, column_name, type, **options) + connection.change_column(table_name, column_name, type, **options) end # Removes the column from the table definition. # # Supplier.remove_column(:qualification) @@ -111,11 +111,11 @@ # The +type+ and +options+ parameters will be ignored if present. It can be helpful # to provide these in a migration's +change+ method so it can be reverted. # In that case, +type+ and +options+ will be used by #add_column. # Indexes on the column are automatically removed. def remove_column(column_name, type = nil, **options) - ActiveRecord::Base.connection.remove_column(table_name, column_name, type, **options) + connection.remove_column(table_name, column_name, type, **options) end # Adds a new index to the table. +column_name+ can be a single Symbol, or # an Array of Symbols. # @@ -227,11 +227,11 @@ # # Concurrently adding an index is not supported in a transaction. # # For more information see the {"Transactional Migrations" section}[rdoc-ref:Migration]. def add_index(column_name, options = {}) - ActiveRecord::Base.connection.add_index(table_name, column_name, **options) + connection.add_index(table_name, column_name, **options) end # Adds a new foreign key. # +to_table+ contains the referenced primary key. # @@ -275,30 +275,30 @@ # [<tt>:on_update</tt>] # Action that happens <tt>ON UPDATE</tt>. Valid values are +:nullify+, +:cascade+ and +:restrict+ # [<tt>:validate</tt>] # (PostgreSQL only) Specify whether or not the constraint should be validated. Defaults to +true+. def add_foreign_key(to_table, **options) - ActiveRecord::Base.connection.add_foreign_key(table_name, to_table, **options) + connection.add_foreign_key(table_name, to_table, **options) end # Adds timestamps (+created_at+ and +updated_at+) columns to this table. # Additional options (like +:null+) are forwarded to #add_column. # # Supplier.add_timestamps(null: true) # def add_timestamps(**options) - ActiveRecord::Base.connection.add_timestamps(table_name, **options) + connection.add_timestamps(table_name, **options) end # Changes the comment for a column or removes it if +nil+. # # Passing a hash containing +:from+ and +:to+ will make this change # reversible in migration: # # Post.change_column_comment(:state, from: "old_comment", to: "new_comment") def change_column_comment(column_name, comment_or_changes) - ActiveRecord::Base.connection.change_column_comment(table_name, column_name, comment_or_changes) + connection.change_column_comment(table_name, column_name, comment_or_changes) end # Sets a new default value for a column: # # Supplier.change_column_default(:qualification, 'new') @@ -312,11 +312,11 @@ # reversible in migration: # # Post.change_column_default(:state, from: nil, to: "draft") # def change_column_default(column_name, default_or_changes) - ActiveRecord::Base.connection.change_column_default(table_name, column_name, default_or_changes) + connection.change_column_default(table_name, column_name, default_or_changes) end # Sets or removes a <tt>NOT NULL</tt> constraint on a column. The +null+ flag # indicates whether the value can be +NULL+. For example # @@ -332,19 +332,19 @@ # <tt>NULL</tt>s with some other value. Use that one when enabling the # constraint if needed, since otherwise those rows would not be valid. # # Please note the fourth argument does not set a column's default. def change_column_null(column_name, null, default = nil) - ActiveRecord::Base.connection.change_column_null(table_name, column_name, null, default) + connection.change_column_null(table_name, column_name, null, default) end # Renames a column. # # Supplier.rename_column(:description, :name) # def rename_column(column_name, new_column_name) - ActiveRecord::Base.connection.rename_column(table_name, column_name, new_column_name) + connection.rename_column(table_name, column_name, new_column_name) end # A block for changing columns in +table+. # # # change_table() yields a Table instance @@ -416,29 +416,29 @@ # t.remove_index :company_id # end # # See also Table for details on all of the various column transformations. def change_table(**options) - ActiveRecord::Base.connection.change_table(table_name, **options) + connection.change_table(table_name, **options) end # Renames a table. # # rename_table('octopi') # def rename_table(new_name) - ActiveRecord::Base.connection.rename_table(table_name, new_name) + connection.rename_table(table_name, new_name) end # Changes the comment for a table or removes it if +nil+. # # Passing a hash containing +:from+ and +:to+ will make this change # reversible in migration: # # Post.change_table_comment(from: "old_comment", to: "new_comment") def change_table_comment(comment_or_changes) - ActiveRecord::Base.connection.change_table_comment(table_name, comment_or_changes) + connection.change_table_comment(table_name, comment_or_changes) end # Drops a table from the database. # # [<tt>:force</tt>] @@ -450,17 +450,17 @@ # # Although this command ignores most +options+ and the block if one is given, # it can be helpful to provide these in a migration's +change+ method so it can be reverted. # In that case, +options+ and the block will be used by #create_table. def drop_table(**options) - ActiveRecord::Base.connection.drop_table(table_name, **options) + connection.drop_table(table_name, **options) end # Returns an array of foreign keys for the given table. # The foreign keys are represented as ForeignKeyDefinition objects. def foreign_keys - ActiveRecord::Base.connection.foreign_keys(table_name) + connection.foreign_keys(table_name) end # Removes the given foreign key from the table. Any option parameters provided # will be used to re-add the foreign key in case of a migration rollback. # It is recommended that you provide any options used when creating the foreign @@ -485,11 +485,11 @@ # The +options+ hash accepts the same keys as SchemaStatements#add_foreign_key # with an addition of # [<tt>:to_table</tt>] # The name of the table that contains the referenced primary key. def remove_foreign_key(to_table = nil, **options) - ActiveRecord::Base.connection.remove_foreign_key(table_name, to_table, **options) + connection.remove_foreign_key(table_name, to_table, **options) end # Removes the given index from the table. # # Removes the index on +branch_id+ in the +accounts+ table if exactly one such index exists. @@ -516,33 +516,33 @@ # # Concurrently removing an index is not supported in a transaction. # # For more information see the {"Transactional Migrations" section}[rdoc-ref:Migration]. def remove_index(options = {}) - ActiveRecord::Base.connection.remove_index(table_name, **options) + connection.remove_index(table_name, **options) end # Removes the timestamp columns (+created_at+ and +updated_at+) from the table definition. # # Supplier.remove_timestamps # def remove_timestamps(**options) - ActiveRecord::Base.connection.remove_timestamps(**options) + connection.remove_timestamps(**options) end # Renames an index. # # Rename the +index_people_on_last_name+ index to +index_users_on_last_name+: # # Person.rename_index 'index_people_on_last_name', 'index_users_on_last_name' # def rename_index(old_name, new_name) - ActiveRecord::Base.connection.rename_index(table_name, old_name, new_name) + connection.rename_index(table_name, old_name, new_name) end # Returns the table comment that's stored in database metadata. def table_comment - ActiveRecord::Base.connection.table_comment(table_name) + connection.table_comment(table_name) end end end end