Ruby on Rails Sunday, February 11, 2018

> On Feb 11, 2018, at 8:55 AM, Robert Phillips <robert.phillips37@gmail.com> wrote:
>
>
>
> On Saturday, 23 December 2017 16:41:57 UTC, Hassan Schroeder wrote:
> On Sat, Dec 23, 2017 at 6:02 AM, Robert Phillips
> <robert.p...@gmail.com> wrote:
>
> As Walter already pointed out, you shouldn't care about the content
> of the id field, but ...
>
> > I get this error
> >
> > C:\rubyarud\hartl\ch2\toy_app>rails db:drop
> > Permission denied @ unlink_internal - C:/rubyarud/h
> > Couldn't drop database 'db/development.sqlite3'
> > rails aborted!
> > Errno::EACCES: Permission denied @ unlink_internal
>
> I would be concerned about not being able to drop and recreate a
> database -- sometimes when you're experimenting you *will* get to
> a place where you want a clean slate.
>
>
> Regarding if I wanted to drop a table.
>
> I think one thing I can do is use DB Browser for SQLite, and delete the table e.g. a table called blahabcs through that.
>
> Then I could do rails destroy model blahabc
>
> Then could do rails db:migrate
>
> Would that be equivalent or the same as dropping the table?

Ideally, you would do all of this with migrations. You can run the migration that created the table down and back up again (look at the Rails Guide for Migrations for the exact syntax, but it's something like rake db:migrate:down VERSION=123456, where 123456 is the numerical part of the migration's filename). Then you can simply run rake db:migrate, and since that migration is down, it will be brought back up. The table, regardless which database engine is hosting it, will be dropped entirely and re-created.

That's an over-simplification of what you would do, because you may have made changes to the table after its initial creation in other migrations. So what you would need to do is run all migrations related to this table down in reverse order (same directions as above), going from newest (highest number) to the oldest (the creation of the table). Then run rake db:migrate and you'll be back to an empty table.

Note that if you had foreign keys to this table in other tables, they will all be wrong (orphan) at this point.

If you are doing this in SQLite, then I also imagine you are working in development, so there really is no point to this surgical removal and re-creation of one table. Just delete the entire database (rm db/development.sqlite3), and run rake db:migrate. You'll be back to a new and empty database.

Walter

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/F9B9A695-CA2B-4ACE-953D-CF2B8FC31610%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment