<commit_before>package Tapper::Schema::TestTools;

# inspired by Test::Fixture::DBIC::Schema

use strict;
use warnings;

BEGIN {
        use Class::C3;
        use MRO::Compat;
        $DBD::SQLite::sqlite_version; # fix "used only once" warning
}

use Tapper::Schema::TestrunDB;
use Tapper::Schema::ReportsDB;

use Tapper::Config;

my $testrundb_schema;
my $reportsdb_schema;

sub setup_testrundb {

        # explicitely prefix into {test} subhash of the config file,
        # to avoid painful mistakes with deploy

        my $dsn = Tapper::Config->subconfig->{test}{database}{TestrunDB}{dsn};

        my ($tmpfname) = $dsn =~ m,dbi:SQLite:dbname=([\w./]+),i;
        unlink $tmpfname;

        $testrundb_schema = Tapper::Schema::TestrunDB->connect($dsn,
                                                                Tapper::Config->subconfig->{test}{database}{TestrunDB}{username},
                                                                Tapper::Config->subconfig->{test}{database}{TestrunDB}{password},
                                                                { ignore_version => 1 }
                                                               );
        $testrundb_schema->deploy;
        $testrundb_schema->upgrade;
}

sub setup_reportsdb {

        # explicitely prefix into {test} subhash of the config file,
        # to avoid painful mistakes with deploy

        my $dsn = Tapper::Config->subconfig->{test}{database}{ReportsDB}{dsn};

        my ($tmpfname) = $dsn =~ m,dbi:SQLite:dbname=([\w./]+),i;
        unlink $tmpfname;

        $reportsdb_schema = Tapper::Schema::ReportsDB->connect($dsn,
                                                                Tapper::Config->subconfig->{test}{database}{ReportsDB}{username},
                                                                Tapper::Config->subconfig->{test}{database}{ReportsDB}{password},
                                                                { ignore_version => 1 }
                                                               );
        $reportsdb_schema->deploy;
        $reportsdb_schema->upgrade;
}


sub import {
        my $pkg = caller(0);
        no strict 'refs';       ## no critic.
        *{"$pkg\::testrundb_schema"}  = sub () { $testrundb_schema };
        *{"$pkg\::reportsdb_schema"}  = sub () { $reportsdb_schema };
}

setup_testrundb;
setup_reportsdb;

1;<commit_msg>[RFK] avoid 'Upgrade not necessary' warnings<commit_after>package Tapper::Schema::TestTools;

# inspired by Test::Fixture::DBIC::Schema

use strict;
use warnings;

BEGIN {
        use Class::C3;
        use MRO::Compat;
        $DBD::SQLite::sqlite_version; # fix "used only once" warning
}

use Tapper::Schema::TestrunDB;
use Tapper::Schema::ReportsDB;

use Tapper::Config;

my $testrundb_schema;
my $reportsdb_schema;

sub setup_db
{
        # explicitely prefix into {test} subhash of the config file,
        # to avoid painful mistakes with deploy

        my ($cfg) = @_;
        my $dsn = $cfg->{dsn};

        my ($tmpfname) = $dsn =~ m,dbi:SQLite:dbname=([\w./]+),i;
        unlink $tmpfname;

        my $schema = Tapper::Schema::TestrunDB->connect($dsn,
                                                                $cfg->{username},
                                                                $cfg->{password},
                                                                { ignore_version => 1 }
                                                               );
        $schema->deploy;
        if ($schema->schema_version > $schema->get_db_version) {
                $schema->upgrade;
        }
        return $schema;
}

sub setup_testrundb {
        $testrundb_schema = setup_db(Tapper::Config->subconfig->{test}{database}{TestrunDB});
}

sub setup_reportsdb {
        $reportsdb_schema = setup_db(Tapper::Config->subconfig->{test}{database}{ReportsDB});
}


sub import {
        my $pkg = caller(0);
        no strict 'refs';       ## no critic.
        *{"$pkg\::testrundb_schema"}  = sub () { $testrundb_schema };
        *{"$pkg\::reportsdb_schema"}  = sub () { $reportsdb_schema };
}

setup_testrundb;
setup_reportsdb;

1;
{'fraction_non_alphanumeric': 0.1295761078998073, 'fraction_numerical': 0.002167630057803468, 'mean_word_length': 1.730440499671269, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4768745', 'n_tokens_mistral': 1198, 'n_tokens_neox': 1146, 'n_words': 220}