For some reason, creating DBIx::Class schemas by
hand has never made sense to me. I like to write my CREATE TABLE
statements instead. DBIx::Class::Schema::Loader
works really well for this.
I keep this schema DDL in version control. I also keep a SQLite database around with some test data (but the database isn't in version control).
I usually find myself writing a little shell script or other program to to regenerate the DBIC schema from that test database. That usually requires me to make manual changes to the test database representing the changes I've just made to the DDL.
After doing this one too many times, I decided to combine DBIx::RunSQL with the schema loader. By creating a SQLite database from my DDL in memory, I can create a schema without me modifying any databases manually.
This was easier than I thought:
#!/usr/bin/env perl
use Modern::Perl;
use DBIx::RunSQL;
use DBIx::Class::Schema::Loader 'make_schema_at';
my $test_dbh = DBIx::RunSQL->create(
dsn => 'dbi:SQLite:dbname=:memory:',
sql => 'db/schema.sql',
force => 1,
verbose => 1,
);
make_schema_at( 'MyApp::Schema',
{
components => [ 'InflateColumn::DateTime', 'TimeStamp' ],
debug => 1,
dump_directory => './lib' ,
},
[ sub { $test_dbh }, {} ]
);
The next step is to connect everything to DBIx::Class::Migration—but first things first.