1Mojo::SQLite::MigrationUss(e3r)Contributed Perl DocumentMaotjioo:n:SQLite::Migrations(3)
2
3
4
6 Mojo::SQLite::Migrations - Migrations
7
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
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
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.
53
55 Mojo::SQLite::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 X Mojolicious!');
77 -- 1 down
78 drop table messages;
79
80 from_file
81 $migrations = $migrations->from_file('/home/dbook/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 integer);
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 Report any issues on the public bugtracker.
124
126 Dan Book, "dbook@cpan.org"
127
129 Copyright 2015, Dan Book.
130
131 This library is free software; you may redistribute it and/or modify it
132 under the terms of the Artistic License version 2.0.
133
135 Mojo::SQLite
136
137
138
139perl v5.30.1 2020-01-30 Mojo::SQLite::Migrations(3)