| 1 | package Mojolicious::Command::clear_database; | ||
| 2 | use Mojo::Base 'Mojolicious::Command'; | ||
| 3 | |||
| 4 | use C; | ||
| 5 | use L; | ||
| 6 | use DBI; | ||
| 7 | |||
| 8 | |||
| 9 | has description => 'Clear database'; | ||
| 10 | has usage => sub { shift->extract_usage }; | ||
| 11 | |||
| 12 | # create user CLEAN_USER with login createdb createrole noinherit password 'password'; | ||
| 13 | |||
| 14 | |||
| 15 | sub run { | ||
| 16 | my( $self, @args ) = @_; | ||
| 17 | |||
| 18 | $args[0] && $args[0] eq '--yes' or return; | ||
| 19 | |||
| 20 | my $DB = C::config->{ DbAccess }; | ||
| 21 | L::warn "Database '$DB->{ NAME }' will be dropped after 10 sec.\nPress CTRL+C to stop operation\n"; | ||
| 22 | sleep 10; | ||
| 23 | |||
| 24 | local $DB->{ DSN }; | ||
| 25 | local $DB->{ schema } = 'DBI'; | ||
| 26 | my $dbh = do{ | ||
| 27 | # We can not drop database we are currently on. Connect to default | ||
| 28 | local $DB->{ NAME } = 'postgres'; | ||
| 29 | local $DB->{ USER } = $DB->{ CLEAN_USER }; | ||
| 30 | local $DB->{ PASS } = $DB->{ CLEAN_PASS }; | ||
| 31 | D::connect(); | ||
| 32 | }; | ||
| 33 | |||
| 34 | $dbh->do( "DROP DATABASE IF EXISTS $DB->{ NAME }" ); | ||
| 35 | $dbh->do( "CREATE DATABASE $DB->{ NAME }" ); | ||
| 36 | |||
| 37 | $dbh->do( <<" SQL" =~ s!^\t\t! !grm | ||
| 38 | DROP ROLE IF EXISTS $DB->{ USER }; | ||
| 39 | CREATE ROLE $DB->{ USER } | ||
| 40 | NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN | ||
| 41 | PASSWORD '$DB->{ PASS }' | ||
| 42 | ; | ||
| 43 | GRANT create ON DATABASE $DB->{ NAME } TO $DB->{ USER }; | ||
| 44 | SQL | ||
| 45 | ); | ||
| 46 | |||
| 47 | |||
| 48 | $dbh = do{ | ||
| 49 | # Create extension requires ROOT privilege | ||
| 50 | local $DB->{ USER } = 'postgres'; | ||
| 51 | local $DB->{ PASS } = $DB->{ ROOT }; | ||
| 52 | D::connect(); | ||
| 53 | }; | ||
| 54 | |||
| 55 | $dbh->do( "CREATE EXTENSION IF NOT EXISTS btree_gist" ); | ||
| 56 | } | ||
| 57 | |||
| 58 | 1; | ||
| 59 | |||
| 60 | |||
| 61 | =encoding utf8 | ||
| 62 | |||
| 63 | =head1 NAME | ||
| 64 | |||
| 65 | Mojolicious::Command::clear_database - Drop and recreate configured database | ||
| 66 | |||
| 67 | =head1 SYNOPSIS | ||
| 68 | |||
| 69 | Usage: APPLICATION clear_database [OPTIONS] | ||
| 70 | |||
| 71 | Options: | ||
| 72 | --yes Clear database | ||
| 73 | |||
| 74 | =head1 DESCRIPTION | ||
| 75 | |||
| 76 | This command drops and recreates the configured database. | ||
| 77 | |||
| 78 | It is intentionally noisy and waits 10 seconds before continuing, to give you a | ||
| 79 | chance to abort with C<CTRL+C>. | ||
| 80 | |||
| 81 | Use this only in development/testing environments. | ||
| 82 | |||
| 83 | =head1 CONFIGURATION | ||
| 84 | |||
| 85 | The command uses C<C::config-E<gt>{DbAccess}> keys, including: | ||
| 86 | |||
| 87 | NAME database to drop/recreate | ||
| 88 | CLEAN_USER privileged user allowed to drop/create databases | ||
| 89 | CLEAN_PASS password for CLEAN_USER | ||
| 90 | USER application role to (re)create | ||
| 91 | PASS password for USER | ||
| 92 | ROOT postgres password used to create extensions | ||
| 93 | |||
| 94 | =head1 OPTIONS | ||
| 95 | |||
| 96 | =over 4 | ||
| 97 | |||
| 98 | =item * C<--yes> | ||
| 99 | |||
| 100 | Perform the operation. Without this flag the command exits immediately. | ||
| 101 | |||
| 102 | =back | ||
| 103 | |||
| 104 | =head1 METHODS | ||
| 105 | |||
| 106 | =head2 run | ||
| 107 | |||
| 108 | Implements the command. | ||
| 109 | |||
| 110 | =head1 SEE ALSO | ||
| 111 | |||
| 112 | L<Mojolicious::Plugin::DbAccess>, L<D> | ||
| 113 | |||
| 114 | =cut |