66
Rails + Legacy Databases Brian Hogan - RailsConf 2009 twitter: bphogan IRC: hoganbp Thursday, May 14, 2009 So the main thing I want you to take away from this talk is...

Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Rails + Legacy Databases

Brian Hogan - RailsConf 2009twitter: bphogan

IRC: hoganbp

Thursday, May 14, 2009

So the main thing I want you to take away from this talk is...

Page 2: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Please don’t do it!

Thursday, May 14, 2009

Page 3: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Questions?

Thursday, May 14, 2009

Page 4: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Just kidding.

Thursday, May 14, 2009

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.

Page 5: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Who’s here?

Thursday, May 14, 2009

How many people here are new to Rails?How many people have to work with legacy databases?How many people want to learn how to work with legacy?

Page 6: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Rails and Legacydatabase schemas

Thursday, May 14, 2009

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.

Page 7: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Thursday, May 14, 2009

A legacy database could be anything that existed before Rails, like a wordpress blog or a custom project management app.

Page 8: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Anything that doesn’t follow ActiveRecord

conventions

Thursday, May 14, 2009

It’s important to note that these aren’t Rails limitations, but rather ActiveRecord limitations.

Page 9: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

This is going to be boring, isn’t it?

Thursday, May 14, 2009

So, we’re going to talk about legacy schemas and Rails. Things like bad table names, non-incrementing primary keys, stored procedures, compound keys

Page 10: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

I bet Obie’s business panel is awesome.

Thursday, May 14, 2009

We’ll look at some simple fixes first, like overriding table names and primary keys...

Page 11: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

or that musical patterns one...

Thursday, May 14, 2009

and then look at some other ways we can make our code easier to read and write while still leveraging the power of Rails

Page 12: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

sorry.

Thursday, May 14, 2009

Page 13: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Simple(er) solutions with ActiveRecord

Thursday, May 14, 2009

ActiveRecord provides mechanisms to handle some simple cases for us.

Page 14: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Simple problems

person_id

firstname

lastname

middlename

address

city

state

zip

Personsingular

table name and prefixed

ID

Thursday, May 14, 2009

Page 15: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Simple solutions

person_id

firstname

lastname

middlename

address

city

state

zip

Person

Thursday, May 14, 2009

Page 16: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Bad Column names

old-school

prefixes

person_id

person_name

person_email

person_sex

Person

Thursday, May 14, 2009

Page 17: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

person_id

person_name

person_email

person_sex

Person

alias_attribute

Thursday, May 14, 2009

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.

Page 18: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Uglier Schemas

Thursday, May 14, 2009

Sometimes the stuff that ActiveRecord gives you just isn’t enough.

Page 19: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Really Bad Column Names

Prefixes!Spaces!Yikes!!

Person id

Person Name

Person e-mail

Person sex

Person

Thursday, May 14, 2009

Some databases (SQL SERVER) let you have column names like this. Soaces, dashes, mixed case.

Page 20: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Person id

Person Name

Person e-mail

Person sex

Person

Use a View!

Thursday, May 14, 2009

Views to the rescue. MySQL, Microsoft SQL Server, and Oracle support inserting into views too, so your Rails database adapter will never know.

Page 21: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Non-incrementing Keys

Thursday, May 14, 2009

Can anyone give me examples of non incrementing keys? Some good examples?

Page 22: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Try to add them.

Thursday, May 14, 2009

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.

Page 23: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Generate them in yourcode somehow.

Thursday, May 14, 2009

This example is BAD BAD BAD. If you don’t know why, then please close your laptop and go sit in the corner.

Page 24: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Wordpress

Thursday, May 14, 2009

Let’s take a look at using Rails with an existing wordpress database.

Page 25: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Compound keys

Thursday, May 14, 2009

This is incredibly common. One of our databases joins 5 columns for course records.

Page 26: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Dr. Nic to the rescue.

http://compositekeys.rubyforge.org/

Thursday, May 14, 2009

Page 27: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Thursday, May 14, 2009

Page 28: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Establish a connection

database.yml

Thursday, May 14, 2009

Page 29: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Establish a connection

environment.rb

Thursday, May 14, 2009

Page 30: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Wordpress

Thursday, May 14, 2009

