Correct the directory name for the populate script
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / Manual / Intro.pod
CommitLineData
af333e79 1package DBIx::Class::DeploymentHandler::Manual::Intro
f59dc433 2
af333e79 3# ABSTRACT: Introduction to DBIx::Class::DeploymentHandler
f59dc433 4
af333e79 5=pod
f59dc433 6
af333e79 7=head1 Why is DBIx::Class::DeploymentHandler worth using?
f59dc433 8
af333e79 9The most obvious reasons for using DBIx::Class::DeploymentHandler are
10that it can run multiple SQL scripts as well as Perl scripts, unlike
11DBIx::Class::Schema::Versioned, which only allows for a single SQL script.
12It is also extremely extensible, and is an opportunity for a break from
13backwards compatibility, so some regrettable decisions are avoided.
f59dc433 14
af333e79 15=head1 Sample database
16
17Follow L<DBIx::Class::Manual::Intro> except for the parts setting up the
18database. After you are done, You should have the following files.
f59dc433 19
298cdd91 20 MyDatabase/
21 |-- Main
22 | |-- Result
23 | | |-- Artist.pm
24 | | |-- Cd.pm
25 | | `-- Track.pm
26 | `-- ResultSet
27 `-- Main.pm
28
af333e79 29Add a line like the following in your MyDatabase::Main file:
30
31 our $VERSION = 1;
32
33or if you are using a newer Perl you can use the prettier syntax:
34
35 package MyDatabase::Main 1;
36
37By default DBIx::Class::DeploymentHandler only uses integers for versions,
38this makes versioning much simpler for figuring out what version is next
39(or previous.)
40
298cdd91 41=head1 install.pl
42
43Our first script, C<install.pl> reads our schema file and creates the tables
44in the database.
45
ee8b3548 46 #!/usr/bin/env perl
298cdd91 47 use strict;
48 use warnings;
49 use aliased 'DBIx::Class::DeploymentHandler' => 'DH';
50 use FindBin;
51 use lib "$FindBin::Bin/../lib";
52 use MyDatabase::Main;
53 my $schema = MyDatabase::Main->connect('dbi:SQLite:mydb');
54
55 my $dh = DH->new({
56 schema => $schema,
57 script_directory => "$FindBin::Bin/dbicdh",
58 databases => 'SQLite',
59 sql_translator_args => { add_drop_table => 0 },
298cdd91 60 });
61
62 $dh->prepare_install;
af333e79 63 $dh->install;
f59dc433 64
65=head2 dbicdh - Our migration scripts
66
298cdd91 67Running C<install.pl> should create the following:
f59dc433 68
298cdd91 69 dbicdh/
70 |-- SQLite
71 | `-- deploy
72 | `-- 1
73 | `-- 001-auto.sql
74 `-- _source
75 `-- deploy
76 `-- 1
77 `-- 001-auto.yml
f59dc433 78
79=head3 001-auto.sql
80
298cdd91 81DBIx::Class::DeploymentHandler automatically generates SQL from our schema
82that is suitable for SQLite
f59dc433 83
84=head3 001-auto.yml
85
298cdd91 86This contains all of the raw information about our schema that is then
87translated into the sql.
f59dc433 88
af333e79 89=head3 Population
90
91To truly take advantage of all DBIx::Class::DeploymentHandler offers, you
92should probably be using it for population. To do that all you need to do
cc64124f 93is create a file called C<dbicdh/_common/deploy/1/create_artists.pl>:
af333e79 94
95 sub {
96 my $schema = shift;
97 $schema->resultset('User')->populate([
98 ['name'],
99 ['Marillion'],
100 ['The Moutain Goats'],
101 ['Ladyhawke'],
102 ]);
103 };
104
f59dc433 105=head1 Upgrading
106
107Add a line to MyDatabase/Main/Result/Cd.pm below
108
298cdd91 109 __PACKAGE__->add_columns(qw/ cdid artist title /);
f59dc433 110
111with
112
298cdd91 113 __PACKAGE__->add_column(isbn => { is_nullable => 1 });
f59dc433 114
298cdd91 115Aside: It must be nullable or have a default - otherwise the upgrade will
116fail for logical reasons. To be clear, if you add a column to a database and
117it is not nullable and has no default, what will the existing rows contain
118for that column?
f59dc433 119
f4c2be04 120Now you need to modify the schema version in your MyDatabase::Main file to
121tell DBIx::Class::DeploymentHandler the new schema version number. You will
122want to remember the earlier advice about integer version numbers.
123
124 our $VERSION = 2;
125
298cdd91 126So here is our next script, C<upgrade.pl>:
f59dc433 127
ee8b3548 128 #!/usr/bin/env perl
298cdd91 129 use strict;
130 use warnings;
131 use aliased 'DBIx::Class::DeploymentHandler' => 'DH';
132 use FindBin;
133 use lib "$FindBin::Bin/../lib";
134 use MyDatabase::Main;
135 my $schema = MyDatabase::Main->connect('dbi:SQLite:mydb');
f59dc433 136
298cdd91 137 my $dh = DH->new({
138 schema => $schema,
139 script_directory => "$FindBin::Bin/dbicdh",
140 databases => 'SQLite',
141 sql_translator_args => { add_drop_table => 0 },
142 });
f59dc433 143
298cdd91 144 $dh->prepare_deploy;
145 $dh->prepare_upgrade({ from_version => 1, to_version => 2});
146 $dh->upgrade;
f59dc433 147
148Our script directory now looks like:
149
150 dbicdh/
151 |-- SQLite
152 | |-- deploy
153 | | |-- 1
154 | | | `-- 001-auto.sql
155 | | `-- 2
156 | | `-- 001-auto.sql
157 | `-- upgrade
158 | `-- 1-2
159 | `-- 001-auto.sql
160 `-- _source
161 `-- deploy
162 |-- 1
163 | `-- 001-auto.yml
164 `-- 2
165 `-- 001-auto.yml
166
298cdd91 167The new C<deploy/001-auto.sql> and C<deploy/001-auto.yml> files are the
168state of the db as at that version. The C<upgrade/1-2/001-auto.sql> file
169is the most interesting one; it is what gets your database from version 1 to 2.
f59dc433 170
af333e79 171And again, you can create a Perl file like we did previously with the
172deploy stage.
173