2 ###############################################################################
7 mig-env - This tool is for tracking and setting environment variables used by
8 B<mig> and its sub-tools.
12 B<mig-env> <create|use> <migration_schema>
14 B<mig-env> <show> [migration_schema]
22 For most invocations, B<mig-env> will either create or use a migration-specific
23 file (~/.mig/<migration_schema>.env) for setting the following environment
30 The name of the migration schema. Convention has this being a single lowercased
31 word or acronym identifying the library, prefixed with 'm_'.
35 The base working directory for containing migration data, scripts, and other
40 The IP address or hostname for the PostgreSQL database used for a migration.
44 The TCP port for the PostgreSQL database.
48 The PostgreSQL user to use for the database.
52 The name of the actual database containing the migration schema.
56 This script may also setup a symlink from a specified Git repository to a
57 scripts/ directory within the migration work directory. The default for this is
58 ~/git/migration-work/past_migrations/MIGSCHEMA --> MIGWORKDIR/scripts
60 It may also create the migration work directory if necessary.
66 =item B<create> <schema>
68 This invocation will prompt for various values and create a .env file for the
69 specified migration schema, and a symlink between the specified Git repository
70 and migration work directory (which will also be created if needed).
74 This command will spawn a bash shell that executes the corresponding
75 ~/.mig/<schema>.env script for setting up environment variables encoded during
78 =item B<show> [schema]
80 This command will show the contents of the corresponding ~/.mig/<schema>.env
81 script, or, if no schema is specified, then it will list pertinent variables in
82 the current environment if they exist.
86 This command will list migration schemas found in ~/.mig
90 Display the documentation you're reading now.
96 ###############################################################################
102 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
103 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
106 use File::Path qw(make_path);
108 my $mig_bin = "$FindBin::Bin/";
109 use lib "$FindBin::Bin/";
111 pod2usage(-verbose => 2) if ! $ARGV[0];
113 my $migration_schema = $ARGV[1] || '';
114 my $filename = "$HOME/.mig/$migration_schema.env";
117 pod2usage(-verbose => 2);
120 pod2usage(-verbose => 2);
123 pod2usage(-verbose => 1) if ! $ARGV[1];
127 pod2usage(-verbose => 1) if ! $ARGV[1];
129 exec '/bin/bash', '--init-file', $filename;
131 die "\n$filename does not exist\n";
136 exec '/bin/cat', $filename;
138 print `env | sort | egrep 'MIG|PG'`;
142 opendir(my $dh, "$HOME/.mig") || die "can't open $HOME/.mig: $!";
143 while (readdir $dh) {
157 print "Re-Creating $filename\n";
158 print `cat $filename`;
160 print "Creating $filename\n";
166 $MIGBASEWORKDIR = "$HOME/data/" unless $MIGBASEWORKDIR;
167 my $migworkdir_default = "$MIGBASEWORKDIR$migration_schema/";
168 print "Main work directory (default $migworkdir_default): ";
169 my $MIGWORKDIR = <STDIN>;
172 $MIGWORKDIR = $migworkdir_default;
174 $MIGBASEGITDIR = "$HOME/git/migration-work/" unless $MIGBASEGITDIR;
175 my $miggitdir_default = "${MIGBASEGITDIR}past_migrations/$migration_schema/";
176 print "git repo for migration-specific scripts (default $miggitdir_default): ";
177 my $MIGGITDIR = <STDIN>;
180 $MIGGITDIR = $miggitdir_default;
185 $PGHOST = 'localhost' unless $PGHOST;
186 my $pghost_default = $PGHOST;
187 print "PGHOST (default $pghost_default): ";
191 $PGHOST = $pghost_default;
193 $PGPORT = 5432 unless $PGPORT;
194 my $pgport_default = $PGPORT;
195 print "PGPORT (default $pgport_default): ";
199 $PGPORT = $pgport_default;
201 $PGDATABASE = 'evergreen' unless $PGDATABASE;
202 my $pgdatabase_default = $PGDATABASE;
203 print "PGDATABASE (default $pgdatabase_default): ";
204 $PGDATABASE = <STDIN>;
207 $PGDATABASE = $pgdatabase_default;
209 $PGUSER = $PGDATABASE unless $PGUSER;
210 my $pguser_default = $PGUSER;
211 print "PGUSER (default $pguser_default): ";
212 my $PGUSER = <STDIN>;
215 $PGUSER = $pguser_default;
218 # create files and directories if needed
221 make_path($MIGGITDIR, { verbose => 1 });
222 `touch $MIGGITDIR/README`;
223 make_path($MIGWORKDIR, { verbose => 1 });
224 symlink $MIGGITDIR, "$MIGWORKDIR/scripts";
225 open FILE, ">$filename";
226 print FILE "export PGHOST=$PGHOST\n";
227 print FILE "export PGPORT=$PGPORT\n";
228 print FILE "export PGDATABASE=$PGDATABASE\n";
229 print FILE "export PGUSER=$PGUSER\n";
230 print FILE "export PGOPTIONS='-c search_path=$migration_schema,public,evergreen'\n";
231 print FILE "export MIGENVPROMPT=$migration_schema\n";
232 print FILE "export MIGSCHEMA=$migration_schema\n";
233 print FILE "export MIGBASEWORKDIR=$MIGBASEWORKDIR\n";
234 print FILE "export MIGWORKDIR=$MIGWORKDIR\n";
235 print FILE "export MIGBASEGITDIR=$MIGBASEGITDIR\n";
236 print FILE "export MIGGITDIR=$MIGGITDIR\n";
237 print FILE "alias wcd='cd `mig wdir`'\n";
238 print FILE "alias gcd='cd `mig gdir`'\n";
239 print FILE "alias scd='cd `mig sdir`'\n";
240 print FILE "source ~/.profile\n";
241 print FILE "env | sort | egrep 'PG|MIG'\n";
242 print FILE 'echo shell PID = $$' . "\n";