| 1 | package Slick::Database; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use 5.036; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 1 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use Types::Standard qw(Str HashRef Int); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use Module::Runtime qw(require_module); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 1 | ▶ | use Carp qw(croak); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 1 | my $first_migration = { | |||||||||||||
| 12 | up => <<'EOF', | ||||||||||||||
| 13 | CREATE TABLE IF NOT EXISTS slick_migrations ( | ||||||||||||||
| 14 | id VARCHAR(255) PRIMARY KEY, | ||||||||||||||
| 15 | up TEXT, | ||||||||||||||
| 16 | down TEXT | ||||||||||||||
| 17 | ); | ||||||||||||||
| 18 | EOF | ||||||||||||||
| 19 | down => <<'EOF' | ||||||||||||||
| 20 | DROP TABLE slick_migrations; | ||||||||||||||
| 21 | EOF | ||||||||||||||
| 22 | }; | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 1 | has conn => ( | |||||||||||||
| 25 | is => 'ro', | ||||||||||||||
| 26 | isa => Str, | ||||||||||||||
| 27 | required => 1 | ||||||||||||||
| 28 | ); | ||||||||||||||
| 29 | |||||||||||||||
| 30 | 1 | has type => ( | |||||||||||||
| 31 | is => 'ro', | ||||||||||||||
| 32 | isa => Str | ||||||||||||||
| 33 | ); | ||||||||||||||
| 34 | |||||||||||||||
| 35 | has auto_migrate => ( | ||||||||||||||
| 36 | is => 'rw', | ||||||||||||||
| 37 | isa => Int, | ||||||||||||||
| 38 | 0 | ▶ | default => sub { return 0; } | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 39 | 1 | ); | |||||||||||||
| 40 | |||||||||||||||
| 41 | has migrations => ( | ||||||||||||||
| 42 | is => 'ro', | ||||||||||||||
| 43 | 0 | ▶ | default => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 44 | 0 | return { 'create_slick_migrations_table' => $first_migration }; | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | 1 | ); | |||||||||||||
| 47 | |||||||||||||||
| 48 | 1 | has _executor => ( | |||||||||||||
| 49 | is => 'ro', | ||||||||||||||
| 50 | handles => [qw(insert update delete select select_one execute dbi)] | ||||||||||||||
| 51 | ); | ||||||||||||||
| 52 | |||||||||||||||
| 53 | 0 | ▶ | sub BUILD { | ||||||||||||
|
Subroutine
BUILD: not called CC=3 SCAR=21.6
Pod
undocumented
| |||||||||||||||
| 54 | 0 | my $self = shift; | |||||||||||||
| 55 | |||||||||||||||
| 56 | 0 | require_module('URI'); | |||||||||||||
| 57 | |||||||||||||||
| 58 | 0 | my $uri = URI->new( $self->conn ); | |||||||||||||
| 59 | |||||||||||||||
| 60 | 0 | ▶ | if ( $uri->scheme =~ /^postgres(?:ql)?$/x ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 61 | 0 | require_module('Slick::DatabaseExecutor::Pg'); | |||||||||||||
| 62 | 0 | $self->{type} = 'Pg'; | |||||||||||||
| 63 | $self->{_executor} = | ||||||||||||||
| 64 | 0 | Slick::DatabaseExecutor::Pg->new( connection => $uri ); | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | elsif ( $uri->scheme =~ /^mysql$/x ) { | ||||||||||||||
| 67 | 0 | require_module('Slick::DatabaseExecutor::MySQL'); | |||||||||||||
| 68 | 0 | $self->{type} = 'mysql'; | |||||||||||||
| 69 | $self->{_executor} = | ||||||||||||||
| 70 | 0 | Slick::DatabaseExecutor::MySQL->new( connection => $uri ); | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | else { | ||||||||||||||
| 73 | 0 | croak q{Unknown scheme or un-supported database: } . $uri->scheme; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 0 | ▶ | try { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.6
| |||||||||||||||
| 77 | 1 | ▶ | no warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 78 | 0 | $self->dbi->do( $first_migration->{up} ); | |||||||||||||
| 79 | $self->insert( | ||||||||||||||
| 80 | 'slick_migrations', | ||||||||||||||
| 81 | { | ||||||||||||||
| 82 | id => 'create_slick_migrations_table', | ||||||||||||||
| 83 | up => $first_migration->{up}, | ||||||||||||||
| 84 | down => $first_migration->{down} | ||||||||||||||
| 85 | } | ||||||||||||||
| 86 | 0 | ); | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 0 | return $self; | |||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | 0 | ▶ | sub migrate_up { | ||||||||||||
|
Subroutine
migrate_up: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||
| 93 | 0 | my $self = shift; | |||||||||||||
| 94 | 0 | my $id = shift; | |||||||||||||
| 95 | |||||||||||||||
| 96 | 0 | my $run_migrations = $self->select( 'slick_migrations', ['id'] ); | |||||||||||||
| 97 | |||||||||||||||
| 98 | 0 | ▶ | if ($id) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | croak qq{Couldn't find migration: $id} | ||||||||||||||
| 100 | 0 | ▶ | unless exists $self->migrations->{$id}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 101 | |||||||||||||||
| 102 | 0 | ▶ | if ( not( grep { $_->{id} eq $id } $run_migrations->@* ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | 0 | my $migration = $self->migrations->{$id}; | |||||||||||||
| 104 | $self->dbi->do( $migration->{up} ) | ||||||||||||||
| 105 | 0 | ▶ | || croak qq{Couldn't migrate up $id - } . $self->dbi->errstr; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | |||||||||||||||
| 107 | $self->insert( | ||||||||||||||
| 108 | 'slick_migrations', | ||||||||||||||
| 109 | { | ||||||||||||||
| 110 | id => $id, | ||||||||||||||
| 111 | up => $migration->{up}, | ||||||||||||||
| 112 | down => $migration->{down} | ||||||||||||||
| 113 | } | ||||||||||||||
| 114 | 0 | ); | |||||||||||||
| 115 | |||||||||||||||
| 116 | 0 | say qq{Migrated $id up successfully.}; | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | } | ||||||||||||||
| 119 | else { | ||||||||||||||
| 120 | 0 | for ( keys $self->migrations->%* ) { | |||||||||||||
| 121 | 0 | my $key = $_; | |||||||||||||
| 122 | next | ||||||||||||||
| 123 | 0 | ▶ | if grep { $_->{id} eq $key } $run_migrations->@*; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 124 | |||||||||||||||
| 125 | $self->dbi->do( $self->migration->{$_}->{up} ) | ||||||||||||||
| 126 | 0 | ▶ | || croak qq{Couldn't migrate up $_ - } . $self->dbi->errstr; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 127 | $self->insert( | ||||||||||||||
| 128 | 'slick_migrations', | ||||||||||||||
| 129 | { | ||||||||||||||
| 130 | id => $key, | ||||||||||||||
| 131 | up => $_->{up}, | ||||||||||||||
| 132 | down => $_->{down} | ||||||||||||||
| 133 | } | ||||||||||||||
| 134 | 0 | ); | |||||||||||||
| 135 | |||||||||||||||
| 136 | 0 | say qq{Migrated $_ up successfully.}; | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | } | ||||||||||||||
| 139 | |||||||||||||||
| 140 | 0 | return $self; | |||||||||||||
| 141 | } | ||||||||||||||
| 142 | |||||||||||||||
| 143 | 0 | ▶ | sub migrate_down { | ||||||||||||
|
Subroutine
migrate_down: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||
| 144 | 0 | my $self = shift; | |||||||||||||
| 145 | 0 | my $id = shift; | |||||||||||||
| 146 | |||||||||||||||
| 147 | 0 | my $run_migrations = $self->select( 'slick_migrations', ['id'] ); | |||||||||||||
| 148 | |||||||||||||||
| 149 | 0 | ▶ | if ($id) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 150 | croak qq{Couldn't find migration: $id} | ||||||||||||||
| 151 | 0 | ▶ | unless exists $self->migrations->{$id}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 152 | |||||||||||||||
| 153 | 0 | ▶ | if ( not( grep { $_->{id} eq $id } $run_migrations->@* ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 154 | 0 | my $migration = $self->migrations->{$id}; | |||||||||||||
| 155 | $self->dbi->do( $migration->{down} ) | ||||||||||||||
| 156 | 0 | ▶ | || croak qq{Couldn't migrate down $id - } . $self->dbi->errstr; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 157 | 0 | $self->delete( 'slick_migrations', { id => $id } ); | |||||||||||||
| 158 | |||||||||||||||
| 159 | 0 | say qq{Migrated $id down successfully.}; | |||||||||||||
| 160 | } | ||||||||||||||
| 161 | } | ||||||||||||||
| 162 | else { | ||||||||||||||
| 163 | 0 | for ( keys $self->migrations->%* ) { | |||||||||||||
| 164 | 0 | my $key = $_; | |||||||||||||
| 165 | next | ||||||||||||||
| 166 | 0 | ▶ | if grep { $_->{id} eq $key } $run_migrations->@*; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 167 | |||||||||||||||
| 168 | $self->dbi->do( $self->migration->{$_}->{down} ) | ||||||||||||||
| 169 | 0 | ▶ | || croak qq{Couldn't migrate down $_ - } . $self->dbi->errstr; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 170 | 0 | $self->delete( 'slick_migrations', { id => $key } ); | |||||||||||||
| 171 | |||||||||||||||
| 172 | 0 | say qq{Migrated $_ down successfully.}; | |||||||||||||
| 173 | } | ||||||||||||||
| 174 | } | ||||||||||||||
| 175 | |||||||||||||||
| 176 | 0 | return $self; | |||||||||||||
| 177 | } | ||||||||||||||
| 178 | |||||||||||||||
| 179 | 0 | ▶ | sub migration { | ||||||||||||
|
Subroutine
migration: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 180 | 0 | my ( $self, $id, $up, $down ) = @_; | |||||||||||||
| 181 | |||||||||||||||
| 182 | 0 | my $migration = { | |||||||||||||
| 183 | up => $up, | ||||||||||||||
| 184 | down => $down | ||||||||||||||
| 185 | }; | ||||||||||||||
| 186 | |||||||||||||||
| 187 | 0 | $self->migrations->{$id} = $migration; | |||||||||||||
| 188 | |||||||||||||||
| 189 | 0 | ▶ | if ( $self->auto_migrate ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 190 | 0 | $self->migrate_up($id); | |||||||||||||
| 191 | } | ||||||||||||||
| 192 | |||||||||||||||
| 193 | 0 | return $self; | |||||||||||||
| 194 | } | ||||||||||||||
| 195 | |||||||||||||||
| 196 | 1 | 1; | |||||||||||||
| 197 | |||||||||||||||
| 198 | =encoding utf8 | ||||||||||||||
| 199 | |||||||||||||||
| 200 | =head1 NAME | ||||||||||||||
| 201 | |||||||||||||||
| 202 | Slick::Database | ||||||||||||||
| 203 | |||||||||||||||
| 204 | =head1 SYNOPSIS | ||||||||||||||
| 205 | |||||||||||||||
| 206 | An OO wrapper around L<DBI> and L<SQL::Abstract>. | ||||||||||||||
| 207 | |||||||||||||||
| 208 | Currently L<Slick::Database> supports C<MySQL> and C<PostgreSQL>. Note, you will need to install the | ||||||||||||||
| 209 | driver that you'd like to use manually, as L<Slick> does not come bundled with any. | ||||||||||||||
| 210 | |||||||||||||||
| 211 | See C<Slick::DatabaseExecutor> for lower level information on how L<Slick::Database> works. | ||||||||||||||
| 212 | |||||||||||||||
| 213 | =head1 API | ||||||||||||||
| 214 | |||||||||||||||
| 215 | =head2 dbi | ||||||||||||||
| 216 | |||||||||||||||
| 217 | Returns the underlying L<DBI> driver object associated with the database. | ||||||||||||||
| 218 | |||||||||||||||
| 219 | =head2 execute | ||||||||||||||
| 220 | |||||||||||||||
| 221 | Runs some statement against the underlying L<DBI> driver object associated with the database. | ||||||||||||||
| 222 | |||||||||||||||
| 223 | =head2 select, select_one, update, delete, insert | ||||||||||||||
| 224 | |||||||||||||||
| 225 | my $users = $s->database('my_postgres') | ||||||||||||||
| 226 | ->select('users', [ 'id', 'name' ]); # SELECT id, name FROM users; | ||||||||||||||
| 227 | |||||||||||||||
| 228 | my $user = $s->database('my_postgres') | ||||||||||||||
| 229 | ->select_one('users', [ 'id', 'name', 'age' ], { id => 1 }); # SELECT id, name, age FROM users WHERE id = 1; | ||||||||||||||
| 230 | |||||||||||||||
| 231 | $s->database('my_postgres') | ||||||||||||||
| 232 | ->insert('users', { name => 'Bob', age => 23 }); # INSERT INTO users (name, age) VALUES ('Bob', 23); | ||||||||||||||
| 233 | |||||||||||||||
| 234 | $s->database('my_postgres') | ||||||||||||||
| 235 | ->update('users', { name => 'John' }, { id => 2 }); # UPDATE users SET name = 'John' WHERE id = 2; | ||||||||||||||
| 236 | |||||||||||||||
| 237 | $s->database('my_postgres') | ||||||||||||||
| 238 | ->delete('users', { id => 2 }); # DELETE FROM users WHERE id = 2; | ||||||||||||||
| 239 | |||||||||||||||
| 240 | Wrapper around L<SQL::Abstract>, see L<SQL::Abstract> for more information on how to use these methods. | ||||||||||||||
| 241 | |||||||||||||||
| 242 | See L<"dbi"> if you would like to directly use the L<DBI> connection instead of L<SQL::Abstract>. | ||||||||||||||
| 243 | |||||||||||||||
| 244 | =head2 migrations | ||||||||||||||
| 245 | |||||||||||||||
| 246 | $s->database('db')->migrations; | ||||||||||||||
| 247 | |||||||||||||||
| 248 | Returns a C<HashRef> with all of the migrations associated with the database. | ||||||||||||||
| 249 | |||||||||||||||
| 250 | =head2 migration | ||||||||||||||
| 251 | |||||||||||||||
| 252 | $s->database('db')->migration( | ||||||||||||||
| 253 | 'migration_id', # id | ||||||||||||||
| 254 | 'CREATE TABLE foo (id INT PRIMARY KEY);', # up | ||||||||||||||
| 255 | 'DROP TABLE foo;' # down | ||||||||||||||
| 256 | ); | ||||||||||||||
| 257 | |||||||||||||||
| 258 | Create a migration and associate it with the database. | ||||||||||||||
| 259 | |||||||||||||||
| 260 | =head2 migrate_up | ||||||||||||||
| 261 | |||||||||||||||
| 262 | $s->database('db')->migrate_up; | ||||||||||||||
| 263 | |||||||||||||||
| 264 | Runs all pending migrations against the database. | ||||||||||||||
| 265 | |||||||||||||||
| 266 | If you wish to only migrate a single migration, you can provide the id of the migration | ||||||||||||||
| 267 | you'd like to run: | ||||||||||||||
| 268 | |||||||||||||||
| 269 | $s->database('db')->migrate_up('migration_id'); | ||||||||||||||
| 270 | |||||||||||||||
| 271 | =head2 migrate_down | ||||||||||||||
| 272 | |||||||||||||||
| 273 | $s->database('db')->migrate_down; | ||||||||||||||
| 274 | |||||||||||||||
| 275 | Migrates all migrations down effectively destroying your database. | ||||||||||||||
| 276 | |||||||||||||||
| 277 | If you wish to only de-migrate a single migration, you can provide the id of the migration | ||||||||||||||
| 278 | you'd like to run: | ||||||||||||||
| 279 | |||||||||||||||
| 280 | $s->database('db')->migrate_down('migration_id'); | ||||||||||||||
| 281 | |||||||||||||||
| 282 | =head1 See also | ||||||||||||||
| 283 | |||||||||||||||
| 284 | =over2 | ||||||||||||||
| 285 | |||||||||||||||
| 286 | =item * L<Slick::Context> | ||||||||||||||
| 287 | |||||||||||||||
| 288 | =item * L<Slick::Database> | ||||||||||||||
| 289 | |||||||||||||||
| 290 | =item * L<Slick::DatabaseExecutor> | ||||||||||||||
| 291 | |||||||||||||||
| 292 | =item * L<Slick::DatabaseExecutor::MySQL> | ||||||||||||||
| 293 | |||||||||||||||
| 294 | =item * L<Slick::DatabaseExecutor::Pg> | ||||||||||||||
| 295 | |||||||||||||||
| 296 | =item * L<Slick::EventHandler> | ||||||||||||||
| 297 | |||||||||||||||
| 298 | =item * L<Slick::Events> | ||||||||||||||
| 299 | |||||||||||||||
| 300 | =item * L<Slick::Methods> | ||||||||||||||
| 301 | |||||||||||||||
| 302 | =item * L<Slick::RouteMap> | ||||||||||||||
| 303 | |||||||||||||||
| 304 | =item * L<Slick::Util> | ||||||||||||||
| 305 | |||||||||||||||
| 306 | =back | ||||||||||||||
| 307 | |||||||||||||||
| 308 | =cut | ||||||||||||||