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