ocket8888 commented on a change in pull request #3186: All functionality for
the admin program is now located in admin.go
URL: https://github.com/apache/trafficcontrol/pull/3186#discussion_r246049473
##########
File path: traffic_ops/app/db/admin.pl
##########
@@ -14,261 +14,8 @@
# limitations under the License.
#
use strict;
-use warnings;
-use English;
-use Getopt::Long;
-use FileHandle;
-use DBI;
-use Cwd;
-use Data::Dumper;
-use Schema;
-use CPAN::Meta;
-use File::Find::Rule;
-
-use YAML;
-use YAML qw(LoadFile);
-use DBIx::Class::Schema::Loader qw/make_schema_at/;
-
-use Env;
-use Env qw(HOME);
+use File::Basename;
print STDERR "WARNING: this script is deprecated, please use the db/admin
binary instead.\n\n";
-
-my $usage = "\n"
- . "Usage: $PROGRAM_NAME [--env
(development|test|production|integration)] [arguments]\t\n\n"
- . "Example: $PROGRAM_NAME --env=test reset\n\n"
- . "Purpose: This script is used to manage database. The environments
are\n"
- . " defined in the dbconf.yml, as well as the database
names.\n\n"
- . "NOTE: \n"
- . "Postgres Superuser: The 'postgres' superuser needs to be created to
run $PROGRAM_NAME and setup databases.\n"
- . "If the 'postgres' superuser has not been created or password has not
been set then run the following commands accordingly. \n\n"
- . "Create the 'postgres' user as a super user (if not created):\n\n"
- . " \$ createuser postgres --superuser --createrole --createdb
--login --pwprompt\n\n"
- . "Modify your $HOME/.pgpass file which allows for easy command line
access by defaulting the user and password for the database\n"
- . "without prompts.\n\n"
- . " Postgres .pgpass file format:\n"
- . " hostname:port:database:username:password\n\n"
- . " ----------------------\n"
- . " Example Contents\n"
- . " ----------------------\n"
- . " *:*:*:postgres:your-postgres-password \n"
- . " *:*:*:traffic_ops:the-password-in-dbconf.yml \n"
- . " ----------------------\n\n"
- . " Save the following example into this file $HOME/.pgpass with the
permissions of this file\n"
- . " so only your user can read and write.\n\n"
- . " \$ chmod 0600 $HOME/.pgpass\n\n"
- .
"===================================================================================================================\n"
- . "$PROGRAM_NAME arguments: \n\n"
- . "createdb - Execute db 'createdb' the database for the current
environment.\n"
- . "create_user - Execute 'create_user' the user for the current
environment (traffic_ops).\n"
- . "dropdb - Execute db 'dropdb' on the database for the current
environment.\n"
- . "down - Roll back a single migration from the current version.\n"
- . "drop_user - Execute 'drop_user' the user for the current
environment (traffic_ops).\n"
- . "patch - Execute sql from db/patches.sql for loading post-migration
data patches.\n"
- . "redo - Roll back the most recently applied migration, then run it
again.\n"
- . "reset - Execute db 'dropdb', 'createdb', load_schema, migrate on
the database for the current environment.\n"
- . "reverse_schema - Reverse engineer the lib/Schema/Result files from
the environment database.\n"
- . "seed - Execute sql from db/seeds.sql for loading static data.\n"
- . "show_users - Execute sql to show all of the user for the current
environment.\n"
- . "status - Print the status of all migrations.\n"
- . "upgrade - Execute migrate, seed, and patches on the database for
the current environment.\n";
-
-
-my $environment = 'development';
-my $db_protocol;
-
-# This is defaulted to 'to_development' so
-# you don't have to specify --env=development for dev workstations
-my $db_name = 'to_development';
-my $db_super_user = 'postgres';
-my $db_user = '';
-my $db_password = '';
-my $host_ip = '';
-my $host_port = '';
-GetOptions( "env=s" => \$environment );
-$ENV{'MOJO_MODE'} = $environment;
-
-parse_dbconf_yml_pg_driver();
-
-STDERR->autoflush(1);
-my $argument = shift(@ARGV);
-if ( defined($argument) ) {
- if ( $argument eq 'createdb' ) {
- createdb();
- }
- elsif ( $argument eq 'dropdb' ) {
- dropdb();
- }
- elsif ( $argument eq 'create_user' ) {
- create_user();
- }
- elsif ( $argument eq 'drop_user' ) {
- drop_user();
- }
- elsif ( $argument eq 'show_users' ) {
- show_users();
- }
- elsif ( $argument eq 'reset' ) {
- create_user();
- dropdb();
- createdb();
- load_schema();
- migrate('up');
- }
- elsif ( $argument eq 'upgrade' ) {
- migrate('up');
- seed();
- patches();
- }
- elsif ( $argument eq 'migrate' ) {
- migrate('up');
- }
- elsif ( $argument eq 'down' ) {
- migrate('down');
- }
- elsif ( $argument eq 'redo' ) {
- migrate('redo');
- }
- elsif ( $argument eq 'status' ) {
- migrate('status');
- }
- elsif ( $argument eq 'dbversion' ) {
- migrate('dbversion');
- }
- elsif ( $argument eq 'seed' ) {
- seed();
- }
- elsif ( $argument eq 'load_schema' ) {
- load_schema();
- }
- elsif ( $argument eq 'reverse_schema' ) {
- reverse_schema();
- }
- elsif ( $argument eq 'patch' ) {
- patches();
- }
- else {
- print $usage;
- }
-}
-else {
- print $usage;
-}
-
-exit(0);
-
-sub parse_dbconf_yml_pg_driver {
- my $db_conf = LoadFile('db/dbconf.yml');
- my $db_connection = $db_conf->{$environment};
- $db_protocol = $db_connection->{driver};
- my $open = $db_connection->{open};
-
- # Goose requires the 'open' line in the dbconf file to be a scalar.
- # example:
- # open: host=127.0.0.1 port=5432 user=to_user
password=twelve dbname=to_development sslmode=disable
- # We need access to these values for db connections so I am
manipulating the 'open'
- # line so that it can be loaded into a hash.
- $open = join "\n", map { s/=/ : /; $_ } split " ", $open;
- my $hash = Load $open;
-
- $host_ip = $hash->{host};
- $host_port = $hash->{port};
- $db_user = $hash->{user};
- $db_password = $hash->{password};
- $db_name = $hash->{dbname};
-}
-
-sub migrate {
- my ($command) = @_;
-
- print "Migrating database...\n";
- if ( system("goose --env=$environment $command") != 0 ) {
- die "Can't run goose\n";
- }
-}
-
-sub seed {
- print "Seeding database w/ required data.\n";
- local $ENV{PGPASSWORD} = $db_password;
- if ( system("psql -h $host_ip -p $host_port -d $db_name -U $db_user -e
-v ON_ERROR_STOP=1 < db/seeds.sql") != 0 ) {
- die "Can't seed database w/ required data\n";
- }
-}
-
-sub patches {
- print "Patching database with required data fixes.\n";
- local $ENV{PGPASSWORD} = $db_password;
- if ( system("psql -h $host_ip -p $host_port -d $db_name -U $db_user -e
-v ON_ERROR_STOP=1 < db/patches.sql") != 0 ) {
- die "Can't patch database w/ required data\n";
- }
-}
-
-sub load_schema {
- print "Creating database tables.\n";
- local $ENV{PGPASSWORD} = $db_password;
- if ( system("psql -h $host_ip -p $host_port -d $db_name -U $db_user -e
-v ON_ERROR_STOP=1 < db/create_tables.sql") != 0 ) {
- die "Can't create database tables\n";
- }
-}
-
-sub dropdb {
- print "Dropping database: $db_name\n";
- if ( system("dropdb -h $host_ip -p $host_port -U $db_super_user -e
--if-exists $db_name;") != 0 ) {
- die "Can't drop db $db_name\n";
- }
-}
-
-sub createdb {
- my $db_exists = `psql -h $host_ip -U $db_super_user -p $host_port -tAc
"SELECT 1 FROM pg_database WHERE datname='$db_name'"`;
- if ($db_exists) {
- print "Database $db_name already exists\n";
- return;
- }
- my $cmd = "createdb -h $host_ip -p $host_port -U $db_super_user -e
--owner $db_user $db_name;";
- if ( system($cmd) != 0 ) {
- die "Can't create db $db_name\n";
- }
-
-}
-
-sub create_user {
- print "Creating user: $db_user\n";
- my $user_exists = `psql -h $host_ip -p $host_port -U $db_super_user
-tAc "SELECT 1 FROM pg_roles WHERE rolname='$db_user'"`;
-
- if (!$user_exists) {
- my $cmd = "CREATE USER $db_user WITH LOGIN ENCRYPTED PASSWORD
'$db_password'";
- if ( system(qq{psql -h $host_ip -p $host_port -U $db_super_user
-etAc "$cmd"}) != 0 ) {
- die "Can't create user $db_user\n";
- }
- }
-}
-
-sub drop_user {
- if ( system("dropuser -h $host_ip -p $host_port -U $db_super_user-i -e
$db_user;") != 0 ) {
- die "Can't drop user $db_user\n";
- }
-}
-
-sub show_users {
- if ( system("psql -h $host_ip -p $host_port -U $db_super_user -ec
'\\du';") != 0 ) {
- die "Can't show users";
- }
-}
-
-sub reverse_schema {
Review comment:
We could, but idk how much that really buys us. It also has a dependency on
`Schema` btw, but I think that's maybe a standard library thing? Not sure.
Leaving it in `admin.pl` means adding two dependencies: `GetOpt::Long`
(standard?) and `YAML`, which I don't personally think of as a huge deal, but
it is two places to maintain the command line argument format. On the other
hand separating it means we now have three programs to maintain for the `admin`
tool, at least until `admin.pl` vanishes. It's also functionality specific to
`admin.pl` since it only deals with Perl data representation and should vanish
with the need for that script
In summary, I'm sort of torn on the idea of putting that in a new script,
and I'm leaning towards saying that that's something that should maybe take
place once `admin.pl` actually gets removed (assuming the Perl back-end still
exists at that point).
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services