SchemaPlus / schema_plus

SchemaPlus provides a collection of enhancements and extensions to ActiveRecord
Other
680 stars 86 forks source link

schema_dumper tsort not taking into account paritioned table dependencies #77

Open rockrep opened 11 years ago

rockrep commented 11 years ago

see Gist

I have a partitioned Postgres table that has an alphabetical ordering before the table it inherits. The schema_dumper orders this table prior to it's parent table in schema.rb which prevents rake db:test:prepare from successfully completing due to a "Relation 'facts_id_seq' does not exist" error.

Looks like the tsort() methods need to be enhanced to handle this type of dependency. Let me know if I can help.

using activerecord 3.2.8 and schema_plus 0.4.1

ronen commented 11 years ago

Geez, can I admit I didn't know about Posgresql table inheritance? Luckily, it looks like a straightforward addition to support it in the dependency ordering. Most principled way to support it would be, i think:

and unless there's something I'm not thinking of, that should take care of it.

Let me know if I can help

Would you be able to take a crack at the above (or some variant of it)? Along with corresponding specs of course. Unfortunately I don't have the time to dive into it right now.

Could also be cool to add an option `create_table "all2012_facts", :inherits => 'facts'``

rockrep commented 11 years ago

Thanks Ronen. I have to admit I didn't know about Postgresql table inheritance until recently either. I'll give the above suggestions a try and see if I can provide a patch.

rockrep commented 11 years ago

Ronen, I submitted a pull request (#79) to fix this issue per your suggestions. I also had to fix an issue with :drop_table to pass a :cascade option on to Postgresql in order to get 'rake db:schema:load' to drop the inherited table successfully. Please let me know your feedback.

I didn't have time yet to add an :inherits option for :create_table, but I think it's needed to ensure a database loaded with db:schema:load is properly configured. As it stands now, I'm limited to using db:migrate. I'd need to also specify the check constraints as well, otherwise the create table call will fail. SchemaDumper would also have to support writing out the check constraints to schema.rb. I could use some suggestions for how you think it's best to approach this. I'm also partly thinking that this support should belong in activerecord itself, but I'd like to hear what your take is on that.