From 0207e712b8c6196a68e8611e89ef0d6c63ee3376 Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Sun, 25 Mar 2012 11:33:02 -0600 Subject: [PATCH] Add sanity_checks.t. --- t/pt-online-schema-change/check_tables.t | 126 ---------------------- t/pt-online-schema-change/sanity_checks.t | 92 ++++++++++++++++ 2 files changed, 92 insertions(+), 126 deletions(-) delete mode 100644 t/pt-online-schema-change/check_tables.t create mode 100644 t/pt-online-schema-change/sanity_checks.t diff --git a/t/pt-online-schema-change/check_tables.t b/t/pt-online-schema-change/check_tables.t deleted file mode 100644 index 650870c1..00000000 --- a/t/pt-online-schema-change/check_tables.t +++ /dev/null @@ -1,126 +0,0 @@ -#!/usr/bin/env perl - -BEGIN { - die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n" - unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH}; - unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib"; -}; - -use strict; -use warnings FATAL => 'all'; -use English qw(-no_match_vars); -use Test::More; - -use PerconaTest; -use Sandbox; -require "$trunk/bin/pt-online-schema-change"; - -my $dp = new DSNParser(opts=>$dsn_opts); -my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp); -my $dbh = $sb->get_dbh_for('master'); - -if ( !$dbh ) { - plan skip_all => 'Cannot connect to sandbox master'; -} -else { - plan tests => 8; -} - -my $vp = new VersionParser(); -my $q = new Quoter(); -my $tp = new TableParser(Quoter => $q); -my $chunker = new TableChunker(Quoter => $q, TableParser => $tp); -my $o = new OptionParser(); - -$o->get_specs("$trunk/bin/pt-online-schema-change"); -$o->get_opts(); -pt_online_schema_change::__set_quiet(1); - -$sb->load_file('master', "t/pt-online-schema-change/samples/small_table.sql"); -$dbh->do('use mkosc'); - -my $old_tbl_struct = $tp->parse($tp->get_create_table($dbh, 'mkosc', 'a')); - -my %args = ( - dbh => $dbh, - db => 'mkosc', - tbl => 'a', - tmp_tbl => '__tmp_a', - old_tbl => '__old_a', # what tbl becomes after swapped with tmp_tbl - VersionParser => $vp, - Quoter => $q, - TableParser => $tp, - OptionParser => $o, - TableChunker => $chunker, -); - -my %tbl_info = pt_online_schema_change::check_tables(%args); -is( - $tbl_info{chunk_column}, - "i", - "check_tables() returns chunk_column" -); - -is( - $tbl_info{chunk_index}, - "PRIMARY", - "check_tables() returns chunk_index" -); - -ok( - exists $tbl_info{tbl_struct}, - "check_tables() returns tbl_struct" -); - -throws_ok( - sub { pt_online_schema_change::check_tables( - %args, - tbl => 'does_not_exist' - ) }, - qr/Table mkosc.does_not_exist does not exist/, - "Table must exist" -); - -@ARGV = qw(--rename-tables); -$o->get_opts(); -throws_ok( - sub { pt_online_schema_change::check_tables( - %args, - old_tbl => 'a', - ) }, - qr/Table mkosc.a exists which will prevent mkosc.a/, - "Old table cannot already exist if --rename-tables" -); - -throws_ok( - sub { pt_online_schema_change::check_tables( - %args, - tmp_tbl => 'a', - ) }, - qr/Temporary table mkosc.a exists/, - "Temporary table cannot already exist" -); - -$dbh->do('CREATE TRIGGER foo AFTER DELETE ON mkosc.a FOR EACH ROW DELETE FROM mkosc.a WHERE 0'); -throws_ok( - sub { pt_online_schema_change::check_tables(%args) }, - qr/Table mkosc.a has triggers/, - "Old table cannot have triggers" -); -$dbh->do('DROP TRIGGER mkosc.foo'); - -$dbh->do('ALTER TABLE mkosc.a DROP COLUMN i'); -my $tmp_struct = $tp->parse($tp->get_create_table($dbh, 'mkosc', 'a')); -throws_ok( - sub { pt_online_schema_change::check_tables( - %args, - ) }, - qr/Table mkosc.a cannot be chunked/, - "Table must have a chunkable index" -); - -# ############################################################################# -# Done. -# ############################################################################# -$sb->wipe_clean($dbh); -exit; diff --git a/t/pt-online-schema-change/sanity_checks.t b/t/pt-online-schema-change/sanity_checks.t new file mode 100644 index 00000000..2c395cc3 --- /dev/null +++ b/t/pt-online-schema-change/sanity_checks.t @@ -0,0 +1,92 @@ +#!/usr/bin/env perl + +BEGIN { + die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n" + unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH}; + unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib"; +}; + +use strict; +use warnings FATAL => 'all'; +use English qw(-no_match_vars); +use Test::More; + +use PerconaTest; +use Sandbox; +require "$trunk/bin/pt-online-schema-change"; + +use Data::Dumper; +$Data::Dumper::Indent = 1; +$Data::Dumper::Sortkeys = 1; +$Data::Dumper::Quotekeys = 0; + +my $dp = new DSNParser(opts=>$dsn_opts); +my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp); +my $master_dbh = $sb->get_dbh_for('master'); +my $slave_dbh = $sb->get_dbh_for('slave1'); + +if ( !$master_dbh ) { + plan skip_all => 'Cannot connect to sandbox master'; +} +else { + plan tests => 4; +} + +my $q = new Quoter(); +my $tp = new TableParser(Quoter => $q); +my @args = qw(--lock-wait-timeout 3); +my $output = ""; +my $dsn = "h=127.1,P=12345,u=msandbox,p=msandbox"; +my $exit = 0; +my $sample = "t/pt-online-schema-change/samples"; +my $rows; + +# ############################################################################# +# Checks for the original table (check_orig_table()). +# ############################################################################# + +# Of course, the orig database and table must exist. +throws_ok( + sub { pt_online_schema_change::main(@args, + "$dsn,t=nonexistent_db.t", qw(--dry-run)) }, + qr/`nonexistent_db`.`t` does not exist/, + "Original database must exist" +); + +throws_ok( + sub { pt_online_schema_change::main(@args, + "$dsn,t=mysql.nonexistent_tbl", qw(--dry-run)) }, + qr/`mysql`.`nonexistent_tbl` does not exist/, + "Original table must exist" +); + +$sb->load_file('master', "$sample/basic_no_fks.sql"); +PerconaTest::wait_for_table($slave_dbh, "pt_osc.t", "id=20"); +$master_dbh->do("USE pt_osc"); +$slave_dbh->do("USE pt_osc"); + +# The orig table cannot have any triggers. +$master_dbh->do("CREATE TRIGGER pt_osc.pt_osc_test AFTER DELETE ON pt_osc.t FOR EACH ROW DELETE FROM pt_osc.t WHERE 0"); +throws_ok( + sub { pt_online_schema_change::main(@args, + "$dsn,t=pt_osc.t", qw(--dry-run)) }, + qr/`pt_osc`.`t` has triggers/, + "Original table cannot have triggers" +); +$master_dbh->do('DROP TRIGGER pt_osc.pt_osc_test'); + +# The orig table must have a pk or unique index so the delete trigger is safe. +$master_dbh->do("ALTER TABLE pt_osc.t DROP COLUMN id"); +$master_dbh->do("ALTER TABLE pt_osc.t DROP INDEX c"); +throws_ok( + sub { pt_online_schema_change::main(@args, + "$dsn,t=pt_osc.t", qw(--dry-run)) }, + qr/`pt_osc`.`t` does not have a PRIMARY KEY or a unique index/, + "Original table must have a PK or unique index" +); + +# ############################################################################# +# Done. +# ############################################################################# +$sb->wipe_clean($master_dbh); +exit;