23 Mar 2022 00:17:05 UTC
- Distribution: Mojo-SQLite
- Module version: 3.009
- Source (raw)
- Browse (raw)
- How to Contribute
- Testers (537 / 1 / 2)
- KwaliteeBus factor: 1
- 93.29% Coverage
- License: artistic_2
- Perl: v5.10.1
- Activity24 month
- Download (40.76KB)
- MetaCPAN Explorer
- Subscribe to distribution
- This version
- Latest version++ed by:9 non-PAUSE usersDBOOK Dan Bookand 3 contributors
- Adam Williamson
- Oliver Kurz
- Stefan Adams
- COPYRIGHT AND LICENSE
- SEE ALSO
Mojo::SQLite::Migrations - Migrations
use Mojo::SQLite::Migrations; my $migrations = Mojo::SQLite::Migrations->new(sqlite => $sql); $migrations->from_file('/home/dbook/migrations.sql')->migrate;
Mojo::SQLite::Migrations is used by Mojo::SQLite to allow database schemas to evolve easily over time. A migration file is just a collection of sql blocks, with one or more statements, separated by comments of the form
-- VERSION UP/DOWN.
-- 1 up create table messages (message text); insert into messages values ('I ♥ Mojolicious!'); -- 1 down drop table messages; -- 2 up (...you can comment freely here...) create table stuff (whatever integer); -- 2 down drop table stuff;
The idea is to let you migrate from any version, to any version, up and down. Migrations are very safe, because they are performed in transactions and only one can be performed at a time. If a single statement fails, the whole migration will fail and get rolled back. Every set of migrations has a "name", which is stored together with the currently active version in an automatically created table named
Mojo::SQLite::Migrations implements the following attributes.
my $name = $migrations->name; $migrations = $migrations->name('foo');
Name for this set of migrations, defaults to
my $sql = $migrations->sqlite; $migrations = $migrations->sqlite(Mojo::SQLite->new);
Mojo::SQLite object these migrations belong to. Note that this attribute is weakened.
my $version = $migrations->active;
Currently active version.
$migrations = $migrations->from_data; $migrations = $migrations->from_data('main'); $migrations = $migrations->from_data('main', 'file_name');
__DATA__ @@ migrations -- 1 up create table messages (message text); insert into messages values ('I ♥ Mojolicious!'); -- 1 down drop table messages;
$migrations = $migrations->from_file('/home/dbook/migrations.sql');
Extract migrations from a file.
$migrations = $migrations->from_string( '-- 1 up create table foo (bar integer); -- 1 down drop table foo;' );
Extract migrations from string.
my $version = $migrations->latest;
Latest version available.
$migrations = $migrations->migrate; $migrations = $migrations->migrate(3);
# Reset database $migrations->migrate(0)->migrate;
my $sql = $migrations->sql_for(5, 10);
Get SQL to migrate from one version to another, up or down.
You can set the
MOJO_MIGRATIONS_DEBUGenvironment variable to get some advanced diagnostics information printed to
Report any issues on the public bugtracker.
Copyright 2015, Dan Book.
This library is free software; you may redistribute it and/or modify it under the terms of the Artistic License version 2.0.