NAME

DBIx::Skinny::Schema::Loader - Schema loader for DBIx::Skinny

SYNOPSIS

dynamic schema loading.

package Your::DB::Schema;
use base qw/DBIx::Skinny::Schema::Loader/;

__PACKAGE__->load_schema;

1;

or you can get static content of schema class. for example, following source save as "publish_schema.pl"

use DBIx::Skinny::Schema::Loader qw/make_schema_at/;
print make_schema_at(
  'Your::DB::Schema',
  {
    # options here
  },
  [ 'dbi:SQLite:test.db', '', '' ]
);

and execute $ perl publish_schema.pl > Your/DB/Schema.pm

DESCRIPTION

DBIx::Skinny::Schema::Loader is schema loader for DBIx::Skinny. Supported dynamic schema loading and static publish.

it supports MySQL and SQLite, PostgreSQL is not supported yet.

METHODS

connect( $dsn, $user, $pass )

perhaps you don't have to use it manually.

invoke concrete db driver class named "DBIx::Skinny::Schema::Loader::DBI::XXXX".

load_schema

loading schema dynamically.

package Your::DB::Schema;
use base qw/DBIx::Skinny::Schema::Loader/;

__PACKAGE__->load_schema;

1;

load_schema refer to connect info in your Skinny class. when your schema class named "Your::DB::Schema", Loader considers "Your::DB" as Skinny class.

load_schema execute install_table for all tables. set pk and columns automatically.

see also how loader find primary keys, additional settings for load_schema section.

make_schema_at( $schema_class, $options, $connect_info )

return schema file content. you can use make_schema_as an imported function.

use DBIx::Skinny::Schema::Loader qw/make_schema_at/;
print make_schema_at(
    'Your::DB::Schema',
    {
      # options here
    },
    [ 'dbi:SQLite:test.db', '', '' ]
);

$schema_class is schema class name that you want publish.

$options detail in options of make_schema_st section.

$connect_info is arrayref of dsn, username, password to connect DB.

HOW LOADER FIND PRIMARY KEYS

surely primary key defined at DB, use it as PK.

in case of primary key is not defined at DB, Loader find PK following logic. 1. if table has only one column, use it 2. if table has column 'id', use it

unless found PK yet, Loader throws exception.

DBIx::Skinny is not support composite primary key. When PK is composite key, load_schema does not set pk, make_schema_at publish "pk ''". You should set pk manually.

ADDITIONAL SETTINGS FOR load_schema

if you want to use additional settings, write like it

package Your::DB::Schema;
use base qw/DBIx::Skinny::Schema::Loader/;

use DBIx::Skinny::Schema;  # import schema functions

install_utf8_columns qw/title content/;

install_table books => schema {
  trigger pre_insert => sub {
    my ($class, $args) = @_;
    $args->{ created_at } ||= DateTime->now;
  };
};

__PACKAGE__->load_schema;

1;

'use DBIx::Skinny::Schema' works to import schema functions. you can write instead of it, 'BEGIN { DBIx::Skinny::Schema->import }' because 'require DBIx::Skinny::Schema' was done by Schema::Loader.

You may worry call install_table without pk and columns doesn't work. Don't worry, DBIx::Skinny allows call install_table twice or more.

OPTIONS OF make_schema_st

before_template

insert your custom template before install_table block.

my $tmpl = << '...';
# custom template
install_utf8_columns qw/title content/;
...

install_table books => schema {
  trigger pre_insert => sub {
    my ($class, $args) = @_;
    $args->{ created_at } ||= DateTime->now;
  }
}
...

print make_schema_at(
    'Your::DB::Schema',
    {
        before_template => $tmpl,
    },
    [ 'dbi:SQLite:test.db', '', '' ]
);

then you get content inserted your template before install_table block.

after_template

after_template works like before_template mostly. after_template inserts template after install_table block.

print make_schema_at(
    'Your::DB::Schema',
    {
        before_template => $before,
        after_template  => $after,
    },
    [ 'dbi:SQLite:test.db', '', '' ]
);

you can use both before_template and after_template all together.

template

DEPLICATED. this option is provided for backward compatibility.

you can use before_template instead of this.

table_template

use your custom template for install_table.

my $table_template = << '...';
install_table [% table %] => schema {
    pk '[% pk %]';
    columns qw/[% columns %]/;
    trigger pre_insert => $created_at;
};

...

print make_schema_at(
    'Your::DB::Schema',
    {
        table_template => $table_template,
    },
    [ 'dbi:SQLite:test.db', '', '' ]
);

your schema's install_table block will be

install_table books => schema {
    pk 'id';
    columns qw/id author_id name/;
    tritter pre_insert => $created_at;
};

make_schema_at replaces some following variables. [% table %] ... table name [% pk %] ... primary key [% columns %] ... columns joined by a space

LAZY SCHEMA LOADING

if you write Your::DB class without setup sentence,

package MyApp::DB;
use DBIx::Skinny;
1;

you should not call load_schema in your class file.

package MyApp::DB::Schema;
use base qw/DBIx::Skinny::Schema::Loader/;
1;

call load_schema with dsn manually in your app.

my $db = MyApp::DB->new;
my $connect_info = {
    dsn      => $dsn,
    username => $user,
    password => $password,
};
$db->connect($connect_info);
$db->schema->load_schema($connect_info);

AUTHOR

Ryo Miyake <ryo.studiom {at} gmail.com>

SEE ALSO

DBIx::Skinny, DBIx::Class::Schema::Loader

AUTHOR

Ryo Miyake <ryo.studiom __at__ gmail.com>

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.