Exporting tables data to yaml file

-----> create a rails application. following command creates a rails application with mysql as database

F:\ror>rails export-fixtures --database=mysql
create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create config/initializers
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/javascripts
create public/stylesheets
create script/performance
create script/process
create test/fixtures
create test/functional
create test/integration
create test/unit
create vendor
create vendor/plugins
create tmp/sessions
create tmp/sockets
create tmp/cache
create tmp/pids
create Rakefile
create README
create app/controllers/application.rb
create app/helpers/application_helper.rb
create test/test_helper.rb
create config/database.yml
create config/routes.rb
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/new_rails_defaults.rb
create config/boot.rb
create config/environment.rb
create config/environments/production.rb
create config/environments/development.rb
create config/environments/test.rb
create script/about
create script/console
create script/dbconsole
create script/destroy
create script/generate
create script/performance/benchmarker
create script/performance/profiler
create script/performance/request
create script/process/reaper
create script/process/spawner
create script/process/inspector
create script/runner
create script/server
create script/plugin
create public/dispatch.rb
create public/dispatch.cgi
create public/dispatch.fcgi
create public/404.html
create public/422.html
create public/500.html
create public/index.html
create public/favicon.ico
create public/robots.txt
create public/images/rails.png
create public/javascripts/prototype.js
create public/javascripts/effects.js
create public/javascripts/dragdrop.js
create public/javascripts/controls.js
create public/javascripts/application.js
create doc/README_FOR_APP
create log/server.log
create log/production.log
create log/development.log
create log/test.log

-----> Navigate to rails application folder
F:\ror\export-fixtures>

-----> Generate scaffold

F:\ror\export-fixtures>ruby script/generate scaffold Country name:string
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/countries
exists app/views/layouts/
exists test/functional/
exists test/unit/
exists public/stylesheets/
create app/views/countries/index.html.erb
create app/views/countries/show.html.erb
create app/views/countries/new.html.erb
create app/views/countries/edit.html.erb
create app/views/layouts/countries.html.erb
create public/stylesheets/scaffold.css
create app/controllers/countries_controller.rb
create test/functional/countries_controller_test.rb
create app/helpers/countries_helper.rb
route map.resources :countries
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/country.rb
create test/unit/country_test.rb
create test/fixtures/countries.yml
create db/migrate
create db/migrate/20080120174827_create_countries.rb

-----> Create the database in the mysql database with the following command.
F:\ror\export-fixtures>rake db:create
(in F:/ror/export-fixtures)

-----> Migrate the database

F:\ror\export-fixtures>rake db:migrate
(in F:/ror/export-fixtures)
== 20080120174827 CreateCountries: migrating ==================================
-- create_table(:countries)
-> 0.0460s
== 20080120174827 CreateCountries: migrated (0.0460s) =========================

-----> write the following code and save it as the fixtures.rake under the
lib/tasks folder



-----> Now we can get this task i.e export_data under the rake tasks

F:\ror\export-fixtures>rake --tasks
(in F:/ror/export-fixtures)
rake db:abort_if_pending_migrations # Raises an error if there are pending...
rake db:charset # Retrieves the charset for the curren...
rake db:collation # Retrieves the collation for the curr...
rake db:create # Create the database defined in confi...
rake db:create:all # Create all the local databases defin...
rake db:drop # Drops the database for the current R...
rake db:drop:all # Drops all the local databases define...
rake db:fixtures:export_data # Create YAML test fixtures from data ...
rake db:fixtures:identify # Search for a fixture given a LABEL o...
rake db:fixtures:load # Load fixtures into the current envir...
rake db:migrate # Migrate the database through scripts...
rake db:migrate:down # Runs the "down" for a given migratio...
rake db:migrate:redo # Rollbacks the database one migration...
rake db:migrate:reset # Resets your database using your migr...
rake db:migrate:up # Runs the "up" for a given migration ...
rake db:reset # Drops and recreates the database fro...
rake db:rollback # Rolls the schema back to the previou...
rake db:schema:dump # Create a db/schema.rb file that can ...
rake db:schema:load # Load a schema.rb file into the database
rake db:sessions:clear # Clear the sessions table
rake db:sessions:create # Creates a sessions migration for use...
rake db:structure:dump # Dump the database structure to a SQL...
rake db:test:clone # Recreate the test database from the ...
rake db:test:clone_structure # Recreate the test databases from the...
rake db:test:load # Recreate the test database from the ...
rake db:test:prepare # Check for pending migrations and loa...
rake db:test:purge # Empty the test database
rake db:version # Retrieves the current schema version...
rake doc:app # Build the app HTML Files
rake doc:clobber_app # Remove rdoc products
rake doc:clobber_plugins # Remove plugin documentation
rake doc:clobber_rails # Remove rdoc products
rake doc:plugins # Generate documentation for all insta...
rake doc:rails # Build the rails HTML Files
rake doc:reapp # Force a rebuild of the RDOC files
rake doc:rerails # Force a rebuild of the RDOC files
rake gems # List the gems that this rails applic...
rake gems:build # Build any native extensions for unpa...
rake gems:install # Installs all required gems for this ...
rake gems:unpack # Unpacks the specified gem into vendo...
rake gems:unpack:dependencies # Unpacks the specified gems and its d...
rake log:clear # Truncates all *.log files in log/ to...
rake notes # Enumerate all annotations
rake notes:fixme # Enumerate all FIXME annotations
rake notes:optimize # Enumerate all OPTIMIZE annotations
rake notes:todo # Enumerate all TODO annotations
rake rails:freeze:edge # Lock to latest Edge Rails, for a spe...
rake rails:freeze:gems # Lock this application to the current...
rake rails:unfreeze # Unlock this application from freeze ...
rake rails:update # Update both configs, scripts and pub...
rake rails:update:configs # Update config/boot.rb from your curr...
rake rails:update:javascripts # Update your javascripts from your cu...
rake rails:update:scripts # Add new scripts to the application s...
rake routes # Print out all defined routes in matc...
rake secret # Generate a crytographically secure s...
rake stats # Report code statistics (KLOCs, etc) ...
rake test # Run all unit, functional and integra...
rake test:functionals # Run tests for functionalsdb:test:pre...
rake test:integration # Run tests for integrationdb:test:pre...
rake test:plugins # Run tests for pluginsenvironment / R...
rake test:recent # Run tests for recentdb:test:prepare ...
rake test:uncommitted # Run tests for uncommitteddb:test:pre...
rake test:units # Run tests for unitsdb:test:prepare /...
rake time:zones:all # Displays names of all time zones rec...
rake time:zones:local # Displays names of time zones recogni...
rake time:zones:us # Displays names of US time zones reco...
rake tmp:cache:clear # Clears all files and directories in ...
rake tmp:clear # Clear session, cache, and socket fil...
rake tmp:create # Creates tmp directories for sessions...
rake tmp:pids:clear # Clears all files in tmp/pids
rake tmp:sessions:clear # Clears all files in tmp/sessions
rake tmp:sockets:clear # Clears all files in tmp/sockets

-----> Now run the program and enter some test data.

F:\ror\export-fixtures>ruby script/server
=> Booting WEBrick...
=> Rails 2.1.1 application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2008-01-20 23:37:17] INFO WEBrick 1.3.1
[2008-01-20 23:37:17] INFO ruby 1.8.6 (2007-09-24) [i386-mswin32]
[2008-01-20 23:37:17] INFO WEBrick::HTTPServer#start: pid=2928 port=3000

-----> Start the browser at the above port number and enter the data.



-----> Export the database data to the yaml file using the following command.

F:\ror\export-fixtures>rake db:fixtures:export_data
(in F:/ror/export-fixtures)

The above command will exports all the tables data to the individual fixture files.

-----> To view the fixtures go to the test/fixtures folder under the rails application folder


-----> To load this fixtures into the database type the following command.

F:\ror\export-fixtures>rake db:fixtures:load
(in F:/ror/export-fixtures)

The above command will load all the fixture files data into the corresponding database.

1 comment:

Tejuteju said...

It was really a nice article and i was really impressed by reading this Ruby on Rails Online Course