annotate .svn/pristine/99/99c710840ac1685d172c3ffd9c93f67d6b1292b0.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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