Class-Usul

 view release on metacpan or  search on metacpan

lib/Class/Usul/Schema.pm  view on Meta::CPAN

   documentation        => 'The database to connect to',
   format               => 's', lazy => TRUE, required => TRUE,
   trigger              => $_rebuild_qdb;

option 'db_admin_accounts' => is => 'ro', isa => HashRef,
   documentation        => 'For each RDBMS the name of the system database',
   default              => sub { { mysql  => 'mysql',
                                   pg     => 'postgres',
                                   sqlite => NUL, } },
   format               => 's%';

option 'db_admin_ids'   => is => 'ro',   isa => HashRef,
   documentation        => 'The default admin user ids for each RDBMS',
   default              => sub { { mysql  => 'root',
                                   pg     => 'postgres',
                                   sqlite => NUL, } },
   format               => 's%';

option 'db_attr'        => is => 'ro',   isa => HashRef,
   documentation        => 'Default database connection attributes',
   default              => sub { { add_drop_table    => TRUE,
                                   no_comments       => TRUE,
                                   quote_identifiers => TRUE, } },
   format               => 's%';

option 'dry_run'        => is => 'ro',   isa => Bool, default => FALSE,
   documentation        => 'Prints out commands, do not execute them',
   short                => 'd';

option 'preversion'     => is => 'rwp',  isa => Str, default => NUL,
   documentation        => 'Previous schema version',
   format               => 's';

option 'rdbms'          => is => 'lazy', isa => ArrayRef, autosplit => COMMA,
   documentation        => 'List of supported RDBMSs',
   default              => sub { [ qw( MySQL PostgreSQL SQLite ) ] },
   format               => 's@';

option 'schema_classes' => is => 'lazy', isa => HashRef, default => sub { {} },
   documentation        => 'The database schema classes',
   format               => 's%';

option 'schema_version' => is => 'ro',   isa => NonEmptySimpleStr,
   documentation        => 'Current schema version',
   default              => '0.1', format => 's';

option 'unlink'         => is => 'rwp',  isa => Bool, default => FALSE,
   documentation        => 'If true remove DDL file before creating new ones';

option 'yes'            => is => 'ro',   isa => Bool, default => FALSE,
   documentation        => 'When true flips the defaults for yes/no questions',
   short                => 'y';

has 'connect_options'   => is => 'lazy', isa => HashRef,
   builder              => $_build_connect_options;

has 'ddl_commands'      => is => 'lazy', isa => HashRef, builder => sub { {
   'mysql'              => {
      'create_user'     => "create user '[_2]'\@'%' identified by '[_3]';",
      'create_db'       => 'create database [_3] default '
                         . 'character set utf8 collate utf8_unicode_ci;',
      'drop_db'         => 'drop database if exists [_3];',
      'drop_user'       => "drop user '[_2]'\@'%';",
      'exists_db'       => 'select 1 from information_schema.SCHEMATA '
                         . "where SCHEMA_NAME = '[_3]';",
      'exists_user'     => 'select 1 from mysql.user '
                         . "where User = '[_2]' and Host = '%';",
      'grant_all'       => "grant all privileges on [_3].* to '[_2]'\@'%' "
                         . 'with grant option;',
      '-execute_ddl'    => 'mysql -A -h [_1] -u [_2] -p"[_3]" [_5]', },
   'pg'                 => {
      'create_user'     => "create role [_2] login password '[_3]';",
      'create_db'       => "create database [_3] owner [_2] encoding 'UTF8';",
      'drop_db'         => 'drop database if exists [_3];',
      'drop_user'       => 'drop user if exists [_2];',
      'exists_db'       => "select 1 from pg_database where datname = '[_3]';",
      'exists_user'     => "select 1 from pg_user where usename = '[_2]';",
      '-execute_ddl'    => 'PGPASSWORD=[_3] '
                         . 'psql -h [_1] -q -t -U [_2] -w -c "[_4]"',
      '-no_pipe'        => TRUE, },
   'sqlite'             => {
      '-execute_ddl'    => "sqlite3 [_6] '[_4]'",
      '-no_pipe'        => TRUE,
      '-qualify_db'     => $_qualify_database_path, }, } };

has 'driver'            => is => 'rwp',  isa => NonEmptySimpleStr,
   builder              => sub { (split m{ [:] }mx, $_[ 0 ]->dsn)[ 1 ] },
   lazy                 => TRUE, trigger => $_rebuild_dsn;

has 'dsn'               => is => 'rwp',  isa => NonEmptySimpleStr,
   builder              => sub { $_[ 0 ]->$_connect_info->[ 0 ] },
   lazy                 => TRUE;

has 'host'              => is => 'rwp',  isa => Maybe[SimpleStr],
   builder              => sub { $_[ 0 ]->$_extract_from_dsn( 'host' ) },
   lazy                 => TRUE, trigger => $_rebuild_dsn;

has 'password'          => is => 'rwp',  isa => SimpleStr,
   builder              => sub { $_[ 0 ]->$_connect_info->[ 2 ] },
   lazy                 => TRUE;

has 'port'              => is => 'rwp',  isa => Maybe[PositiveInt],
   builder              => sub { $_[ 0 ]->$_extract_from_dsn( 'port' ) },
   lazy                 => TRUE, trigger => $_rebuild_dsn;

has 'user'              => is => 'rwp',  isa => SimpleStr,
   builder              => sub { $_[ 0 ]->$_connect_info->[ 1 ] },
   lazy                 => TRUE;

has '_qualified_db'     => is => 'rwp',  isa => NonEmptySimpleStr,
   builder              => $_build_qdb, lazy => TRUE, trigger => $_rebuild_dsn;

# Private functions
my $_inflate = sub {
   return inflate_placeholders [ 'undef', 'null', TRUE ], @_;
};

my $_unquote = sub {
   local $_ = $_[ 0 ]; s{ \A [\'\"] }{}mx; s{ [\'\"] \z }{}mx; return $_;
};



( run in 0.736 second using v1.01-cache-2.11-cpan-39bf76dae61 )