Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Rails + Legacy
  Databases
 Brian Hogan - RailsConf 2009
       twitter: bphogan
         IRC: hoganbp
Please don’t do it!
Questions?
Just kidding.
Who’s here?
Rails and Legacy
database schemas
Anything that doesn’t
follow ActiveRecord
    conventions
This is going to be
 boring, isn’t it?
I bet Obie’s business
  panel is awesome.
or that musical
patterns one...
sorry.
Simple(er) solutions
 with ActiveRecord
Simple problems
    Person
             singular
person_id
               table
firstname
lastname
             name and
mi...
Simple solutions
    Person
person_id
firstname
lastname
middlename
address
city
state
zip
Bad Column names
    Person
                 old-
person_id
                school
person_name
person_email
              ...
alias_attribute
    Person
person_id
person_name
person_email
person_sex
Uglier Schemas
Really Bad
         Column Names
    Person
                Prefixes!
Person id
                 Spaces!
Person Name
      ...
Use a View!

    Person
Person id
Person Name
Person e-mail
Person sex
Non-incrementing Keys
Try to add them.
Generate them in your
   code somehow.
Wordpress
Compound keys
Dr. Nic to the rescue.

  http://compositekeys.rubyforge.org/
Establish a
      connection
database.yml
Establish a
            connection
environment.rb
Wordpress
Foreign Keys are easy
Working with
 Legacy Data
Getting even this
was a challenge!
THIS IS NOT ALL THE
        DATA.
Data security
• Secure internal network between the database
  server and the mainframe
• Replication to the application d...
Views are a security
       tool!
Views are often a
performance gain!
Stored Procedures
There’s no direct
support for stored
     procs in
  ActiveRecord...
...but the connection
   class will work!
Call a procedure
Get a return value
Get Rows
DataMapper
This is nice.
Biggest problem with
 Rails and Legacy is
 “automagical code”
DataMapper
Drawbacks?
Doesn’t work well on
    Windows yet
No Microsoft SQL
 Server support.
Moving From Legacy
Where are you coming
 from and where are
     you going?
Datatypes
Oracle
Oracle

 NUMBER datatype is
used for integers and
       floats
DATE is used for Time
     AND Date.
Oracle

NUMBER and CHAR can
   both be used
   for :boolean!
Oracle Enhanced
           Driver

• http://github.com/rsim/oracle-enhanced
   • Raimonds Simanovskis

   • Good slides on...
SQL Server



SQL Server 2000 and
   2005 are very
     different.
SQL Server


TEXT fields with AR
    are really
   troublesome.
SQL Server
   2000-2005-sqlserver-
         adapter
                   Ken Collins

http://github.com/rails-sqlserver/2000...
Finally, watch out for
      triggers!
Dealing with
            Constraints
• Avoid using fixtures for tests. They’re bad news
  anyway
• Use .new() and stubs
• D...
Migrating to a new
          DBMS

• rake db:schema:dump, and then paste that in as a
  new base migration
   • sometimes ...
MS SQL Databases


• Tables within different databases on the same
  server can be joined as if they are in the same
  dat...
Rails and Legacy Databases - RailsConf 2009
Rails and Legacy Databases - RailsConf 2009
Rails and Legacy Databases - RailsConf 2009
Rails and Legacy Databases - RailsConf 2009
Upcoming SlideShare
Loading in …5
×

Rails and Legacy Databases - RailsConf 2009

16,424 views

Published on

Slides from my RailsConf 2009 talk showing some methods for using legacy database schemas with the Ruby on Rails framework.

Published in: Technology, Business

Rails and Legacy Databases - RailsConf 2009

  1. 1. Rails + Legacy Databases Brian Hogan - RailsConf 2009 twitter: bphogan IRC: hoganbp
  2. 2. Please don’t do it!
  3. 3. Questions?
  4. 4. Just kidding.
  5. 5. Who’s here?
  6. 6. Rails and Legacy database schemas
  7. 7. Anything that doesn’t follow ActiveRecord conventions
  8. 8. This is going to be boring, isn’t it?
  9. 9. I bet Obie’s business panel is awesome.
  10. 10. or that musical patterns one...
  11. 11. sorry.
  12. 12. Simple(er) solutions with ActiveRecord
  13. 13. Simple problems Person singular person_id table firstname lastname name and middlename prefixed address city ID state zip
  14. 14. Simple solutions Person person_id firstname lastname middlename address city state zip
  15. 15. Bad Column names Person old- person_id school person_name person_email prefixes person_sex
  16. 16. alias_attribute Person person_id person_name person_email person_sex
  17. 17. Uglier Schemas
  18. 18. Really Bad Column Names Person Prefixes! Person id Spaces! Person Name Yikes!! Person e-mail Person sex
  19. 19. Use a View! Person Person id Person Name Person e-mail Person sex
  20. 20. Non-incrementing Keys
  21. 21. Try to add them.
  22. 22. Generate them in your code somehow.
  23. 23. Wordpress
  24. 24. Compound keys
  25. 25. Dr. Nic to the rescue. http://compositekeys.rubyforge.org/
  26. 26. Establish a connection database.yml
  27. 27. Establish a connection environment.rb
  28. 28. Wordpress
  29. 29. Foreign Keys are easy
  30. 30. Working with Legacy Data
  31. 31. Getting even this was a challenge!
  32. 32. THIS IS NOT ALL THE DATA.
  33. 33. Data security • Secure internal network between the database server and the mainframe • Replication to the application database server using Microsoft DTS packages • Access restricted at the table level • Access granted to primary developers via views • Access granted to third-party develpers by stored procedures.
  34. 34. Views are a security tool!
  35. 35. Views are often a performance gain!
  36. 36. Stored Procedures
  37. 37. There’s no direct support for stored procs in ActiveRecord...
  38. 38. ...but the connection class will work!
  39. 39. Call a procedure
  40. 40. Get a return value
  41. 41. Get Rows
  42. 42. DataMapper
  43. 43. This is nice.
  44. 44. Biggest problem with Rails and Legacy is “automagical code”
  45. 45. DataMapper Drawbacks?
  46. 46. Doesn’t work well on Windows yet
  47. 47. No Microsoft SQL Server support.
  48. 48. Moving From Legacy
  49. 49. Where are you coming from and where are you going?
  50. 50. Datatypes
  51. 51. Oracle
  52. 52. Oracle NUMBER datatype is used for integers and floats
  53. 53. DATE is used for Time AND Date.
  54. 54. Oracle NUMBER and CHAR can both be used for :boolean!
  55. 55. Oracle Enhanced Driver • http://github.com/rsim/oracle-enhanced • Raimonds Simanovskis • Good slides on this at http://www.slideshare.net/rsim/using-ruby-on- rails-with-legacy-oracle-databases-presentation
  56. 56. SQL Server SQL Server 2000 and 2005 are very different.
  57. 57. SQL Server TEXT fields with AR are really troublesome.
  58. 58. SQL Server 2000-2005-sqlserver- adapter Ken Collins http://github.com/rails-sqlserver/2000-2005-adapter
  59. 59. Finally, watch out for triggers!
  60. 60. Dealing with Constraints • Avoid using fixtures for tests. They’re bad news anyway • Use .new() and stubs • Duplicate validation / constraints in your code • Use exception handling • Use Stubble - http://github.com/dchelimsky/ stubble
  61. 61. Migrating to a new DBMS • rake db:schema:dump, and then paste that in as a new base migration • sometimes columns aren’t usable this way. Dump schema as SQL and use Regex instead to convert to Ruby.
  62. 62. MS SQL Databases • Tables within different databases on the same server can be joined as if they are in the same database. •

×