Hi, I''m wondering is there a way to load default data in the migration script? So, for example I''m creating new table to store the order status, I also want to pre-populate the table with some data from a sql file. I have done a quick search on the list but cannot find anything. thanks, - reynard -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060714/65c04ba8/attachment.html
well actually this is just as simple as executing a command in the up method `mysql -u dev -psecret mydb_dev < db/data.sql` On 7/14/06, Reynard Hilman <reynard.list@gmail.com> wrote:> > Hi, > I''m wondering is there a way to load default data in the migration script? > > So, for example I''m creating new table to store the order status, I also > want to pre-populate the table with some data from a sql file. > I have done a quick search on the list but cannot find anything. > thanks, > - reynard >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060714/eada0de9/attachment.html
You should actually consider using a fixture to do this so that it''s database-independent. There are plugins that will do this for you. On 7/14/06, Reynard Hilman <reynard.list@gmail.com> wrote:> > well actually this is just as simple as executing a command in the up > method > `mysql -u dev -psecret mydb_dev < db/data.sql` > > > On 7/14/06, Reynard Hilman <reynard.list@gmail.com> wrote: > > > > Hi, > > I''m wondering is there a way to load default data in the migration > > script? > > So, for example I''m creating new table to store the order status, I also > > want to pre-populate the table with some data from a sql file. > > I have done a quick search on the list but cannot find anything. > > thanks, > > - reynard > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060715/0bc951bb/attachment.html
On 7/15/06, Brian Hogan <bphogan@gmail.com> wrote:> > On 7/14/06, Reynard Hilman <reynard.list@gmail.com> wrote: > > well actually this is just as simple as executing a command in the up method > > `mysql -u dev -psecret mydb_dev < db/data.sql` > > You should actually consider using a fixture to do this so that it''s > database-independent. There are plugins that will do this for you.I agree. I made a mistake once of putting sample data into a migration instead of a fixture, and the first time I reloaded the fixtures with rake, all the data went away. The moral? If the data is sample, put it in fixtures. If the data is necessary for the site, put it in both. Sincerely, Tom Lieber http://AllTom.com/ http://GadgetLife.org/
The AR Fixture is a great plugin. Thanks for the suggestions. - reynard On 7/15/06, Tom Lieber <alltom@gmail.com> wrote:> > On 7/15/06, Brian Hogan <bphogan@gmail.com> wrote: > > > On 7/14/06, Reynard Hilman <reynard.list@gmail.com> wrote: > > > well actually this is just as simple as executing a command in the up > method > > > `mysql -u dev -psecret mydb_dev < db/data.sql` > > > > You should actually consider using a fixture to do this so that it''s > > database-independent. There are plugins that will do this for you. > > I agree. I made a mistake once of putting sample data into a migration > instead of a fixture, and the first time I reloaded the fixtures with > rake, all the data went away. The moral? If the data is sample, put it > in fixtures. If the data is necessary for the site, put it in both. > > Sincerely, > > Tom Lieber > http://AllTom.com/ > http://GadgetLife.org/ > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060715/920185b5/attachment-0001.html
Reynard List wrote:> The AR Fixture is a great plugin. > Thanks for the suggestions. > - reynardFor what it is worth, I recently created a small helper method to load initialization data from fixtures into new table migrations. The code resides in ./lib and is invoked in the migration using require migration_fixture class MyMigration < ActiveRecord::Migration extend MigrationFixture def self.up create_table :my_migrations do |t| t.column ... end load_migration_data :my_migrations end By default the migration data is considered to reside in "./db/migrate/fixtures/my_migrations.yml" but you can pass an optional directory parameter is you wish to have a different name or nested subdirectories. For example: load_migration_data "my_migations" "data/001_create_my_migrations" Here is the code: module MigrationFixtures # Load initialization data from migration def load_fixture_data(from_fixture_name, from_fixture_directory=nil) # This helper module takes the fixture name and location as # arguments and loads the data contained therein. To use from within # a migration add ''require "migration_fixtures"'' to the top of # the migration file (before the "class" extension of ActiveRecord # and add the line ''extend MigrationFixtures'' below the "class" # statement. You can then call the helper using: # # load_fixture_data(:fixture_name, :fixture_directory) # or # load_fixture_data(:fixture_name) # # The fixture name is the table name to be loaded. The # fixture directory is relative to the migration directory and, # if absent, the default ./fixtures is used. The fixture file # always takes the form "table_name.yml" # if from_fixture_directory != nil then @mf_dir = File.join("../db/migrate", from_fixture_directory.to_s) else @mf_dir = "../db/migrate/fixtures/" end directory = File.join(File.dirname(__FILE__),@mf_dir) fixture_name = File.join(directory, (from_fixture_name.to_s + ".yml")) # Test that fixture exists and if so, load it; otherwise skip it. if File.exist?(fixture_name) then Fixtures.create_fixtures(directory, from_fixture_name) else puts "No fixture #{fixture_name} exists." end end end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---