From 29d5647f28bf74d1b214d84b9efd14779950bf52 Mon Sep 17 00:00:00 2001 From: franck cuny Date: Mon, 14 Feb 2011 21:27:39 +0100 Subject: script to deploy and migrate DB schema --- scripts/jitterbug_db | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100755 scripts/jitterbug_db (limited to 'scripts/jitterbug_db') diff --git a/scripts/jitterbug_db b/scripts/jitterbug_db new file mode 100755 index 0000000..119b364 --- /dev/null +++ b/scripts/jitterbug_db @@ -0,0 +1,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 -- cgit 1.4.1