Re: dh_config_model_upgrade: package upgrade with Config::Model
Le dimanche 13 décembre 2009 17:48:13, Stefano Zacchiroli a écrit :
> Can I write my own upgrade script in Perl and have it play with
> the models involved in the upgrade (i.e., the old model and the new
> model) in the same scripts to migrate from one to the other in some
> ad-hoc way?.
>
Yes. Using Config::Model::Itself and some (untested) code like (please fasten
your seat belt):
my $meta_model = Config::Model -> new();
my $old_meta_inst
= $meta_model->instance (root_class_name => 'Itself::Model' ,
instance_name => 'old model' ,
);
my $old_meta_root = $old_meta_inst -> config_root ;
# create object to load model from files
my $old_rw_obj = Config::Model::Itself -> new(model_object => $old_meta_root);
# now load old model from old model files
$old_rw_obj -> read_all( model_dir => $old_model_dir,
root_model => 'SomeModel',
) ;
# serialise old model
my $old_dump = $old_meta_root->dump_tree;
# create new meta instance to hold new model
my $new_meta_inst
= $meta_model->instance (root_class_name => 'Itself::Model' ,
instance_name => 'new model' ,
);
# still empty model
my $new_meta_root = $new_meta_inst -> config_root ;
# copy old model in new instance
$new_meta_root->load($old_dump);
# customize new model
$new_meta_root->load('class:SomeModel element:foobar default="new default");
#create another rw object
my $new_rw_obj = Config::Model::Itself -> new(model_object => $new_meta_root);
# write the modified model somewhere else
$new_rw_obj -> write_all(model_dir => $new_model_dir,
root_model => 'SomeModel',
) ;
Depending on what you want to achieve, you may also directly modify the old
model in the program with:
$old_meta_root->load('class:SomeModel element:foobar default="new default");
and write the customised model files somewhere else as shown above.
HTH
Dominique
--
http://config-model.wiki.sourceforge.net/ -o- http://search.cpan.org/~ddumont/
http://www.ohloh.net/accounts/ddumont
Reply to: