1
package Padre::DB::Migrate;
3
# This is a highly modified variant of ORLite::Migrate
10
use File::Spec 3.2701 ();
11
use File::Path 2.04 ();
12
use File::Basename ();
13
use Params::Util 0.37 ();
15
use DBD::SQLite 1.21 ();
18
our $VERSION = '0.90';
22
my $class = ref $_[0] || $_[0];
24
# Check for debug mode
26
if ( defined Params::Util::_STRING( $_[-1] ) and $_[-1] eq '-DEBUG' ) {
31
# Check params and apply defaults
33
if ( defined Params::Util::_STRING( $_[1] ) ) {
35
# Migrate needs at least two params
36
Carp::croak("Padre::DB::Migrate must be invoked in HASH form");
37
} elsif ( Params::Util::_HASH( $_[1] ) ) {
40
Carp::croak("Missing, empty or invalid params HASH");
42
$params{create} = $params{create} ? 1 : 0;
44
defined Params::Util::_STRING( $params{file} )
49
Carp::croak("Missing or invalid file param");
51
unless ( defined $params{readonly} ) {
52
$params{readonly} = $params{create} ? 0 : !-w $params{file};
54
unless ( defined $params{tables} ) {
57
unless ( defined $params{package} ) {
58
$params{package} = scalar caller;
60
unless ( Params::Util::_CLASS( $params{package} ) ) {
61
Carp::croak("Missing or invalid package class");
64
# We don't support readonly databases
65
if ( $params{readonly} ) {
66
Carp::croak("Padre::DB::Migrate does not support readonly databases");
69
# Get the schema version
70
my $file = File::Spec->rel2abs( $params{file} );
71
my $created = !-f $params{file};
74
# Create the parent directory
75
my $dir = File::Basename::dirname($file);
77
my @dirs = File::Path::mkpath( $dir, { verbose => 0 } );
78
$class->prune(@dirs) if $params{prune};
80
$class->prune($file) if $params{prune};
83
# We're done with the prune setting now
86
# Get the current schema version
87
my $dsn = "dbi:SQLite(AutoCommit=>1,RaiseError=>1,PrintError=>0):$file";
88
my $dbh = DBI->connect($dsn);
89
my $version = $dbh->selectrow_arrayref('pragma user_version')->[0];
90
my $want = $params{user_version};
92
# Attempt to roll the schema version forwards
93
if ( $want and $want > $version ) {
94
require Padre::DB::Timeline;
95
Padre::DB::Timeline->new( dbh => $dbh )->upgrade($want);
96
Class::Unload->unload('Padre::DB::Timeline');
99
# We are finished with the database
102
local $SIG{__WARN__} = sub {
103
return if $_[0] =~ /Subroutine \w+ redefined at/;
107
# Hand off to the regular constructor
108
$class->SUPER::import(
110
$DEBUG ? '-DEBUG' : ()
116
# Copyright 2008-2011 The Padre development team as listed in Padre.pm.
118
# This program is free software; you can redistribute it and/or
119
# modify it under the same terms as Perl 5 itself.