| 1 | package Mojo::SQLite::Migrations; | ||||||||||||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use Mojo::File 'path'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Mojo::Loader 'data_section'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Mojo::Util 'decode'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use constant DEBUG => $ENV{MOJO_MIGRATIONS_DEBUG} || 0; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MOJO_MIGRATIONS_DEBUG'} || 0
MC/DC0%0/1
$ENV{'MOJO_MIGRATIONS_DEBUG'} || 0
$ENV{'MOJO_MIGRATIONS_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MOJO_MIGRATIONS_DEBUG'}
| |||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||
| 11 | 6 | our $VERSION = 'v4.0.0'; | |||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||
| 13 | 6 | has name => 'migrations'; | |||||||||||||||||||||||||||||||||||||
| 14 | 6 | has sqlite => undef, weak => 1; | |||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | 23 | ▶ | sub active { $_[0]->_active($_[0]->sqlite->db) } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
active: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||
| 18 | 8 | ▶ | sub from_data { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
from_data: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 19 | 8 | my ($self, $class, $name) = @_; | |||||||||||||||||||||||||||||||||||||
| 20 | 8 | ▶ | return $self->from_string( | ||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
$class //= caller
$name // $self->name
MC/DC0%0/2
$class //= caller
$class
caller
MC/DC0%0/2
$name // $self->name
$name
$self->name
Truth table66%2/3
A //= B
A= $classB= caller
Truth table66%2/3
A // B
A= $nameB= $self->name
| |||||||||||||||||||||||||||||||||||||||
| 21 | data_section($class //= caller, $name // $self->name)); | ||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | sub from_file { shift->from_string(decode 'UTF-8', path(pop)->slurp) } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
from_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||
| 26 | 15 | ▶ | sub from_string { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
from_string: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 27 | 15 | my ($self, $sql) = @_; | |||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||
| 29 | 15 | my ($version, $way); | |||||||||||||||||||||||||||||||||||||
| 30 | 15 | my $migrations = $self->{migrations} = {up => {}, down => {}}; | |||||||||||||||||||||||||||||||||||||
| 31 | 15 | ▶ | for my $line (split "\n", $sql // '') { | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$sql // ""
MC/DC100%1/1
$sql // ""
$sql
Truth table100%2/2
A
A= $sql
| |||||||||||||||||||||||||||||||||||||||
| 32 | 74 | ▶ | ($version, $way) = ($1, lc $2) if $line =~ /^\s*--\s*(\d+)\s*(up|down)/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 33 | 74 | ▶ | $migrations->{$way}{$version} .= "$line\n" if $version; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||
| 36 | 15 | return $self; | |||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||
| 39 | 34 | ▶ | sub latest { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
latest: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 40 | 34 | ▶ | (sort { $a <=> $b } keys %{shift->{migrations}{up}})[-1] || 0; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||
| 43 | 25 | ▶ | sub migrate { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
migrate: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 44 | 25 | my ($self, $target) = @_; | |||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||
| 46 | # Unknown version | ||||||||||||||||||||||||||||||||||||||
| 47 | 25 | my $latest = $self->latest; | |||||||||||||||||||||||||||||||||||||
| 48 | 25 | ▶ | $target //= $latest; | ||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$target //= $latest
MC/DC0%0/2
$target //= $latest
$target
$latest
Truth table66%2/3
A //= B
A= $targetB= $latest
| |||||||||||||||||||||||||||||||||||||||
| 49 | 25 | my ($up, $down) = @{$self->{migrations}}{qw(up down)}; | |||||||||||||||||||||||||||||||||||||
| 50 | 25 | ▶ | croak "Version $target has no migration" if $target != 0 && !$up->{$target}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$target != 0 and not $up->{$target}
MC/DC100%2/2
$target != 0 and not $up->{$target}
$target != 0
not $up->{$target}
Truth table100%3/3
A and not(B)
A= $target != 0B= not $up->{$target}
| |||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||
| 52 | # Already the right version (make sure migrations table exists) | ||||||||||||||||||||||||||||||||||||||
| 53 | 24 | my $db = $self->sqlite->db; | |||||||||||||||||||||||||||||||||||||
| 54 | 24 | ▶ | return $self if $self->_active($db, 0) == $target; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||
| 56 | # Lock migrations table and check version again | ||||||||||||||||||||||||||||||||||||||
| 57 | 22 | my $tx = $db->begin('exclusive'); | |||||||||||||||||||||||||||||||||||||
| 58 | 22 | ▶ | return $self if (my $active = $self->_active($db, 1)) == $target; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||
| 60 | # Newer version | ||||||||||||||||||||||||||||||||||||||
| 61 | 22 | ▶ | croak "Active version $active is greater than the latest version $latest" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 62 | if $active > $latest; | ||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||
| 64 | 20 | my $query = $self->sql_for($active, $target); | |||||||||||||||||||||||||||||||||||||
| 65 | 20 | warn "-- Migrate ($active -> $target)\n$query\n" if DEBUG; | |||||||||||||||||||||||||||||||||||||
| 66 | 20 | local $db->dbh->{sqlite_allow_multiple_statements} = 1; | |||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||
| 68 | # Disable update hook during migrations | ||||||||||||||||||||||||||||||||||||||
| 69 | 20 | my $hook = $db->dbh->sqlite_update_hook(undef); | |||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||
| 71 | # Catch the error so we can croak it | ||||||||||||||||||||||||||||||||||||||
| 72 | 20 | my ($errored, $error, $result); | |||||||||||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||||||||||||
| 74 | 20 | local $@; | |||||||||||||||||||||||||||||||||||||
| 75 | 20 | ▶ | eval { $result = $db->dbh->do($query); 1 } or $errored = 1; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 76 | 20 | ▶ | $error = $@ if $errored; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||
| 79 | # Re-enable update hook | ||||||||||||||||||||||||||||||||||||||
| 80 | 20 | $db->dbh->sqlite_update_hook($hook); | |||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||
| 82 | 20 | ▶ | croak $error if $errored; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 83 | 19 | ▶ | return $self unless defined $result; # RaiseError disabled | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||
| 85 | 19 | ▶ | $db->query('update mojo_migrations set version = ? where name = ?', | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 86 | $target, $self->name) and $tx->commit; | ||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||
| 88 | 19 | return $self; | |||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||
| 91 | 25 | ▶ | sub sql_for { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_for: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 92 | 25 | my ($self, $from, $to) = @_; | |||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||
| 94 | # Up | ||||||||||||||||||||||||||||||||||||||
| 95 | 25 | my ($up, $down) = @{$self->{migrations}}{qw(up down)}; | |||||||||||||||||||||||||||||||||||||
| 96 | 25 | ▶ | if ($from < $to) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 97 | 13 | ▶ | my @up = grep { $_ <= $to && $_ > $from } keys %$up; | ||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$_ <= $to && $_ > $from
MC/DC100%2/2
$_ <= $to && $_ > $from
$_ <= $to
$_ > $from
Truth table100%3/3
A && B
A= $_ <= $toB= $_ > $from
| |||||||||||||||||||||||||||||||||||||||
| 98 | 13 | return join '', @$up{sort { $a <=> $b } @up}; | |||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||
| 101 | # Down | ||||||||||||||||||||||||||||||||||||||
| 102 | 12 | ▶ | my @down = grep { $_ > $to && $_ <= $from } keys %$down; | ||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$_ > $to && $_ <= $from
MC/DC100%2/2
$_ > $to && $_ <= $from
$_ > $to
$_ <= $from
Truth table100%3/3
A && B
A= $_ > $toB= $_ <= $from
| |||||||||||||||||||||||||||||||||||||||
| 103 | 12 | return join '', @$down{reverse sort { $a <=> $b } @down}; | |||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||
| 106 | 69 | ▶ | sub _active { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_active: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||
| 107 | 69 | my ($self, $db, $create) = @_; | |||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||
| 109 | 69 | my $name = $self->name; | |||||||||||||||||||||||||||||||||||||
| 110 | 69 | my $results; | |||||||||||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||||||||||||
| 112 | 69 | local $db->dbh->{RaiseError} = 0; | |||||||||||||||||||||||||||||||||||||
| 113 | 69 | my $query = 'select version from mojo_migrations where name = ?'; | |||||||||||||||||||||||||||||||||||||
| 114 | 69 | $results = $db->query($query, $name); | |||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||
| 116 | 69 | ▶ | my $next = $results ? $results->array : undef; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 117 | 69 | ▶ | if ($next || !$create) { return $next->[0] || 0 } | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
$next or not $create
$next->[0] || 0
MC/DC100%2/2
$next or not $create
$next
not $create
MC/DC100%1/1
$next->[0] || 0
$next->[0]
Truth table100%3/3
A or not(B)
A= $nextB= not $create
Truth table100%2/2
A
A= $next->[0]
| |||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||
| 119 | $db->query( | ||||||||||||||||||||||||||||||||||||||
| 120 | 7 | ▶ | 'create table if not exists mojo_migrations ( | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $results or $results->sth->err
MC/DC50%1/2
not $results or $results->sth->err
not $results
$results->sth->err
Truth table66%2/3
not(A) or B
A= not $resultsB= $results->sth->err
| |||||||||||||||||||||||||||||||||||||||
| 121 | name text not null primary key, | ||||||||||||||||||||||||||||||||||||||
| 122 | version integer not null check (version >= 0) | ||||||||||||||||||||||||||||||||||||||
| 123 | )' | ||||||||||||||||||||||||||||||||||||||
| 124 | ) if !$results or $results->sth->err; | ||||||||||||||||||||||||||||||||||||||
| 125 | 7 | $db->query('insert into mojo_migrations values (?, ?)', $name, 0); | |||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||
| 127 | 7 | return 0; | |||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||
| 130 | 6 | 1; | |||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||
| 132 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||
| 134 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||
| 136 | Mojo::SQLite::Migrations - Migrations | ||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||
| 138 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||
| 140 | use Mojo::SQLite::Migrations; | ||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||
| 142 | my $migrations = Mojo::SQLite::Migrations->new(sqlite => $sql); | ||||||||||||||||||||||||||||||||||||||
| 143 | $migrations->from_file('/home/dbook/migrations.sql')->migrate; | ||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||
| 145 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||
| 147 | L<Mojo::SQLite::Migrations> is used by L<Mojo::SQLite> to allow database | ||||||||||||||||||||||||||||||||||||||
| 148 | schemas to evolve easily over time. A migration file is just a collection of | ||||||||||||||||||||||||||||||||||||||
| 149 | sql blocks, with one or more statements, separated by comments of the form | ||||||||||||||||||||||||||||||||||||||
| 150 | C<-- VERSION UP/DOWN>. | ||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||
| 152 | -- 1 up | ||||||||||||||||||||||||||||||||||||||
| 153 | create table messages (message text); | ||||||||||||||||||||||||||||||||||||||
| 154 | insert into messages values ('I ♥ Mojolicious!'); | ||||||||||||||||||||||||||||||||||||||
| 155 | -- 1 down | ||||||||||||||||||||||||||||||||||||||
| 156 | drop table messages; | ||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||
| 158 | -- 2 up (...you can comment freely here...) | ||||||||||||||||||||||||||||||||||||||
| 159 | create table stuff (whatever integer); | ||||||||||||||||||||||||||||||||||||||
| 160 | -- 2 down | ||||||||||||||||||||||||||||||||||||||
| 161 | drop table stuff; | ||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||
| 163 | The idea is to let you migrate from any version, to any version, up and down. | ||||||||||||||||||||||||||||||||||||||
| 164 | Migrations are very safe, because they are performed in transactions and only | ||||||||||||||||||||||||||||||||||||||
| 165 | one can be performed at a time. If a single statement fails, the whole | ||||||||||||||||||||||||||||||||||||||
| 166 | migration will fail and get rolled back. Every set of migrations has a | ||||||||||||||||||||||||||||||||||||||
| 167 | L</"name">, which is stored together with the currently active version in an | ||||||||||||||||||||||||||||||||||||||
| 168 | automatically created table named C<mojo_migrations>. | ||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||
| 170 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||
| 172 | L<Mojo::SQLite::Migrations> implements the following attributes. | ||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||
| 174 | =head2 name | ||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||
| 176 | my $name = $migrations->name; | ||||||||||||||||||||||||||||||||||||||
| 177 | $migrations = $migrations->name('foo'); | ||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||
| 179 | Name for this set of migrations, defaults to C<migrations>. | ||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||
| 181 | =head2 sqlite | ||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||
| 183 | my $sql = $migrations->sqlite; | ||||||||||||||||||||||||||||||||||||||
| 184 | $migrations = $migrations->sqlite(Mojo::SQLite->new); | ||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||
| 186 | L<Mojo::SQLite> object these migrations belong to. Note that this attribute is | ||||||||||||||||||||||||||||||||||||||
| 187 | weakened. | ||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||
| 189 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||
| 191 | L<Mojo::SQLite::Migrations> inherits all methods from L<Mojo::Base> and | ||||||||||||||||||||||||||||||||||||||
| 192 | implements the following new ones. | ||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||
| 194 | =head2 active | ||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||
| 196 | my $version = $migrations->active; | ||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||
| 198 | Currently active version. | ||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||
| 200 | =head2 from_data | ||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||
| 202 | $migrations = $migrations->from_data; | ||||||||||||||||||||||||||||||||||||||
| 203 | $migrations = $migrations->from_data('main'); | ||||||||||||||||||||||||||||||||||||||
| 204 | $migrations = $migrations->from_data('main', 'file_name'); | ||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||
| 206 | Extract migrations from a file in the DATA section of a class with | ||||||||||||||||||||||||||||||||||||||
| 207 | L<Mojo::Loader/"data_section">, defaults to using the caller class and | ||||||||||||||||||||||||||||||||||||||
| 208 | L</"name">. | ||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||
| 210 | __DATA__ | ||||||||||||||||||||||||||||||||||||||
| 211 | @@ migrations | ||||||||||||||||||||||||||||||||||||||
| 212 | -- 1 up | ||||||||||||||||||||||||||||||||||||||
| 213 | create table messages (message text); | ||||||||||||||||||||||||||||||||||||||
| 214 | insert into messages values ('I ♥ Mojolicious!'); | ||||||||||||||||||||||||||||||||||||||
| 215 | -- 1 down | ||||||||||||||||||||||||||||||||||||||
| 216 | drop table messages; | ||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||
| 218 | =head2 from_file | ||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||
| 220 | $migrations = $migrations->from_file('/home/dbook/migrations.sql'); | ||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||
| 222 | Extract migrations from a file. | ||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||
| 224 | =head2 from_string | ||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||
| 226 | $migrations = $migrations->from_string( | ||||||||||||||||||||||||||||||||||||||
| 227 | '-- 1 up | ||||||||||||||||||||||||||||||||||||||
| 228 | create table foo (bar integer); | ||||||||||||||||||||||||||||||||||||||
| 229 | -- 1 down | ||||||||||||||||||||||||||||||||||||||
| 230 | drop table foo;' | ||||||||||||||||||||||||||||||||||||||
| 231 | ); | ||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||
| 233 | Extract migrations from string. | ||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||
| 235 | =head2 latest | ||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||
| 237 | my $version = $migrations->latest; | ||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||
| 239 | Latest version available. | ||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||
| 241 | =head2 migrate | ||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||
| 243 | $migrations = $migrations->migrate; | ||||||||||||||||||||||||||||||||||||||
| 244 | $migrations = $migrations->migrate(3); | ||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||
| 246 | Migrate from L</"active"> to a different version, up or down, defaults to using | ||||||||||||||||||||||||||||||||||||||
| 247 | L</"latest">. All version numbers need to be positive, with version C<0> | ||||||||||||||||||||||||||||||||||||||
| 248 | representing an empty database. | ||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||
| 250 | # Reset database | ||||||||||||||||||||||||||||||||||||||
| 251 | $migrations->migrate(0)->migrate; | ||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||
| 253 | =head2 sql_for | ||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||
| 255 | my $sql = $migrations->sql_for(5, 10); | ||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||
| 257 | Get SQL to migrate from one version to another, up or down. | ||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||
| 259 | =head1 DEBUGGING | ||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||
| 261 | You can set the C<MOJO_MIGRATIONS_DEBUG> environment variable to get some | ||||||||||||||||||||||||||||||||||||||
| 262 | advanced diagnostics information printed to C<STDERR>. | ||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||
| 264 | MOJO_MIGRATIONS_DEBUG=1 | ||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||
| 266 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||
| 268 | Report any issues on the public bugtracker. | ||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||
| 270 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||
| 272 | Dan Book, C<dbook@cpan.org> | ||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||
| 274 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||
| 276 | Copyright 2015, Dan Book. | ||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||
| 278 | This library is free software; you may redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||
| 279 | the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||
| 281 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||
| 283 | L<Mojo::SQLite> | ||||||||||||||||||||||||||||||||||||||