script/mojomojo_spawn_db.pl untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 82 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/env perl
2  
3 =head1 NAME
4  
5 mojomojo_spawn_db.pl - hash plain text passwords
6  
7 =head1 AUTHOR
8  
9 Jonathan Rockway <jrockway@cpan.org>
10  
11 =head1 COPYRIGHT
12  
13 2007 Jonathan Rockway <jrockway@cpan.org>
14  
15 =cut
16  
17 BEGIN { $ENV{CATALYST_DEBUG} = 0 }
18 use strict;
19 use warnings;
20 use FindBin '$Bin';
21 use lib "$Bin/../lib";
22 use MojoMojo::Schema;
23 use Config::JFDI;
24 use Getopt::Long;
25  
26 my $jfdi = Config::JFDI->new(name => "MojoMojo");
27 my $config = $jfdi->get;
28  
29 my ($dsn, $user, $password, $unicode_option);
30  
31 my $default_user = $ENV{USER} || 'unknown';
32 my %opts = (
33 wiki_name => $config->{name},
34 admin_username => 'admin',
35 admin_password => 'admin',
36 admin_fullname => $default_user,
37 admin_email => "$default_user\@localhost",
38 anonymous_email => 'anonymous.coward@localhost',
39 );
40  
41 my $help;
42 GetOptions(
43 'help' => \$help,
44 'dsn:s' => \$dsn,
45 'db-user:s' => \$user,
46 'db-password:s' => \$password,
47 'wiki:s' => \$opts{wiki_name},
48 'admin-username:s' => \$opts{admin_username},
49 'admin-password:s' => \$opts{admin_password},
50 'admin-fullname:s' => \$opts{admin_fullname},
51 'admin-email:s' => \$opts{admin_email},
52 'anon-email:s' => \$opts{anonymous_email},
53 );
54  
55 if ($help) {
56 print <<"EOF";
57  
58 mojomojo_spawn_db.pl ...
59  
60 This script looks in the mojomojo.conf file for database connection
61 info if none is passed on the command line. Set the MOJOMOJO_CONFIG
62 environment variable to tell it where the file is.
63  
64 Accepts the following options:
65  
66 --dsn Default taken from mojomojo.conf
67 --db-user Default taken from mojomojo.conf
68 --db-password Default taken from mojomojo.conf
69 --wiki Wiki name, default taken from mojomojo.conf
70 --admin-username Admin username, default is admin
71 --admin-password Admin password, default is admin
72 --admin-fullname Admin name, default is $default_user
73 --admin-email Admin email address, default is $default_user\@localhost
74 --anon-email Anon user email address, default is anonymous.coward\@localhost
75  
76 EOF
77  
78 exit;
79 }
80  
81  
82 eval {
83 if (!$dsn)
84 {
85 if (ref $config->{'Model::DBIC'}->{'connect_info'}) {
86 $dsn = $config->{'Model::DBIC'}->{'connect_info'}->{dsn};
87 $user = $config->{'Model::DBIC'}->{'connect_info'}->{user};
88 $password = $config->{'Model::DBIC'}->{'connect_info'}->{password};
89  
90 # Determine database type amongst: SQLite, Pg or MySQL
91 my $db_type = lc($dsn =~ m/^dbi:(\w+)/);
92 my %unicode_connection_for_db = (
93 'sqlite' => { sqlite_unicode => 1 },
94 'pg' => { pg_enable_utf8 => 1 },
95 'mysql' => { mysql_enable_utf8 => 1 },
96 );
97 $unicode_option = $unicode_connection_for_db{$db_type};
98 }
99 else {
100 $dsn = $config->{'Model::DBIC'}->{'connect_info'};
101 }
102 }
103 };
104 if ($@) {
105 die "Your DSN line in mojomojo.conf doesn't look like a valid DSN."
106 . " Add one, or pass it on the command line.";
107 }
108 die "No valid Data Source Name (DSN).\n" if !$dsn;
109 $dsn =~ s/__HOME__/$FindBin::Bin\/\.\./g;
110  
111 my $schema = MojoMojo::Schema->connect($dsn, $user, $password, $unicode_option)
112 or die "Failed to connect to database";
113  
114 # Check if database is already deployed by
115 # examining if the table Person exists and has a record.
116 eval { $schema->resultset('MojoMojo::Schema::Result::Person')->count };
117 if (!$@) {
118 die "You have already deployed your database\n";
119 }
120  
121 print <<"EOF";
122  
123 Creating a new wiki ...
124  
125 dsn: $dsn
126 wiki name: $opts{wiki_name}
127 admin username: $opts{admin_username}
128 admin password: $opts{admin_password}
129 admin name: $opts{admin_fullname}
130 admin email: $opts{admin_email}
131 anon email: $opts{anonymous_email}
132  
133 EOF
134  
135 print "Deploying schema to $dsn\n";
136 $schema->deploy;
137  
138 $schema->create_initial_data($config, \%opts);