Author Topic: script to create ruby ActiveRecord pluto database models  (Read 1029 times)


  • Guru
  • ****
  • Posts: 261
    • View Profile
script to create ruby ActiveRecord pluto database models
« on: April 09, 2008, 09:08:35 am »

I'm finally starting to feel pretty confident in the models generated by this script so thought I'd share it.  I'm using the models in a media manager that lets you select sets of media objects then perform actions on the sets.  But I'm finding the models very useful to do queries from the rail's console (irb).

This script just queries the mysql pluto databases, then uses the sqlcvs naming convention to create the model associations (belongs_to, has_one, has_many (it doesn't look like pluto uses any pure join tables so there are no has_and_belongs_to_many associations)).

The script is at:

Here's my notes on setting up a rails project and using this script:
Code: [Select]
# install graphviz to be able to create class diagrams using railroad
# note, railroad and graphviz are development tools, not required
# for our rails application.

# on your dev system, assuming kubuntu for these notes
# install rails and composite_primary_keys gem
$ sudo apt-get install rubygems
$ sudo apt-get install ruby1.8-dev
$ sudo apt-get install build-essential
$ sudo apt-get install rails
$ sudo gem install composite_primary_keys
$ sudo gem install railroad
$ sudo gem install rails
$ sudo gem install mongrel
### select: mongrel 1.1.4 (ruby)
### select: fastthread 1.0.1 (ruby)
$ sudo gem install will_paginate
$ sudo apt-get install libmysql-ruby1.8
$ sudo apt-get install libmysql-ruby
$ sudo apt-get install irb

# on linuxmce box, backup linuxmce database
# create script:  /home/linuxmce/dump_pluto_databases
$ cat dump_pluto_databases
rm pluto_*.sql
mysqldump -u root --databases pluto_media    >pluto_media.sql
mysqldump -u root --databases pluto_main     >pluto_main.sql
mysqldump -u root --databases pluto_myth     >pluto_myth.sql
mysqldump -u root --databases pluto_security >pluto_security.sql
mysqldump -u root --databases pluto_telecom  >pluto_telecom.sql

# copy the databases to your dev system:
scp "linuxmce@*.sql" .

# on your dev system, create script to load pluto databases:
$ cat load_pluto_databases
mysql -u root --password=secret <pluto_main.sql
mysql -u root --password=secret <pluto_media.sql
mysql -u root --password=secret <pluto_telecom.sql
mysql -u root --password=secret <pluto_security.sql
mysql -u root --password=secret <pluto_myth.sql

# install linuxmce database on development system

# create rails project

$ rails media_manager
      create  app/controllers
      create  log/test.log

# now we need to generate the models and database hooks for
# the linuxmce databases.  We use mysql2models.rb to do this.
# To see the command line options:

linuxmce/media_manager $ ../mysql2models.rb --help
Generate ActiveRecord models from a pluto sqlCVS mysql database.
Usage: mysql2models.rb [options] db_name1 [db_name2...]
Usage: mysql2models [options]
    -p PASSWORD                      Database Password (no password is the default)
    -u USER                          Database User (root is the default)
    -g, --[no-]generate              Generate the models for rails (should only do once)
    -r, --[no-]replace               Replace the data models for rails
    -s, --[no-]stdout                Print the data models to stdout
    -d, --[no-]dbconfig              Create a config/database.yml for the databases
    -h, --help                       This usage information

# normally on first creation, you need the -g, -r, & -d options.  You need
# the -p if your database requires a password, which mine does on my
# development system while a LMCE system does not.
# Note, the -g option does take a while as it is calling "script/generate model MODELNAME"
# for each of the database tables.

media_manager $ ../mysql2models.rb -p secret -grd
pluto_main, processing...
pluto_media, processing...
pluto_myth, processing...
pluto_security, processing...
pluto_telecom, processing...
      create  app/models/pluto_security
      create  test/unit/pluto_security
      create  test/unit/pluto_main/vert_alignment_table_test.rb

# create rails databases, these are separate from the linuxmce databases

$ rake db:create
$ rake db:migrate

# now to see if it works:

media_manager $ script/console
Loading development environment (Rails 2.0.2)

# hey it didn't blow up, so let's take a look at a table

>> PlutoMedia::File.find(:first)
=> #<PlutoMedia::File PK_File: 1, EK_MediaType: 24, FK_MediaSubType: nil, FK_FileFormat: nil, FK_FileGroup: nil, DateAdded: "2008-01-15 09:49:27", Path: "/home/public/data", Filename: "documents", Missing: 0, IsDirectory: 1, EK_Users_Private: nil, EK_Device: nil, ModificationDate: "2008-04-05 23:40:48", AttrCount: 0, AttrDate: nil, DateLastViewed: nil, IsNew: true, Ignore: false, INode: 507998, MD5: "", Source: "F", psc_id: nil, psc_batch: nil, psc_user: nil, psc_frozen: false, psc_mod: "2008-04-05 23:41:59", psc_restrict: nil>

# cool, so let's see what dynamic associations we have:

>> PlutoMedia::File.find(:first).class.reflect_on_all_associations.collect{|a|}
=> [:picture_file, :user, :bookmark, :picture, :media_sub_type, :cover_art_scan, :disc, :media_type, :file_attribute, :file_format, :rip_status, :device, :file_group, :playlist_entry, :file_user, :attribute, :long_attribute]

# so let's try one of the dynamic associations:

>> PlutoMedia::File.find(:first).media_type
=> #<PlutoMain::MediaType PK_MediaType: 24, Define: "misc_DocViewer", Description: "Doc Viewer", FK_DesignObj: nil, DCEAware: 1, Extensions: nil, Subdirectory: "documents", IsExternalTransmission: false, FK_Pipe: nil, CanBookmark: false, EK_AttributeType_DefaultSort: nil, psc_id: 21, psc_batch: 356, psc_user: 33129, psc_frozen: false, psc_mod: nil, psc_restrict: nil>

Have fun,