Execute additional commands after creating database?

I’m writing some code that accesses some lagacy database on
PostgreSQL. Since the old database had some really ughly and weird
naming conventions (like CamelCase). To work around this I created a
second schema (named rails) in the same database and gave the tables
and fields some rails-friendly names. The database user that Rails
uses has its search_path set to the rails schema. That way Rails won’t
try to find models for the legacy tables. This worked fine so far. I
even got triggers working to be able to write into the real tables.

Now I installed RSpec and tried running some tests. This failed
because the test database gets recreated on every test run. Since I
specified config.active_record.table_name_prefix to be “rails.” and a
fresh database doesn’t contain that schema yet it fails miserably. I
tried executing a “create schema rails” in the first migration but
somehow Globalize bit me in my ass because it tries to create it’s own
tables first although there is no such migration in db/migrations.
What can I do?

To illustrate, this is what it looks like.

% rake spec --trace
(in /Users/fastjack/Sites/rpps)
** Invoke spec (first_time)
** Invoke db:test:prepare (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:test:prepare
** Invoke db:test:clone (first_time)
** Invoke db:schema:dump (first_time)
** Invoke environment
** Execute db:schema:dump
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
** Execute db:test:clone
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
rake aborted!
PGError: ERROR: schema “rails” does not exist
: CREATE TABLE rails.globalize_countries (“id” serial primary key,
“code” character varying(2) DEFAULT NULL, “english_name” character
varying(255) DEFAULT NULL, “date_format” character varying(255)
DEFAULT NULL, “currency_format” character varying(255) DEFAULT NULL,
“currency_code” character varying(3) DEFAULT NULL, “thousands_sep”
character varying(2) DEFAULT NULL, “decimal_sep” character varying(2)
DEFAULT NULL, “currency_decimal_sep” character varying(2) DEFAULT
NULL, “number_grouping_scheme” character varying(255) DEFAULT NULL)
/Users/fastjack/Sites/rpps/config/…/vendor/rails/activerecord/lib/
active_record/connection_adapters/abstract_adapter.rb:128:in log' /Users/fastjack/Sites/rpps/config/../vendor/rails/activerecord/lib/ active_record/connection_adapters/postgresql_adapter.rb:152:inexecute’
/Users/fastjack/Sites/rpps/config/…/vendor/rails/activerecord/lib/
active_record/connection_adapters/abstract/schema_statements.rb:104:in
create_table' /Users/fastjack/Sites/rpps/config/../vendor/rails/activerecord/lib/ active_record/migration.rb:275:insend’
/Users/fastjack/Sites/rpps/config/…/vendor/rails/activerecord/lib/
active_record/migration.rb:275:in method_missing' /Users/fastjack/Sites/rpps/config/../vendor/rails/activerecord/lib/ active_record/migration.rb:259:insay_with_time’
/usr/local/lib/ruby/1.8/benchmark.rb:293:in measure' /Users/fastjack/Sites/rpps/config/../vendor/rails/activerecord/lib/ active_record/migration.rb:259:insay_with_time’
/Users/fastjack/Sites/rpps/config/…/vendor/rails/activerecord/lib/
active_record/migration.rb:273:in method_missing' ./db/schema.rb:7 /Users/fastjack/Sites/rpps/config/../vendor/rails/activerecord/lib/ active_record/schema.rb:43:ininstance_eval’
/Users/fastjack/Sites/rpps/config/…/vendor/rails/activerecord/lib/
active_record/schema.rb:43:in define' ./db/schema.rb:5 /Users/fastjack/Sites/rpps/config/../vendor/rails/activerecord/ lib/../../activesupport/lib/active_support/dependencies.rb:488:inload’
/Users/fastjack/Sites/rpps/config/…/vendor/rails/activerecord/
lib/…/…/activesupport/lib/active_support/dependencies.rb:488:in
load' /Users/fastjack/Sites/rpps/config/../vendor/rails/activerecord/ lib/../../activesupport/lib/active_support/dependencies.rb:342:innew_constants_in’
/Users/fastjack/Sites/rpps/config/…/vendor/rails/activerecord/
lib/…/…/activesupport/lib/active_support/dependencies.rb:488:in
load' /Users/fastjack/Sites/rpps/config/../vendor/rails/railties/lib/tasks/ databases.rake:31 /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:incall’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in
execute' /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:ineach’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in
execute' /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:362:ininvoke’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in
synchronize' /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:ininvoke’
/Users/fastjack/Sites/rpps/config/…/vendor/rails/railties/lib/tasks/
databases.rake:76
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in call' /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:inexecute’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in each' /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:inexecute’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:362:in
invoke' /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:insynchronize’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in
invoke' /Users/fastjack/Sites/rpps/config/../vendor/rails/railties/lib/tasks/ databases.rake:153 /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:incall’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in
execute' /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:ineach’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in
execute' /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:362:ininvoke’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in
synchronize' /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:ininvoke’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:369:in
invoke_prerequisites' /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1003:ineach’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1003:in
send' /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1003:ineach’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:368:in
invoke_prerequisites' /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:361:ininvoke’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in
synchronize' /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:ininvoke’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in
top_level' /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:ineach’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in
top_level' /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1761:instandard_exception_handling’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1733:in
top_level' /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1711:inrun’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1761:in
standard_exception_handling' /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1708:inrun’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/bin/rake:7
/usr/local/bin/rake:16:in `load’
/usr/local/bin/rake:16

On 8/7/07, Martin M. [email protected] wrote:

Now I installed RSpec and tried running some tests.
Have you successfully run any tests using rails baked-in testing?

On Aug 7, 9:32 pm, “David C.” [email protected] wrote:

On 8/7/07, Martin M. [email protected] wrote:
[…]

Now I installed RSpec and tried running some tests.

Have you successfully run any tests using rails baked-in testing?

I never used them before. I tried running them now and got the same
error.

Martin M. wrote:

Now I installed RSpec and tried running some tests. This failed
because the test database gets recreated on every test run. Since I
specified config.active_record.table_name_prefix to be “rails.” and a
fresh database doesn’t contain that schema yet it fails miserably. I
tried executing a “create schema rails” in the first migration but
somehow Globalize bit me in my ass because it tries to create it’s own
tables first although there is no such migration in db/migrations.
What can I do?

I agree that it can be pretty irritating. One thing you can do is create
your database and schema with a different owner and then grant all
privileges on the schema to the user that Rails is set to. That’ll cause
createdb and dropdb to fail yet still allow you to create and drop
tables.


Roderick van Domburg

On Aug 8, 9:35 am, Roderick van Domburg <rails-mailing-l…@andreas-
s.net> wrote:

I agree that it can be pretty irritating. One thing you can do is create
your database and schema with a different owner and then grant all
privileges on the schema to the user that Rails is set to. That’ll cause
createdb and dropdb to fail yet still allow you to create and drop
tables.

Well, it started promising but then got ughly. I’m not quite sure what
exactly went wrong but I suspect it’s globalize’s fault.

Here’s the log:
** Invoke spec (first_time)
** Invoke db:test:prepare (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:test:prepare
** Invoke db:test:clone (first_time)
** Invoke db:schema:dump (first_time)
** Invoke environment
** Execute db:schema:dump
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
dropdb: database removal failed: ERROR: must be owner of database
rpps_test
createdb: database creation failed: ERROR: permission denied to
create database
** Execute db:test:clone
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
NOTICE: CREATE TABLE will create implicit sequence
“globalize_countries_id_seq” for serial column
“globalize_countries.id”
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
“globalize_countries_pkey” for table “globalize_countries”
rake aborted!
PGError: ERROR: duplicate key violates unique constraint
“pg_attribute_relid_attnam_index”
: CREATE INDEX “index_globalize_countries_on_code” ON
rpps.globalize_countries (“code”, “code”)