1Mojo::Pg::Migrations(3)User Contributed Perl DocumentatioMnojo::Pg::Migrations(3)
2
3
4

NAME

6       Mojo::Pg::Migrations - Migrations
7

SYNOPSIS

9         use Mojo::Pg::Migrations;
10
11         my $migrations = Mojo::Pg::Migrations->new(pg => $pg);
12         $migrations->from_file('/home/sri/migrations.sql')->migrate;
13

DESCRIPTION

15       Mojo::Pg::Migrations is used by Mojo::Pg to allow database schemas to
16       evolve easily over time. A migration file is just a collection of sql
17       blocks, with one or more statements, separated by comments of the form
18       "-- VERSION UP/DOWN".
19
20         -- 1 up
21         CREATE TABLE messages (message TEXT);
22         INSERT INTO messages VALUES ('I ♥ Mojolicious!');
23         -- 1 down
24         DROP TABLE messages;
25
26         -- 2 up (...you can comment freely here...)
27         CREATE TABLE stuff (whatever INT);
28         -- 2 down
29         DROP TABLE stuff;
30
31       The idea is to let you migrate from any version, to any version, up and
32       down. Migrations are very safe, because they are performed in
33       transactions and only one can be performed at a time. If a single
34       statement fails, the whole migration will fail and get rolled back.
35       Every set of migrations has a "name", which is stored together with the
36       currently active version in an automatically created table named
37       "mojo_migrations".
38

ATTRIBUTES

40       Mojo::Pg::Migrations implements the following attributes.
41
42   name
43         my $name    = $migrations->name;
44         $migrations = $migrations->name('foo');
45
46       Name for this set of migrations, defaults to "migrations".
47
48   pg
49         my $pg      = $migrations->pg;
50         $migrations = $migrations->pg(Mojo::Pg->new);
51
52       Mojo::Pg object these migrations belong to. Note that this attribute is
53       weakened.
54

METHODS

56       Mojo::Pg::Migrations inherits all methods from Mojo::Base and
57       implements the following new ones.
58
59   active
60         my $version = $migrations->active;
61
62       Currently active version.
63
64   from_data
65         $migrations = $migrations->from_data;
66         $migrations = $migrations->from_data('main');
67         $migrations = $migrations->from_data('main', 'file_name');
68
69       Extract migrations from a file in the DATA section of a class with
70       "data_section" in Mojo::Loader, defaults to using the caller class and
71       "name".
72
73         __DATA__
74         @@ migrations
75         -- 1 up
76         CREATE TABLE messages (message TEXT);
77         INSERT INTO messages VALUES ('I ♥ Mojolicious!');
78         -- 1 down
79         DROP TABLE messages;
80
81   from_dir
82         $migrations = $migrations->from_dir('/home/sri/migrations');
83
84       Extract migrations from a directory tree where each versioned migration
85       is in a directory, named for the version, and each migration has one or
86       both of the files named "up.sql" or "down.sql".
87
88         migrations/1/up.sql
89         migrations/1/down.sql
90         migrations/2/up.sql
91         migrations/3/up.sql
92         migrations/3/down.sql
93
94   from_file
95         $migrations = $migrations->from_file('/home/sri/migrations.sql');
96
97       Extract migrations from a file.
98
99   from_string
100         $migrations = $migrations->from_string(
101           '-- 1 up
102            CREATE TABLE foo (bar INT);
103            -- 1 down
104            DROP TABLE foo;'
105         );
106
107       Extract migrations from string.
108
109   latest
110         my $version = $migrations->latest;
111
112       Latest version available.
113
114   migrate
115         $migrations = $migrations->migrate;
116         $migrations = $migrations->migrate(3);
117
118       Migrate from "active" to a different version, up or down, defaults to
119       using "latest". All version numbers need to be positive, with version 0
120       representing an empty database.
121
122         # Reset database
123         $migrations->migrate(0)->migrate;
124
125   sql_for
126         my $sql = $migrations->sql_for(5, 10);
127
128       Get SQL to migrate from one version to another, up or down.
129

DEBUGGING

131       You can set the "MOJO_MIGRATIONS_DEBUG" environment variable to get
132       some advanced diagnostics information printed to "STDERR".
133
134         MOJO_MIGRATIONS_DEBUG=1
135

SEE ALSO

137       Mojo::Pg, Mojolicious::Guides, <https://mojolicious.org>.
138
139
140
141perl v5.32.1                      2021-02-07           Mojo::Pg::Migrations(3)
Impressum