terrorista
terrorista

Reputation: 227

Exporting database from development to production

I'm using the gem yaml_db to export my Postgres development database to production, which is on a virtual machine where my Ruby on Rails app is.

I'm using rake db:data:dump RAILS_ENV=development to create data.yml and rake db:data:load RAILS_ENV=productionto import the data, but I'm getting erros in importing.

rake aborted!
ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: ERROR:  current transaction is aborted, commands ignored until end of transaction block
: DELETE FROM "improvement_actions"

PG::FeatureNotSupported: ERROR:  cannot truncate a table referenced in a foreign key constraint
DETAIL:  Table "comments" references "improvement_actions".

Does anyone know how to solve this? Or is there another method to export my database?

Upvotes: 1

Views: 1373

Answers (1)

Dave Schweisguth
Dave Schweisguth

Reputation: 37617

The error is because there your database has foreign keys, which yaml_db doesn't support.

I think the easiest solution is to export your development database using pg_dump with the --disable-triggers flag and import it to your production database using pg_restore.

However, if you really want to use yaml_db, a rather complicated solution has been documented. The heart of that solution is to

  • make all of your foreign keys deferrable initially immediate if they aren't already. The documented solution uses the schema_plus gem, or you could do it in Postgres.
  • monkey-patch yaml_db's SerializationHelper::Base#load to defer all constraints during each loading transaction with set constraints all deferred;
  • monkey-patch yaml_db's SerializationHelper::Base#truncate_table to truncate tables by attempting to truncate as usual (which is not allowed on a table that has delayed triggers) and deleting if the truncation fails.

Upvotes: 1

Related Questions