use Bugzilla;
use strict;

do 'globals.pl';

open WORDS, '/usr/share/dict/words'
    or die "Can't open /usr/share/dict/words: $!\n";
chomp(my @words = <WORDS>);
close WORDS;

my $dbh = Bugzilla->dbh;

$dbh->do(qq{
    CREATE TABLE realcol (bug_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
                          cf_foo TINYTEXT)
});

$dbh->do(qq{
    CREATE TABLE distcol (bug_id INTEGER NOT NULL PRIMARY KEY,
                          field_id INTEGER NOT NULL,
                          value TINYTEXT,
                          INDEX (field_id))
});

my $insert1 = $dbh->prepare(qq{
    INSERT INTO realcol VALUES (NULL, ?)
});

my $insert2 = $dbh->prepare(qq{
    INSERT INTO distcol VALUES (?, ?, ?)
});

my $lastins = $dbh->prepare(qq{
    SELECT LAST_INSERT_ID()
});

my $fieldid = 0;

my %metafield;

for (1 .. 300_000) {
    my $string = join " ", map $words[ rand $#words ], 1 .. 10;
    if (rand(1000) < 1) {
        substr($string, rand(length($string)), 0) = 'meta';
    }
    ++$metafield{$fieldid} if $string =~ /meta/i;
    $insert1->execute($fieldid == 0 ? $string : undef);
    $lastins->execute();
    my $bugid = $lastins->fetchrow_array();
    $insert2->execute($bugid, $fieldid, $string);
    $fieldid = ($fieldid + 1) % 100;
}

for my $id (sort { $a <=> $b } keys %metafield) {
    print "Field $id: $metafield{$id} values have 'meta' in them\n";
}

