Hi,

I've been using cayenne three years from now.

Using the modeller i tried to rename a table and then a class with distinct 
levels of problems with broken relationships. Until i see the modeller does 
atempts to refactor some things but many gets broken.

I've found a tricky method to refactor things with relative confidence:

First:

1.- Classes
Rename classes: the entity and the cayenne entity (underscore first character) 
using the refactor capabilities of your IDE.  

2.- XML files
Here it's a very artesanal perl script that refactor a table and the related 
entity. If you call it refactor-table-name.pl you can call it this way:

Move where cayenne files are located and:

refactor-table-name.pl old-table-name new-table-name *.xml

3.- So far some things still may be broken

Then open the modeller and your project, the use Project > Validate Project 
there may be the broken object relationships, these must be deleted an 
re-synchronised and will be fine.

4.- Database
Finally: rename the table in the database using alter table.


Please if there is some better, safer way to do it please let me know.

The code of the perl script follows. Save it under your bin directory and name 
it refactor-table-name.pl, then chmod 755 refactor-table-name.pl.

#!/usr/bin/perl


## Refactor cayenne table and entity, give table names assumed parts of the 
name separated by underscores.
## Use under your own risk on a copy of your data.

use strict;
use warnings;

use File::Copy; 

die "Error, call format: $0 old-table-name new-table-name file1 [ .. more files 
] "     if @ARGV < 3;

my $old_name = shift;
my $new_name = shift;

# extrapolar object names

my $old_name_obj = "";
foreach (split /_/, $old_name) {
        $old_name_obj .= ucfirst($_);
}

my $new_name_obj = "";
foreach (split /_/, $new_name) {
        $new_name_obj .= ucfirst($_);
}

my @files = @ARGV;

printf "Renaming table %s to %s\n", $old_name, $new_name;
printf "Renaming clase %s to %s\n", $old_name_obj, $new_name_obj;

my $i = 1;
foreach my $file (@files) {

        print "Processing file $file\n";

        open IN, $file or die "File $file, error $!\n";
        
        my $tmp = "/tmp/refactor-$i-cayenne-$$";
        $i++;

        open TMP, ">$tmp" or die "File $file, error $!\n";

        my $flag_modif = 0;

        while (<IN>) {

                my $orig_line = $_;

                s/db-entity name="$old_name"/db-entity name="$new_name"/;
                s/dbEntityName="$old_name"/dbEntityName="$new_name"/;
                s/target="$old_name"/target="$new_name"/;
                s/source="$old_name"/source="$new_name"/;
                
s/db-relationship-path="$old_name"/db-relationship-path="$new_name"/;
                s/dbEntityName="$old_name"/dbEntityName="$new_name"/;


                s/<obj-entity name="$old_name_obj"/<obj-entity 
name="$new_name_obj"/;
                
s/className="(.*?)\.$old_name_obj"/className="$1.$new_name_obj"/;
                s/source="$old_name_obj"/source="$new_name_obj"/;
                s/target="$old_name_obj"/target="$new_name_obj"/;

                $flag_modif = 1         if $orig_line ne $_;

                print TMP $_;

        }

        close IN;
        close TMP;

        if ($flag_modif == 1) {
                print "\t * File modified\n";
                copy $tmp, $file or die $!;
         }

}


exit 0;



Bye
Hans, Welinux S.A.

Reply via email to