1DBIx::Class::DeploymentUDHsBaeInrxd:lC:eoCrnl:ta:rsMisab:nu:utDaeeldp:l:PoCeyarmtleanlDtyoHscatunImdneltnertroa:(t:3iM)oannual::CatalystIntro(3)
2
3
4

NAME

6       DBIx::Class::DeploymentHandler::Manual::CatalystIntro - Introduction to
7       using DBIx::Class::DeploymentHandler with a new Catalyst Project
8

Background

10       This introduction will use PostgreSQL and Catalyst.  Background
11       information on using PostgreSQL with Catalyst can be found at
12       Catalyst::Manual::Tutorial::10_Appendices.  This guide will assume that
13       you have some understanding of Catalyst.  Please go through the
14       Catalyst tutorials first if you have not yet done so.
15

Database Setup

17       Start by creating a user "catalyst", with password "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 "deploymentintro"
27
28        sudo -u postgres createdb -O catalyst deploymentintro
29

Create the project

31        $ catalyst.pl DeploymentIntro
32        $ cd DeploymentIntro
33        $ perl Makefile.PL
34

Create the Schema

36        $ script/deploymentintro_create.pl model DB DBIC::Schema DeploymentIntro::Schema \
37            create=static 'dbi:Pg:dbname=deploymentintro' 'catalyst' 'catalyst' '{ AutoCommit => 1 }'
38
39        $ mkdir -p lib/Schema/Result
40
41       Remove the following from "lib/DeploymentIntro/Model/DB.pm":
42
43        connect_info => {
44          dsn => 'dbi:Pg:dbname=deploymentintro',
45          user => 'catalyst',
46          password => 'catalyst',
47          AutoCommit => q{1},
48        }
49
50       Remove "deploymentintro.conf" and create a new file called
51       "deploymentintro_local.pl" with the following:
52
53           {
54               name => "DeploymentIntro",
55
56               "Model::DB" => {
57                   schema_class => 'DeploymentIntro::Schema',
58
59                   connect_info => {
60                       dsn        => 'dbi:Pg:dbname=deploymentintro',
61                       user       => 'catalyst',
62                       password   => 'catalyst',
63                       AutoCommit => 1,
64                   }
65               }
66           }
67
68       Copy the following program into scripts, under the name
69       "deploymentintro_dbicdh.pl"
70
71        #!/usr/bin/env perl
72
73        use strict;
74        use warnings;
75
76        use feature ":5.10";
77
78        use aliased 'DBIx::Class::DeploymentHandler' => 'DH';
79        use FindBin;
80        use lib "$FindBin::Bin/../lib";
81        use DeploymentIntro::Schema;
82        use Config::JFDI;
83
84        my $config = Config::JFDI->new( name => 'DeploymentIntro' );
85        my $config_hash  = $config->get;
86        my $connect_info = $config_hash->{"Model::DB"}{"connect_info"};
87        my $schema       = DeploymentIntro::Schema->connect($connect_info);
88
89        my $dh = DH->new({
90          schema           => $schema,
91          script_directory => "$FindBin::Bin/../dbicdh",
92          databases        => 'PostgreSQL',
93        });
94
95        sub install {
96          $dh->prepare_install;
97          $dh->install;
98        }
99
100        sub upgrade {
101          die "Please update the version in Schema.pm"
102            if ( $dh->version_storage->version_rs->search({version => $dh->schema_version})->count );
103
104          die "We only support positive integers for versions around these parts."
105            unless $dh->schema_version =~ /^\d+$/;
106
107          $dh->prepare_deploy;
108          $dh->prepare_upgrade;
109          $dh->upgrade;
110        }
111
112        sub current_version {
113          say $dh->database_version;
114        }
115
116        sub help {
117        say <<'OUT';
118        usage:
119          install
120          upgrade
121          current-version
122        OUT
123        }
124
125        help unless $ARGV[0];
126
127        given ( $ARGV[0] ) {
128            when ('install')         { install()         }
129            when ('upgrade')         { upgrade()         }
130            when ('current-version') { current_version() }
131        }
132
133       Copy the following files into "lib/DeploymentIntro/Schema/Result":
134
135       "Cd.pm"
136
137        package DeploymentIntro::Schema::Result::Cd;
138
139        use strict;
140        use warnings;
141
142        use parent 'DBIx::Class::Core';
143
144        __PACKAGE__->load_components(qw(InflateColumn::DateTime));
145        __PACKAGE__->table('cd');
146
147        __PACKAGE__->add_columns(
148          id => {
149            data_type => 'integer',
150            is_auto_increment => 1,
151          },
152          artist_id => {
153            data_type => 'integer'
154          },
155          title => {
156            data_type => 'text'
157          },
158        );
159
160        __PACKAGE__->set_primary_key('id');
161
162        __PACKAGE__->belongs_to(
163          artist => 'DeploymentIntro::Schema::Result::Artist', 'artist_id' );
164
165        __PACKAGE__->has_many(
166          tracks => 'DeploymentIntro::Schema::Result::Track', 'cd_id' );
167
168        1;
169
170       "Artist.pm"
171
172        package DeploymentIntro::Schema::Result::Artist;
173
174        use strict;
175        use warnings;
176
177        use parent 'DBIx::Class::Core';
178
179        __PACKAGE__->table('artist');
180
181        __PACKAGE__->add_columns(
182          id => {
183            data_type => 'integer',
184            is_auto_increment => 1,
185          },
186          name => {
187            data_type => 'text'
188          },
189        );
190
191        __PACKAGE__->set_primary_key('id');
192
193        __PACKAGE__->has_many(
194          cds => 'DeploymentIntro::Schema::Result::Cd', 'artist_id' );
195
196        1;
197
198       "Track.pm"
199
200        package DeploymentIntro::Schema::Result::Track;
201
202        use strict;
203        use warnings;
204
205        use parent 'DBIx::Class::Core';
206
207        __PACKAGE__->table('track');
208
209        __PACKAGE__->add_columns(
210          id => {
211            data_type => 'integer',
212            is_auto_increment => 1,
213          },
214          cd_id => {
215            data_type => 'integer',
216          },
217          title => {
218            data_type => 'text',
219          }
220        );
221
222        __PACKAGE__->set_primary_key('id');
223
224        __PACKAGE__->belongs_to(
225          cd => 'DeploymentIntro::Schema::Result::Cd', 'cd_id' );
226
227        1;
228
229       And then edit "lib/DeploymentIntro/Schema.pm" and add the following
230       above the 1 at the bottom
231
232        our $VERSION = 1;
233
234       Now it is just a matter of running
235
236        ./script/deploymentintro_dbicdh.pl install
237

AUTHOR

239       Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
240
242       This software is copyright (c) 2019 by Arthur Axel "fREW" Schmidt.
243
244       This is free software; you can redistribute it and/or modify it under
245       the same terms as the Perl 5 programming language system itself.
246
247
248
249perl v5.36.0          DBIx::Class:2:0D2e3p-l0o1y-m2e0ntHandler::Manual::CatalystIntro(3)
Impressum