Rails and Legacy Databases - RailsConf 2009
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Rails and Legacy Databases - RailsConf 2009

on

  • 18,379 views

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

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

Statistics

Views

Total Views
18,379
Views on SlideShare
17,291
Embed Views
1,088

Actions

Likes
18
Downloads
256
Comments
2

11 Embeds 1,088

http://rubyonwhales.org 421
http://en.oreilly.com 319
https://gitter.im 177
http://www.napcsweb.com 105
http://www.slideshare.net 57
http://coderwall.com 4
http://localhost:3000 1
http://static.slidesharecdn.com 1
https://si0.twimg.com 1
http://www.linkedin.com 1
http://dev.smartplus.tv 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Brian, this is so awesome, thanks for saving the day!!
    Are you sure you want to
    Your message goes here
    Processing…
  • can I double like this? Seriously, thank you for making my job much easier!
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • So the main thing I want you to take away from this talk is... <br />
  • <br />
  • <br />
  • The point of this talk is to show you that there are ways to work with legacy systems, but that you really, should try to work around them. <br />
  • How many people here are new to Rails? <br /> How many people have to work with legacy databases? <br /> How many people want to learn how to work with legacy? <br />
  • This is a topic that’s close to me because I work days at a university that uses Oracle and SQL Server databases. In order to use Rails, there were a lot of things I had to figure out. <br />
  • A legacy database could be anything that existed before Rails, like a wordpress blog or a custom project management app. <br />
  • It’s important to note that these aren’t Rails limitations, but rather ActiveRecord limitations. <br />
  • So, we’re going to talk about legacy schemas and Rails. Things like bad table names, non-incrementing primary keys, stored procedures, compound keys <br />
  • We’ll look at some simple fixes first, like overriding table names and primary keys... <br />
  • and then look at some other ways we can make our code easier to read and write while still leveraging the power of Rails <br />
  • <br />
  • ActiveRecord provides mechanisms to handle some simple cases for us. <br />
  • <br />
  • <br />
  • <br />
  • Rails’ alias_attribute method is a macro for redefining the method at runtime. However, it only works with fieldnames that don’t violate Rails’ conventions. <br />
  • Sometimes the stuff that ActiveRecord gives you just isn’t enough. <br />
  • Some databases (SQL SERVER) let you have column names like this. Soaces, dashes, mixed case. <br /> <br />
  • Views to the rescue. MySQL, Microsoft SQL Server, and Oracle support inserting into views too, so your Rails database adapter will never know. <br /> <br />
  • Can anyone give me examples of non incrementing keys? Some good examples? <br />
  • Not always possible if there’s a certain level of beaurocracy in your institution. Some people also just think meaningful keys are the most bestest idea. <br />
  • This example is BAD BAD BAD. If you don’t know why, then please close your laptop and go sit in the corner. <br />
  • Let’s take a look at using Rails with an existing wordpress database. <br />
  • This is incredibly common. One of our databases joins 5 columns for course records. <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • Wordpress and Rails can work well together. <br />
  • <br />
  • <br />
  • Some public directory information from a university. It’s fed from a Unisys mainframe on a nightly basis using flatfiles. <br />
  • <br />
  • The feed also includes social security numbers. We have to protect those <br />
  • <br />
  • Never forget that views are a security tool. Aside from the benefit they give us with regard to changing how data looks, we can use them to abstract availability of data. I *never* see these used in Rails. <br />
  • In some databases you can use views to cut down complex join logic and still treat them like tables. MySQL doesn’t allow subselects, but MS SQL sure does. <br />
  • Sometimes logic is encapsulated within the database. <br />
  • <br />
  • <br />
  • This throws the results away, but it’s fine if you just want to call something that has no return value. Execute is good for calling any arbitrary SQL, but <br />
  • This will get you a single return value from a procedure. <br />
  • Results come back as an array of hashes, so you access them a little differently, but it’s still pretty nice. A little meta-magic and you can map these to properties of a new instance. <br />
  • There is no escape! <br />
  • Using Oracle? MySQL? PostgreSQL? Try DataMapper! <br />
  • This declares properties and methods. This has lots of potential. <br />
  • If you can declare your columns like you can with DataMapper or even with a view, you’ve solved the major problems. <br />
  • <br />
  • Cygwin and other methods, but it’s known to be slow. <br />
  • A call for help. I know virtually nothing about DataMapper, but a lot about SQL Server. There’s been interest, but no volunteers yet. I’m tempted but I need help. <br />
  • What are some strategies you can use to move from legacy systems? <br />
  • If you’re going to move from one platform to another, you need to know what the differences between the platforms. <br />
  • Each database manufacturer has different types. <br />
  • <br />
  • NUMBER datatype can be used for floats and integers <br />
  • DATE is used for times and dates. <br />
  • NUMBER and CHAR - zero and one - for booleans. <br />
  • Raimonds is a pretty swell guy, and he’s done some great work on this driver which makes working with Oracle very nice. <br />
  • <br />
  • They’re deprecated anyway. If you have to use a database that uses this type, you have to watch out for statements that use equality. <br />
  • This solves a lot of stuff. Different datatypes, date fixes, and excellent stored procedure support. <br />
  • Legacy databases tend to have a lot of these. You need to write code to handle exceptions that your ORM classes might throw if a trigger causes constraints to be violated or if triggers start doing strange things. <br />
  • <br />
  • <br />
  • <br />

