Class-Usul

 view release on metacpan or  search on metacpan

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

   my $user = $self->db_admin_ids->{ lc $self->driver } || NUL;

   $copts->{user} = $self->get_line( $prompt, $user, TRUE, 0 );
   $prompt = '+Database administrator password';
   $copts->{password} = $self->get_line( $prompt, AS_PASSWORD );
   return $copts;
};

my $_build_qdb = sub {
   my $self = shift;
   my $cmds = $self->ddl_commands->{ lc $self->driver };
   my $code = $cmds ? $cmds->{ '-qualify_db' } : undef;

   return $code ? $code->( $self, $self->database ) : $self->database;
};

my $_connect_info = sub {
   my $self = shift;

   return $self->get_connect_info( $self, { database => $self->database } );
};

my $_extract_from_dsn = sub {
   my ($self, $field, $dsn) = @_;

   $self->options and $self->options->{bootstrap} and return;

   return (map  { s{ \A $field [=] }{}mx; $_ }
           grep { m{ \A $field [=] }mx }
           split  m{           [;] }mx, $dsn // $self->dsn)[ 0 ];
};

my $_qualify_database_path = sub {
   return $_[ 0 ]->config->datadir->catfile( $_[ 1 ].'.db' )->pathname;
};

my $_rebuild_dsn = sub {
   my $self = shift;
   my $dsn  = 'dbi:'.$self->driver.':database='.$self->_qualified_db;

   $self->host and $dsn .= ';host='.$self->host;
   $self->port and $dsn .= ';port='.$self->port;

   return $self->_set_dsn( $dsn );
};

my $_rebuild_qdb = sub {
   my $self = shift; $self->_set__qualified_db( $self->$_build_qdb ); return;
};

# Public attributes
option 'all'            => is => 'ro',  isa => Bool, default => FALSE,
   documentation        => 'Perform operation for all possible schema',
   short                => 'a';

option 'database'       => is => 'rwp', isa => NonEmptySimpleStr,
   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]"',



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