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. Note that this attribute is
53 weakened.
54
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_file
82 $migrations = $migrations->from_file('/home/sri/migrations.sql');
83
84 Extract migrations from a file.
85
86 from_string
87 $migrations = $migrations->from_string(
88 '-- 1 up
89 create table foo (bar int);
90 -- 1 down
91 drop table foo;'
92 );
93
94 Extract migrations from string.
95
96 latest
97 my $version = $migrations->latest;
98
99 Latest version available.
100
101 migrate
102 $migrations = $migrations->migrate;
103 $migrations = $migrations->migrate(3);
104
105 Migrate from "active" to a different version, up or down, defaults to
106 using "latest". All version numbers need to be positive, with version 0
107 representing an empty database.
108
109 # Reset database
110 $migrations->migrate(0)->migrate;
111
112 sql_for
113 my $sql = $migrations->sql_for(5, 10);
114
115 Get SQL to migrate from one version to another, up or down.
116
118 You can set the "MOJO_MIGRATIONS_DEBUG" environment variable to get
119 some advanced diagnostics information printed to "STDERR".
120
121 MOJO_MIGRATIONS_DEBUG=1
122
124 Mojo::Pg, Mojolicious::Guides, <https://mojolicious.org>.
125
126
127
128perl v5.32.0 2020-07-28 Mojo::Pg::Migrations(3)