--source include/have_innodb.inc

CREATE TABLE t1 ( id BIGINT NOT NULL AUTO_INCREMENT, content LONGBLOB, PRIMARY KEY (id)) ENGINE=InnoDB ROW_FORMAT=DYNAMIC;

INSERT INTO t1 (content) VALUES (REPEAT('A', 200000));
COMMIT;

--let $_server_id= `SELECT @@server_id`
--let $_expect_file= $MYSQLTEST_VARDIR/tmp/mysqld.$_server_id.expect

--source include/shutdown_mysqld.inc

--perl
use File::Find;

my $vardir = $ENV{MYSQLTEST_VARDIR} or die "MYSQLTEST_VARDIR not set in environment";
my $file;
find(sub {
    return unless $_ eq 't1.ibd';
    return if $File::Find::name =~ m{/std_data/};
    return unless $File::Find::name =~ m{/data/test/t1\.ibd$};
    $file = $File::Find::name;
}, $vardir);
die "t1.ibd not found under $vardir (excluding std_data)" unless $file;

open(my $fh, '+<:raw', $file) or die "Cannot open $file: $!";
local $/;
my $data = <$fh>;
my $page_size = 16384;  # default innodb_page_size

# 1. Find the overflow page holding our BLOB content by searching for
#    a long run of 'A' bytes (REPEAT('A', 200000)), then identify which
#    page number it falls on.
my $marker = 'A' x 4096;
my $content_pos = index($data, $marker);
die "BLOB content marker not found in $file" if $content_pos < 0;
my $blob_page_no_in_file = int($content_pos / $page_size);
my $blob_page_start = $blob_page_no_in_file * $page_size;

# 2. Read that page's own header to get its real space_id/page_no.
#    FIL_PAGE_OFFSET=4, FIL_PAGE_SPACE_ID=34 are stable InnoDB file-page
#    header offsets.
my $real_page_no = unpack("N", substr($data, $blob_page_start + 4, 4));
my $real_space_id = unpack("N", substr($data, $blob_page_start + 34, 4));

# 3. Search the whole file for the 20-byte external reference pointing
#    at (real_space_id, real_page_no), rather than assuming a fixed
#    clustered-index page number.
my $pattern = pack("N", $real_space_id) . pack("N", $real_page_no)
            . pack("N", 0x26) . pack("N", 0) . pack("N", 200000);
my $pos = index($data, $pattern);
die "External BLOB reference pattern not found in $file "
    . "(space_id=$real_space_id, page_no=$real_page_no)" if $pos < 0;

substr($data, $pos, 20) = "\x00" x 20;
seek($fh, 0, 0);
print $fh $data;
close($fh);

# 4. Resolve innochecksum relative to the running server's own bindir,
#    rather than a hardcoded path from one specific tree.
my $innochecksum;
for my $candidate (
    ($ENV{MTR_BINDIR} ? "$ENV{MTR_BINDIR}/bin/innochecksum" : ()),
    "$vardir/../bin/innochecksum",
    "$vardir/../../bin/innochecksum",
) {
    if (-x $candidate) { $innochecksum = $candidate; last; }
}
die "innochecksum not found relative to this build" unless $innochecksum;

system($innochecksum, "--no-check", "--write", $file) == 0
    or die "innochecksum --write failed on $file (exit $?)";
EOF

--source include/start_mysqld.inc

SET SESSION tx_isolation='REPEATABLE-READ';

--exec echo "wait" > $_expect_file

--echo # Expect server to abort here with:
--echo # InnoDB: Assertion failure in file row0sel.cc line 3034
--echo # Failing assertion: prebuilt->trx->isolation_level == TRX_ISO_READ_UNCOMMITTED
--error 2013,2006
SELECT id, LENGTH(content) FROM t1 WHERE id=1;

--source include/wait_until_disconnected.inc

--let SEARCH_FILE = $MYSQLTEST_VARDIR/log/mysqld.$_server_id.err
--let SEARCH_PATTERN = Failing assertion: prebuilt->trx->isolation_level == TRX_ISO_READ_UNCOMMITTED
--source include/search_pattern_in_file.inc

--exec echo "restart" > $_expect_file
--enable_reconnect
--source include/wait_until_connected_again.inc
