| 1 | package Mojo::Pg::Migrations; | ||
| 2 | use Mojo::Base -base; | ||
| 3 | |||
| 4 | use Carp qw(croak); | ||
| 5 | use Mojo::File qw(path); | ||
| 6 | use Mojo::Loader qw(data_section); | ||
| 7 | use Mojo::Util qw(decode); | ||
| 8 | |||
| 9 | use constant DEBUG => $ENV{MOJO_MIGRATIONS_DEBUG} || 0; | ||
| 10 | |||
| 11 | has name => 'migrations'; | ||
| 12 | has pg => undef, weak => 1; | ||
| 13 | |||
| 14 | sub active { $_[0]->_active($_[0]->pg->db) } | ||
| 15 | |||
| 16 | sub from_data { | ||
| 17 | my ($self, $class, $name) = @_; | ||
| 18 | return $self->from_string(data_section($class //= caller, $name // $self->name)); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub from_dir { | ||
| 22 | my ($self, $dir) = @_; | ||
| 23 | |||
| 24 | my $migrations = $self->{migrations} = {up => {}, down => {}}; | ||
| 25 | for my $file (path($dir)->list_tree({max_depth => 2})->each) { | ||
| 26 | next unless my ($way) = ($file->basename =~ /^(up|down)\.sql$/); | ||
| 27 | next unless my ($version) = ($file->dirname->basename =~ /^(\d+)$/); | ||
| 28 | $migrations->{$way}{$version} = decode 'UTF-8', $file->slurp; | ||
| 29 | } | ||
| 30 | |||
| 31 | return $self; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub from_file { shift->from_string(decode 'UTF-8', path(pop)->slurp) } | ||
| 35 | |||
| 36 | sub from_string { | ||
| 37 | my ($self, $sql) = @_; | ||
| 38 | |||
| 39 | my ($version, $way); | ||
| 40 | my $migrations = $self->{migrations} = {up => {}, down => {}}; | ||
| 41 | for my $line (split "\n", $sql // '') { | ||
| 42 | ($version, $way) = ($1, lc $2) if $line =~ /^\s*--\s*(\d+)\s*(up|down)/i; | ||
| 43 | $migrations->{$way}{$version} .= "$line\n" if $version; | ||
| 44 | } | ||
| 45 | |||
| 46 | return $self; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub latest { | ||
| 50 | (sort { $a <=> $b } keys %{shift->{migrations}{up}})[-1] || 0; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub migrate { | ||
| 54 | my ($self, $target) = @_; | ||
| 55 | |||
| 56 | # Unknown version | ||
| 57 | my $latest = $self->latest; | ||
| 58 | $target //= $latest; | ||
| 59 | my ($up, $down) = @{$self->{migrations}}{qw(up down)}; | ||
| 60 | croak "Version $target has no migration" if $target != 0 && !$up->{$target}; | ||
| 61 | |||
| 62 | # Already the right version (make sure migrations table exists) | ||
| 63 | my $db = $self->pg->db; | ||
| 64 | return $self if $self->_active($db) == $target; | ||
| 65 | |||
| 66 | # Lock migrations table and check version again | ||
| 67 | $db->query( | ||
| 68 | 'CREATE TABLE IF NOT EXISTS mojo_migrations ( | ||
| 69 | name TEXT PRIMARY KEY, | ||
| 70 | version BIGINT NOT NULL CHECK (version >= 0) | ||
| 71 | )' | ||
| 72 | ); | ||
| 73 | my $tx = $db->begin; | ||
| 74 | $db->query('LOCK TABLE mojo_migrations IN EXCLUSIVE MODE'); | ||
| 75 | return $self if (my $active = $self->_active($db)) == $target; | ||
| 76 | |||
| 77 | # Newer version | ||
| 78 | croak "Active version $active is greater than the latest version $latest" if $active > $latest; | ||
| 79 | |||
| 80 | my $sql = $self->sql_for($active, $target); | ||
| 81 | warn "-- Migrate ($active -> $target)\n$sql\n" if DEBUG; | ||
| 82 | $sql .= ';INSERT INTO mojo_migrations (name, version) VALUES ($2, $1)'; | ||
| 83 | $sql .= ' ON CONFLICT (name) DO UPDATE SET version = $1;'; | ||
| 84 | $db->query($sql, $target, $self->name) and $tx->commit; | ||
| 85 | |||
| 86 | return $self; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub sql_for { | ||
| 90 | my ($self, $from, $to) = @_; | ||
| 91 | |||
| 92 | # Up | ||
| 93 | my ($up, $down) = @{$self->{migrations}}{qw(up down)}; | ||
| 94 | if ($from < $to) { | ||
| 95 | my @up = grep { $_ <= $to && $_ > $from } keys %$up; | ||
| 96 | return join '', @$up{sort { $a <=> $b } @up}; | ||
| 97 | } | ||
| 98 | |||
| 99 | # Down | ||
| 100 | my @down = grep { $_ > $to && $_ <= $from } keys %$down; | ||
| 101 | return join '', @$down{reverse sort { $a <=> $b } @down}; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub _active { | ||
| 105 | my ($self, $db) = @_; | ||
| 106 | |||
| 107 | my $name = $self->name; | ||
| 108 | my $results; | ||
| 109 | { | ||
| 110 | local $db->dbh->{RaiseError} = 0; | ||
| 111 | my $sql = 'SELECT version FROM mojo_migrations WHERE name = $1'; | ||
| 112 | $results = $db->query($sql, $name); | ||
| 113 | }; | ||
| 114 | if (my $next = $results->array) { return $next->[0] || 0 } | ||
| 115 | return 0; | ||
| 116 | } | ||
| 117 | |||
| 118 | 1; | ||
| 119 | |||
| 120 | =encoding utf8 | ||
| 121 | |||
| 122 | =head1 NAME | ||
| 123 | |||
| 124 | Mojo::Pg::Migrations - Migrations | ||
| 125 | |||
| 126 | =head1 SYNOPSIS | ||
| 127 | |||
| 128 | use Mojo::Pg::Migrations; | ||
| 129 | |||
| 130 | my $migrations = Mojo::Pg::Migrations->new(pg => $pg); | ||
| 131 | $migrations->from_file('/home/sri/migrations.sql')->migrate; | ||
| 132 | |||
| 133 | =head1 DESCRIPTION | ||
| 134 | |||
| 135 | L<Mojo::Pg::Migrations> is used by L<Mojo::Pg> to allow database schemas to evolve easily over time. A migration file | ||
| 136 | is just a collection of sql blocks, with one or more statements, separated by comments of the form C<-- VERSION | ||
| 137 | UP/DOWN>. | ||
| 138 | |||
| 139 | -- 1 up | ||
| 140 | CREATE TABLE messages (message TEXT); | ||
| 141 | INSERT INTO messages VALUES ('I ♥ Mojolicious!'); | ||
| 142 | -- 1 down | ||
| 143 | DROP TABLE messages; | ||
| 144 | |||
| 145 | -- 2 up (...you can comment freely here...) | ||
| 146 | CREATE TABLE stuff (whatever INT); | ||
| 147 | -- 2 down | ||
| 148 | DROP TABLE stuff; | ||
| 149 | |||
| 150 | The idea is to let you migrate from any version, to any version, up and down. Migrations are very safe, because they | ||
| 151 | are performed in transactions and only one can be performed at a time. If a single statement fails, the whole migration | ||
| 152 | will fail and get rolled back. Every set of migrations has a L</"name">, which is stored together with the currently | ||
| 153 | active version in an automatically created table named C<mojo_migrations>. | ||
| 154 | |||
| 155 | =head1 ATTRIBUTES | ||
| 156 | |||
| 157 | L<Mojo::Pg::Migrations> implements the following attributes. | ||
| 158 | |||
| 159 | =head2 name | ||
| 160 | |||
| 161 | my $name = $migrations->name; | ||
| 162 | $migrations = $migrations->name('foo'); | ||
| 163 | |||
| 164 | Name for this set of migrations, defaults to C<migrations>. | ||
| 165 | |||
| 166 | =head2 pg | ||
| 167 | |||
| 168 | my $pg = $migrations->pg; | ||
| 169 | $migrations = $migrations->pg(Mojo::Pg->new); | ||
| 170 | |||
| 171 | L<Mojo::Pg> object these migrations belong to. Note that this attribute is weakened. | ||
| 172 | |||
| 173 | =head1 METHODS | ||
| 174 | |||
| 175 | L<Mojo::Pg::Migrations> inherits all methods from L<Mojo::Base> and implements the following new ones. | ||
| 176 | |||
| 177 | =head2 active | ||
| 178 | |||
| 179 | my $version = $migrations->active; | ||
| 180 | |||
| 181 | Currently active version. | ||
| 182 | |||
| 183 | =head2 from_data | ||
| 184 | |||
| 185 | $migrations = $migrations->from_data; | ||
| 186 | $migrations = $migrations->from_data('main'); | ||
| 187 | $migrations = $migrations->from_data('main', 'file_name'); | ||
| 188 | |||
| 189 | Extract migrations from a file in the DATA section of a class with L<Mojo::Loader/"data_section">, defaults to using | ||
| 190 | the caller class and L</"name">. | ||
| 191 | |||
| 192 | __DATA__ | ||
| 193 | @@ migrations | ||
| 194 | -- 1 up | ||
| 195 | CREATE TABLE messages (message TEXT); | ||
| 196 | INSERT INTO messages VALUES ('I ♥ Mojolicious!'); | ||
| 197 | -- 1 down | ||
| 198 | DROP TABLE messages; | ||
| 199 | |||
| 200 | =head2 from_dir | ||
| 201 | |||
| 202 | $migrations = $migrations->from_dir('/home/sri/migrations'); | ||
| 203 | |||
| 204 | Extract migrations from a directory tree where each versioned migration is in a directory, named for the version, and | ||
| 205 | each migration has one or both of the files named C<up.sql> or C<down.sql>. | ||
| 206 | |||
| 207 | migrations/1/up.sql | ||
| 208 | migrations/1/down.sql | ||
| 209 | migrations/2/up.sql | ||
| 210 | migrations/3/up.sql | ||
| 211 | migrations/3/down.sql | ||
| 212 | |||
| 213 | =head2 from_file | ||
| 214 | |||
| 215 | $migrations = $migrations->from_file('/home/sri/migrations.sql'); | ||
| 216 | |||
| 217 | Extract migrations from a file. | ||
| 218 | |||
| 219 | =head2 from_string | ||
| 220 | |||
| 221 | $migrations = $migrations->from_string( | ||
| 222 | '-- 1 up | ||
| 223 | CREATE TABLE foo (bar INT); | ||
| 224 | -- 1 down | ||
| 225 | DROP TABLE foo;' | ||
| 226 | ); | ||
| 227 | |||
| 228 | Extract migrations from string. | ||
| 229 | |||
| 230 | =head2 latest | ||
| 231 | |||
| 232 | my $version = $migrations->latest; | ||
| 233 | |||
| 234 | Latest version available. | ||
| 235 | |||
| 236 | =head2 migrate | ||
| 237 | |||
| 238 | $migrations = $migrations->migrate; | ||
| 239 | $migrations = $migrations->migrate(3); | ||
| 240 | |||
| 241 | Migrate from L</"active"> to a different version, up or down, defaults to using L</"latest">. All version numbers need | ||
| 242 | to be positive, with version C<0> representing an empty database. | ||
| 243 | |||
| 244 | # Reset database | ||
| 245 | $migrations->migrate(0)->migrate; | ||
| 246 | |||
| 247 | =head2 sql_for | ||
| 248 | |||
| 249 | my $sql = $migrations->sql_for(5, 10); | ||
| 250 | |||
| 251 | Get SQL to migrate from one version to another, up or down. | ||
| 252 | |||
| 253 | =head1 DEBUGGING | ||
| 254 | |||
| 255 | You can set the C<MOJO_MIGRATIONS_DEBUG> environment variable to get some advanced diagnostics information printed to | ||
| 256 | C<STDERR>. | ||
| 257 | |||
| 258 | MOJO_MIGRATIONS_DEBUG=1 | ||
| 259 | |||
| 260 | =head1 SEE ALSO | ||
| 261 | |||
| 262 | L<Mojo::Pg>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||
| 263 | |||
| 264 | =cut |