Start companion to Catalyst introduction
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / Manual / CatalystIntro.pod
1 package DBIx::Class::DeploymentHandler::Manual::CatalystIntro
2
3 # ABSTRACT: Introduction to using DBIx::Class::DeploymentHandler with a new Catalyst Project
4
5
6 __END__
7 =pod
8
9 =head1 NAME
10
11 DBIx::Class::DeploymentHandler::Manual::CatalystIntro - Introduction to using DBIx::Class::DeploymentHandler with a new Catalyst Project
12
13 =head1 Background
14
15 This introduction will use PostgreSQL and Catalyst.  Background information on using PostgreSQL with Catalyst can be found at L<Catalyst::Manual::Tutorial::10_Appendices>.  This guide will assume that you have some understanding of Catalyst.  Please go through the Catalyst tutorials first, if you have not yet done so.
16
17 =head1 Database Setup
18
19 Start by creating a user catalyst, with password catalyst
20
21     $ sudo -u postgres createuser -P catalyst
22     Enter password for new role: <catalyst>
23     Enter it again: <catalyst>
24     Shall the new role be a superuser? (y/n) n
25     Shall the new role be allowed to create databases? (y/n) n
26     Shall the new role be allowed to create more new roles? (y/n) n
27
28 Then create a new database, deploymentintro
29
30     sudo -u postgres createdb -O catalyst deploymentintro
31
32 =head1 Create the project
33
34     $ catalyst.pl DeploymentIntro
35     $ cd DeploymentIntro
36     $ perl Makefile.PL
37
38 =head1 Create the Schema
39
40     $ script/deploymentintro_create.pl model DB DBIC::Schema DeploymentIntro::Schema \
41         create=static 'dbi:Pg:dbname=deploymentintro' 'catalyst' 'catalyst' '{ AutoCommit => 1 }'
42
43     $ mkdir lib/Schema
44     $ mkdir lib/Schema/Result
45
46 Edit out the following from lib/DeploymentIntro/Model/DB.pm:
47
48     __PACKAGE__->config(
49         schema_class => 'DeploymentIntro::Schema',
50
51         connect_info => {
52             dsn => 'dbi:Pg:dbname=deploymentintro',
53             user => 'catalyst',
54             password => 'catalyst',
55             AutoCommit => q{1},
56         }
57     );
58
59 Remove deploymentintro.conf and create a new file, deploymentintro_local.pl with the following:
60
61     {
62         name => "DeploymentIntro",
63
64         "Model::DB" => {
65             schema_class => 'DeploymentIntro::Schema',
66
67             connect_info => {
68                 dsn        => 'dbi:Pg:dbname=deploymentintro',
69                 user       => 'catalyst',
70                 password   => 'catalyst',
71                 AutoCommit => q{1},
72             }
73         }
74     }
75
76 Copy the following program into scripts, under the name deploymentintro_dbicdh.pl
77
78     #!/usr/bin/env perl
79     use strict;
80     use warnings;
81     use feature ":5.10";
82     use aliased 'DBIx::Class::DeploymentHandler' => 'DH';
83     use FindBin;
84     use lib "$FindBin::Bin/../lib";
85     use DeploymentIntro::Schema;
86     use Config::JFDI;
87     my $config = Config::JFDI->new( name => 'DeploymentIntro' );
88     my $config_hash  = $config->get;
89     my $connect_info = $config_hash->{"Model::DB"}{"connect_info"};
90     my $schema       = DeploymentIntro::Schema->connect($connect_info);
91
92     my $dh = DH->new(
93         {
94             schema           => $schema,
95             script_directory => "$FindBin::Bin/../dbicdh",
96             databases        => 'PostgreSQL',
97         }
98     );
99
100     sub install {
101         $dh->prepare_install;
102         $dh->install;
103     }
104
105     sub upgrade {
106         die "Please set the version in Schema.pm"
107             unless $dh->schema->schema_version;
108         die "Please update the version in Schema.pm"
109             if ( $dh->version_storage->version_rs->search({version => $dh->schema_version})->count );
110         die "We only support positive integers for versions around these parts."
111             unless $dh->schema_version =~ /^\d+$/;
112
113         $dh->prepare_deploy;
114         $dh->prepare_upgrade;
115         $dh->upgrade;
116     }
117
118     sub current_version {
119         print $dh->database_version. "\n";
120     }
121
122     sub help {
123
124     print qq/
125     usage:
126         install
127         upgrade
128         upgrade-to [version]
129         downgrade-to [version]
130         current-version
131     /;
132
133     }
134
135     help unless $ARGV[0];
136
137     given ( $ARGV[0] ) {
138         when ("install")         { install(); }
139         when ("upgrade")         { upgrade(); }
140         when ("current-version") { current_version(); }
141     }
142
143 Copy the following files into lib/DeploymentIntro/Schema/Result
144
145 Cd.pm
146
147     package DeploymentIntro::Schema::Result::Cd;
148     use base qw/DBIx::Class::Core/;
149     __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
150     __PACKAGE__->table('cd');
151     __PACKAGE__->add_columns(
152         'cdid',   { data_type => 'integer' },
153         'artist', { data_type => 'integer' },
154         'title',  { data_type => 'text' }
155     );
156     __PACKAGE__->set_primary_key('cdid');
157     __PACKAGE__->belongs_to(
158         'artist' => 'DeploymentIntro::Schema::Result::Artist' );
159     __PACKAGE__->has_many( 'tracks' => 'DeploymentIntro::Schema::Result::Track' );
160
161     1;
162
163
164 Artist.pm
165
166     package DeploymentIntro::Schema::Result::Artist;
167     use base qw/DBIx::Class::Core/;
168     __PACKAGE__->table('artist');
169     __PACKAGE__->add_columns(
170         'artistid', { data_type => 'integer' },
171         'name',     { data_type => 'text' }
172     );
173     __PACKAGE__->set_primary_key('artistid');
174     __PACKAGE__->has_many( 'cds' => 'DeploymentIntro::Schema::Result::Cd' );
175
176     1;
177
178
179 Track.pm
180
181     package DeploymentIntro::Schema::Result::Track;
182     use base qw/DBIx::Class::Core/;
183     __PACKAGE__->table('track');
184     __PACKAGE__->add_columns(
185         'trackid', { data_type => 'integer' },
186         'cd',      { data_type => 'integer' },
187         'title',   { data_type => 'text' }
188     );
189     __PACKAGE__->set_primary_key('trackid');
190     __PACKAGE__->belongs_to( 'cd' => 'DeploymentIntro::Schema::Result::Cd' );
191
192     1;
193
194
195 And then edit lib/DeploymentIntro/Schema.pm and add the following above the 1 at the bottom
196
197    use vars qw($VERSION);
198    $VERSION = "1";
199
200
201 Now it is just a matter of running
202
203     ./script/deploymentintro_dbicdh.pl  install
204