Skip to content
Snippets Groups Projects
Commit dec19837 authored by Gerhard Gonter's avatar Gerhard Gonter :speech_balloon:
Browse files

first attempt

parents
No related branches found
No related tags found
No related merge requests found
package Redmine::DB;
use strict;
sub new
{
my $class= shift;
my $self={};
bless $self, $class;
$self->set (@_);
$self;
}
sub set
{
my $self= shift;
my %par= @_;
foreach my $an (keys %par) { $self->{$an}= $par{$an}; }
}
1;
package Redmine::DB::MySQL;
use strict;
use parent 'Redmine::DB';
# use Redmine::DB::Project;
my $show_fetched= 0;
sub connect
{
my $self= shift;
my $dbh= $self->{'_dbh'};
return $dbh if (defined ($dbh));
my $db_con= join (':', 'dbi', map { $self->{$_} } qw(adapter database host));
print "db_con=[$db_con]\n";
$dbh= DBI->connect($db_con, map { $self->{$_} } qw(username password));
print "dbh=[$dbh]\n";
$self->{'_dbh'}= $dbh;
}
sub table
{
my $self= shift;
my $table= shift;
my $t= $self->{$table};
$t= $self->{$table}= {} unless (defined ($t));
# print "accessing table=[$table]: ", main::Dumper($self);
$t;
}
sub get_all_x
{
my $self= shift;
my $table= shift;
my $where= shift;
my $dbh= $self->connect();
return undef unless (defined ($dbh));
# my $project= new Redmine::DB::Project (%par);
# print "project: ", main::Dumper ($project);
my $ss = qq/SELECT * FROM $table/;
my @v= ();
if (defined ($where))
{
print "where: ", main::Dumper ($where);
$ss .= ' WHERE ' . shift (@$where);
@v= @$where;
}
print "ss=[$ss]\n";
my $sth= $dbh->prepare($ss) or print $dbh->errstr;
print "sth=[$sth]\n";
$sth->execute(@v);
my $t= $self->table($table);
while (defined (my $x= $sth->fetchrow_hashref()))
{
print "x: ", main::Dumper ($x) if ($show_fetched);
$t->{$x->{'id'}}= $x;
}
$t;
}
=head1 REDMINE STUFF
before that, this might be usable for other Rails applications
TODO: factor out...
=cut
sub get_all_projects { shift->get_all_x ('projects'); }
sub get_all_users { shift->get_all_x ('users'); }
sub get_project_members
{
my $self= shift;
my $proj_id= shift;
$self->get_all_x ('members', [ 'project_id=?', $proj_id ]);
}
=head1 REDMINE SYNC STUFF
=cut
sub get_members
{
my $self= shift;
my $proj_id= shift;
my $res= $self->table('x_sync');
my $proj= $self->get_all_x ('projects', [ 'id=?', $proj_id ]);
my $members= $self->get_all_x ('members', [ 'project_id=?', $proj_id ]);
$res->{'project'}= $proj;
$res->{'members'}= $members;
print "proj: ", main::Dumper($proj);
# --------------------------------------------------------------------
# check for members and users
my $users= $self->table('users');
# print "users: ", main::Dumper($users);
my @missing_users=();
foreach my $member_id (keys %$members)
{
# my $member= $members->{$member_id};
# my $user_id= $member->{'user_id'};
my $user_id= $members->{$member_id}->{'user_id'};
push (@missing_users, $user_id) unless (exists ($users->{$user_id}));
# last if (@missing_users > 3);
}
if (@missing_users)
{
print "missing users: [", join (' ', @missing_users), "]\n";
my $in= 'id IN ('. join(',', map { '?' } @missing_users) . ')';
# $show_fetched= 1;
$self->get_all_x ('users', [ $in, @missing_users ]),
}
$res;
}
sub get_wiki
{
my $self= shift;
my $proj_id= shift;
my $res= $self->table('x_sync');
# --------------------------------------------------------------------
# check for wiki stuff
my $wikis= $self->get_all_x ('wikis', [ 'project_id=?', $proj_id ]);
if (defined ($wikis))
{
$res->{'wikis'}= $wikis;
# CHECK: there shouldn't be more than wiki per project, right?
my @wiki_ids= keys %$wikis;
if (@wiki_ids > 1)
{
print "ATTN: too many(?) wikis for project=$proj_id ";
print main::Dumper ($wikis);
}
foreach my $wiki_id (@wiki_ids)
{
my $wiki_pages= $self->get_all_x ('wiki_pages', [ 'wiki_id=?', $proj_id ]);
print "wiki_id=[$wiki_id] wiki_pages: ", main::Dumper ($wiki_pages);
}
}
$res;
}
1;
__END__
package Redmine::DB::Project;
use strict;
use parent 'Redmine::DB::MySQL';
1;
t_sync.pl 0 → 100755
#!/usr/bin/perl
use strict;
use warnings;
=head1 NAME
t_sync.pl
=head1 DESCRIPTION
do stuff on a Redmine database
=head1 OPERATION MODES
=cut
use lib 'lib';
binmode( STDOUT, ':utf8' );
use YAML::Syck;
# use XML::LibXML;
use DBI;
use DBD::mysql;
use Encode;
use Data::Dumper;
$Data::Dumper::Indent= 1;
use Redmine::DB::MySQL;
# use Redmine::DB::Project; nothing here yet ...
# yeah, this should be in a config file, but
my $setup_file;
my $setup=
{
'src' =>
{
'config' => '/home/gg/etc/src/database.yml',
'db' => 'production',
},
'dst' =>
{
'config' => '/home/gg/etc/dst/database.yml',
'db' => 'production',
},
'sync_projects' =>
[
{
'src_proj' => 170,
'dst_proj' => 1,
}
]
};
my @parameters= ();
my $op_mode= 'usage';
my %op_modes= map { $_ => 1 } qw(sync sdp prep);
while (my $arg= shift (@ARGV))
{
if ($arg eq '--') { push (@parameters, @ARGV); @ARGV= (); }
elsif ($arg =~ m#^--(.+)#)
{
my $opt= $1;
# print "opt=[$opt]\n";
if (exists ($op_modes{$opt})) { $op_mode= $opt; }
elsif ($opt eq 'help') { usage(); }
else { die "unknown option [$opt]"; }
}
elsif ($arg =~ m#^-(.+)#)
{
my @opts= split ('|', $1);
# print "opts: ", Dumper (\@opts);
foreach my $opt (@opts)
{
if ($opt eq 'h') { usage(); }
else { die "unknown option [$opt]"; }
}
}
else { push (@parameters, $arg); }
}
sub usage
{
system ('perldoc', $0);
exit (0);
}
if ($op_mode eq 'usage') { usage(); }
elsif ($op_mode eq 'prep')
{
my $dst= read_configs($setup, 'dst');
prepare_sync_table ($dst);
}
elsif ($op_mode eq 'sdp')
{
my $dst= read_configs($setup, 'dst');
my $dst_proj= $dst->get_all_projects();
print "dst_proj: ", Dumper ($dst_proj);
}
elsif ($op_mode eq 'sync')
{
my $src= read_configs($setup, 'src');
my $dst= read_configs($setup, 'dst');
# print "setup: ", Dumper ($setup);
foreach my $sp (@{$setup->{'sync_projects'}})
{
print "sp: ", Dumper ($sp);
my $sx= $src->get_project_members ($sp->{'src_proj'});
print "sx: ", Dumper ($sx);
}
}
elsif ($op_mode eq 'diag')
{
my $src= read_configs($setup, 'src');
my $dst= read_configs($setup, 'dst');
# print "setup: ", Dumper ($setup);
# my $src_proj= $src->get_all_projects();
# print "src_proj: ", Dumper ($src_proj);
# my $src_usr= $src->get_all_users();
# print "src_usr: ", Dumper ($src_usr);
# print "src: ", Dumper ($src);
# my $src_members= $src->get_project_members (170);
# print "src_members: ", Dumper ($src_members);
# print "src: ", Dumper ($src);
}
else
{
die("unknown op_mode=[$op_mode]");
}
exit (0);
sub read_configs
{
my $stp= shift;
my $s= shift;
my $ss= $stp->{$s};
my ($yml, $db)= map { $ss->{$_} } qw(config db);
print "s=[$s] yml=[$yml] db=[$db]\n";
my $x= YAML::Syck::LoadFile ($yml);
# $ss->{'_cfg'}=
my $c= $x->{$db};
$c->{'adapter'}= 'mysql' if ($c->{'adapter'} eq 'mysql2');
my $m= new Redmine::DB::MySQL (%$c);
$ss->{'m'}= $m;
my $dbh= $m->connect();
$m;
}
sub prepare_sync_table
{
my $con= shift;
my $dbh= $con->connect();
print "dbh=[$dbh]\n";
my $ss= <<'EOX';
CREATE TABLE `syncs`
(
`id` int(11) NOT NULL AUTO_INCREMENT,
`context_id` int(11) DEFAULT NULL,
`table` varchar(255) DEFAULT NULL,
`src_id` int(11) DEFAULT NULL,
`dst_id` int(11) DEFAULT NULL,
`sync_date` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
EOX
print "perform this on the database\n", "--- 8< ---\n", $ss, "--- >8 ---\n";
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment