Commit | Line | Data |
c9d2e0a2 |
1 | #!/usr/bin/perl |
2 | use strict; |
3 | use warnings; |
4 | use Test::More; |
86ef9f0d |
5 | use File::Spec; |
f925f7cb |
6 | use File::Copy; |
c9d2e0a2 |
7 | |
1d48fcff |
8 | #warn "$dsn $user $pass"; |
9 | my ($dsn, $user, $pass); |
10 | |
c9d2e0a2 |
11 | BEGIN { |
1d48fcff |
12 | ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/}; |
13 | |
14 | plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test' |
8424c090 |
15 | unless ($dsn); |
1d48fcff |
16 | |
17 | |
18 | eval "use DBD::mysql; use SQL::Translator 0.08;"; |
c9d2e0a2 |
19 | plan $@ |
1d48fcff |
20 | ? ( skip_all => 'needs DBD::mysql and SQL::Translator 0.08 for testing' ) |
b4b1e91c |
21 | : ( tests => 13 ); |
c9d2e0a2 |
22 | } |
23 | |
b4b1e91c |
24 | my $version_table_name = 'dbix_class_schema_versions'; |
25 | my $old_table_name = 'SchemaVersions'; |
26 | |
c9d2e0a2 |
27 | use lib qw(t/lib); |
c9d2e0a2 |
28 | use_ok('DBICVersionOrig'); |
29 | |
1d48fcff |
30 | my $schema_orig = DBICVersion::Schema->connect($dsn, $user, $pass); |
b4b1e91c |
31 | eval { $schema_orig->storage->dbh->do('drop table ' . $version_table_name) }; |
32 | eval { $schema_orig->storage->dbh->do('drop table ' . $old_table_name) }; |
c9d2e0a2 |
33 | |
1d48fcff |
34 | is($schema_orig->ddl_filename('MySQL', 't/var', '1.0'), File::Spec->catfile('t', 'var', 'DBICVersion-Schema-1.0-MySQL.sql'), 'Filename creation working'); |
35 | unlink('t/var/DBICVersion-Schema-1.0-MySQL.sql') if (-e 't/var/DBICVersion-Schema-1.0-MySQL.sql'); |
36 | $schema_orig->create_ddl_dir('MySQL', undef, 't/var'); |
c9d2e0a2 |
37 | |
1d48fcff |
38 | ok(-f 't/var/DBICVersion-Schema-1.0-MySQL.sql', 'Created DDL file'); |
39 | $schema_orig->deploy({ add_drop_table => 1 }); |
40 | $schema_orig->upgrade(); |
c9d2e0a2 |
41 | |
1d48fcff |
42 | my $tvrs = $schema_orig->{vschema}->resultset('Table'); |
a2800991 |
43 | is($schema_orig->_source_exists($tvrs), 1, 'Created schema from DDL file'); |
c9d2e0a2 |
44 | |
45 | eval "use DBICVersionNew"; |
f925f7cb |
46 | { |
1d48fcff |
47 | unlink('t/var/DBICVersion-Schema-2.0-MySQL.sql'); |
48 | unlink('t/var/DBICVersion-Schema-1.0-2.0-MySQL.sql'); |
f925f7cb |
49 | |
1d48fcff |
50 | my $schema_upgrade = DBICVersion::Schema->connect($dsn, $user, $pass); |
f925f7cb |
51 | is($schema_upgrade->get_db_version(), '1.0', 'get_db_version ok'); |
1d48fcff |
52 | is($schema_upgrade->schema_version, '2.0', 'schema version ok'); |
53 | $schema_upgrade->create_ddl_dir('MySQL', '2.0', 't/var', '1.0'); |
54 | ok(-f 't/var/DBICVersion-Schema-1.0-2.0-MySQL.sql', 'Created DDL file'); |
55 | $schema_upgrade->upgrade(); |
56 | is($schema_upgrade->get_db_version(), '2.0', 'db version number upgraded'); |
f925f7cb |
57 | |
58 | eval { |
f925f7cb |
59 | $schema_upgrade->storage->dbh->do('select NewVersionName from TestVersion'); |
60 | }; |
61 | is($@, '', 'new column created'); |
f925f7cb |
62 | } |
b4b1e91c |
63 | |
64 | { |
65 | my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass); |
66 | eval { |
67 | $schema_version->storage->dbh->do('select * from ' . $version_table_name); |
68 | }; |
69 | is($@, '', 'version table exists'); |
70 | |
71 | eval { |
72 | $schema_version->storage->dbh->do("DROP TABLE IF EXISTS $old_table_name"); |
73 | $schema_version->storage->dbh->do("RENAME TABLE $version_table_name TO $old_table_name"); |
74 | }; |
75 | is($@, '', 'versions table renamed to old style table'); |
76 | |
77 | $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass); |
78 | is($schema_version->get_db_version, '2.0', 'transition from old table name to new okay'); |
79 | |
80 | eval { |
81 | $schema_version->storage->dbh->do('select * from ' . $old_table_name); |
82 | }; |
83 | ok($@, 'old version table gone'); |
84 | |
85 | } |