To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / .svn / pristine / 99 / 99c710840ac1685d172c3ffd9c93f67d6b1292b0.svn-base @ 1297:0a574315af3e
History | View | Annotate | Download (5.29 KB)
| 1 |
# Contains the enhancements to Rails' migrations system to support the |
|---|---|
| 2 |
# Engines::Plugin::Migrator. See Engines::RailsExtensions::Migrations for more |
| 3 |
# information. |
| 4 |
|
| 5 |
require "engines/plugin/migrator" |
| 6 |
|
| 7 |
# = Plugins and Migrations: Background |
| 8 |
# |
| 9 |
# Rails uses migrations to describe changes to the databases as your application |
| 10 |
# evolves. Each change to your application - adding and removing models, most |
| 11 |
# commonly - might require tweaks to your schema in the form of new tables, or new |
| 12 |
# columns on existing tables, or possibly the removal of tables or columns. Migrations |
| 13 |
# can even include arbitrary code to *transform* data as the underlying schema |
| 14 |
# changes. |
| 15 |
# |
| 16 |
# The point is that at any particular stage in your application's development, |
| 17 |
# migrations serve to transform the database into a state where it is compatible |
| 18 |
# and appropriate at that time. |
| 19 |
# |
| 20 |
# == What about plugins? |
| 21 |
# |
| 22 |
# If you want to share models using plugins, chances are that you might also |
| 23 |
# want to include the corresponding migrations to create tables for those models. |
| 24 |
# With the engines plugin installed, plugins can carry migration data easily: |
| 25 |
# |
| 26 |
# vendor/ |
| 27 |
# | |
| 28 |
# plugins/ |
| 29 |
# | |
| 30 |
# my_plugin/ |
| 31 |
# |- init.rb |
| 32 |
# |- lib/ |
| 33 |
# |- db/ |
| 34 |
# |-migrate/ |
| 35 |
# |- 20081105123419_add_some_new_feature.rb |
| 36 |
# |- 20081107144959_and_something_else.rb |
| 37 |
# |- ... |
| 38 |
# |
| 39 |
# When you install a plugin which contains migrations, you are undertaking a |
| 40 |
# further step in the development of your application, the same as the addition |
| 41 |
# of any other code. With this in mind, you may want to 'roll back' the |
| 42 |
# installation of this plugin at some point, and the database should be able |
| 43 |
# to migrate back to the point without this plugin in it too. |
| 44 |
# |
| 45 |
# == An example |
| 46 |
# |
| 47 |
# For example, our current application is at version 20081106164503 (according to the |
| 48 |
# +schema_migrations+ table), when we decide that we want to add a tagging plugin. The |
| 49 |
# tagging plugin chosen includes migrations to create the tables it requires |
| 50 |
# (say, _tags_ and _taggings_, for instance), along with the models and helpers |
| 51 |
# one might expect. |
| 52 |
# |
| 53 |
# After installing this plugin, these tables should be created in our database. |
| 54 |
# Rather than running the migrations directly from the plugin, they should be |
| 55 |
# integrated into our main migration stream in order to accurately reflect the |
| 56 |
# state of our application's database *at this moment in time*. |
| 57 |
# |
| 58 |
# $ script/generate plugin_migration |
| 59 |
# exists db/migrate |
| 60 |
# create db/migrate/20081108120415_my_plugin_to_version_20081107144959.rb |
| 61 |
# |
| 62 |
# This migration will take our application to version 20081108120415, and contains the |
| 63 |
# following, typical migration code: |
| 64 |
# |
| 65 |
# class TaggingToVersion20081107144959 < ActiveRecord::Migration |
| 66 |
# def self.up |
| 67 |
# Engines.plugins[:tagging].migrate(20081107144959) |
| 68 |
# end |
| 69 |
# def self.down |
| 70 |
# Engines.plugins[:tagging].migrate(0) |
| 71 |
# end |
| 72 |
# end |
| 73 |
# |
| 74 |
# When we migrate our application up, using <tt>rake db:migrate</tt> as normal, |
| 75 |
# the plugin will be migrated up to its latest version (20081108120415 in this example). If we |
| 76 |
# ever decide to migrate the application back to the state it was in at version 20081106164503, |
| 77 |
# the plugin migrations will be taken back down to version 0 (which, typically, |
| 78 |
# would remove all tables the plugin migrations define). |
| 79 |
# |
| 80 |
# == Upgrading plugins |
| 81 |
# |
| 82 |
# It might happen that later in an application's life, we update to a new version of |
| 83 |
# the tagging plugin which requires some changes to our database. The tagging plugin |
| 84 |
# provides these changes in the form of its own migrations. |
| 85 |
# |
| 86 |
# In this case, we just need to re-run the plugin_migration generator to create a |
| 87 |
# new migration from the current revision to the newest one: |
| 88 |
# |
| 89 |
# $ script/generate plugin_migration |
| 90 |
# exists db/migrate |
| 91 |
# create db/migrate/20081210131437_tagging_to_version_20081201172034.rb |
| 92 |
# |
| 93 |
# The contents of this migration are: |
| 94 |
# |
| 95 |
# class TaggingToVersion20081108120415 < ActiveRecord::Migration |
| 96 |
# def self.up |
| 97 |
# Engines.plugins[:tagging].migrate(20081201172034) |
| 98 |
# end |
| 99 |
# def self.down |
| 100 |
# Engines.plugins[:tagging].migrate(20081107144959) |
| 101 |
# end |
| 102 |
# end |
| 103 |
# |
| 104 |
# Notice that if we were to migrate down to revision 20081108120415 or lower, the tagging plugin |
| 105 |
# will be migrated back down to version 20081107144959 - the version we were previously at. |
| 106 |
# |
| 107 |
# |
| 108 |
# = Creating migrations in plugins |
| 109 |
# |
| 110 |
# In order to use the plugin migration functionality that engines provides, a plugin |
| 111 |
# only needs to provide regular migrations in a <tt>db/migrate</tt> folder within it. |
| 112 |
# |
| 113 |
# = Explicitly migrating plugins |
| 114 |
# |
| 115 |
# It's possible to migrate plugins within your own migrations, or any other code. |
| 116 |
# Simply get the Plugin instance, and its Plugin#migrate method with the version |
| 117 |
# you wish to end up at: |
| 118 |
# |
| 119 |
# Engines.plugins[:whatever].migrate(version) |
| 120 |
# |
| 121 |
# |
| 122 |
# = Upgrading from previous versions of the engines plugin |
| 123 |
# |
| 124 |
# Thanks to the tireless work of the plugin developer community, we can now relying on the migration |
| 125 |
# mechanism in Rails 2.1+ to do much of the plugin migration work for us. This also means that we |
| 126 |
# don't need a seperate schema_info table for plugins. |
| 127 |
# |
| 128 |
# To update your application, run |
| 129 |
# |
| 130 |
# rake db:migrate:upgrade_plugin_migrations |
| 131 |
# |
| 132 |
# This will ensure that migration information is carried over into the main schema_migrations table. |
| 133 |
# |