From: Matt S Trout Date: Fri, 27 Jan 2006 01:57:13 +0000 (+0000) Subject: We can now generate our own test schema X-Git-Tag: v0.05005~98 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0009fa4905ab7f4585c4b470bfe049666bce8e17;p=dbsrgits%2FDBIx-Class.git We can now generate our own test schema --- diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP index 261402e..2b46817 100644 --- a/MANIFEST.SKIP +++ b/MANIFEST.SKIP @@ -32,3 +32,6 @@ # Don't ship the last dist we built :) \.tar\.gz$ + +# Skip maint stuff +^maint/ diff --git a/lib/SQL/Translator/Parser/DBIx/Class.pm b/lib/SQL/Translator/Parser/DBIx/Class.pm index 4ec18ef..6225724 100644 --- a/lib/SQL/Translator/Parser/DBIx/Class.pm +++ b/lib/SQL/Translator/Parser/DBIx/Class.pm @@ -40,11 +40,12 @@ sub parse { # print Dumper($dbixschema->registered_classes); - foreach my $tableclass ($dbixschema->registered_classes) + #foreach my $tableclass ($dbixschema->registered_classes) + foreach my $moniker ($dbixschema->sources) { - eval "use $tableclass"; - print("Can't load $tableclass"), next if($@); - my $source = $tableclass->result_source_instance; + #eval "use $tableclass"; + #print("Can't load $tableclass"), next if($@); + my $source = $dbixschema->source($moniker); my $table = $schema->add_table( name => $source->name, @@ -57,13 +58,15 @@ sub parse { # data_type is a number, column_type is text? my %colinfo = ( name => $col, - default_value => '', size => 0, is_auto_increment => 0, is_foreign_key => 0, is_nullable => 0, %{$source->column_info($col)} ); + if ($colinfo{is_nullable}) { + $colinfo{default} = '' unless exists $colinfo{default}; + } my $f = $table->add_field(%colinfo) || die $table->error; } $table->primary_key($source->primary_columns); diff --git a/maint/gen-schema.pl b/maint/gen-schema.pl new file mode 100755 index 0000000..b16bd15 --- /dev/null +++ b/maint/gen-schema.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use lib qw(lib t/lib); + +use UNIVERSAL::require; + +my $from = 'SQL::Translator::Parser::DBIx::Class'; +my $to = 'SQL::Translator::Producer::SQLite'; +my $sqlt = 'SQL::Translator'; +my $schema = 'DBICTest::Schema'; + +$from->require; +$to->require; +$sqlt->require; +$schema->require; + +my $tr = $sqlt->new; + +$from->can("parse")->($tr, $schema); +print $to->can("produce")->($tr); diff --git a/t/lib/DBICTest/Schema/Artist.pm b/t/lib/DBICTest/Schema/Artist.pm index 47f22e8..2200cd4 100644 --- a/t/lib/DBICTest/Schema/Artist.pm +++ b/t/lib/DBICTest/Schema/Artist.pm @@ -3,7 +3,16 @@ package DBICTest::Schema::Artist; use base 'DBIx::Class::Core'; DBICTest::Schema::Artist->table('artist'); -DBICTest::Schema::Artist->add_columns('artistid', {}, 'name'); +DBICTest::Schema::Artist->add_columns( + 'artistid' => { + data_type => 'integer', + is_auto_increment => 1 + }, + 'name' => { + data_type => 'varchar', + is_nullable => 1, + }, +); DBICTest::Schema::Artist->set_primary_key('artistid'); __PACKAGE__->mk_classdata('field_name_for', { diff --git a/t/lib/DBICTest/Schema/ArtistUndirectedMap.pm b/t/lib/DBICTest/Schema/ArtistUndirectedMap.pm index 3c0a379..8e58312 100644 --- a/t/lib/DBICTest/Schema/ArtistUndirectedMap.pm +++ b/t/lib/DBICTest/Schema/ArtistUndirectedMap.pm @@ -3,7 +3,10 @@ package DBICTest::Schema::ArtistUndirectedMap; use base 'DBIx::Class::Core'; __PACKAGE__->table('artist_undirected_map'); -__PACKAGE__->add_columns(qw/id1 id2/); +__PACKAGE__->add_columns( + 'id1' => { data_type => 'integer' }, + 'id2' => { data_type => 'integer' }, +); __PACKAGE__->set_primary_key(qw/id1 id2/); 1; diff --git a/t/lib/DBICTest/Schema/CD.pm b/t/lib/DBICTest/Schema/CD.pm index 35b8acd..8e04c16 100644 --- a/t/lib/DBICTest/Schema/CD.pm +++ b/t/lib/DBICTest/Schema/CD.pm @@ -3,7 +3,21 @@ package DBICTest::Schema::CD; use base 'DBIx::Class::Core'; DBICTest::Schema::CD->table('cd'); -DBICTest::Schema::CD->add_columns(qw/cdid artist title year/); +DBICTest::Schema::CD->add_columns( + 'cdid' => { + data_type => 'integer', + is_auto_increment => 1, + }, + 'artist' => { + data_type => 'integer', + }, + 'title' => { + data_type => 'varchar', + }, + 'year' => { + data_type => 'varchar', + }, +); DBICTest::Schema::CD->set_primary_key('cdid'); DBICTest::Schema::CD->add_unique_constraint(artist_title => [ qw/artist title/ ]); diff --git a/t/lib/DBICTest/Schema/CD_to_Producer.pm b/t/lib/DBICTest/Schema/CD_to_Producer.pm index 4c111c4..762e806 100644 --- a/t/lib/DBICTest/Schema/CD_to_Producer.pm +++ b/t/lib/DBICTest/Schema/CD_to_Producer.pm @@ -3,7 +3,10 @@ package DBICTest::Schema::CD_to_Producer; use base 'DBIx::Class::Core'; __PACKAGE__->table('cd_to_producer'); -__PACKAGE__->add_columns(qw/cd producer/); +__PACKAGE__->add_columns( + cd => { data_type => 'integer' }, + producer => { data_type => 'integer' }, +); __PACKAGE__->set_primary_key(qw/cd producer/); 1; diff --git a/t/lib/DBICTest/Schema/FourKeys.pm b/t/lib/DBICTest/Schema/FourKeys.pm index be18952..f9112fa 100644 --- a/t/lib/DBICTest/Schema/FourKeys.pm +++ b/t/lib/DBICTest/Schema/FourKeys.pm @@ -3,7 +3,12 @@ package DBICTest::Schema::FourKeys; use base 'DBIx::Class::Core'; DBICTest::Schema::FourKeys->table('fourkeys'); -DBICTest::Schema::FourKeys->add_columns(qw/foo bar hello goodbye/); +DBICTest::Schema::FourKeys->add_columns( + 'foo' => { data_type => 'integer' }, + 'bar' => { data_type => 'integer' }, + 'hello' => { data_type => 'integer' }, + 'goodbye' => { data_type => 'integer' }, +); DBICTest::Schema::FourKeys->set_primary_key(qw/foo bar hello goodbye/); 1; diff --git a/t/lib/DBICTest/Schema/LinerNotes.pm b/t/lib/DBICTest/Schema/LinerNotes.pm index 442a5c1..1f35b4b 100644 --- a/t/lib/DBICTest/Schema/LinerNotes.pm +++ b/t/lib/DBICTest/Schema/LinerNotes.pm @@ -3,7 +3,14 @@ package DBICTest::Schema::LinerNotes; use base qw/DBIx::Class::Core/; DBICTest::Schema::LinerNotes->table('liner_notes'); -DBICTest::Schema::LinerNotes->add_columns(qw/liner_id notes/); +DBICTest::Schema::LinerNotes->add_columns( + 'liner_id' => { + data_type => 'integer', + }, + 'notes' => { + data_type => 'varchar', + }, +); DBICTest::Schema::LinerNotes->set_primary_key('liner_id'); 1; diff --git a/t/lib/DBICTest/Schema/OneKey.pm b/t/lib/DBICTest/Schema/OneKey.pm index 72d8de3..081c94b 100644 --- a/t/lib/DBICTest/Schema/OneKey.pm +++ b/t/lib/DBICTest/Schema/OneKey.pm @@ -3,7 +3,18 @@ package DBICTest::Schema::OneKey; use base 'DBIx::Class::Core'; DBICTest::Schema::OneKey->table('onekey'); -DBICTest::Schema::OneKey->add_columns(qw/id artist cd/); +DBICTest::Schema::OneKey->add_columns( + 'id' => { + data_type => 'integer', + is_auto_increment => 1, + }, + 'artist' => { + data_type => 'integer', + }, + 'cd' => { + data_type => 'integer', + }, +); DBICTest::Schema::OneKey->set_primary_key('id'); diff --git a/t/lib/DBICTest/Schema/Producer.pm b/t/lib/DBICTest/Schema/Producer.pm index c8dbf70..01fa843 100644 --- a/t/lib/DBICTest/Schema/Producer.pm +++ b/t/lib/DBICTest/Schema/Producer.pm @@ -3,7 +3,15 @@ package DBICTest::Schema::Producer; use base 'DBIx::Class::Core'; __PACKAGE__->table('producer'); -__PACKAGE__->add_columns(qw/producerid name/); +__PACKAGE__->add_columns( + 'producerid' => { + data_type => 'integer', + is_auto_increment => 1 + }, + 'name' => { + data_type => 'varchar', + }, +); __PACKAGE__->set_primary_key('producerid'); 1; diff --git a/t/lib/DBICTest/Schema/SelfRef.pm b/t/lib/DBICTest/Schema/SelfRef.pm index 8c065c2..48c8290 100644 --- a/t/lib/DBICTest/Schema/SelfRef.pm +++ b/t/lib/DBICTest/Schema/SelfRef.pm @@ -3,7 +3,15 @@ package DBICTest::Schema::SelfRef; use base 'DBIx::Class::Core'; __PACKAGE__->table('self_ref'); -__PACKAGE__->add_columns(qw/id name/); +__PACKAGE__->add_columns( + 'id' => { + data_type => 'integer', + is_auto_increment => 1, + }, + 'name' => { + data_type => 'varchar', + }, +); __PACKAGE__->set_primary_key('id'); 1; diff --git a/t/lib/DBICTest/Schema/SelfRefAlias.pm b/t/lib/DBICTest/Schema/SelfRefAlias.pm index 736e418..d4ef35c 100644 --- a/t/lib/DBICTest/Schema/SelfRefAlias.pm +++ b/t/lib/DBICTest/Schema/SelfRefAlias.pm @@ -3,7 +3,14 @@ package DBICTest::Schema::SelfRefAlias; use base 'DBIx::Class::Core'; __PACKAGE__->table('self_ref_alias'); -__PACKAGE__->add_columns(qw/self_ref alias/); +__PACKAGE__->add_columns( + 'self_ref' => { + data_type => 'integer', + }, + 'alias' => { + data_type => 'integer', + }, +); __PACKAGE__->set_primary_key('self_ref alias'); 1; diff --git a/t/lib/DBICTest/Schema/Tag.pm b/t/lib/DBICTest/Schema/Tag.pm index a356d8f..0a303d6 100644 --- a/t/lib/DBICTest/Schema/Tag.pm +++ b/t/lib/DBICTest/Schema/Tag.pm @@ -3,7 +3,18 @@ package DBICTest::Schema::Tag; use base qw/DBIx::Class::Core/; DBICTest::Schema::Tag->table('tags'); -DBICTest::Schema::Tag->add_columns(qw/tagid cd tag/); +DBICTest::Schema::Tag->add_columns( + 'tagid' => { + data_type => 'varchar', + is_auto_increment => 1, + }, + 'cd' => { + data_type => 'integer', + }, + 'tag' => { + data_type => 'varchar' + }, +); DBICTest::Schema::Tag->set_primary_key('tagid'); 1; diff --git a/t/lib/DBICTest/Schema/Track.pm b/t/lib/DBICTest/Schema/Track.pm index 2e6fa7f..be0f273 100644 --- a/t/lib/DBICTest/Schema/Track.pm +++ b/t/lib/DBICTest/Schema/Track.pm @@ -3,7 +3,21 @@ package DBICTest::Schema::Track; use base 'DBIx::Class::Core'; DBICTest::Schema::Track->table('track'); -DBICTest::Schema::Track->add_columns(qw/trackid cd position title/); +DBICTest::Schema::Track->add_columns( + 'trackid' => { + data_type => 'integer', + is_auto_increment => 1, + }, + 'cd' => { + data_type => 'integer', + }, + 'position' => { + data_type => 'integer', + }, + 'title' => { + data_type => 'varchar', + }, +); DBICTest::Schema::Track->set_primary_key('trackid'); 1; diff --git a/t/lib/DBICTest/Schema/TwoKeys.pm b/t/lib/DBICTest/Schema/TwoKeys.pm index ea6fa92..e4bb1b0 100755 --- a/t/lib/DBICTest/Schema/TwoKeys.pm +++ b/t/lib/DBICTest/Schema/TwoKeys.pm @@ -3,7 +3,10 @@ package DBICTest::Schema::TwoKeys; use base 'DBIx::Class::Core'; DBICTest::Schema::TwoKeys->table('twokeys'); -DBICTest::Schema::TwoKeys->add_columns(qw/artist cd/); +DBICTest::Schema::TwoKeys->add_columns( + 'artist' => { data_type => 'integer' }, + 'cd' => { data_type => 'integer' }, +); DBICTest::Schema::TwoKeys->set_primary_key(qw/artist cd/); 1; diff --git a/t/lib/DBICTest/Setup.pm b/t/lib/DBICTest/Setup.pm index 94de234..de113db 100755 --- a/t/lib/DBICTest/Setup.pm +++ b/t/lib/DBICTest/Setup.pm @@ -15,40 +15,6 @@ my $schema = DBICTest::Schema->compose_connection('DBICTest' => $dsn); my $dbh = DBI->connect($dsn); my $sql = <.$sql; } + +close IN; + $dbh->do($_) for split(/\n\n/, $sql); $schema->storage->dbh->do("PRAGMA synchronous = OFF"); 1; + +__DATA__ + +CREATE TABLE artist (artistid INTEGER NOT NULL PRIMARY KEY, name VARCHAR); + +CREATE TABLE cd (cdid INTEGER NOT NULL PRIMARY KEY, artist INTEGER NOT NULL, + title VARCHAR, year VARCHAR); + +CREATE TABLE liner_notes (liner_id INTEGER NOT NULL PRIMARY KEY, notes VARCHAR); + +CREATE TABLE track (trackid INTEGER NOT NULL PRIMARY KEY, cd INTEGER NOT NULL, + position INTEGER NOT NULL, title VARCHAR); + +CREATE TABLE tags (tagid INTEGER NOT NULL PRIMARY KEY, cd INTEGER NOT NULL, + tag VARCHAR); + +CREATE TABLE twokeys (artist INTEGER NOT NULL, cd INTEGER NOT NULL, + PRIMARY KEY (artist, cd) ); + +CREATE TABLE fourkeys (foo INTEGER NOT NULL, bar INTEGER NOT NULL, + hello INTEGER NOT NULL, goodbye INTEGER NOT NULL, + PRIMARY KEY (foo, bar, hello, goodbye) ); + +CREATE TABLE onekey (id INTEGER NOT NULL PRIMARY KEY, + artist INTEGER NOT NULL, cd INTEGER NOT NULL ); + +CREATE TABLE self_ref (id INTEGER NOT NULL PRIMARY KEY, + name VARCHAR ); + +CREATE TABLE self_ref_alias (self_ref INTEGER NOT NULL, alias INTEGER NOT NULL, + PRIMARY KEY( self_ref, alias ) ); + +CREATE TABLE artist_undirected_map (id1 INTEGER NOT NULL, id2 INTEGER NOT NULL, PRIMARY KEY(id1, id2)); + +CREATE TABLE producer (producerid INTEGER NOT NULL PRIMARY KEY, name VARCHAR); + +CREATE TABLE cd_to_producer (cd INTEGER NOT NULL, producer INTEGER NOT NULL); + diff --git a/t/lib/sqlite.sql b/t/lib/sqlite.sql new file mode 100644 index 0000000..8bc7146 --- /dev/null +++ b/t/lib/sqlite.sql @@ -0,0 +1,124 @@ +-- +-- Created by SQL::Translator::Producer::SQLite +-- Created on Fri Jan 27 01:16:24 2006 +-- +BEGIN TRANSACTION; + +-- +-- Table: twokeys +-- +CREATE TABLE twokeys ( + artist integer NOT NULL, + cd integer NOT NULL, + PRIMARY KEY (artist, cd) +); + +-- +-- Table: liner_notes +-- +CREATE TABLE liner_notes ( + liner_id INTEGER PRIMARY KEY NOT NULL, + notes varchar NOT NULL +); + +-- +-- Table: cd_to_producer +-- +CREATE TABLE cd_to_producer ( + cd integer NOT NULL, + producer integer NOT NULL, + PRIMARY KEY (cd, producer) +); + +-- +-- Table: artist +-- +CREATE TABLE artist ( + artistid INTEGER PRIMARY KEY NOT NULL, + name varchar +); + +-- +-- Table: self_ref_alias +-- +CREATE TABLE self_ref_alias ( + self_ref integer NOT NULL, + alias integer NOT NULL +); + +-- +-- Table: fourkeys +-- +CREATE TABLE fourkeys ( + foo integer NOT NULL, + bar integer NOT NULL, + hello integer NOT NULL, + goodbye integer NOT NULL, + PRIMARY KEY (foo, bar, hello, goodbye) +); + +-- +-- Table: cd +-- +CREATE TABLE cd ( + cdid INTEGER PRIMARY KEY NOT NULL, + artist integer NOT NULL, + title varchar NOT NULL, + year varchar NOT NULL +); + +-- +-- Table: artist_undirected_map +-- +CREATE TABLE artist_undirected_map ( + id1 integer NOT NULL, + id2 integer NOT NULL, + PRIMARY KEY (id1, id2) +); + +-- +-- Table: onekey +-- +CREATE TABLE onekey ( + id INTEGER PRIMARY KEY NOT NULL, + artist integer NOT NULL, + cd integer NOT NULL +); + +-- +-- Table: track +-- +CREATE TABLE track ( + trackid INTEGER PRIMARY KEY NOT NULL, + cd integer NOT NULL, + position integer NOT NULL, + title varchar NOT NULL +); + +-- +-- Table: producer +-- +CREATE TABLE producer ( + producerid INTEGER PRIMARY KEY NOT NULL, + name varchar NOT NULL +); + +-- +-- Table: self_ref +-- +CREATE TABLE self_ref ( + id INTEGER PRIMARY KEY NOT NULL, + name varchar NOT NULL +); + +-- +-- Table: tags +-- +CREATE TABLE tags ( + tagid varchar NOT NULL, + cd integer NOT NULL, + tag varchar NOT NULL, + PRIMARY KEY (tagid) +); + +COMMIT; diff --git a/t/run/01core.tl b/t/run/01core.tl index ddae2c4..27868df 100644 --- a/t/run/01core.tl +++ b/t/run/01core.tl @@ -143,6 +143,8 @@ cmp_ok($or_rs->next->cdid, '==', $rel_rs->next->cdid, 'Related object ok'); ok($schema->storage(), 'Storage available'); +$schema->source("Artist")->{_columns}{'artistid'} = {}; + my $typeinfo = $schema->source("Artist")->column_info('artistid'); is($typeinfo->{data_type}, 'INTEGER', 'column_info ok'); } diff --git a/t/run/04db.tl b/t/run/04db.tl index 452c0b9..5adf439 100644 --- a/t/run/04db.tl +++ b/t/run/04db.tl @@ -34,7 +34,7 @@ my $test_type_info = { 'data_type' => 'INTEGER' }, 'name' => { - 'data_type' => 'VARCHAR' + 'data_type' => 'varchar' } }; is_deeply($type_info, $test_type_info, 'columns_info_for - column data types'); diff --git a/t/run/16joins.tl b/t/run/16joins.tl index 0f59ab7..1b3ff15 100644 --- a/t/run/16joins.tl +++ b/t/run/16joins.tl @@ -151,8 +151,8 @@ is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch # count the SELECTs DBI->trace(0, undef); -my $selects = 0; -my $trace = IO::File->new('t/var/dbic.trace', '<') +$selects = 0; +$trace = IO::File->new('t/var/dbic.trace', '<') or die "Unable to read trace file"; while (<$trace>) { $selects++ if /SELECT(?!.*WHERE 1=0.*)/;