change to the data directly automatically with mig env use
[migration-tools.git] / kmig.d / bin / mig-init
1 #!/usr/bin/perl -w
2 ###############################################################################
3 =pod
4
5 =head1 NAME
6
7 mig-init - This will add or recreate tracking tables for the B<mig> toolset
8 to the database specified by the current mig environment.
9
10 In practice, you should invoke 'mig env use schema_name' prior to calling
11 B<init>
12
13 =head1 SYNOPSIS
14
15 B<mig-init>
16
17 B<mig-init> <help>
18
19 =cut
20
21 ###############################################################################
22
23 use strict;
24 use Switch;
25 use Env qw(
26     HOME MYSQL_HOST MYSQL_TCP_PORT MYSQL_USER MYSQL_DATABASE MYSQL_PW
27     MIGSCHEMA MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
28 );
29 use Pod::Usage;
30 use DBI;
31 use FindBin;
32 my $mig_bin = "$FindBin::Bin/";
33 my $mig_sql = $mig_bin . "../sql/init/";
34 use lib "$FindBin::Bin/";
35 use KMig;
36
37 pod2usage(-verbose => 2) if $ARGV[0];
38
39 KMig::die_if_no_env_migschema();
40
41 if (! KMig::check_for_db_migschema()) {
42     die "could not find the schema";
43 }
44
45 KMig::die_if_mig_tracking_table_exists();
46 KMig::die_if_mig_column_tracking_table_exists();
47 loop_through_mig_sql_templates();
48
49 exit 0;
50
51 ###############################################################################
52
53 sub loop_through_mig_sql_templates {
54     print "Looping through mig.d/sql/init/ templates\n";
55     opendir my $dir, $mig_sql or die "Cannot open directory: $!";
56     my @files = sort readdir $dir;
57     closedir $dir;
58     foreach my $file (@files) {
59         if ($file =~ /.sql$/) {
60             print "executing $file:\n";
61             system( $mig_bin . "mig-sql", ('-e',"source $mig_sql$file") )
62         }
63     }
64 }
65