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