Verified Commit 44507ccf authored by Luc Didry's avatar Luc Didry

Add MySQL support

parent e41b0860
Pipeline #82882 passed with stages
in 3 minutes and 46 seconds
......@@ -78,7 +78,7 @@ carton:
expire_in: 1 week
dependencies: []
script:
- carton install --deployment --without=sqlite --without=postgresql
- carton install --deployment --without=sqlite --without=postgresql --without=mysql
when: always
retry: 2
......@@ -88,15 +88,15 @@ carton:
carton_sqlite:
<<: *carton_bdd_definition
script:
- carton install --deployment --without=postgresql
- carton install --deployment --without=postgresql --without=mysql
carton_postgresql:
<<: *carton_bdd_definition
script:
- carton install --deployment --without=sqlite
#carton_mysql:
# <<: *carton_bdd_definition
# script:
# - carton install --deployment --without=sqlite --without=postgresql
- carton install --deployment --without=sqlite --without=mysql
carton_mysql:
<<: *carton_bdd_definition
script:
- carton install --deployment --without=sqlite --without=postgresql
### SQLite tests
##
......@@ -119,8 +119,8 @@ postgresql:
### MySQL tests
##
#
#mysql:
# <<: *mysql_definition
# script:
# - MOJO_CONFIG=t/mysql.conf make test
# - MOJO_CONFIG=t/mysql.conf make cover
mysql:
<<: *mysql_definition
script:
- MOJO_CONFIG=t/mysql.conf make test
- MOJO_CONFIG=t/mysql.conf make cover
......@@ -15,6 +15,7 @@ Revision history for Lufi
- Use a recurrent task to provision shorts
- Add a command to migrate data from SQLite to an other database
- Add a test suite
- MySQL support
0.02.2 2017-09-18
- Fix cron tasks bug
......
......@@ -38,4 +38,9 @@ feature 'postgresql', 'PostgreSQL support' => sub {
};
feature 'sqlite', 'SQLite support' => sub {
requires 'Mojo::SQLite', '>= 3.000';
}
};
feature 'mysql', 'MySQL support' => sub {
requires 'DBD::mysql', '== 4.046';
requires 'Mojo::mysql';
requires 'Mojolicious::Plugin::PgURLHelper';
};
......@@ -148,6 +148,20 @@ DISTRIBUTIONS
Test::More 0.47
Tie::Hash 0
perl 5.006
DBD-mysql-4.046
pathname: C/CA/CAPTTOFU/DBD-mysql-4.046.tar.gz
provides:
Bundle::DBD::mysql 4.046
DBD::mysql 4.046
DBD::mysql::GetInfo undef
DBD::mysql::db 4.046
DBD::mysql::dr 4.046
DBD::mysql::st 4.046
requirements:
DBI 1.609
Data::Dumper 0
ExtUtils::MakeMaker 0
perl 5.008001
DBI-1.641
pathname: T/TI/TIMB/DBI-1.641.tar.gz
provides:
......@@ -323,6 +337,16 @@ DISTRIBUTIONS
Data::Validate::Domain 0
Data::Validate::IP 0
ExtUtils::MakeMaker 0
Devel-CheckLib-1.13
pathname: M/MA/MATTN/Devel-CheckLib-1.13.tar.gz
provides:
Devel::CheckLib 1.13
requirements:
Exporter 0
ExtUtils::MakeMaker 0
File::Spec 0
File::Temp 0.16
perl 5.00405
Devel-Cover-1.31
pathname: P/PJ/PJCJ/Devel-Cover-1.31.tar.gz
provides:
......@@ -1018,6 +1042,23 @@ DISTRIBUTIONS
URI::db 0.15
URI::file 4.21
perl 5.010001
Mojo-mysql-1.07
pathname: J/JH/JHTHORSEN/Mojo-mysql-1.07.tar.gz
provides:
Blog undef
Blog::Controller::Posts undef
Blog::Model::Posts undef
Mojo::mysql 1.07
Mojo::mysql::Database undef
Mojo::mysql::Migrations undef
Mojo::mysql::PubSub undef
Mojo::mysql::Results undef
Mojo::mysql::Transaction undef
requirements:
DBD::mysql 4.042
ExtUtils::MakeMaker 0
Mojolicious 7.55
SQL::Abstract 1.81
Mojolicious-8.04
pathname: S/SR/SRI/Mojolicious-8.04.tar.gz
provides:
......
......@@ -115,6 +115,9 @@ sub new {
} elsif ($dbtype eq 'postgresql') {
use Lufi::DB::File::Pg;
$c = Lufi::DB::File::Pg->new(@_);
} elsif ($dbtype eq 'mysql') {
use Lufi::DB::File::Mysql;
$c = Lufi::DB::File::Mysql->new(@_);
}
}
......
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
package Lufi::DB::File::Mysql;
use Mojo::Base 'Lufi::DB::File';
sub new {
my $c = shift;
$c = $c->SUPER::new(@_);
return $c;
}
1;
......@@ -67,6 +67,9 @@ sub new {
} elsif ($dbtype eq 'postgresql') {
use Lufi::DB::Slice::Pg;
$c = Lufi::DB::Slice::Pg->new(@_);
} elsif ($dbtype eq 'mysql') {
use Lufi::DB::Slice::Mysql;
$c = Lufi::DB::Slice::Mysql->new(@_);
}
}
......
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
package Lufi::DB::Slice::Mysql;
use Mojo::Base 'Lufi::DB::Slice';
sub new {
my $c = shift;
$c = $c->SUPER::new(@_);
return $c;
}
1;
......@@ -24,6 +24,17 @@ sub register {
} else {
$migrations->from_file('utilities/migrations/pg.sql')->migrate(2);
}
} elsif ($app->config('dbtype') eq 'mysql') {
require Mojo::mysql;
$app->helper(dbi => \&_mysql);
# Database migration
my $migrations = Mojo::mysql::Migrations->new(mysql => $app->dbi);
if ($app->mode eq 'development' && $ENV{LUFI_DEV}) {
$migrations->from_file('utilities/migrations/mysql.sql')->migrate(0)->migrate(1);
} else {
$migrations->from_file('utilities/migrations/mysql.sql')->migrate(1);
}
} elsif ($app->config('dbtype') eq 'sqlite') {
require Mojo::SQLite;
$app->helper(dbi => \&_sqlite);
......@@ -61,10 +72,30 @@ sub register {
sub _pg {
my $c = shift;
state $pg = Mojo::Pg->new($c->app->pg_url($c->app->config('pgdb')));
my $pgdb = $c->config('pgdb');
my $port = (defined $pgdb->{port}) ? $pgdb->{port}: 5432;
my $addr = $c->pg_url({
host => $pgdb->{host}, port => $port, database => $pgdb->{database}, user => $pgdb->{user}, pwd => $pgdb->{pwd}
});
state $pg = Mojo::Pg->new($addr);
$pg->max_connections($pgdb->{max_connections}) if defined $pgdb->{max_connections};
return $pg;
}
sub _mysql {
my $c = shift;
my $mysqldb = $c->config('mysqldb');
my $port = (defined $mysqldb->{port}) ? $mysqldb->{port}: 3306;
my $addr = $c->pg_url({
host => $mysqldb->{host}, port => $port, database => $mysqldb->{database}, user => $mysqldb->{user}, pwd => $mysqldb->{pwd}
});
$addr =~ s/postgresql/mysql/;
state $mysql = Mojo::mysql->new($addr);
$mysql->max_connections($mysqldb->{max_connections}) if defined $mysqldb->{max_connections};
return $mysql;
}
sub _sqlite {
my $c = shift;
......
......@@ -114,7 +114,7 @@
#mail_sender => 'no-reply@lufi.io',
# choose what database you want to use
# valid choices are sqlite and postgresql (all lowercase)
# valid choices are sqlite, postgresql and mysql (all lowercase)
# optional, default is sqlite
#dbtype => 'sqlite',
......@@ -131,8 +131,28 @@
#pgdb => {
# database => 'lufi',
# host => 'localhost',
# #user => 'DBUSER',
# #pwd => 'DBPASSWORD'
# # optional, default is 5432
# #port => 5432,
# user => 'DBUSER',
# pwd => 'DBPASSWORD',
# # https://mojolicious.org/perldoc/Mojo/Pg#max_connections
# # optional, default is 1
# #max_connections => 1,
#},
# MySQL ONLY - only used if dbtype is set to mysql
# these are the credentials to access the MySQL database
# mandatory if you choosed mysql as dbtype
#mysqldb => {
# database => 'lufi',
# host => 'localhost',
# # optional, default is 3306
# #port => 3306,
# user => 'DBUSER',
# pwd => 'DBPASSWORD',
# # https://metacpan.org/pod/Mojo::mysql#max_connections
# # optional, default is 5 (set to 0 to disable persistent connections)
# #max_connections => 5,
#},
# define a path to the upload directory, where the uploaded files will be stored
......
This diff is collapsed.
......@@ -114,7 +114,7 @@
#mail_sender => 'no-reply@lufi.io',
# choose what database you want to use
# valid choices are sqlite and postgresql (all lowercase)
# valid choices are sqlite, postgresql and mysql (all lowercase)
# optional, default is sqlite
dbtype => 'postgresql',
......@@ -131,10 +131,28 @@
pgdb => {
database => 'lufi_db',
host => 'postgres',
# optional, default is 5432
#port => 5432,
user => 'lufi',
pwd => 'lufi_pwd'
# # optional, default is 1
# #max_connections => 1,
},
# MySQL ONLY - only used if dbtype is set to mysql
# these are the credentials to access the MySQL database
# mandatory if you choosed mysql as dbtype
#mysqldb => {
# database => 'lufi',
# host => 'localhost',
# # optional, default is 3306
# #port => 3306,
# user => 'DBUSER',
# pwd => 'DBPASSWORD',
# # optional, default is 5 (set to 0 to disable persistent connections)
# #max_connections => 5,
#},
# define a path to the upload directory, where the uploaded files will be stored
# you can define it relative to lufi directory or set an absolute path
# remember that it has to be in a directory writable by Lufi user
......
......@@ -114,7 +114,7 @@
#mail_sender => 'no-reply@lufi.io',
# choose what database you want to use
# valid choices are sqlite and postgresql (all lowercase)
# valid choices are sqlite, postgresql and mysql (all lowercase)
# optional, default is sqlite
#dbtype => 'sqlite',
......@@ -131,8 +131,28 @@
#pgdb => {
# database => 'lufi',
# host => 'localhost',
# #user => 'DBUSER',
# #pwd => 'DBPASSWORD'
# # optional, default is 5432
# #port => 5432,
# user => 'DBUSER',
# pwd => 'DBPASSWORD',
# # https://mojolicious.org/perldoc/Mojo/Pg#max_connections
# # optional, default is 1
# #max_connections => 1,
#},
# MySQL ONLY - only used if dbtype is set to mysql
# these are the credentials to access the MySQL database
# mandatory if you choosed mysql as dbtype
#mysqldb => {
# database => 'lufi',
# host => 'localhost',
# # optional, default is 3306
# #port => 3306,
# user => 'DBUSER',
# pwd => 'DBPASSWORD',
# # https://metacpan.org/pod/Mojo::mysql#max_connections
# # optional, default is 5 (set to 0 to disable persistent connections)
# #max_connections => 5,
#},
# define a path to the upload directory, where the uploaded files will be stored
......
......@@ -111,7 +111,7 @@ sub test_upload_file {
->message_like(qr@"created_at":\d+@)
->message_like(qr@"del_at_first_view":false@)
->message_like(qr@"delay":"0"@)
->message_like(qr@"duration":0@)
->message_like(qr@"duration":\d+@)
->message_like(qr@"i":0@)
->message_like(qr@"j":0@)
->message_like(qr@"name":"foobar\.txt"@)
......
-- 1 up
CREATE TABLE IF NOT EXISTS files (
short varchar(255) PRIMARY KEY,
deleted boolean default false,
mediatype varchar(255),
filename varchar(255),
filesize integer,
counter integer default 0,
delete_at_first_view boolean,
delete_at_day integer,
created_at integer,
created_by varchar(255),
last_access_at integer,
mod_token varchar(255),
nbslices integer,
complete boolean default false,
passwd varchar(255),
abuse integer
);
CREATE TABLE IF NOT EXISTS slices (
short varchar(255) NOT NULL REFERENCES files(short) ON DELETE CASCADE,
j integer NOT NULL,
path varchar(255) unique NOT NULL,
constraint slice_short_j UNIQUE (short, j)
);
-- 1 down
DROP TABLE slices;
DROP TABLE files;
  • 😁 That was mostly to see if my DB abstraction layer would allow me to add it without too much work. And it works.

Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment