Repository: incubator-trafficcontrol Updated Branches: refs/heads/master 03f565fa0 -> 6f8a062c7
createdb creates user if doesn't exist; relies on user connected as db admin Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/c104ceff Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/c104ceff Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/c104ceff Branch: refs/heads/master Commit: c104ceffcc209e8a6c0a745bf1b1c442202f92bd Parents: 3d43215 Author: Dan Kirkwood <dang...@gmail.com> Authored: Wed Jan 18 15:32:06 2017 -0700 Committer: Dan Kirkwood <dang...@gmail.com> Committed: Wed Jan 18 15:34:57 2017 -0700 ---------------------------------------------------------------------- traffic_ops/app/db/admin.pl | 51 ++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/c104ceff/traffic_ops/app/db/admin.pl ---------------------------------------------------------------------- diff --git a/traffic_ops/app/db/admin.pl b/traffic_ops/app/db/admin.pl index dd19b0f..d1cd85d 100755 --- a/traffic_ops/app/db/admin.pl +++ b/traffic_ops/app/db/admin.pl @@ -93,6 +93,7 @@ if ( defined($argument) ) { seed(); } elsif ( $argument eq 'setup' ) { + createuser(); dropdb(); createdb(); load_schema(); @@ -158,43 +159,67 @@ sub migrate { my ($command) = @_; print "Migrating database...\n"; - system( 'goose --env=' . $environment . ' ' . $command ); + if ( system( "goose --env=$environment $command" ) != 0 ) { + die "Can't run goose\n"; + } } sub seed { print "Seeding database.\n"; - system("psql -h $host_ip -p $host_port -d $db_name -U $db_username -e < db/seeds.sql"); + if ( system("psql -h $host_ip -p $host_port -d $db_name -U $db_username -e < db/seeds.sql") != 0 ) { + die "Can't seed database\n"; + } } sub load_schema { print "Creating database tables.\n"; - system("psql -h $host_ip -p $host_port -d $db_name -U $db_username -e < db/create_tables.sql"); + if ( system("psql -h $host_ip -p $host_port -d $db_name -U $db_username -e < db/create_tables.sql") != 0 ) { + die "Can't create database tables\n"; + } } sub dropdb { - system("dropdb -h $host_ip -p $host_port -U $db_username -e --if-exists $db_name;"); + if ( system("dropdb -h $host_ip -p $host_port -U $db_username -e --if-exists $db_name;") != 0 ) { + die "Can't drop db $db_name\n"; + } } sub createdb { - system("createdb -h $host_ip -p $host_port -U $db_username -e $db_name;"); + createuser(); + my $db_exists = `psql -tAc "SELECT 1 FROM pg_database WHERE datname='$db_name'"`; + if ( $db_exists ) { + print "Database $db_name already exists\n"; + return; + } + + if ( system("createdb -h $host_ip -p $host_port -U $db_username -e $db_name;") != 0 ) { + die "Can't create db $db_name\n"; + } } sub createuser { - my $user_exists = system("psql postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='USR_NAME'" | grep -q 1 || createuser ... - - system("createuser -U traffic_ops -h $host_ip -p $host_port -P -e --superuser $db_username;"); - system("createdb -U traffic_ops -h $host_ip -p $host_port -U $db_username -e $db_name;"); - system("psql -U traffic_ops -h $host_ip -p $host_port -e -c 'GRANT ALL PRIVILEGES ON DATABASE $db_name TO $db_username;'"); - system("psql $db_name -U traffic_ops -h $host_ip -p $host_port -e -c 'GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO $db_username;'"); + my $user_exists = `psql postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='$db_username'"`; + if ( $user_exists ) { + print "Role $db_username already exists\n"; + return; + } + my $cmd = "CREATE USER $db_username WITH SUPERUSER CREATEROLE CREATEDB ENCRYPTED PASSWORD '$db_password'"; + if ( system( qq{psql -h $host_ip -p $host_port -tAc "$cmd"} ) != 0 ) { + die "Can't create user $db_username\n"; + } } sub dropuser { - system("dropuser -U traffic_ops -h $host_ip -p $host_port -i -e $db_username;"); + if ( system("dropuser -h $host_ip -p $host_port -i -e $db_username;") != 0 ) { + die "Can't drop user $db_username\n"; + } } sub showusers { - system("psql postgres -c '\\du';"); + if ( system("psql postgres -ec '\\du';") != 0 ) { + die "Can't show users"; + } } sub reverse_schema {