12 my ($dsn, $user, $pass) = ('dbi:SQLite::memory:','','');
15 eval { require Time::HiRes }
16 || plan skip_all => 'Test needs Time::HiRes';
17 Time::HiRes->import(qw/time sleep/);
21 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy')
22 unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy')
26 use DBICTest; # do not remove even though it is not used
28 use_ok('DBICVersion_v1');
30 my $version_table_name = 'dbix_class_schema_versions';
31 my $old_table_name = 'SchemaVersions';
33 my $ddl_dir = dir ('t', 'var');
34 mkdir ($ddl_dir) unless -d $ddl_dir;
37 v1 => $ddl_dir->file ('DBICVersion-Schema-1.0-MySQL.sql'),
38 v2 => $ddl_dir->file ('DBICVersion-Schema-2.0-MySQL.sql'),
39 v3 => $ddl_dir->file ('DBICVersion-Schema-3.0-MySQL.sql'),
40 trans_v12 => $ddl_dir->file ('DBICVersion-Schema-1.0-2.0-MySQL.sql'),
41 trans_v23 => $ddl_dir->file ('DBICVersion-Schema-2.0-3.0-MySQL.sql'),
44 my $schema_v1 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
45 eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
46 eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
48 is($schema_v1->ddl_filename('MySQL', '1.0', $ddl_dir), $fn->{v1}, 'Filename creation working');
49 unlink( $fn->{v1} ) if ( -e $fn->{v1} );
50 $schema_v1->create_ddl_dir('MySQL', undef, $ddl_dir);
52 ok(-f $fn->{v1}, 'Created DDL file');
53 $schema_v1->deploy({ add_drop_table => 1 });
55 my $tvrs = $schema_v1->{vschema}->resultset('Table');
56 is($schema_v1->_source_exists($tvrs), 1, 'Created schema from DDL file');
58 # loading a new module defining a new version of the same table
59 DBICVersion::Schema->_unregister_source ('Table');
60 use_ok('DBICVersion_v2');
62 my $schema_v2 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
65 unlink($fn->{trans_v12});
67 is($schema_v2->get_db_version(), '1.0', 'get_db_version ok');
68 is($schema_v2->schema_version, '2.0', 'schema version ok');
69 $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0');
70 ok(-f $fn->{trans_v12}, 'Created DDL file');
73 sub { $schema_v2->upgrade() },
74 qr/DB version .+? is lower than the schema version/,
75 'Warn before upgrade',
78 is($schema_v2->get_db_version(), '2.0', 'db version number upgraded');
81 $schema_v2->storage->dbh->do('select NewVersionName from TestVersion');
82 }, 'new column created' );
85 sub { $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0') },
87 qr/Overwriting existing DDL file - $fn->{v2}/,
88 qr/Overwriting existing diff file - $fn->{trans_v12}/,
90 'An overwrite warning generated for both the DDL and the diff',
95 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
97 $schema_version->storage->dbh->do('select * from ' . $version_table_name);
98 }, 'version table exists');
101 $schema_version->storage->dbh->do("DROP TABLE IF EXISTS $old_table_name");
102 $schema_version->storage->dbh->do("RENAME TABLE $version_table_name TO $old_table_name");
103 }, 'versions table renamed to old style table');
105 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
106 is($schema_version->get_db_version, '2.0', 'transition from old table name to new okay');
109 $schema_version->storage->dbh->do('select * from ' . $old_table_name);
110 }, 'old version table gone');
114 # repeat the v1->v2 process for v2->v3 before testing v1->v3
115 DBICVersion::Schema->_unregister_source ('Table');
116 use_ok('DBICVersion_v3');
118 my $schema_v3 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
121 unlink($fn->{trans_v23});
123 is($schema_v3->get_db_version(), '2.0', 'get_db_version 2.0 ok');
124 is($schema_v3->schema_version, '3.0', 'schema version 3.0 ok');
125 $schema_v3->create_ddl_dir('MySQL', '3.0', $ddl_dir, '2.0');
126 ok(-f $fn->{trans_v23}, 'Created DDL 2.0 -> 3.0 file');
129 sub { $schema_v3->upgrade() },
130 qr/DB version .+? is lower than the schema version/,
131 'Warn before upgrade',
134 is($schema_v3->get_db_version(), '3.0', 'db version number upgraded');
137 $schema_v3->storage->dbh->do('select ExtraColumn from TestVersion');
138 }, 'new column created');
141 # now put the v1 schema back again
143 # drop all the tables...
144 eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
145 eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
146 eval { $schema_v1->storage->dbh->do('drop table TestVersion') };
149 local $DBICVersion::Schema::VERSION = '1.0';
152 is($schema_v1->get_db_version(), '1.0', 'get_db_version 1.0 ok');
155 # attempt v1 -> v3 upgrade
157 local $SIG{__WARN__} = sub { warn if $_[0] !~ /Attempting upgrade\.$/ };
158 $schema_v3->upgrade();
159 is($schema_v3->get_db_version(), '3.0', 'db version number upgraded');
162 # check behaviour of DBIC_NO_VERSION_CHECK env var and ignore_version connect attr
164 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
166 $schema_version->storage->dbh->do("DELETE from $version_table_name");
170 warnings_like ( sub {
171 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
172 }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr' );
174 warnings_like ( sub {
175 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
176 }, [], 'warning not detected with attr set');
179 local $ENV{DBIC_NO_VERSION_CHECK} = 1;
180 warnings_like ( sub {
181 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
182 }, [], 'warning not detected with env var set');
184 warnings_like ( sub {
185 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 0 });
186 }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
189 # attempt a deploy/upgrade cycle within one second
191 eval { $schema_v2->storage->dbh->do('drop table ' . $version_table_name) };
192 eval { $schema_v2->storage->dbh->do('drop table ' . $old_table_name) };
193 eval { $schema_v2->storage->dbh->do('drop table TestVersion') };
195 # this attempts to sleep until the turn of the second
197 sleep (int ($t) + 1 - $t);
198 note ('Fast deploy/upgrade start: ', time() );
201 local $DBICVersion::Schema::VERSION = '2.0';
205 local $SIG{__WARN__} = sub { warn if $_[0] !~ /Attempting upgrade\.$/ };
206 $schema_v2->upgrade();
208 is($schema_v2->get_db_version(), '3.0', 'Fast deploy/upgrade');
211 unless ($ENV{DBICTEST_KEEP_VERSIONING_DDL}) {
212 unlink $_ for (values %$fn);