Rework tutorial
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / Manual / Intro.pod
CommitLineData
f59dc433 1=head1 NAME
2
3DBIx::Class::DeploymentHandler::Intro - Introduction to DBIx::Class::DeploymentHandler
4
5=head1 Why DBIx::Class::DeploymentHandler is worth using
6
7
8=head1 Our Sample database
9
10Follow L<DBIx::Class::Manual::Intro> except for the parts setting up the database.
11After you are done, You should have the following files.
12
298cdd91 13 MyDatabase/
14 |-- Main
15 | |-- Result
16 | | |-- Artist.pm
17 | | |-- Cd.pm
18 | | `-- Track.pm
19 | `-- ResultSet
20 `-- Main.pm
21
22=head1 install.pl
23
24Our first script, C<install.pl> reads our schema file and creates the tables
25in the database.
26
27 #!perl
28 use strict;
29 use warnings;
30 use aliased 'DBIx::Class::DeploymentHandler' => 'DH';
31 use FindBin;
32 use lib "$FindBin::Bin/../lib";
33 use MyDatabase::Main;
34 my $schema = MyDatabase::Main->connect('dbi:SQLite:mydb');
35
36 my $dh = DH->new({
37 schema => $schema,
38 script_directory => "$FindBin::Bin/dbicdh",
39 databases => 'SQLite',
40 sql_translator_args => { add_drop_table => 0 },
41 schema_version => 1,
42 });
43
44 $dh->prepare_install;
45 $dh->install({ version => 1 });
f59dc433 46
47=head2 dbicdh - Our migration scripts
48
298cdd91 49Running C<install.pl> should create the following:
f59dc433 50
298cdd91 51 dbicdh/
52 |-- SQLite
53 | `-- deploy
54 | `-- 1
55 | `-- 001-auto.sql
56 `-- _source
57 `-- deploy
58 `-- 1
59 `-- 001-auto.yml
f59dc433 60
61=head3 001-auto.sql
62
298cdd91 63DBIx::Class::DeploymentHandler automatically generates SQL from our schema
64that is suitable for SQLite
f59dc433 65
66=head3 001-auto.yml
67
298cdd91 68This contains all of the raw information about our schema that is then
69translated into the sql.
f59dc433 70
71=head1 Upgrading
72
73Add a line to MyDatabase/Main/Result/Cd.pm below
74
298cdd91 75 __PACKAGE__->add_columns(qw/ cdid artist title /);
f59dc433 76
77with
78
298cdd91 79 __PACKAGE__->add_column(isbn => { is_nullable => 1 });
f59dc433 80
298cdd91 81Aside: It must be nullable or have a default - otherwise the upgrade will
82fail for logical reasons. To be clear, if you add a column to a database and
83it is not nullable and has no default, what will the existing rows contain
84for that column?
f59dc433 85
298cdd91 86So here is our next script, C<upgrade.pl>:
f59dc433 87
298cdd91 88 #!perl
89 use strict;
90 use warnings;
91 use aliased 'DBIx::Class::DeploymentHandler' => 'DH';
92 use FindBin;
93 use lib "$FindBin::Bin/../lib";
94 use MyDatabase::Main;
95 my $schema = MyDatabase::Main->connect('dbi:SQLite:mydb');
f59dc433 96
298cdd91 97 my $dh = DH->new({
98 schema => $schema,
99 script_directory => "$FindBin::Bin/dbicdh",
100 databases => 'SQLite',
101 sql_translator_args => { add_drop_table => 0 },
102 });
f59dc433 103
298cdd91 104 $dh->prepare_deploy;
105 $dh->prepare_upgrade({ from_version => 1, to_version => 2});
106 $dh->upgrade;
f59dc433 107
108Our script directory now looks like:
109
110 dbicdh/
111 |-- SQLite
112 | |-- deploy
113 | | |-- 1
114 | | | `-- 001-auto.sql
115 | | `-- 2
116 | | `-- 001-auto.sql
117 | `-- upgrade
118 | `-- 1-2
119 | `-- 001-auto.sql
120 `-- _source
121 `-- deploy
122 |-- 1
123 | `-- 001-auto.yml
124 `-- 2
125 `-- 001-auto.yml
126
298cdd91 127The new C<deploy/001-auto.sql> and C<deploy/001-auto.yml> files are the
128state of the db as at that version. The C<upgrade/1-2/001-auto.sql> file
129is the most interesting one; it is what gets your database from version 1 to 2.
f59dc433 130