Skip to content

Commit e80e145

Browse files
authored
Merge pull request #39 from charsbar/mariadb_support
Support DBD::MariaDB
2 parents 5d94019 + 627236f commit e80e145

4 files changed

Lines changed: 56 additions & 4 deletions

File tree

.github/workflows/build.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ jobs:
3535
- name: Run tests
3636
run: DOD_TEST_DRIVER=MySQL prove -lr -j4 t
3737

38+
mariadb:
39+
runs-on: ubuntu-latest
40+
41+
steps:
42+
- uses: actions/checkout@v1
43+
- name: perl -V
44+
run: perl -V
45+
- name: setup mariadb repo
46+
run: curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash
47+
- name: apt-get
48+
run: sudo apt-get update && sudo apt-get install -y libmariadb-dev mariadb-server
49+
- name: Install dependencies
50+
run: curl -sL https://git.io/cpm | sudo perl - install -g --with-recommends --with-test --with-configure --show-build-log-on-failure --feature=test_mariadb
51+
- name: Run tests
52+
run: DOD_TEST_DRIVER=MariaDB prove -lr -j4 t
53+
3854
postgresql:
3955
runs-on: ubuntu-latest
4056

cpanfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ feature 'test_mysql', 'Test MySQL' => sub {
3434
requires 'SQL::Translator';
3535
};
3636

37+
feature 'test_mariadb', 'Test MariaDB' => sub {
38+
requires 'DBD::MariaDB';
39+
requires 'Test::mysqld';
40+
requires 'SQL::Translator';
41+
};
42+
3743
feature 'test_postgresql', 'Test PostgreSQL' => sub {
3844
requires 'DBD::Pg';
3945
requires 'Test::PostgreSQL';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# $Id$
2+
3+
package Data::ObjectDriver::Driver::DBD::MariaDB;
4+
use strict;
5+
use warnings;
6+
use base qw( Data::ObjectDriver::Driver::DBD::mysql );
7+
8+
sub fetch_id { $_[3]->{mariadb_insertid} || $_[3]->{insertid} }
9+
10+
1;

t/lib/DodTestUtil.pm

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ our @EXPORT = qw/setup_dbs teardown_dbs disconnect_all/;
1010
my %Requires = (
1111
SQLite => 'DBD::SQLite',
1212
MySQL => 'Test::mysqld',
13+
MariaDB => 'Test::mysqld',
1314
PostgreSQL => 'Test::PostgreSQL',
1415
Oracle => 'DBD::Oracle',
1516
SQLServer => 'DBD::ODBC',
@@ -51,20 +52,38 @@ sub db_filename {
5152
$dbname . $$ . '.db';
5253
}
5354

55+
my $test_mysqld_dsn;
5456
sub dsn {
5557
my($dbname) = @_;
5658
my $driver = driver();
5759
if ( my $dsn = env('DOD_TEST_DSN', $dbname) ) {
5860
return "$dsn;dbname=$dbname";
5961
}
60-
if ( $driver eq 'MySQL' ) {
62+
if ( $driver =~ /MySQL|MariaDB/ ) {
63+
if ( $driver eq 'MariaDB' && !$test_mysqld_dsn ) {
64+
my $help = `mysql --help`;
65+
my ($mariadb_version) = $help =~ /\A.*?([0-9]+\.[0-9]+)\.[0-9]+\-MariaDB/;
66+
no warnings 'redefine';
67+
$test_mysqld_dsn = \&Test::mysqld::dsn;
68+
*Test::mysqld::dsn = sub {
69+
my $dsn = $test_mysqld_dsn->(@_);
70+
# cf. https://github.com/kazuho/p5-test-mysqld/issues/32
71+
$dsn =~ s/;user=root// if $mariadb_version && $mariadb_version > 10.3;
72+
$dsn;
73+
};
74+
}
6175
$TestDB{$dbname} ||= Test::mysqld->new(
6276
my_cnf => {
6377
'skip-networking' => '', # no TCP socket
6478
'sql-mode' => 'TRADITIONAL,NO_AUTO_VALUE_ON_ZERO,ONLY_FULL_GROUP_BY',
6579
}
6680
) or die $Test::mysqld::errstr;
67-
return $TestDB{$dbname}->dsn;
81+
my $dsn = $TestDB{$dbname}->dsn;
82+
if ( $driver eq 'MariaDB' ) {
83+
$dsn =~ s/^dbi:mysql/dbi:MariaDB/i;
84+
$dsn =~ s/mysql_/mariadb_/ig;
85+
}
86+
return $dsn;
6887
}
6988
if ( $driver eq 'PostgreSQL' ) {
7089
$TestDB{$dbname} ||= Test::PostgreSQL->new(
@@ -86,8 +105,8 @@ sub setup_dbs {
86105
for my $dbname (keys %$info) {
87106
my $dbh = DBI->connect(
88107
dsn($dbname),
89-
env('DOD_TEST_USER', $dbname),
90-
env('DOD_TEST_PASS', $dbname),
108+
env('DOD_TEST_USER', $dbname) || undef,
109+
env('DOD_TEST_PASS', $dbname) || undef,
91110
{ RaiseError => 1, PrintError => 0, ShowErrorStatement => 1 });
92111
for my $table (@{ $info->{$dbname} }) {
93112
$dbh->do($_) for create_sql($table);
@@ -138,6 +157,7 @@ sub disconnect_all {
138157
sub create_sql {
139158
my($table) = @_;
140159
my $driver = driver();
160+
$driver = 'MySQL' if $driver eq 'MariaDB';
141161
my $file = File::Spec->catfile('t', 'schemas', $table . '.sql');
142162
open my $fh, $file or die "Can't open $file: $!";
143163
my $sql = do { local $/; <$fh> };

0 commit comments

Comments
 (0)