postgres at dac.e4ward.com postgres at dac.e4ward.com
Fri Nov 23 06:21:48 PST 2007
Hi all.

I want to know if anyone here has any experience using slony based
replication on Ruby on Rails applications.

As you may know (or not) there's a problem doing this because of
rails' applications using rails' migrations to version and change the
database schema. So rails applications deployment normally
automatically updates the database schema based on ruby script that
generates the required DDL statements.

On top of that, it's normal practice to add a column and inmediately
initialize existing records' values for that new column. So that makes
it impossible (or at least very difficult) to separate the process of
adding the new column via an slonik EXECUTE script and populating the
new field. The rails app would issue the DDL statement directly to the
master node and then update the new column data on existing records.
Slony wouldn't replicate the schema change, but will fail when trying
to replicate the new data to the slave nodes because of the new column
not existing there yet.

So... I've implemented a new Rails plugin that would detect CREATE,
DROP and ALTER statements just before they're issued to the master
node and simply synchronously delegate it's execution to an external
script passing all needed connection data AND the DDL statement as
parameters. That external DDL handler should return an exit status 0
on success. Any other exit status would cause the plugin to raise an
exception as expected by rails when the statement is invalid.

I've managed to use this to execute that DDL statements via psql and
slonik_execute_script | slonik.

Obviously this doesn't automate the process of creating new sets and
merging them in case of new tables being created. So if the rails
migration involves a new table the new table would be automatically
created in all nodes, but it's data will not replicate until you
manually tell slony to. But at least it doesn't fail.

I'd like to know if anyone has found a better way to do this. Or for
you guys that don't use slony with rails but have more experience on
slony, maybe you could suggest me an alternative.

I'm quite happy with the result.
I'll open source it in case there's interest.

Thank you
-- 
Diego Algorta Casamayou
http://www.oboxodo.com - http://diego.algorta.net


More information about the Slony1-general mailing list