Wordpress and Rails can work well together.

Page 31: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Foreign Keys are easy

Thursday, May 14, 2009

Page 32: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Working with Legacy Data

Thursday, May 14, 2009

Page 33: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Thursday, May 14, 2009

Some public directory information from a university. It’s fed from a Unisys mainframe on a nightly basis using flatfiles.

Page 34: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Getting even this was a challenge!

Thursday, May 14, 2009

Page 35: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

THIS IS NOT ALL THE DATA.

Thursday, May 14, 2009

The feed also includes social security numbers. We have to protect those

Page 36: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

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.

Thursday, May 14, 2009

Page 37: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Views are a security tool!

Thursday, May 14, 2009

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.

Page 38: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Views are often a performance gain!

Thursday, May 14, 2009

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.

Page 39: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Stored Procedures

Thursday, May 14, 2009

Sometimes logic is encapsulated within the database.

Page 40: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

There’s no direct support for stored

procs in ActiveRecord...

Thursday, May 14, 2009

Page 41: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

...but the connection class will work!

Thursday, May 14, 2009

Page 42: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Call a procedure

Thursday, May 14, 2009

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

Page 43: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Get a return value

Thursday, May 14, 2009

This will get you a single return value from a procedure.

Page 44: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Get Rows

Thursday, May 14, 2009

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.

Page 45: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Thursday, May 14, 2009

There is no escape!

Page 46: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

DataMapper

Thursday, May 14, 2009

Using Oracle? MySQL? PostgreSQL? Try DataMapper!

Page 47: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

This is nice.

Thursday, May 14, 2009

This declares properties and methods. This has lots of potential.

Page 48: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Biggest problem with Rails and Legacy is“automagical code”

Thursday, May 14, 2009

If you can declare your columns like you can with DataMapper or even with a view, you’ve solved the major problems.

Page 49: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

DataMapper Drawbacks?

Thursday, May 14, 2009

Page 50: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Doesn’t work well on Windows yet

Thursday, May 14, 2009

Cygwin and other methods, but it’s known to be slow.

Page 51: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

No Microsoft SQL Server support.

Thursday, May 14, 2009

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.

Page 52: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Moving From Legacy

Thursday, May 14, 2009

What are some strategies you can use to move from legacy systems?

Page 53: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Where are you coming from and where are

you going?

Thursday, May 14, 2009

If you’re going to move from one platform to another, you need to know what the differences between the platforms.

Page 54: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Datatypes

Thursday, May 14, 2009

Each database manufacturer has different types.

Page 55: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Oracle

Thursday, May 14, 2009

Page 56: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

NUMBER datatype is used for integers and

floats

Oracle

Thursday, May 14, 2009

NUMBER datatype can be used for floats and integers

Page 57: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

DATE is used for Time AND Date.

Thursday, May 14, 2009

DATE is used for times and dates.

Page 58: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Oracle

NUMBER and CHAR can both be used for :boolean!

Thursday, May 14, 2009

NUMBER and CHAR - zero and one - for booleans.

Page 59: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

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

Thursday, May 14, 2009

Raimonds is a pretty swell guy, and he’s done some great work on this driver which makes working with Oracle very nice.

Page 60: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

SQL Server

SQL Server 2000 and 2005 are very

different.

Thursday, May 14, 2009

Page 61: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

SQL Server

TEXT fields with AR are really

troublesome.

Thursday, May 14, 2009

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.

Page 62: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

SQL Server

2000-2005-sqlserver-adapter

http://github.com/rails-sqlserver/2000-2005-adapter

Ken Collins

Thursday, May 14, 2009

This solves a lot of stuff. Different datatypes, date fixes, and excellent stored procedure support.

Page 63: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

Finally, watch out for triggers!

Thursday, May 14, 2009

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.

Page 64: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

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

Thursday, May 14, 2009

Page 65: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

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.

Thursday, May 14, 2009

Page 66: Rails + Legacy Databasesblog.napcs.com/wp-content/uploads/2009/05/rails_legacy... · 2016. 7. 16. · Rails and Legacy database schemas Thursday, May 14, 2009 This is a topic that’s

MS SQL Databases

• Tables within different databases on the same server can be joined as if they are in the same database.

Thursday, May 14, 2009