Search code examples
perlpostgresqldbix-class

How do you deal with multiple PostgreSQL schemas and DBIx::Class?


In my database I have 5 separate schemas so that I can organize objects into logical groups. I used dbicdump to create my DBIx::Class schema, but noticed that it only loaded tables from the public schema. I figured out how to load all my schemas using dbicdump, but now I'm at a loss on where to go from here setting everything up and using it. I've looked online for any resources, and found very little documentation. The only thing I did find, I really wasn't too happy with the solution. I am wondering if anyone has encountered this before, and how they dealt with it.


Solution

  • You'll need a recent DBIx::Class I believe, but I use something like this:

    #!/usr/bin/perl
    use v5.14;
    
    use DBIx::Class::Schema::Loader qw/ make_schema_at /;
    use Path::Tiny;
    
    my $dest_dir = '.';
    my $pkg_base = 'Foo';
    (my $pkg_dir = $pkg_base) =~ s/::/\//g;
    
    # Delete any previous run
    path($dest_dir.'/'.$pkg_base)->remove_tree;
    
    make_schema_at(
        $pkg_base.'::Schema',
        {
            debug          => 0,
            dump_directory => $dest_dir,
            db_schema      => '%',
            moniker_parts  => ['schema', 'name'],
            #rescan         => 1,
        },
        [ 'dbi:Pg:dbname=mydb port=5432', 'dbuser', 'dbpass' ],
    );
    
    exit;
    

    That gives me something like:

    Foo::Schema::Result::PublicTable1
    Foo::Schema::Result::Schema2Table2
    etc
    

    There are details of the various options in the docs. I seem to remember I cross-referenced the docs with the source of dbicdump when I put it together.

    HTH