Created
March 16, 2024 17:31
-
-
Save dylanwh/3211e4d78f4ebf83f23dd48fe9cfd58f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use 5.30.0; # or any version above 5.20 really | |
use experimental 'signatures'; | |
use IO::Async; | |
use IO::Async::Loop; | |
use DBI; | |
use Future::Utils qw(repeat try_repeat); | |
my $res = assert_database({db_name => "bugs", db_host => "127.0.0.1", db_user => "bugs", db_pass => "bugs"})->get(); | |
print $res; | |
sub assert_database($lc) { | |
my $assert_dbierr = ""; | |
my $assert_dbierrstr = ""; | |
my $loop = IO::Async::Loop->new; | |
for my $var (qw(db_name db_host db_user db_pass)) { | |
return $loop->new_future->die("$var is not set!") unless $lc->{$var}; | |
} | |
my $dsn = "dbi:mysql:database=$lc->{db_name};host=$lc->{db_host}"; | |
my $repeat = repeat { | |
$loop->delay_future(after => 0.25)->then(sub { | |
my $attrs = {RaiseError => 0, PrintError => 0}; | |
my ($ssl_ca_file, $ssl_ca_path, $ssl_cert, $ssl_key, $ssl_pubkey) = | |
@$lc{qw(db_mysql_ssl_ca_file db_mysql_ssl_ca_path | |
db_mysql_ssl_client_cert db_mysql_ssl_client_key db_mysql_ssl_get_pubkey)}; | |
if ($ssl_ca_file || $ssl_ca_path || $ssl_cert || $ssl_key || $ssl_pubkey) { | |
$attrs->{'mysql_ssl'} = 1; | |
$attrs->{'mysql_ssl_ca_file'} = $ssl_ca_file if $ssl_ca_file; | |
$attrs->{'mysql_ssl_ca_path'} = $ssl_ca_path if $ssl_ca_path; | |
$attrs->{'mysql_ssl_client_cert'} = $ssl_cert if $ssl_cert; | |
$attrs->{'mysql_ssl_client_key'} = $ssl_key if $ssl_key; | |
$attrs->{'mysql_get_server_pubkey'} = $ssl_pubkey if $ssl_pubkey; | |
} | |
warn "connecting..\n"; | |
my $dbh | |
= DBI->connect($dsn, $lc->{db_user}, $lc->{db_pass}, $attrs); | |
$assert_dbierr = DBI->err() || 0; | |
$assert_dbierrstr = DBI->errstr() || ''; | |
warn "error: $assert_dbierrstr\n"; | |
warn "dbh: $dbh\n"; | |
Future->wrap($dbh); | |
}); | |
} | |
until => sub { defined shift->get }; | |
my $timeout | |
= $loop->timeout_future(after => 60)->else_fail('assert_database timeout'); | |
my $any_f = Future->wait_any($repeat, $timeout); | |
return $any_f->transform( | |
done => sub {return}, | |
fail => sub {"unable to connect to $dsn as $lc->{db_user}: $assert_dbierr: $assert_dbierrstr"}, | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
docker run --rm -p 3306:3306 -d --name mysql --tmpfs /tmp -e MYSQL_DATABASE=bugs -e MYSQL_USER=bugs -e MYSQL_PASSWORD=bugs -e MYSQL_ALLOW_EMPTY_PASSWORD=1 mysql:8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment