DBIx::Skinny::Schema::Loader - Schema loader for DBIx::Skinny
Run-time schema loading:
package Your::DB::Schema; use base qw/DBIx::Skinny::Schema::Loader/; __PACKAGE__->load_schema; 1;
Preloaded schema:
Given a the following source code 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', '', '' ] );
you can execute
$ perl publish_schema.pl > Your/DB/Schema.pm
to create a static schema class.
DBIx::Skinny::Schema::Loader is schema loader for DBIx::Skinny. It can dynamically load schema at run-time or statically publish them.
It supports MySQL and SQLite, and PostgreSQL.
Probably no need for public use.
Instead, invoke concrete db driver class named "DBIx::Skinny::Schema::Loader::DBI::XXXX".
Dynamically load the schema
load_schema refers to connect info in your Skinny class. When your schema class is named Your::DB::Schema, Loader considers Your::DB as a Skinny class.
load_schema
connect info
Your::DB::Schema
Your::DB
load_schema executes install_table for all tables, automatically setting primary key and columns.
install_table
Also the sections how loader find primary keys and additional settings for load_schema.
how loader find primary keys
additional settings for load_schema
Return schema file content as a string. This function is exportable.
$schema_class is schema class name that you want publish.
$schema_class
$options are described in the options of make_schema_at section.
$options
options of make_schema_at
$connect_info is ArrayRef or HashRef. If it is an arrayref, it contains dsn, username, password to connect to the database. If it is an hashref, it contains same parameters as DBIx::Skinny->new(\%opts).
$connect_info
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
Here is how to use additional settings:
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 might be concerned that calling install_table without pk and columns doesn't work. However, DBIx::Skinny allows install_table to be called twice or more.
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 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', '', '' ] );
there are more detailed example in $before_template section.
$before_template
you can use both before_template and after_template all together.
DEPRECATED. this option is provided for backward compatibility.
you can use before_template instead of this.
use your custom template for install_table.
my $table_template = << '...'; install_table [% table %] => schema { pk qw/[% 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 keys joined by a space [% columns %] ... columns joined by a space
make_schema_at
you can exclude tables that matching any rules declared in ignore_rules from the schema.
ignore_rules => [ qr/rs$/, qr/^no/ ],
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);
Ryo Miyake <ryo.studiom {at} gmail.com>
DBIx::Skinny, DBIx::Class::Schema::Loader
Ryo Miyake <ryo.studiom __at__ gmail.com>
<ryo.studiom __at__ gmail.com>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install DBIx::Skinny::Schema::Loader, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::Skinny::Schema::Loader
CPAN shell
perl -MCPAN -e shell install DBIx::Skinny::Schema::Loader
For more information on module installation, please visit the detailed CPAN module installation guide.