DBIx::C...ystIntro(3)

πŸ’– NAME πŸ’–

DBIx::Class::DeploymentHandler::Manual::CatalystIntro β€” okay so this is the intro to using DBIx::Class::DeploymentHandler with a fresh new Catalyst project. πŸ¦‹ It's part of the User Contributed Perl Documentation, and it's here to hold your hand through the whole setup. iykyk. πŸ’…


🌸 Background 🌸

So this lil intro is gonna use PostgreSQL and Catalyst together, bestie. If you want the background tea on running PostgreSQL with Catalyst, it's over at Catalyst::Manual::Tutorial::10_Appendices. πŸ‘‘

Also, real talk: this guide assumes you already have some understanding of Catalyst. So if you haven't done the Catalyst tutorials yet β€” go do those first, no cap. 🫢


πŸ’Ύ Database Setup πŸ’Ύ

First thing first: make a user called catalyst, with the 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 go ahead and create a new database called deploymentintro. πŸ’–

sudo -u postgres createdb -O catalyst deploymentintro

πŸŽ€ Create the project πŸŽ€

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

main character energy fr fr. ✨


πŸ› οΈ 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

Now, remove the following from lib/DeploymentIntro/Model/DB.pm: πŸ™ˆ

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

Then remove deploymentintro.conf, and make a new file called deploymentintro_local.pl with this in it: πŸ’œ

{
    name => "DeploymentIntro",

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

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

Copy this whole program into scripts, and name it 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() }
}

Next up, copy these 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 this right above the 1 at the bottom: πŸ’…

our $VERSION = 1;

And now it's literally just a matter of running πŸ”₯

./script/deploymentintro_dbicdh.pl install

slay. ✨


πŸ‘‘ AUTHOR πŸ‘‘

Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com> β€” the icon, the legend. 🫢


This software is copyright (c) 2025 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. πŸ’–


okay bestie that's the whole page!! 🌸 this was generated by perl v5.42.0 on 2026-01-17 β€” go build something iconic with it, mwah πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from