3 DBIx::Class::DeploymentHandler::Intro - Introduction to DBIx::Class::DeploymentHandler
5 =head1 Why DBIx::Class::DeploymentHandler is worth using
8 =head1 Our Sample database
10 Follow L<DBIx::Class::Manual::Intro> except for the parts setting up the database.
11 After you are done, You should have the following files.
24 Our first script, C<install.pl> reads our schema file and creates the tables
30 use aliased 'DBIx::Class::DeploymentHandler' => 'DH';
32 use lib "$FindBin::Bin/../lib";
34 my $schema = MyDatabase::Main->connect('dbi:SQLite:mydb');
38 script_directory => "$FindBin::Bin/dbicdh",
39 databases => 'SQLite',
40 sql_translator_args => { add_drop_table => 0 },
45 $dh->install({ version => 1 });
47 =head2 dbicdh - Our migration scripts
49 Running C<install.pl> should create the following:
63 DBIx::Class::DeploymentHandler automatically generates SQL from our schema
64 that is suitable for SQLite
68 This contains all of the raw information about our schema that is then
69 translated into the sql.
73 Add a line to MyDatabase/Main/Result/Cd.pm below
75 __PACKAGE__->add_columns(qw/ cdid artist title /);
79 __PACKAGE__->add_column(isbn => { is_nullable => 1 });
81 Aside: It must be nullable or have a default - otherwise the upgrade will
82 fail for logical reasons. To be clear, if you add a column to a database and
83 it is not nullable and has no default, what will the existing rows contain
86 So here is our next script, C<upgrade.pl>:
91 use aliased 'DBIx::Class::DeploymentHandler' => 'DH';
93 use lib "$FindBin::Bin/../lib";
95 my $schema = MyDatabase::Main->connect('dbi:SQLite:mydb');
99 script_directory => "$FindBin::Bin/dbicdh",
100 databases => 'SQLite',
101 sql_translator_args => { add_drop_table => 0 },
105 $dh->prepare_upgrade({ from_version => 1, to_version => 2});
108 Our script directory now looks like:
114 | | | `-- 001-auto.sql
127 The new C<deploy/001-auto.sql> and C<deploy/001-auto.yml> files are the
128 state of the db as at that version. The C<upgrade/1-2/001-auto.sql> file
129 is the most interesting one; it is what gets your database from version 1 to 2.