1Mojo::Pg::Migrations(3)User Contributed Perl DocumentatioMnojo::Pg::Migrations(3)
2
3
4
6 Mojo::Pg::Migrations - Migrations
7
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
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
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.
53
55 Mojo::Pg::Migrations inherits all methods from Mojo::Base and
56 implements the following new ones.
57
58 active
59 my $version = $migrations->active;
60
61 Currently active version.
62
63 from_data
64 $migrations = $migrations->from_data;
65 $migrations = $migrations->from_data('main');
66 $migrations = $migrations->from_data('main', 'file_name');
67
68 Extract migrations from a file in the DATA section of a class with
69 "data_section" in Mojo::Loader, defaults to using the caller class and
70 "name".
71
72 __DATA__
73 @@ migrations
74 -- 1 up
75 create table messages (message text);
76 insert into messages values ('I ♥ Mojolicious!');
77 -- 1 down
78 drop table messages;
79
80 from_file
81 $migrations = $migrations->from_file('/home/sri/migrations.sql');
82
83 Extract migrations from a file.
84
85 from_string
86 $migrations = $migrations->from_string(
87 '-- 1 up
88 create table foo (bar int);
89 -- 1 down
90 drop table foo;'
91 );
92
93 Extract migrations from string.
94
95 latest
96 my $version = $migrations->latest;
97
98 Latest version available.
99
100 migrate
101 $migrations = $migrations->migrate;
102 $migrations = $migrations->migrate(3);
103
104 Migrate from "active" to a different version, up or down, defaults to
105 using "latest". All version numbers need to be positive, with version 0
106 representing an empty database.
107
108 # Reset database
109 $migrations->migrate(0)->migrate;
110
111 sql_for
112 my $sql = $migrations->sql_for(5, 10);
113
114 Get SQL to migrate from one version to another, up or down.
115
117 You can set the "MOJO_MIGRATIONS_DEBUG" environment variable to get
118 some advanced diagnostics information printed to "STDERR".
119
120 MOJO_MIGRATIONS_DEBUG=1
121
123 Mojo::Pg, Mojolicious::Guides, <https://mojolicious.org>.
124
125
126
127perl v5.28.0 2018-05-08 Mojo::Pg::Migrations(3)