Skip to content

Commit 5d85f20

Browse files
committed
[0.0.8] Migration work
1 parent 9e31b5b commit 5d85f20

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

examples/multi-file/app.pl

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
my $slick = Slick->new;
88

99
$slick->helper( item_validator => sub { return exists shift->{name} } );
10-
$slick->database( items_db => 'sqlite://db.db' );
10+
$slick->database( items_db => 'sqlite://db.db', auto_migrate => 1 );
11+
$slick->items_db->migration(
12+
'create_items_table',
13+
'CREATE TABLE items ( id INT, name TEXT )',
14+
'DROP TABLE items'
15+
);
16+
$slick->items_db->migrate_up;
1117

1218
$slick->register( MyApp::ItemRouter->router );
1319

lib/Slick.pm

+3-3
Original file line numberDiff line numberDiff line change
@@ -314,20 +314,20 @@ has a migration, and also serves some JSON.
314314
$s->database(my_db => 'postgresql://user:[email protected]:5432/schema');
315315
$s->database(corporate_db => 'mysql://corporate:[email protected]:3306/schema');
316316
317-
$s->database('my_db')->migration(
317+
$s->my_db->migration(
318318
'create_user_table', # id
319319
'CREATE TABLE user ( id SERIAL PRIMARY KEY AUTOINCREMENT, name TEXT, age INT );', #up
320320
'DROP TABLE user;' # down
321321
);
322322
323-
$s->database('my_db')->migrate_up; # Migrates all pending migrations
323+
$s->my_db->migrate_up; # Migrates all pending migrations
324324
325325
$s->get('/users/{id}' => sub {
326326
my $app = shift;
327327
my $context = shift;
328328
329329
# Queries follow SQL::Abstract's notations
330-
my $user = $app->database('my_db')->select_one('user', { id => $context->params('id') });
330+
my $user = $app->my_db->select_one('user', { id => $context->params('id') });
331331
332332
# Render the user hashref as JSON.
333333
$context->json($user);

lib/Slick/Database.pm

+3-12
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,7 @@ sub BUILD {
7979
croak q{Unknown scheme or un-supported database: } . $uri->scheme;
8080
}
8181

82-
try {
83-
no warnings;
84-
$self->dbi->do( $first_migration->{up} );
85-
$self->insert(
86-
'slick_migrations',
87-
{
88-
id => 'create_slick_migrations_table',
89-
up => $first_migration->{up},
90-
down => $first_migration->{down}
91-
}
92-
);
93-
}
82+
$self->migrate_up if $self->auto_migrate;
9483

9584
return $self;
9685
}
@@ -99,6 +88,8 @@ sub migrate_up {
9988
my $self = shift;
10089
my $id = shift;
10190

91+
eval { $self->dbi->do( $first_migration->{up} ); };
92+
10293
my $run_migrations = $self->select( 'slick_migrations', ['id'] );
10394

10495
if ($id) {

lib/Slick/DatabaseExecutor.pm

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,17 @@ has dbi_options => (
5151
);
5252

5353
sub dbi {
54-
my ($self) = @_;
54+
my $self = shift;
5555
return $self->dbh->dbh;
5656
}
5757

5858
sub do {
59-
my ($self) = @_;
59+
my $self = shift;
6060
return $self->dbi->do(@_);
6161
}
6262

6363
sub prepare {
64-
my ($self) = @_;
64+
my $self = shift;
6565
return $self->dbi->prepare(@_);
6666
}
6767

0 commit comments

Comments
 (0)