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]
16 B<mig-env> <clone> [orig_migration_schema] [new_migration_schema]
24 For most invocations, B<mig-env> will either create or use a migration-specific
25 file (~/.mig/<migration_schema>.env) for setting the following environment
32 The name of the migration schema. Convention has this being a single lowercased
33 word or acronym identifying the library, prefixed with 'm_'.
37 The base working directory for containing migration data, scripts, and other
42 The IP address or hostname for the PostgreSQL database used for a migration.
46 The TCP port for the PostgreSQL database.
50 The PostgreSQL user to use for the database.
54 The name of the actual database containing the migration schema.
58 This script may also setup a symlink from a specified Git repository to a
59 scripts/ directory within the migration work directory. The default for this is
60 ~/git/migration-work/MIGSCHEMA --> MIGWORKDIR/scripts
62 It may also create the migration work directory if necessary.
68 =item B<create> <schema>
70 This invocation will prompt for various values and create a .env file for the
71 specified migration schema, and a symlink between the specified Git repository
72 and migration work directory (which will also be created if needed).
76 This command will spawn a bash shell that executes the corresponding
77 ~/.mig/<schema>.env script for setting up environment variables encoded during
80 =item B<show> [schema]
82 This command will show the contents of the corresponding ~/.mig/<schema>.env
83 script, or, if no schema is specified, then it will list pertinent variables in
84 the current environment if they exist.
86 =item B<clone> [orig schema] [new schema]
88 This command will create a "shallow" clone of the orig schema, in that it will
89 share database credentials as well as git and data directories, but will have a
94 This command will list migration schemas found in ~/.mig
98 Display the documentation you're reading now.
104 ###############################################################################
110 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
111 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
114 use File::Path qw(make_path);
116 my $mig_bin = "$FindBin::Bin/";
117 use lib "$FindBin::Bin/";
119 pod2usage(-verbose => 2) if ! $ARGV[0];
121 my $migration_schema = $ARGV[1] || '';
122 my $filename = "$HOME/.mig/$migration_schema.env";
125 pod2usage(-verbose => 2);
128 pod2usage(-verbose => 2);
131 pod2usage(-verbose => 1) if ! $ARGV[1];
135 pod2usage(-verbose => 1) if ! $ARGV[2];
136 $migration_schema = $ARGV[2] || '';
137 $filename = "$HOME/.mig/$migration_schema.env";
141 pod2usage(-verbose => 1) if ! $ARGV[1];
143 exec '/bin/bash', '--init-file', $filename;
145 die "\n$filename does not exist\n";
150 exec '/bin/cat', $filename;
152 print `env | sort | egrep 'MIG|PG'`;
156 opendir(my $dh, "$HOME/.mig") || die "can't open $HOME/.mig: $!";
157 while (readdir $dh) {
171 print "Re-Creating $filename\n";
172 print `cat $filename`;
174 print "Creating $filename\n";
180 $MIGBASEWORKDIR = "$HOME/data/" unless $MIGBASEWORKDIR;
181 my $migworkdir_default = "$MIGBASEWORKDIR$migration_schema/";
182 print "Main work directory (default $migworkdir_default): ";
183 my $MIGWORKDIR = <STDIN>;
186 $MIGWORKDIR = $migworkdir_default;
188 $MIGBASEGITDIR = "$HOME/git/migration-work/" unless $MIGBASEGITDIR;
189 my $miggitdir_default = "${MIGBASEGITDIR}/$migration_schema/";
190 print "git repo for migration-specific scripts (default $miggitdir_default): ";
191 my $MIGGITDIR = <STDIN>;
194 $MIGGITDIR = $miggitdir_default;
199 $PGHOST = 'localhost' unless $PGHOST;
200 my $pghost_default = $PGHOST;
201 print "PGHOST (default $pghost_default): ";
205 $PGHOST = $pghost_default;
207 $PGPORT = 5432 unless $PGPORT;
208 my $pgport_default = $PGPORT;
209 print "PGPORT (default $pgport_default): ";
213 $PGPORT = $pgport_default;
215 $PGDATABASE = 'evergreen' unless $PGDATABASE;
216 my $pgdatabase_default = $PGDATABASE;
217 print "PGDATABASE (default $pgdatabase_default): ";
218 $PGDATABASE = <STDIN>;
221 $PGDATABASE = $pgdatabase_default;
223 $PGUSER = $PGDATABASE unless $PGUSER;
224 my $pguser_default = $PGUSER;
225 print "PGUSER (default $pguser_default): ";
226 my $PGUSER = <STDIN>;
229 $PGUSER = $pguser_default;
232 # create files and directories if needed
235 make_path($MIGGITDIR, { verbose => 1 });
236 `touch $MIGGITDIR/README`;
237 make_path($MIGWORKDIR, { verbose => 1 });
238 symlink $MIGGITDIR, "$MIGWORKDIR/scripts";
239 open FILE, ">$filename";
240 print FILE "export PGHOST=$PGHOST\n";
241 print FILE "export PGPORT=$PGPORT\n";
242 print FILE "export PGDATABASE=$PGDATABASE\n";
243 print FILE "export PGUSER=$PGUSER\n";
244 print FILE "export PGOPTIONS='-c search_path=$migration_schema,public,evergreen'\n";
245 print FILE "export MIGENVPROMPT=$migration_schema\n";
246 print FILE "export MIGSCHEMA=$migration_schema\n";
247 print FILE "export MIGBASEWORKDIR=$MIGBASEWORKDIR\n";
248 print FILE "export MIGWORKDIR=$MIGWORKDIR\n";
249 print FILE "export MIGBASEGITDIR=$MIGBASEGITDIR\n";
250 print FILE "export MIGGITDIR=$MIGGITDIR\n";
251 print FILE "alias wcd='cd `mig wdir`'\n";
252 print FILE "alias gcd='cd `mig gdir`'\n";
253 print FILE "alias scd='cd `mig sdir`'\n";
254 print FILE "source ~/.profile\n";
255 print FILE "env | sort | egrep 'PG|MIG'\n";
256 print FILE 'echo shell PID = $$' . "\n";
261 my $orig_migration_schema = $ARGV[1] || '';
262 my $orig_filename = "$HOME/.mig/$orig_migration_schema.env";
263 `cp $orig_filename $filename`;
264 `sed -i 's/export PGOPTIONS=.*/export PGOPTIONS='"'"'-c search_path=$migration_schema,public,evergreen'"'"'/' $filename`;
265 `sed -i 's/export MIGENVPROMPT=.*/export MIGENVPROMPT=$migration_schema/' $filename`;
266 `sed -i 's/export MIGSCHEMA=.*/export MIGSCHEMA=$migration_schema/' $filename`;