Rails and Legacy Databases - RailsConf 2009 Presentation Transcript

  • 1. Rails + Legacy Databases Brian Hogan - RailsConf 2009 twitter: bphogan IRC: hoganbp
  • 2. Please don’t do it!
  • 3. Questions?
  • 4. Just kidding.
  • 5. Who’s here?
  • 6. Rails and Legacy database schemas
  • 7. Anything that doesn’t follow ActiveRecord conventions
  • 8. This is going to be boring, isn’t it?
  • 9. I bet Obie’s business panel is awesome.
  • 10. or that musical patterns one...
  • 11. sorry.
  • 12. Simple(er) solutions with ActiveRecord
  • 13. Simple problems Person singular person_id table firstname lastname name and middlename prefixed address city ID state zip
  • 14. Simple solutions Person person_id firstname lastname middlename address city state zip
  • 15. Bad Column names Person old- person_id school person_name person_email prefixes person_sex
  • 16. alias_attribute Person person_id person_name person_email person_sex
  • 17. Uglier Schemas
  • 18. Really Bad Column Names Person Prefixes! Person id Spaces! Person Name Yikes!! Person e-mail Person sex
  • 19. Use a View! Person Person id Person Name Person e-mail Person sex
  • 20. Non-incrementing Keys
  • 21. Try to add them.
  • 22. Generate them in your code somehow.
  • 23. Wordpress
  • 24. Compound keys
  • 25. Dr. Nic to the rescue. http://compositekeys.rubyforge.org/
  • 26. Establish a connection database.yml
  • 27. Establish a connection environment.rb
  • 28. Wordpress
  • 29. Foreign Keys are easy
  • 30. Working with Legacy Data
  • 31. Getting even this was a challenge!
  • 32. THIS IS NOT ALL THE DATA.
  • 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. Views are a security tool!
  • 35. Views are often a performance gain!
  • 36. Stored Procedures
  • 37. There’s no direct support for stored procs in ActiveRecord...
  • 38. ...but the connection class will work!
  • 39. Call a procedure
  • 40. Get a return value
  • 41. Get Rows
  • 42. DataMapper
  • 43. This is nice.
  • 44. Biggest problem with Rails and Legacy is “automagical code”
  • 45. DataMapper Drawbacks?
  • 46. Doesn’t work well on Windows yet
  • 47. No Microsoft SQL Server support.
  • 48. Moving From Legacy
  • 49. Where are you coming from and where are you going?
  • 50. Datatypes
  • 51. Oracle
  • 52. Oracle NUMBER datatype is used for integers and floats
  • 53. DATE is used for Time AND Date.
  • 54. Oracle NUMBER and CHAR can both be used for :boolean!
  • 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. SQL Server SQL Server 2000 and 2005 are very different.
  • 57. SQL Server TEXT fields with AR are really troublesome.
  • 58. SQL Server 2000-2005-sqlserver- adapter Ken Collins http://github.com/rails-sqlserver/2000-2005-adapter
  • 59. Finally, watch out for triggers!
  • 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. 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. MS SQL Databases • Tables within different databases on the same server can be joined as if they are in the same database. •