1Mojo::SQLite::MigrationUss(e3r)Contributed Perl DocumentMaotjioo:n:SQLite::Migrations(3)
2
3
4

NAME

6       Mojo::SQLite::Migrations - Migrations
7

SYNOPSIS

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

DESCRIPTION

15       Mojo::SQLite::Migrations is used by Mojo::SQLite to allow database
16       schemas to evolve easily over time. A migration file is just a
17       collection of sql blocks, with one or more statements, separated by
18       comments of the form "-- VERSION UP/DOWN".
19
20         -- 1 up
21         create table messages (message text);
22         insert into messages values ('I X Mojolicious!');
23         -- 1 down
24         drop table messages;
25
26         -- 2 up (...you can comment freely here...)
27         create table stuff (whatever integer);
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::SQLite::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   sqlite
49         my $sql     = $migrations->sqlite;
50         $migrations = $migrations->sqlite(Mojo::SQLite->new);
51
52       Mojo::SQLite object these migrations belong to. Note that this
53       attribute is weakened.
54

METHODS

56       Mojo::SQLite::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 X Mojolicious!');
78         -- 1 down
79         drop table messages;
80
81   from_file
82         $migrations = $migrations->from_file('/home/dbook/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 integer);
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

DEBUGGING

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

BUGS

124       Report any issues on the public bugtracker.
125

AUTHOR

127       Dan Book, "dbook@cpan.org"
128
130       Copyright 2015, Dan Book.
131
132       This library is free software; you may redistribute it and/or modify it
133       under the terms of the Artistic License version 2.0.
134

SEE ALSO

136       Mojo::SQLite
137
138
139
140perl v5.32.0                      2020-07-28       Mojo::SQLite::Migrations(3)
Impressum