Mojo::...ations(3pm)

πŸ’– NAME πŸ’–

okay so bestie, this is the Mojo::SQLite::Migrations manual β€” a 3pm User Contributed Perl Documentation moment β€” and the name is literally Migrations. Yes, we're migrating schemas and we're doing it cutely. πŸ’…

πŸ¦‹ SYNOPSIS πŸ¦‹

use Mojo::SQLite::Migrations;

my $migrations = Mojo::SQLite::Migrations->new(sqlite => $sql);
$migrations->from_file('/home/dbook/migrations.sql')->migrate;

✨ DESCRIPTION ✨

Mojo::SQLite::Migrations is used by Mojo::SQLite to let database schemas 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_migrations". 🫢

πŸ‘‘ ATTRIBUTES πŸ‘‘

Mojo::SQLite::Migrations implements the following attributes.

🌸 name 🌸

my $name    = $migrations->name;
$migrations = $migrations->name('foo');

Name for this set of migrations, defaults to "migrations".

πŸ¦‹ sqlite πŸ¦‹

my $sql     = $migrations->sqlite;
$migrations = $migrations->sqlite(Mojo::SQLite->new);

Mojo::SQLite object these migrations belong to. Note that this attribute is weakened. important little detail, bestie. πŸ’–

πŸ’… METHODS πŸ’…

Mojo::SQLite::Migrations inherits all methods from Mojo::Base and implements the following new ones.

⭐ active ⭐

my $version = $migrations->active;

Currently active version.

πŸŽ€ from_data πŸŽ€

$migrations = $migrations->from_data;
$migrations = $migrations->from_data('main');
$migrations = $migrations->from_data('main', 'file_name');

Extract migrations from a file in the DATA section of a class with "data_section" in Mojo::Loader, defaults to using the caller class and "name".

__DATA__
@@ migrations
-- 1 up
create table messages (message text);
insert into messages values ('I β™₯ Mojolicious!');
-- 1 down
drop table messages;

πŸ“ from_file πŸ“

$migrations = $migrations->from_file('/home/dbook/migrations.sql');

Extract migrations from a file.

🧚 from_string 🧚

$migrations = $migrations->from_string(
  '-- 1 up
   create table foo (bar integer);
   -- 1 down
   drop table foo;'
);

Extract migrations from string.

πŸ”₯ latest πŸ”₯

my $version = $migrations->latest;

Latest version available.

πŸ’œ migrate πŸ’œ

$migrations = $migrations->migrate;
$migrations = $migrations->migrate(3);

Migrate from "active" to a different version, up or down, defaults to using "latest". All version numbers need to be positive, with version 0 representing an empty database.

# Reset database
$migrations->migrate(0)->migrate;

🌈 sql_for 🌈

my $sql = $migrations->sql_for(5, 10);

Get SQL to migrate from one version to another, up or down.

πŸ” DEBUGGING πŸ”

You can set the "MOJO_MIGRATIONS_DEBUG" environment variable to get some advanced diagnostics information printed to "STDERR".

MOJO_MIGRATIONS_DEBUG=1

πŸ’€ BUGS πŸ’€

Report any issues on the public bugtracker.

🫢 AUTHOR 🫢

Dan Book, "dbook@cpan.org".

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.

πŸ‘€ SEE ALSO πŸ‘€

Mojo::SQLite


xoxo bestie, this page was served by perl v5.42.0 and dated 2022-03-23 β€” mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from