Extracting the Schema of an Existing DB.

Overview

Do you have an existing database app that you’d like to port to DBIC? Well allow me to introduce you to the make_schema_at method of DBIx::Class::Schema::Loader. Let’s say I want to port an old database app to Catalyst and use DBIC in the model.

Examples

Dump Schema - no restrictions

perl -MDBIx::Class::Schema::Loader=make_schema_at,dump_to_dir:./lib  -e '
make_schema_at("HomePage::Schema",  
{ debug => 1 }, 
[ "DBI:mysql:notes:localhost","dbuser", "pass" ])'

Dump only tables matching radacct - constraint

perl -MDBIx::Class::Schema::Loader=make_schema_at,dump_to_dir:./lib  -e '
make_schema_at("Radius::Schema",  

    constraint => qr/radacct/,
    debug => 1,
 }, 
[ "DBI:mysql:radius:localhost","dbuser""pass" ])'

Dump all table except cdr_* ones - exclude

perl -MDBIx::Class::Schema::Loader=make_schema_at,dump_to_dir:./  -e '
make_schema_at("F::DB",  

    exclude => qr/^cdr_/,
    debug => 1,
 }, 
[ "DBI:mysql:pbxtra:localhost","dbuser""pass" ])'

Output

When you run make_schema_at on your database, you get back a genuine DBIC schema complete with Result classes. Here is an example of HomePage::Schema::Main:

package HomePage::Schema::Main;

use strict;
use warnings;

use base 'DBIx::Class';

__PACKAGE__->load_components("Core");
__PACKAGE__->table("main");
__PACKAGE__->add_columns(
  "id",
  {
    data_type => "SMALLINT",
    default_value => undef,
    is_nullable => 0,
    size => 6,
  },
  "cat",
  { data_type => "VARCHAR", default_value => "", is_nullable => 0, size => 16 },
  "event",
  { data_type => "VARCHAR", default_value => "", is_nullable => 0, size => 64 },
  "content",
  {
    data_type => "TEXT",
    default_value => undef,
    is_nullable => 1,
    size => 65535,
  },
  "last_modified",
  {
    data_type => "TIMESTAMP",
    default_value => "CURRENT_TIMESTAMP",
    is_nullable => 0,
    size => 14,
  },
  "f_year",
  { data_type => "INT", default_value => undef, is_nullable => 1, size => 4 },
  "f_month",
  { data_type => "INT", default_value => undef, is_nullable => 1, size => 2 },
  "f_day",
  { data_type => "INT", default_value => undef, is_nullable => 1, size => 2 },
  "project",
  { data_type => "INT", default_value => undef, is_nullable => 1, size => 11 },
);
__PACKAGE__->set_primary_key("id");


# Created by DBIx::Class::Schema::Loader v0.04005 @ 2009-03-10 15:13:38
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:oJ8e4MDZJRGsQ3aX4R4Qjg


# You can replace this text with custom content, and it will be preserved on regeneration
1;

See the DBIC Tutorial for more information on Result classes.

My tags:
 
Popular tags:
 
Powered by MojoMojo