daily pastebin goal
79%
SHARE
TWEET

Untitled

a guest Jul 22nd, 2018 65 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. #!/usr/bin/env perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use File::Spec;
  7. use Freestock::Web;
  8. use SQL::Translator;
  9. use DBIx::Class::DeploymentHandler;
  10. use DBIx::Class::Fixtures;
  11.  
  12. my $schema = Freestock::Web->model('Schema')->schema;
  13.  
  14. my $dh = DBIx::Class::DeploymentHandler->new({
  15.   schema => $schema,
  16.   databases => [qw/SQLite PostgreSQL MySQL/],
  17.   script_directory => Freestock::Web->path_to('share','migrations')->stringify,
  18.   sql_translator_args => { add_drop_table => 0 },
  19. });
  20.  
  21. sub dump_fixtures {
  22.   my $fixtures_dir = Freestock::Web->config->{fixtures_dir};
  23.   my $config_dir = Freestock::Web->path_to('share', 'migrations', 'fixture_configs');
  24.   my @configs = map { $_->basename } grep { -f $_ } $config_dir->children;
  25.   for my $config (@configs) {
  26.     next unless $config=~/json$/;
  27.     my $directory = File::Spec->catdir($fixtures_dir, $dh->database_version, $config);
  28.     print "Dumping $config to $directory\n";
  29.     DBIx::Class::Fixtures
  30.       ->new({
  31.         config_dir => $config_dir,
  32.       })->dump({
  33.         config => $config,
  34.         schema => $schema->clone,
  35.         directory => $directory,
  36.       });
  37.   }
  38. }
  39.  
  40. sub populate_db {
  41.   my $fixtures_dir = Freestock::Web->config->{fixtures_dir};  
  42.   my $fixtures = DBIx::Class::Fixtures->new({
  43.     config_dir => Freestock::Web->path_to('share', 'migrations', 'fixture_configs'),
  44.   });
  45.  
  46.   $fixtures->populate({
  47.     no_deploy => 1,
  48.     schema => $schema,
  49.     directory => $fixtures_dir,
  50.   });
  51. }
  52.  
  53. sub prepare_install {
  54.   $dh->prepare_install;
  55. }
  56.  
  57. sub current_version {
  58.   print $dh->database_version ."\n";
  59. }
  60.  
  61. sub generate_diagram {
  62.   print "Generating diagram of schema...";
  63.   SQL::Translator->new(
  64.     parser => 'SQL::Translator::Parser::DBIx::Class',
  65.     parser_args => {
  66.       package => $schema,
  67.     },
  68.     to => 'GraphViz',
  69.     producer_args => {
  70.       out_file =>  Freestock::Web->path_to('share','migrations','schema_v'.$dh->database_version.'.png'),
  71.       width => 48.0,
  72.       height => 33.3,
  73.       pagewidth => 48.6,
  74.       pageheight => 34.2,
  75.       show_constraints => 1,
  76.       show_datatypes => 1,
  77.       show_sizes => 1,
  78.       layout => 'dot',
  79.     },
  80.   )->translate;
  81.  
  82.   print "DONE!\n";
  83. }
  84.  
  85. sub help {
  86.   print <<'OUT';
  87. usage:
  88.   prepare-install
  89.   install
  90.   populate
  91.   dump-fixtures
  92.   version
  93.   generate-diagram
  94. OUT
  95. }
  96.  
  97. if(my $cmd = $ARGV[0]) {
  98.   if($cmd eq 'prepare-install') {
  99.     prepare_install();
  100.   } elsif($cmd eq 'install') {
  101.     $dh->install;
  102.   } elsif($cmd eq 'generate-diagram') {
  103.     generate_diagram();
  104.   } elsif($cmd eq 'dump-fixtures') {
  105.     dump_fixtures();
  106.   } elsif($cmd eq 'populate') {
  107.     populate_db();
  108.   } elsif($cmd eq 'version') {
  109.     print "The Database is at version: ". $dh->database_version. "\n";
  110.   } elsif($cmd eq 'upgrade') {
  111.     $dh->upgrade;
  112.   } else {
  113.     help();
  114.   }
  115. } else {
  116.   help();
  117. }
  118.  
  119. __END__
  120.  
  121.   my $version = $dh->database_version;
  122.   print "Preparing $version";
  123.   if ( $version >= 1 ) {
  124.     $dh->prepare_upgrade({
  125.       from_version => $version,
  126.       to_version   => $version+1,
  127.     });
  128.  
  129.     $dh->prepare_downgrade({
  130.       from_version => $version+1,
  131.       to_version   => $version,
  132.     });
  133.  }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top