Browse Source

Add mysql to possible databases

This commit adds mysql support for the potential databases that can
be used.  Included is a script to set up the database and user,
schema, and config checks for required fields if mysql.
add-test-db-and-override
Blaine Motsinger 12 months ago
parent
commit
dc2fa5f5b9
  1. 7
      .pasteburnrc.example
  2. 6
      db/schema/schema.mysql
  3. 4
      db/scripts/create_database_and_user.mysql
  4. 10
      lib/Pasteburn/Config.pm
  5. 13
      lib/Pasteburn/DB.pm

7
.pasteburnrc.example

@ -2,4 +2,9 @@
secret_key = default
[database]
type = sqlite
type = mysql
hostname = 127.0.0.1
port = 3306
dbname = pasteburn
username = pasteburn
password = password

6
db/schema/schema.mysql

@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS `secrets` (
id CHAR(64) NOT NULL,
passphrase VARCHAR(223) NOT NULL,
secret VARCHAR(749) NOT NULL,
created_at INT(10) SIGNED NOT NULL
);

4
db/scripts/create_database_and_user.mysql

@ -0,0 +1,4 @@
create database if not exists pasteburn;
create user 'pasteburn'@'localhost' IDENTIFIED BY 'password';
grant all privileges on pasteburn.* to 'pasteburn'@'localhost';
flush privileges;

10
lib/Pasteburn/Config.pm

@ -61,10 +61,18 @@ sub _validate {
die "config section database type is required\n";
}
unless ( List::MoreUtils::any { $config->{database}{type} eq $_ } (qw{ sqlite }) ) {
unless ( List::MoreUtils::any { $config->{database}{type} eq $_ } (qw{ sqlite mysql }) ) {
die "config section database type " . $config->{database}{type} . " is unknown\n";
}
if ( $config->{database}{type} eq 'mysql' ) {
foreach my $required (qw{ hostname port dbname username password }) {
unless ( exists $config->{database}{$required} ) {
die "config section database $required is required\n";
}
}
}
return 1;
}

13
lib/Pasteburn/DB.pm

@ -10,9 +10,10 @@ use DBI;
our $VERSION = '0.001';
sub connect_db {
my $dsn = load();
my ( $dsn, $user, $password ) = load();
my $dbh = DBI->connect(
$dsn, undef, undef,
$dsn, $user,
$password,
{ PrintError => 0,
RaiseError => 1,
AutoCommit => 1,
@ -35,7 +36,13 @@ sub load {
die "$db is not readable";
}
return "dbi:SQLite:dbname=$db";
return ( "dbi:SQLite:dbname=$db", undef, undef );
}
elsif ( $conf->{database}{type} eq 'mysql' ) {
return (
"dbi:mysql:database=" . $conf->{database}{dbname} . ";host=" . $conf->{database}{hostname} . ";port=" . $conf->{database}{port},
$conf->{database}{username}, $conf->{database}{password}
);
}
}

Loading…
Cancel
Save