On Thu, 2008-06-05 at 19:09 -0700, Phlip wrote:
You are trying “too hard” to do things the wrong way. The right way is to add
indexes with add_index, and then to enforce further constraints with Ruby code,
not necessarily with database code.
If you simply must do things like this, do not write on schema.rb (or integrate
it into your Version Controller). It is an automatic, write-only file. Same with
development.sql.
I do actually enforce things in controller code (i.e., you can’t
‘destroy’ a CaseManager that has clients but that method doesn’t kick in
with unit tests. That’s why I asked at the start of the thread if I am
doing things wrong here.
If there is a way to ‘protect’ me from deleting a CaseManager that has a
Client within the model, I’ve not figured a way to do that…(Class
CaseManager has_many :clients - Class Client belongs_to :case_manager)
Maybe I’m just too dumb but it seems to me that the table constraints
within postgres not only protect me from dumb actions in other software
applications but also protected me from failing to catch bad ‘destroy’
actions that had related records in rails.
execute ‘ALTER TABLE ONLY assessments ADD CONSTRAINT
fk_associations_personnel FOREIGN KEY (personnel_id) REFERENCES
personnels(id);’
NoMethodError: undefined method `execute’ for #Object:0xb809f9a8
.execute is not a method of the Ruby God Object. It’s a method of either
ActiveRecord::Base or ActiveRecord::Base.connection.
clearly
but they work inside of migrations…
That’s because the migrator inherits the right ActiveRecord:: stuff. But…
Testing is more important than putting this or that constraint into the
database. Do whatever it takes to provide your tests with a simple database
that they can beat on. Leave this fine-tuning for later, after you have enough
real data in your database that you can detect the real slow spots!
I have a bunch of data in my production db and my development db is my
production db from January and migrated up to current. I am going
backwards by trying to institute testing.
I have my controller tests virtually up to date and they work. I had
unit tests working when I was running migrations on my test db.
The unit tests broke when I did things like ‘rake test:functionals’ or
‘rake db:test:clone’ etc. which give all appearance to drop the test db
and recreate it from schema.rb (I’m surmising that this is what is
occurring). With that, I lost the postgres ‘CONSTRAINTS’ which I was
relying upon in some of my unit tests. If this is a bad concept to test
for…then I will remove them.
At the start of this thread I asked… and still haven’t a clue why:
-
testing like ‘rake test:functionals’ or ‘rake test:units’ acts as user
postgres when I don’t specify user postgres anywhere in database.yml at
all. This completely defies explanation…I even went through my plugins
and removed the postgres user from all plugin tests.
-
none of the commands like ‘rake db:test:clone’ or ‘rake
db:test:clone_structure’ actually clone the development db including
things like ‘CONSTRAINTS’ (and I presume TRIGGERS but I am not using
them at the moment).
-
using a postgres schema for testing seems pointless and counter
productive. I’ve given that up completely. Someone suggested sqllite for
test db which does make sense and I may end up going that route.
Thanks Phlip (and Frederick/Cayce/Rob)
Craig