I'm sorry but disagree: At this point I think MetaRails (MR) is going
in the opposite direction. Migrations files are more importatn and we
cannot generate wihtout break or confuse the user: in migrations files
we can add more than tables and fields, we can write indexes and maybe
other SQL stuff.
Today MR, read the Database to infer models. So we have to fight with
DB, yml models & migratrions files. Too many points to one concept:
this is a bad smell IMHO.
Solution?
I think we need reverse the problem: extend the migrations file. Our
user put all the relevant info in one point: the migrations files, and
MR forget aboout DB & yaml. How can do it?. See that:
# inspired by SexyMigrations
module MetaMigrations
module Table
def belogns_to(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
args.each do |col|
column(id = "#{col}_id", :integer, options)
if options[:ref]
reference = options[:ref] == true ? col.to_s.tableize :
options[:ref].to_s
(@fk_references ||= []) << [id, reference]
end
end
end
def inheritable
string :type
end
end
module Schema
alias real_create_table create_table
def create_table(name, options = {}, &block)
create_yml_model(name, options, block)
real_create_table(name, options, block)
end
alias real_drop_table drop_table
def drop_table(name)
drop_yml_model(name, options, block)
real_drop_table(name, options, block)
end
end
end
ActiveRecord::ConnectionAdapters::TableDefinition.send :include,
MetaMigrations::Table
ActiveRecord::ConnectionAdapters::AbstractAdapter.send :include,
MetaMigrations::Schema
Here I try to show how we can hook migrations files and create the
same current yml models, but without the yml files. Of course this is
only a guess, but I'm pretty confident I can write the real code. This
way we get some callbacks to infer our class model using only the
migrations files, no yml, no db.
I hope you understand the idea: my English is limited :)
What do you think about this crazy idea? Seeing any problem?
On Feb 1, 2:16 pm, "Sergio Espeja" <
sergio.esp...@gmail.com> wrote:
> I think that one good solution could be to make rails read the database
> definition YAML during the initialization process, then rails could take the
> model relationships from this file. This will lead to leave the rails models
> clean of relation definitions.
>
> But I think that rails migrations must behave as they do, because one of the
> best features of migrations is that you can go up and down trough them
> knowing exactly what you have in the database. And you can make changes to
> the db creating a new migration and this changes will be reproduced exactly
> to other users of the application just updating its code and calling "rake
> db:migrate".
>
> In summary, my proposal is to have a meta rails database definition YAML,
> with this file we can create the migrations automatically and infer the
> rails models relations. The rails models files can exist or not depending if
> we want to add extra methods to them. This won't be difficult to develop
> (seehttp://
magicmodels.rubyforge.org/).
>
> Jorge, do you agree?
> Piotr, is this yaml file the one that you are thinking on?
>
> Sergio