package Vee::Schema;

use strict;
use warnings;
use base 'DBIx::Class::Schema';

__PACKAGE__->load_classes;

=head1 METHODS

=head2 add_simple_column_types

Iterates over every column in every table in the schema.  Primary key columns
named C<id> are given a C<data_type> of C<integer>; all other columns are set
C<is_nullable>.

This method is really a hack for deploying the schema to SQLite and testing with
dummy data; autoincrement will only work on single-column integral primary keys,
and defaults for every column would be a pain to mark just for tests.

=cut

sub add_simple_column_types {
    my ($self) = @_;

    for my $table_name ($self->sources) {
        my $table = $self->source($table_name);

        for my $column_name ($table->columns) {
            my $column_info = $table->column_info($column_name);

            # sessions table uses a string, as dictated by Session plugin
            if ($table_name ne 'Sessions' and
                $column_name eq 'id' and
                join("\0", $table->primary_columns) eq 'id')
            {
                $column_info->{data_type} = 'integer';
            } else {
                $column_info->{is_nullable} = 1;
            }
        }
    }
}

1;
