| 1 | package Daje::Database::Migrator; | ||
| 2 | 0 | ▶ | use Mojo::Base -base, -signatures; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 3 | use v5.40; | ||
| 4 | |||
| 5 | use Mojo::Loader qw(load_class); | ||
| 6 | |||
| 7 | # NAME | ||
| 8 | # ==== | ||
| 9 | # | ||
| 10 | # Daje::Database::Migrator - It's the database migrate plugin for Daje::Workflow | ||
| 11 | # | ||
| 12 | # SYNOPSIS | ||
| 13 | # ======== | ||
| 14 | # | ||
| 15 | # use Daje::Database::Migrator; | ||
| 16 | # | ||
| 17 | # push @{$migrations}, {class => 'Daje::Workflow::Database', name => 'workflow', migration => 2}; | ||
| 18 | # | ||
| 19 | # push @{$migrations}, {file => '/home/user/schema/users.sql', name => 'users'}; | ||
| 20 | # | ||
| 21 | # Daje::Database::Migrator->new( | ||
| 22 | # pg => $pg, | ||
| 23 | # migrations => $migrations, | ||
| 24 | # )->migrate(); | ||
| 25 | # | ||
| 26 | # DESCRIPTION | ||
| 27 | # =========== | ||
| 28 | # | ||
| 29 | # Daje::Database::Migrator is the Database migrate plugin for Daje | ||
| 30 | # | ||
| 31 | # LICENSE | ||
| 32 | # ======= | ||
| 33 | # | ||
| 34 | # Copyright (C) janeskil1525. | ||
| 35 | # | ||
| 36 | # This library is free software; you can redistribute it and/or modify | ||
| 37 | # it under the same terms as Perl itself. | ||
| 38 | # | ||
| 39 | # AUTHOR | ||
| 40 | # ====== | ||
| 41 | # | ||
| 42 | # janeskil1525 E<lt>janeskil1525@gmail.comE<gt> | ||
| 43 | # | ||
| 44 | |||
| 45 | our $VERSION = "1.06"; | ||
| 46 | |||
| 47 | has 'pg'; | ||
| 48 | has 'migrations'; | ||
| 49 | |||
| 50 | sub migrate($self) { | ||
| 51 | |||
| 52 | my $length = scalar @{$self->migrations}; | ||
| 53 | for (my $i = 0; $i < $length; $i++) { | ||
| 54 | if (exists @{$self->migrations}[$i]->{class}) { | ||
| 55 | try { | ||
| 56 | my $cl = load_class @{$self->migrations}[$i]->{class}; | ||
| 57 | $self->pg->migrations->name( | ||
| 58 | @{$self->migrations}[$i]->{name} | ||
| 59 | )->from_data( | ||
| 60 | @{$self->migrations}[$i]->{class}, | ||
| 61 | @{$self->migrations}[$i]->{name} | ||
| 62 | )->migrate( | ||
| 63 | @{$self->migrations}[$i]->{migration} | ||
| 64 | ); | ||
| 65 | } | ||
| 66 | catch($e) { | ||
| 67 | say $e; | ||
| 68 | say @{$self->migrations}[$i]->{class}; | ||
| 69 | say @{$self->migrations}[$i]->{name}; | ||
| 70 | say @{$self->migrations}[$i]->{migration}; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | elsif (exists @{$self->migrations}[$i]->{file}) { | ||
| 74 | $self->pg->migrations->name( | ||
| 75 | @{$self->migrations}[$i]->{name} | ||
| 76 | )->from_file( | ||
| 77 | @{$self->migrations}[$i]->{file} | ||
| 78 | )->migrate( | ||
| 79 | @{$self->migrations}[$i]->{migration} | ||
| 80 | ); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | return 1; | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | 1; | ||
| 89 | __END__ | ||