Search code examples
perldbix-class

Is there a way to connect to a DBIx::Class schema using an existing DBI database handle?


If I already have an active DBI database handle, is there a way to instantiate a DBIx::Class schema using that database handle, rather than creating a new connection, e.g. something like

my $schema = MyApp::Schema->connect($dbh);

(This is because of some legacy code using some newer DBIC-based code. No, I cannot connect to the schema and pass the schema's database handle to the legacy code, and no, I am not able to rewrite the legacy code to use DBIC.)


Solution

  • I think this will work

    my $schema = MyApp::Schema->connect(sub { $dbh });
    

    from perldoc DBIx::Class::Storage::DBI