NAME

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

Background

This introduction will use PostgreSQL and Catalyst. Background information on using PostgreSQL with Catalyst can be found at 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.

Database Setup

Start by creating a user catalyst, with password catalyst

$ sudo -u postgres createuser -P catalyst
Enter password for new role: <catalyst>
Enter it again: <catalyst>
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n

Then create a new database called deploymentintro

sudo -u postgres createdb -O catalyst deploymentintro

Create the project

$ catalyst.pl DeploymentIntro
$ cd DeploymentIntro
$ perl Makefile.PL

Create the Schema

$ script/deploymentintro_create.pl model DB DBIC::Schema DeploymentIntro::Schema \
    create=static 'dbi:Pg:dbname=deploymentintro' 'catalyst' 'catalyst' '{ AutoCommit => 1 }'

$ mkdir -p lib/Schema/Result

Remove the following from lib/DeploymentIntro/Model/DB.pm:

connect_info => {
  dsn => 'dbi:Pg:dbname=deploymentintro',
  user => 'catalyst',
  password => 'catalyst',
  AutoCommit => q{1},
}

Remove deploymentintro.conf and create a new file called deploymentintro_local.pl with the following:

{
    name => "DeploymentIntro",

    "Model::DB" => {
        schema_class => 'DeploymentIntro::Schema',

        connect_info => {
            dsn        => 'dbi:Pg:dbname=deploymentintro',
            user       => 'catalyst',
            password   => 'catalyst',
            AutoCommit => 1,
        }
    }
}

Copy the following program into scripts, under the name deploymentintro_dbicdh.pl

#!/usr/bin/env perl

use strict;
use warnings;

use feature ":5.10";

use aliased 'DBIx::Class::DeploymentHandler' => 'DH';
use FindBin;
use lib "$FindBin::Bin/../lib";
use DeploymentIntro::Schema;
use Config::JFDI;

my $config = Config::JFDI->new( name => 'DeploymentIntro' );
my $config_hash  = $config->get;
my $connect_info = $config_hash->{"Model::DB"}{"connect_info"};
my $schema       = DeploymentIntro::Schema->connect($connect_info);

my $dh = DH->new({
  schema           => $schema,
  script_directory => "$FindBin::Bin/../dbicdh",
  databases        => 'PostgreSQL',
});

sub install {
  $dh->prepare_install;
  $dh->install;
}

sub upgrade {
  die "Please update the version in Schema.pm"
    if ( $dh->version_storage->version_rs->search({version => $dh->schema_version})->count );

  die "We only support positive integers for versions around these parts."
    unless $dh->schema_version =~ /^\d+$/;

  $dh->prepare_deploy;
  $dh->prepare_upgrade;
  $dh->upgrade;
}

sub current_version {
  say $dh->database_version;
}

sub help {
say <<'OUT';
usage:
  install
  upgrade
  current-version
OUT
}

help unless $ARGV[0];

given ( $ARGV[0] ) {
    when ('install')         { install()         }
    when ('upgrade')         { upgrade()         }
    when ('current-version') { current_version() }
}

Copy the following files into lib/DeploymentIntro/Schema/Result:

Cd.pm

package DeploymentIntro::Schema::Result::Cd;

use strict;
use warnings;

use parent 'DBIx::Class::Core';

__PACKAGE__->load_components(qw(InflateColumn::DateTime));
__PACKAGE__->table('cd');

__PACKAGE__->add_columns(
  id => {
    data_type => 'integer',
    is_auto_increment => 1,
  },
  artist_id => {
    data_type => 'integer'
  },
  title => {
    data_type => 'text'
  },
);

__PACKAGE__->set_primary_key('id');

__PACKAGE__->belongs_to(
  artist => 'DeploymentIntro::Schema::Result::Artist', 'artist_id' );

__PACKAGE__->has_many(
  tracks => 'DeploymentIntro::Schema::Result::Track', 'cd_id' );

1;

Artist.pm

package DeploymentIntro::Schema::Result::Artist;

use strict;
use warnings;

use parent 'DBIx::Class::Core';

__PACKAGE__->table('artist');

__PACKAGE__->add_columns(
  id => {
    data_type => 'integer',
    is_auto_increment => 1,
  },
  name => {
    data_type => 'text'
  },
);

__PACKAGE__->set_primary_key('id');

__PACKAGE__->has_many(
  cds => 'DeploymentIntro::Schema::Result::Cd', 'artist_id' );

1;

Track.pm

package DeploymentIntro::Schema::Result::Track;

use strict;
use warnings;

use parent 'DBIx::Class::Core';

__PACKAGE__->table('track');

__PACKAGE__->add_columns(
  id => {
    data_type => 'integer',
    is_auto_increment => 1,
  },
  cd_id => {
    data_type => 'integer',
  },
  title => {
    data_type => 'text',
  }
);

__PACKAGE__->set_primary_key('id');

__PACKAGE__->belongs_to(
  cd => 'DeploymentIntro::Schema::Result::Cd', 'cd_id' );

1;

And then edit lib/DeploymentIntro/Schema.pm and add the following above the 1 at the bottom

our $VERSION = 1;

Now it is just a matter of running

./script/deploymentintro_dbicdh.pl install

AUTHOR

Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2024 by Arthur Axel "fREW" Schmidt.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.