Forum: Ruby on Rails test db missing constraints

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aad37b5f7116c8d1f547d23b37566032?d=identicon&s=25 Greg Donald (destiney)
on 2009-02-18 20:21
(Received via mailing list)
What is everyone doing with regards to database constraints not being
created in schema.rb ?  I'm using Oracle and I just noticed none of
the constraints I am creating in my migrations appear in my test
schema.  This makes me question the value of my test suite since I'm
not running it on the same db constraints that will be present in
production.

I tried setting

config.active_record.schema_format = :sql

as suggested in a DHH post I found Googling, but the SQL file doesn't
contain anymore than the schema.rb contains, it's just in a different
format is all.


Thanks,


--
Greg Donald
http://destiney.com/
0d56133042e4814c1b2255ea655f2361?d=identicon&s=25 Jeff Burlysystems (jburly)
on 2009-02-18 21:04
(Received via mailing list)
Hi Greg,

Here's a simple mysql-specific example for adding cascade delete
constraint, for authors----0..*books, that should give you an idea of
what to do in your oracle proj (using oracle-specific constraints):

# in config/environment.rb:
  ...
  config.active_record.schema_format = :sql
  ...

# in db/migrate/002_create_books.rb
class CreateBooks < ActiveRecord::Migration
  def self.up
    create_table :books do |t|
      t.integer :author_id, :null=>false
      ...
    end

    add_index :books, :author_id
    ...

    #NOTE: mysql-specific fk-constraints.
    execute "alter table books add constraint fk_book_author foreign
key (author_id) references authors(id) on delete cascade"

  end
  ...
end

Then, after migrating, that constraint should show up in db/
development_structure.sql, which your tests will then use when re-
creating test db:

...
CREATE TABLE `books` (
  ...
  CONSTRAINT `fk_book_author` FOREIGN KEY (`author_id`) REFERENCES
`authors` (`id`) ON DELETE CASCADE
  ...

Jeff
Aad37b5f7116c8d1f547d23b37566032?d=identicon&s=25 Greg Donald (destiney)
on 2009-02-18 22:24
(Received via mailing list)
On Wed, Feb 18, 2009 at 2:03 PM, Jeff Lewis <jeff.burly@gmail.com>
wrote:
> Then, after migrating, that constraint should show up in db/
> development_structure.sql, which your tests will then use when re-
> creating test db:
>
> ...
> CREATE TABLE `books` (
>  ...
>  CONSTRAINT `fk_book_author` FOREIGN KEY (`author_id`) REFERENCES
> `authors` (`id`) ON DELETE CASCADE

Right, that's what I'm saying.  This does not occur for Oracle.

The development_structure.sql I get is CONSTRAINT-less.



--
Greg Donald
http://destiney.com/
0d56133042e4814c1b2255ea655f2361?d=identicon&s=25 Jeff Burlysystems (jburly)
on 2009-02-19 07:01
(Received via mailing list)
I don't have access to oracle setup at this time, and it's hard to
tell what the problem might be without specific info about your
setup ....  but, since you mentioned schema.rb in your op, I'm
wondering if by chance you forgot to re-run your tests after mod'ing
environment.rb to use sql (which results the dumping of db/
development_structure.sql) instead of AR's schema dumper (which
results in dumping of db/schema.rb)?

Assuming you did mod db/environment.rb accordingly, and you saved your
oracle-specific constraints defined in the approp place(s) in your
migrations files (and successfully re-migrated and confirmed that
those constraints are in effect in your dev db?), try the following:

$ rm db/development_structure.sql

$ rake test:units
...

$ cat db/development_structure.sql
...

and see if those constraints don't show up in db/
development_structure.sql then.

If they don't, another longshot thought is .... are you running an old
version of rails, ie 1.x?  Because iirc I used to have to do some
extra fiddling to get such migrations-defined constraints to apply to
test db under rails 1.x.  No longer needed under rails 2.x tho.

If still having problems, maybe post your specific config/setup and
code?

Jeff
Aad37b5f7116c8d1f547d23b37566032?d=identicon&s=25 Greg Donald (destiney)
on 2009-02-19 16:43
(Received via mailing list)
On Thu, Feb 19, 2009 at 12:00 AM, Jeff Lewis <jeff.burly@gmail.com>
wrote:
> I'm wondering if by chance you forgot to re-run your tests after mod'ing
> environment.rb to use sql (which results the dumping of db/
> development_structure.sql) instead of AR's schema dumper (which
> results in dumping of db/schema.rb)?

No, I did not forget.  I did not even have a development_structure.sql
until I made the change in my environment.rb and re-ran my tests.

> $ cat db/development_structure.sql
> ...
>
> and see if those constraints don't show up in db/
> development_structure.sql then.

I removed db/development_structure.sql.

rm db/development_structure.sql

I checked my config/environment.rb file:

> cat config/environment.rb |grep schema_format
  config.active_record.schema_format = :sql

I rebuilt my database:

rake db:migrate VERSION=0; rake db:migrate

I reran my tests.

rake

I checked for constraints:

cat db/development_structure.sql |grep -i constraint

Nothing.


> If they don't, another longshot thought is .... are you running an old
> version of rails, ie 1.x?

No, I am running Rails 2.2.2.

cat config/environment.rb |grep 2.2.2
RAILS_GEM_VERSION = '2.2.2' unless defined? RAILS_GEM_VERSION


> If still having problems, maybe post your specific config/setup and
> code?

Sure.

cat db/migrate/002_create_users.rb |grep -i constraint
    execute "alter table users add constraint pk_users primary key
(user_id)"

ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]

gem list --local |grep oracle
activerecord-oracle_enhanced-adapter (1.1.9)

gem list --local|grep rails
rails (2.2.2)

What else would you like to see?



--
Greg Donald
http://destiney.com/
This topic is locked and can not be replied to.