mojolicious / mojo-pg

Mojolicious :heart: PostgreSQL
https://metacpan.org/release/Mojo-Pg
Artistic License 2.0
99 stars 46 forks source link

Accept protocol as postgresql and postgres #33

Closed polettix closed 7 years ago

polettix commented 8 years ago

Summary

This PR allows Mojo::Pg to accept urls like postgres://... in addition to postgresql://.... It helps complying with the generic setting for PostgreSQL, as e.g. stated here.

This also helps reduce friction with automated systems that set urls in the former way, e.g. the Dokku plugin for PostgreSQL.

Motivation

This allows broader compliance to PostgreSQL rules and it is also a nice-to-have to avoid need for some munging e.g. in a Mojolicious application when receiving the DATABASE_URL parameter, like this:

(my $url = $ENV{DATABASE_URL}) =~ s{^postgres:}{postgresql:};

References

PostgreSQL documentation explicitly states that both URI scheme are good:

The URI scheme designator can be either postgresql:// or postgres://.

I hit this "impedance mismatch" while writing an article on deploying Perl applications on Dokku, you can find the article's relevant section here. This to say that there actually other softwares out there that decided to adopt the other way of expressing URLs.

Implementation

I chose to use regexps for compactness, TIMTOWTDI of course e.g.:

unless ($url->protocol eq 'postgresql')
    || ($url->protocol eq 'postgres');
kraih commented 8 years ago

Afraid i can't accept this pull request without a test case.

polettix commented 7 years ago

Hi! Thanks for looking into it.

It seems that leveraging t/connection.t should be the right thing to do, but there are a few ways I can think of right now:

Do you have a preference or some other way to suggest?