Acrux-DBI

 view release on metacpan or  search on metacpan

lib/Acrux/DBI/Dump.pm  view on Meta::CPAN


=head1 DESCRIPTION

This class is used by L<Acrux::DBI> to allow database schemas import.
A dump file is just a collection of sql blocks, with one or more statements, separated by comments of the form
C<-- #NAME> or C<-- # NAME>

  -- #foo
  CREATE TABLE `pets` (`pet` TEXT);
  INSERT INTO `pets` VALUES ('cat');
  INSERT INTO `pets` VALUES ('dog');
  delimiter //
  CREATE PROCEDURE `test`()
  BEGIN
    SELECT `pet` FROM `pets`;
  END
  //

  -- #bar
  DROP TABLE `pets`;
  DROP PROCEDURE `test`;

  -- #baz (...you can comment freely here...)
  -- ...and here...
  CREATE TABLE `stuff` (`whatever` INT);

  -- #main
  DROP TABLE `stuff`;

This idea is to let you import SQL dumps step by step by its names

=head1 ATTRIBUTES

This class implements the following attributes

=head2 dbi

    $dump = $dump->dbi($dbi);
    my $dbi = $dump->dbi;

The object these processing belong to

=head2 name

    my $name = $dump->name;
    $dump = $dump->name('foo');

Name for this dump, defaults to C<schema>

=head1 METHODS

This class implements all methods from L<Mojo::Base> and implements
the following new ones

=head2 from_data

    $dump = $dump->from_data;
    $dump = $dump->from_data('main');
    $dump = $dump->from_data('main', 'file_name');

Extract dump data from a file in the DATA section of a class with
L<Mojo::Loader/"data_section">, defaults to using the caller class and
L</"name">.

  __DATA__
  @@ schema

  -- # up
  CREATE TABLE `pets` (`pet` TEXT);
  INSERT INTO `pets` VALUES ('cat');
  INSERT INTO `pets` VALUES ('dog');

  -- # down
  DROP TABLE `pets`

=head2 from_file

    $dump = $dump->from_file('/tmp/schema.sql');

Read dump data from a file

=head2 from_string

    $dump = $dump->from_string('
      -- # up
      CREATE TABLE `pets` (`pet` TEXT);

      -- # down
      DROP TABLE `pets`
    ');

Read dump data from string

=head2 peek

    my $sqls = $dump->peek; # 'main'
    my $sqls = $dump->peek('foo');
    my @sqls = $dump->peek('foo');

This method returns an array/arrayref of SQL statements stored at a specified dump location by tag-name.
By default will be used the C<main> tag

=head2 poke

    $dump = $dump->poke; # 'main'
    $dump = $dump->poke('foo');

Import named data-block of SQL dump to database by tag-name. By default will be used the C<main> tag

=head1 HISTORY

See C<Changes> file

=head1 TO DO

See C<TODO> file

=head1 SEE ALSO

L<Acrux::DBI>, L<Mojo::mysql>, L<Mojo::Pg>

=head1 AUTHOR

Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>



( run in 1.867 second using v1.01-cache-2.11-cpan-140bd7fdf52 )