Skip to content

Instantly share code, notes, and snippets.

@perlpunk
Created March 14, 2022 13:31
Show Gist options
  • Save perlpunk/db8b4f3cda9852fd9f32d3f352f5f9f9 to your computer and use it in GitHub Desktop.
Save perlpunk/db8b4f3cda9852fd9f32d3f352f5f9f9 to your computer and use it in GitHub Desktop.
migrations.t
use Mojo::Base -strict;
use Test::More;
use File::Spec::Functions 'catfile';
use File::Temp;
use FindBin;
use Mojo::SQLite;
my $tempdir = File::Temp->newdir;
my $tempfile = catfile($tempdir, 'test.db');
# Clean up before start
my $sql = Mojo::SQLite->new->from_filename($tempfile);
$sql->db->query('drop table if exists mojo_migrations');
subtest 'XXXXXXXXXXXXXXXXXXXXXXXX' => sub {
$sql->migrations->name('migrations_test_foo')->from_string(<<EOF);
# Subtest: Enqueue
-- Migrate (0 -> 10)
-- 1 up
create table if not exists minion_jobs (
id integer not null primary key autoincrement,
args blob not null,
created text not null default current_timestamp,
delayed text not null,
finished text,
priority integer not null,
result blob,
retried text,
retries integer not null default 0,
started text,
state text not null default 'inactive',
task text not null,
worker integer,
queue text not null default 'default'
);
create index if not exists minion_jobs_priority_created on minion_jobs (priority desc, created);
create index if not exists minion_jobs_state on minion_jobs (state);
create table if not exists minion_workers (
id integer not null primary key autoincrement,
host text not null,
pid integer not null,
started text not null default current_timestamp,
notified text not null default current_timestamp
);
-- 2 up
alter table minion_jobs add column attempts integer not null default 1;
-- 3 up
create table minion_jobs_NEW (
id integer not null primary key autoincrement,
args text not null,
created text not null default current_timestamp,
delayed text not null,
finished text,
priority integer not null,
result text,
retried text,
retries integer not null default 0,
started text,
state text not null default 'inactive',
task text not null,
worker integer,
queue text not null default 'default',
attempts integer not null default 1
);
insert into minion_jobs_NEW select * from minion_jobs;
drop table minion_jobs;
alter table minion_jobs_NEW rename to minion_jobs;
-- 4 up
alter table minion_jobs add column parents text not null default '[]';
-- 5 up
alter table minion_workers add column inbox text not null
check(json_valid(inbox) and json_type(inbox) = 'array') default '[]';
-- 6 up
drop index if exists minion_jobs_priority_created;
drop index if exists minion_jobs_state;
create index if not exists minion_jobs_state_priority_id on minion_jobs (state, priority desc, id);
-- 7 up
alter table minion_workers add column status text not null
check(json_valid(status) and json_type(status) = 'object') default '{}';
-- 8 up
create table if not exists minion_locks (
id integer not null primary key autoincrement,
name text not null,
expires text not null
);
create index if not exists minion_locks_name_expires on minion_locks (name, expires);
alter table minion_jobs add column notes text not null
check(json_valid(notes) and json_type(notes) = 'object') default '{}';
-- 9 up
alter table minion_jobs add column sequence text;
alter table minion_jobs add column next integer;
create unique index minion_jobs_next on minion_jobs (next);
create index minion_jobs_sequence_next on minion_jobs (sequence, next);
-- 10 up
create table minion_jobs_NEW (
id integer not null primary key autoincrement,
args text not null,
created text not null default current_timestamp,
delayed text not null,
finished text,
priority integer not null,
result text,
retried text,
retries integer not null default 0,
started text,
state text not null default 'inactive',
task text not null,
worker integer,
queue text not null default 'default',
attempts integer not null default 1,
parents text not null default '[]',
notes text not null
check(json_valid(notes) and json_type(notes) = 'object') default '{}',
expires text,
lax boolean not null default 0
);
insert into minion_jobs_NEW
(id,args,created,delayed,finished,priority,result,retried,retries,
started,state,task,worker,queue,attempts,parents,notes)
select id,args,created,delayed,finished,priority,result,retried,retries,
started,state,task,worker,queue,attempts,parents,notes from minion_jobs;
drop table minion_jobs;
alter table minion_jobs_NEW rename to minion_jobs;
create index if not exists minion_jobs_state_priority_id on minion_jobs (state, priority desc, id);
create index minion_jobs_expires on minion_jobs (expires);
EOF
is $sql->migrations->latest, 10, 'latest version is 10';
is $sql->migrations->active, 0, 'active version is 0';
is $sql->migrations->migrate->active, 10, 'active version is 10';
};
done_testing();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment