# Execute additional commands after creating database?

**URL:** <https://www.ruby-forum.com/t/execute-additional-commands-after-creating-database/111429>\
**Category:** Rails\
**Created:** [August 7, 2007, 8:28am UTC](https://www.ruby-forum.com/t/execute-additional-commands-after-creating-database/111429 "2007-08-07T08:28:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/57b2e6/32.png) [@martinos](https://www.ruby-forum.com/u/martinos)\
**Post date:** [August 7, 2007, 8:28am UTC](https://www.ruby-forum.com/t/execute-additional-commands-after-creating-database/111429/1 "2007-08-07T08:28:06Z")

</div>

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:in`execute’  
/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:in`send’  
/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:in`say\_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:in`say\_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:in`instance\_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:in`load’  
/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:in`new\_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:in`call’  
/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:in`each’  
/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:in`invoke’  
/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:in`invoke’  
/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:in`execute’  
/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:in`execute’  
/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:in`synchronize’  
/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:in`call’  
/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:in`each’  
/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:in`invoke’  
/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:in`invoke’  
/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:in`each’  
/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:in`each’  
/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:in`invoke’  
/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:in`invoke’  
/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:in`each’  
/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:in`standard\_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:in`run’  
/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:in`run’  
/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

---

<div class="post-metadata">

**Author:** ![martinos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/57b2e6/32.png) [@martinos](https://www.ruby-forum.com/u/martinos)\
**Post date:** [August 7, 2007, 9:32pm UTC](https://www.ruby-forum.com/t/execute-additional-commands-after-creating-database/111429/2 "2007-08-07T21:32:54Z")

</div>

On 8/7/07, Martin M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

---

<div class="post-metadata">

**Author:** ![martinos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/57b2e6/32.png) [@martinos](https://www.ruby-forum.com/u/martinos)\
**Post date:** [August 8, 2007, 9:07am UTC](https://www.ruby-forum.com/t/execute-additional-commands-after-creating-database/111429/3 "2007-08-08T09:07:58Z")

</div>

On Aug 7, 9:32 pm, “David C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 8/7/07, Martin M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![martinos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/57b2e6/32.png) [@martinos](https://www.ruby-forum.com/u/martinos)\
**Post date:** [August 8, 2007, 9:35am UTC](https://www.ruby-forum.com/t/execute-additional-commands-after-creating-database/111429/4 "2007-08-08T09:35:53Z")

</div>

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

> **[De domeinnaam Nedforce.nl is te koop.](http://www.nedforce.nl)**
>
> Deze mooie domeinnaam kan al over een paar minuten van jou zijn. Betalen gaat eenvoudig en vertrouwd via iDEAL. Direct na de betaling ontvang je de verhuiscode per e-mail en kun je aan de slag.

---

<div class="post-metadata">

**Author:** ![martinos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/57b2e6/32.png) [@martinos](https://www.ruby-forum.com/u/martinos)\
**Post date:** [August 9, 2007, 9:05am UTC](https://www.ruby-forum.com/t/execute-additional-commands-after-creating-database/111429/5 "2007-08-09T09:05:17Z")

</div>

On Aug 8, 9:35 am, Roderick van Domburg \<rails-mailing-l…@andreas-  
[s.net](http://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”)
