about summary refs log tree commit diff
path: root/scripts/jitterbug_db
blob: 119b3648f30174932e53738a9eca9f5bf598cad5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env perl
use strict;
use warnings;

use YAML qw/LoadFile/;
use Getopt::Long;
use Pod::Usage;

use DBIx::Class::DeploymentHandler;
use SQL::Translator;
use jitterbug::Schema;

GetOptions(
    'deploy'   => \my $deploy,
    'migrate'  => \my $migrate,
    'config=s' => \my $config,
);

pod2usage(-exitstatus => 0, -verbose => 2) if !$config;

die "need configuration file" unless $config;

my $jitterbug_conf = LoadFile($config);
my $dbix_conf      = $jitterbug_conf->{plugins}->{DBIC}->{schema};
my $schema = jitterbug::Schema->connect( @{ $dbix_conf->{connect_info} } );

if ($deploy) {
    _deploy();
}
elsif ($migrate) {
    _migrate();
}
else {
    pod2usage(1);
}

sub _deploy {
    print "deploying jitterbug::Schema...\n";
    $schema->deploy;
    print "done!\n";
}

sub _migrate {
    my $version = jitterbug::Schema->VERSION;
    die "version is missing in the schema" unless $version;

    print "processing version $version of jitterbug::Schema...\n";

    my $dh = DBIx::Class::DeploymentHandler->new(
        {
            schema              => $schema,
            databases           => [qw/ SQLite PostgreSQL MySQL /],
            sql_translator_args => { add_drop_table => 0, },
        }
    );

    print "generating deployment script\n";
    $dh->prepare_install;

    if ( $version > 1 ) {
        print "generating upgrade script\n";
        $dh->prepare_upgrade(
            {
                from_version => $version - 1,
                to_version   => $version,
                version_set  => [ $version - 1, $version ],
            }
        );

        print "generating downgrade script\n";
        $dh->prepare_downgrade(
            {
                from_version => $version,
                to_version   => $version - 1,
                version_set  => [ $version, $version - 1 ],
            }
        );
    }

    print "done\n";
}

__END__

=head1 NAME

jitterbug_db - manage your jitterbug database

=head1 SYNOPSIS

jitterbug_db [options]

  Options:
   --config   path to the configuration file
   --deploy   deploy the schema
   --migrate  migrate the schema to a newer version

=head1 OPTIONS

=over 4

=item B<--config>

path to the configuration file. The configuration file should contain information about the connection to the database

=item B<--deploy>

Instruct to deploy the schema

=item B<--migrate>

Instruct to migrate the schema to another version

=back

=cut