Rails therefore offers conventions for handling routing, stateful data, asset management, and more to provide the baseline functionality that most web applications need. It takes an opinionated approach to application development, assuming that set conventions best serve developers where there is a common goal. If you'd prefer to use numeric prefixes, you can turn timestamped migrations off by setting: config.active_record.Rails is a web application framework written in Ruby. The prefix is a generation timestamp (in UTC). Timestamped Migrationsīy default, Rails generates migrations that look like: 20080717013526_your_migration_name.rb The phrase “Updating salaries…” would then be printed, along with the benchmark for the block when the block completes. You can also insert your own messages and benchmarks by using the say_with_time method: def up You can quiet them down by setting ActiveRecord::Migration.verbose = false. P.update_attribute :salary, Salar圜pute(p)īy default, migrations will describe the actions they are taking, writing them to the console as they happen, along with benchmarks describing how long each step took. ![]() Example: class AddPeopleSalary < ActiveRecord::Migration In that case, you'll need to make a call to Base#reset_column_information in order to ensure that the model has the latest column data from after the new column was added. Sometimes you'll want to add a column in a migration and populate it immediately after. Remove_column :items, :completed_items_countĪdd_column :items, :incomplete_items_countĪdd_column :items, :completed_items_countĪnd sometimes you need to do something in SQL not abstracted directly by migrations: class MakeJoinUnique < ActiveRecord::MigrationĮxecute "ALTER TABLE `pages_linked_pages` ADD UNIQUE `page_id_linked_page_id` (`page_id`,`linked_page_id`)"Įxecute "ALTER TABLE `pages_linked_pages` DROP INDEX `page_id_linked_page_id`" Remove_column :items, :incomplete_items_count Others remove columns when they migrate up instead of down: class RemoveUnnecessaryItemAttributes < ActiveRecord::Migration Raise ActiveRecord::IrreversibleMigration, "Can't recover the deleted tags" # not much we can do to restore deleted data A default value can be specified by passing an options hash like The options hash is for fragments like “DEFAULT CHARSET=UTF-8” that are appended to the create table definition.Īdd_column(table_name, column_name, type, options): Adds a new column to the table called table_name named column_name specified to be one of the following types: :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean. See ActiveRecord::ConnectionAdapters::SchemaStatements#create_join_table for details.Ĭreate_table(name, options): Creates a table called name and makes the table object available to a block that can then add columns to it, following the same format as add_column. Available transformations CreationĬreate_join_table(table_1, table_2, options): Creates a join table having its name as the lexical order of the first two arguments. It also uses the more advanced create_table syntax where you can specify a complete table schema in one block call. This migration first adds the system_settings table, then creates the very first row in it using the Active Record model that relies on the table. These methods can consist of both the migration specific methods like add_column and remove_column, but may also contain regular Ruby code for generating data needed for the transformations.Įxample of a more complex migration that also needs to initialize data: class AddSystemSettings < ActiveRecord::Migration ![]() It shows how all migrations have two methods up and down that describes the transformations required to implement or remove the migration. This migration will add a boolean flag to the accounts table and remove it if you're backing out of the migration. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind.Įxample of a simple migration: class AddSsl < ActiveRecord::MigrationĪdd_column :accounts, :ssl_enabled, :boolean, default: true ![]() It's a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. Migrations can manage the evolution of a schema used by several physical databases.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |