1

I'd like to drop and setup my rails db while in runtime using ActiveRecord. What's the best way to do that? I'm assuming I need to drop the existing connection, and reconnect?

Does anyone have experience in doing this, and can you share what steps are involved to doing it properly?

Nathan
  • 7,627
  • 11
  • 46
  • 80
  • Isn't this going to just crash the app? – David Aldridge Nov 06 '16 at 22:32
  • @DavidAldridge It's an internal training app, so should be ok. Possible to do it without restarting rails? – Nathan Nov 07 '16 at 05:37
  • maybe you could get away with truncating all of the tables ... *maybe* with dropping and recreating them, but dropping the db sounds tricky. I think you'd need the app restarting, even if you were creating a new database to connect to instead of just dropping the current one. Might help if we knew what the aim of this is. – David Aldridge Nov 07 '16 at 11:27
  • @DavidAldridge Problem with truncating tables is that foreign key constraints exist on some of them. I suppose I can research the order of those and truncate them first, but if new constraints are added later this could break my implementation. The aim of this is to reset data in the app for training. I figured I could just drop, prepare, seed. Make sense? – Nathan Nov 07 '16 at 15:44

2 Answers2

0

You can very easily user these commands rake db:drop, rake db:create, rake db:migrate, but is this what you really want? For more info about this commands have a look here -> Difference between rake db:migrate db:reset and db:schema:load

Community
  • 1
  • 1
Vlad Balanescu
  • 664
  • 5
  • 27
  • I think you misunderstood the request. He wants to perform those actions *while his application is running*. – MarsAtomic Nov 06 '16 at 21:27
  • Assuming you have made changes to the views. Just do what @vlad is saying and reload the page no need to disconnect or reconnect anything – christoandrew Nov 06 '16 at 21:35
  • Well, first obstacle is that the db (postgres in this case) complains of an existing connection so it can't perform those actions. I can drop the db connection using AR. I can then perform those actions using `Rake::Task['db:drop'].invoke`, etc.. but then reconnecting to the DB does not seem to work properly. – Nathan Nov 07 '16 at 05:39
  • I personally don't think you can perform this kind of actions on a running database. It has to be in the stopped phae to enable these kind of actions – Vlad Balanescu Nov 07 '16 at 08:54
0

I didn't have much luck with doing the typical drop, create, migrate in run-time, so I used the following approach (suggested by @David Aldridge):

connection ||= ActiveRecord::Base.connection
connection.tables.each do |t|
    connection.execute("TRUNCATE \"#{t}\" RESTART IDENTITY CASCADE") unless t == 'schema_migrations'
end

I should first mention that I'm using postgres.

The problem I was running into previously was that I was getting blocked by index constraints in the db, but using the CASCADE argument solved that.

CASCADE allows for associated tables to also be truncated -- essentially bypassing constraints RESTART IDENTITY allows for resetting the table increment to zero.

Nathan
  • 7,627
  • 11
  • 46
  • 80