Skip to content
Permalink
Browse files

epadmin: added --dry-run options and various bug fixes.

  • Loading branch information...
jiadiyao committed Dec 8, 2015
1 parent 3143600 commit 13c2c3f9ec0df82bf17515c4bf5190c0e139d681
Showing with 80 additions and 33 deletions.
  1. +80 −33 bin/epadmin
@@ -80,6 +80,8 @@ Where I<command> is one of:
=item update
=item update_dry_run
=item upgrade
=back
@@ -205,6 +207,10 @@ Remove the database entries for the given field, can not be undone!
This will add tables and columns to your SQL database to bring it in-line with your current configuration. It will not remove data. Use with caution on a live database. Database backup is recommended befire use on live systems.
=item B<epadmin> update_dry_run I<repository_id>
This will tell you which tables and columns will be added to your SQL database to bring it in-line with your current configuration. As update does not remove any data it will not tell you about any tables or columns that are in your database but not in your current configuration.
=item B<epadmin> upgrade I<repository_id>
After upgrading EPrints, use this to update the database tables. It will advise any other tasks that are required.
@@ -331,6 +337,7 @@ else
elsif( $action eq "schema" ) { schema( $repoid ); }
elsif( $action eq "update_database_structure" ) { update_database_structure( $repoid ); }
elsif( $action eq "update" ) { update_database_structure( $repoid ); }
elsif( $action eq "update_dry_run" ) { update_database_structure( $repoid, 1 ); }
elsif( $action eq "upgrade_mysql_charset" ) { upgrade_mysql_charset( $repoid ); }
elsif( $action eq "rebuild_triples" ) { rebuild_triples( $repoid, @ARGV ); }
elsif( $action eq "recommit" )
@@ -1800,14 +1807,16 @@ sub rehash
# undocumented option - use with caution!
sub update_database_structure
{
my( $repoid ) = @_;
my $repoid = shift;
my $dry_run = 0;
$dry_run = shift if @_;

my $repo = &repository( $repoid );

my $db = $repo->get_db();

update_datasets( $repo, $db );
update_counters( $repo, $db );
update_datasets( $repo, $db, $dry_run );
update_counters( $repo, $db, $dry_run );
}

###################################
@@ -2198,7 +2207,10 @@ sub migrate_to_multilang
# This will check for any missing datasets or fields and add them
sub update_datasets
{
my( $repo, $db ) = @_;
my $repo = shift;
my $db = shift;
my $dry_run = 0;
$dry_run = shift if @_;

my $success = 1;
my $count = 0;
@@ -2209,7 +2221,12 @@ sub update_datasets
my $dataset = $repo->dataset( $_ );
if( !$db->has_dataset( $dataset ) )
{
if( $db->create_dataset_tables( $dataset ) )
if ( $dry_run )
{
++$count;
print "Dry run: Added dataset $_\n";
}
elsif( $db->create_dataset_tables( $dataset ) )
{
++$count;
print "Added dataset $_\n" if $noise;
@@ -2225,29 +2242,23 @@ sub update_datasets
next if defined $field->get_property( "sub_name" );
if( !$db->has_field( $dataset, $field ) )
{
if( $db->add_field( $dataset, $field ) )
if ( $dry_run )
{
++$field_count;
print "Dry run: Added ".$field->get_name." to dataset $_\n";
}
elsif( $db->add_field( $dataset, $field ) )
{
++$field_count;
print "Added ".$field->get_name." to dataset $_\n" if $noise;

# perhaps we can pre-populate with existing data:

# 1- if field changes from single to multiple values
if( $field->get_property( 'multiple' ) && !$field->is_virtual )
{
if( $db->has_column( $dataset->get_sql_table_name, $field->get_name ) )
{
migrate_to_multiple_values( $db, $dataset, $field );
}
}
# 2- if field changes from 'text' to 'multilang' (language will default to repository's default language
elsif( $field->isa( "EPrints::MetaField::Multilang" ) )
{
if( $db->has_column( $dataset->get_sql_table_name, $field->get_name ) )
{
migrate_to_multilang( $repo, $db, $dataset, $field );
}
}
}
else
{
@@ -2266,12 +2277,19 @@ sub update_datasets
my @cols = $db->get_primary_key( $table );
if( @cols != 2 || $cols[0] ne $dataset->key_field->get_sql_name || $cols[1] ne "pos" )
{
if( @cols )
if ( $dry_run )
{
$db->do("ALTER TABLE ".$db->quote_identifier($table)." DROP PRIMARY KEY");
print "Dry run: Fixed PRIMARY KEY on $table\n";
}
else
{
if( @cols )
{
$db->do("ALTER TABLE ".$db->quote_identifier($table)." DROP PRIMARY KEY");
}
$db->do("ALTER TABLE ".$db->quote_identifier($table)." ADD PRIMARY KEY (".$db->quote_identifier($dataset->key_field->get_sql_name).",".$db->quote_identifier("pos").")");
print "Fixed PRIMARY KEY on $table\n";
}
$db->do("ALTER TABLE ".$db->quote_identifier($table)." ADD PRIMARY KEY (".$db->quote_identifier($dataset->key_field->get_sql_name).",".$db->quote_identifier("pos").")");
print "Fixed PRIMARY KEY on $table\n";
}
}
# check the __rindex collation
@@ -2281,7 +2299,13 @@ sub update_datasets
my $table = $dataset->get_sql_rindex_table_name();
foreach my $col (qw( field word ))
{
my $sth = $db->prepare(<<EOS);
if ( $dry_run )
{
print "Dry run: Fixed $table.$col collation\n";
}
else
{
my $sth = $db->prepare(<<EOS);
SELECT
COLLATION_NAME
FROM
@@ -2293,25 +2317,37 @@ WHERE
LOWER(COLLATION_NAME)!='utf8_bin'
LIMIT 1
EOS
$db->execute( $sth );
my( $collate ) = $sth->fetchrow_array or next;
$db->do("ALTER TABLE ".$db->quote_identifier($table)." CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin");
print "Fixed $table.$col collation\n" if $noise;
$db->execute( $sth );
my( $collate ) = $sth->fetchrow_array or next;
$db->do("ALTER TABLE ".$db->quote_identifier($table)." CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin");
print "Fixed $table.$col collation\n" if $noise;
}
last;
}
}
}

print "$count datasets added\n" if $noise;
print "$field_count fields added\n" if $noise;

if ( $dry_run )
{
print "Dry run: $count datasets added\n";
print "Dry run: $field_count fields added\n";
}
else
{
print "$count datasets added\n" if $noise;
print "$field_count fields added\n" if $noise;
}

return $success;
}

# This will check for any missing counters and add them
sub update_counters
{
my( $repo, $db ) = @_;
my $repo = shift;
my $db = shift;
my $dry_run = 0;
$dry_run = shift if @_;

my $success = 1;
my $count = 0;
@@ -2320,7 +2356,12 @@ sub update_counters
{
if( !$db->has_counter( $_ ) )
{
if( $db->create_counter( $_ ) )
if ( $dry_run )
{
++$count;
print "Dry run: Added counter $_\n";
}
elsif( $db->create_counter( $_ ) )
{
++$count;
print "Added counter $_\n" if $noise;
@@ -2333,8 +2374,14 @@ sub update_counters
}
}

print "$count counters were added\n" if $noise;

if ( $dry_run )
{
print "Dry run: $count counters were added\n";
}
else
{
print "$count counters were added\n" if $noise;
}
return $success;
}

0 comments on commit 13c2c3f

Please sign in to comment.
You can’t perform that action at this time.