script/migraine.pl untested

filter: Statementstmt 0.0% 0 / 127 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 207 CC 1 SCAR 6.9
CC
1
Cov
0%
HELP_MESSAGE
2.0
VERSION_MESSAGE
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2  
3 eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
4 if 0; # not running under some shell
5  
6 use warnings;
7 use strict;
8  
9 use Migraine;
10 use YAML;
11 use Getopt::Std;
12 use IO::Handle;
13 use File::Basename;
14  
15 sub HELP_MESSAGE {
16 print STDERR "Syntax: migraine [options] <db-conn.yml>\n";
17 print STDERR "-c db-conn.yml reads configuration from db-conn.yml (compatibility)\n";
18 print STDERR "-f force (don't stop on missing migrations)\n";
19 print STDERR "-m migrations sets the migrations directory to migrations\n";
20 print STDERR "-n doesn't execute anything on the database\n";
21 print STDERR "-o mid only apply the given migration\n";
22 print STDERR "-u upgrades the migraine metadata format if needed\n";
23 print STDERR "-v mid applies migrations up to mid, instead of all\n";
24 print STDERR "-V verbose mode\n";
25 exit 1;
26 }
27  
28 sub VERSION_MESSAGE {
29 print STDERR "migraine version $Migraine::VERSION\n";
30 }
31  
32  
33 $Getopt::Std::STANDARD_HELP_VERSION = 1;
34  
35 # Command-line options =======================================================
36 my %opts;
37 getopts('nc:m:v:Vuo:f', \%opts) || HELP_MESSAGE;
38  
39 my $config_file = $opts{c} || shift @ARGV;
40 if (!$config_file) {
41 HELP_MESSAGE;
42 }
43 my $force = $opts{f};
44 my $migrations_directory = $opts{m} || dirname($config_file)."/migrations";
45 my $no_act = $opts{n} || 0;
46 my $only_apply_migration = $opts{o};
47 my $upgrade_database_format = $opts{u};
48 my $migrate_to_version = $opts{v};
49 my $verbose = $opts{V};
50  
51 if (exists $opts{o} && exists $opts{v}) {
52 print STDERR "ERROR: You can't specify both a single migration to apply\n\n";
53 print STDERR " and a version to migrate to\n\n";
54 HELP_MESSAGE;
55 }
56  
57 if (scalar @ARGV > 0) {
58 HELP_MESSAGE;
59 }
60  
61 # Load config and initial checks =============================================
62 if (! -r $config_file) {
63 print STDERR "ERROR: Can't read configuration file $config_file\n\n";
64 HELP_MESSAGE;
65 }
66 open F, $config_file;
67 my $config_data = join("", <F>);
68 close F;
69 my $config = YAML::Load($config_data);
70  
71 # The dbname and dbmasterhost is here for compatibility with some old stuff.
72 # Explicitly undocumented, please don't use it :-)
73 my $dsn = $config->{dsn} || "db:mysql:dbname=$config->{dbname};host=$config->{dbmasterhost}";
74 my $migrator = Migraine->new($config->{dsn},
75 user => $config->{user},
76 password => $config->{password},
77 migrations_dir => $migrations_directory);
78  
79 print "Operating in $config->{dsn}\n";
80 print "=========================================\n";
81  
82 if (!$migrator->migraine_metadata_present) {
83 print "It seems you haven't run migrations in this DB.\n";
84 unless ($no_act) {
85 print "Creating migraine metadata...";
86 $migrator->create_migraine_metadata;
87 print " done.\n";
88 }
89 print "=========================================\n";
90 }
91 else {
92 my $db_format = $migrator->migraine_metadata_version;
93 if ($db_format < $Migraine::SUPPORTED_METADATA_FORMAT) {
94 if ($upgrade_database_format) {
95 print "Upgrading database format...";
96 my $r = 1;
97 unless ($no_act) {
98 $r = $migrator->upgrade_migraine_metadata;
99 }
100  
101 if ($r) {
102 print " done.\n";
103 }
104 else {
105 print "\n";
106 print STDERR "ERROR: Couldn't upgrade database format";
107 exit 1;
108 }
109 }
110 else {
111 print STDERR "ERROR: Old migraine metadata format $db_format.\n";
112 print STDERR "To upgrade (won't be compatible with older migraine versions!), use -u\n";
113 exit 1;
114 }
115 }
116 elsif ($db_format > $Migraine::SUPPORTED_METADATA_FORMAT) {
117 print STDERR "ERROR: This version of migraine is too old for your database\n";
118 print STDERR "This migraine supports metadata format: $Migraine::SUPPORTED_METADATA_FORMAT\n";
119 print STDERR "Your database has metadata format: $db_format\n";
120 exit 1;
121 }
122 }
123  
124 my %migration_options = (no_act => $no_act,
125 skip_missing_migrations => $force,
126 before_migrate => sub {
127 my ($id, $path) = @_;
128 STDOUT->autoflush(1);
129 print "Applying migration $id ($path)... ";
130 if ($verbose) {
131 print "\n";
132 print "Migration contents:\n";
133 open F, $path;
134 print join("", <F>), "\n";
135 close F;
136 }
137 },
138 after_migrate => sub {
139 my ($id) = @_;
140 print "done\n";
141 if ($verbose) {
142 print "-" x 50, "\n";
143 }
144 });
145  
146 # Prepare which migrations to apply ==========================================
147 my $latest_version = $migrator->latest_version;
148 print "Currently applied migrations: ",
149 join(", ", $migrator->applied_migration_ranges), "\n";
150 print "Latest available: $latest_version\n";
151 print "=========================================\n";
152  
153 # We either apply a single migration, or all migrations up to a given version
154 if (defined $only_apply_migration) {
155 print "Attempting to apply SINGLE migration $only_apply_migration\n";
156 print "-----------------------------------------\n";
157 if ($migrator->migration_applied($only_apply_migration)) {
158 print STDERR "ERROR: Migration $only_apply_migration is already applied\n";
159 exit 1;
160 }
161 else {
162 $migrator->apply_migration($only_apply_migration,
163 %migration_options);
164 }
165 }
166 else {
167 $migration_options{version} = $migrate_to_version || $latest_version;
168 if ($migration_options{version} > $latest_version) {
169 print STDERR "Can't migrate to version $migration_options{version}. Latest is $latest_version\n";
170 exit 1;
171 }
172 else {
173 # Calculate if there are any migrations pending to apply
174 my @pending_migrations = ();
175 foreach my $migration (1 .. $migration_options{version}) {
176 if (!$migrator->migration_applied($migration)) {
177 push @pending_migrations, $migration;
178 }
179 }
180 if (!@pending_migrations) {
181 print "No migrations left to apply to reach version $migration_options{version}.\n";
182 }
183 else {
184 print "Attempting migration to VERSION: ", $migration_options{version}, "\n";
185 print "-----------------------------------------\n";
186 $migrator->migrate(%migration_options);
187 }
188 }
189 }
190  
191 __END__