File: 20-version-table-rename.t

package info (click to toggle)
libdbix-class-deploymenthandler-perl 0.002234-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 552 kB
  • sloc: perl: 4,139; makefile: 2
file content (95 lines) | stat: -rw-r--r-- 2,504 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!perl

use strict;
use warnings;

use lib 't/version-table-rename-lib';
use DBICDHTest;
use DBIx::Class::DeploymentHandler;
use aliased 'DBIx::Class::DeploymentHandler', 'DH';

use Test::More;
use File::Temp 'tempdir';
use Test::Fatal qw(lives_ok dies_ok);

my $dbh = DBICDHTest::dbh();
my @connection = (sub { $dbh }, { ignore_version => 1 });
my $sql_dir = tempdir( CLEANUP => 1 );

VERSION1: {
  use_ok 'DBICVersion_v1';
  my $s = DBICVersion::Schema->connect(@connection);
  $DBICVersion::Schema::VERSION = 1;
  ok($s, 'DBICVersion::Schema 1 instantiates correctly');
  my $handler = DH->new({
    script_directory => $sql_dir,
    schema => $s,
    databases => 'SQLite',
    sql_translator_args => { add_drop_table => 0 },
  });

  ok($handler, 'DBIx::Class::DeploymentHandler w/1 instantiates correctly');

  my $version = $s->schema_version;
  $handler->prepare_install;

  dies_ok {
    $s->resultset('Foo')->create({
      bar => 'frew',
    })
  } 'schema not deployed';
  $handler->install;
  lives_ok {
    $s->resultset('Foo')->create({
      bar => 'frew',
    })
  } 'schema is deployed';
}

VERSION2: {
  use_ok 'DBICVersion_v2';
  my $s = DBICVersion::Schema->connect(@connection);
  $DBICVersion::Schema::VERSION = 2;
  $s->unregister_source('__VERSION'); # remove leftover version source
  ok($s, 'DBICVersion::Schema 2 instantiates correctly');

  my $handler = DH->new({
    initial_version => 1,
    script_directory => $sql_dir,
    schema => $s,
    databases => 'SQLite',
    version_source => 'DBICDHVersion',
    version_class => 'DBICVersion::Version',
  });
  ok($handler, 'DBIx::Class::DeploymentHandler w/2 instantiates correctly');

  my $version = $s->schema_version();
  $handler->prepare_install;
  $handler->prepare_upgrade({ from_version => 1, to_version => $version} );

  # manually add SQL to rename the version table
  open(my $fh, '>', "$sql_dir/SQLite/upgrade/1-2/002-version-table-rename.sql")
    or die "unable to open '$sql_dir/SQLite/upgrade/1-2/002-version-table-rename.sql' ($!)";
  print {$fh} <<SQL;
    ALTER TABLE dbix_class_deploymenthandler_versions RENAME TO dbic_version;
SQL
  close $fh;

  dies_ok {
    $s->resultset('Foo')->create({
      bar => 'frew',
      baz => 'frew',
    })
  } 'schema not deployed';
  $handler->upgrade;
  lives_ok {
    $s->resultset('Foo')->create({
      bar => 'frew',
      baz => 'frew',
    })
  } 'schema is deployed';

  is $handler->database_version, 2, 'correct schema version is set';
}

done_testing;