delete all rows in a table

1,217 views
Skip to first unread message

Justin Collum

unread,
Apr 1, 2011, 12:46:25 PM4/1/11
to FluentMigrator Google Group
This is my first stab at a real FM deploy to a dev database. I've got
the table create syntax working find (as far as I know). Next I've got
the "insert base data" migration -- version 2 of the database. So I've
got 3 rows that I need to insert into a table:

Insert.IntoTable("Types").Row(new { Description = "A" });
Insert.IntoTable("Types").Row(new { Description = "5" });
Insert.IntoTable("Types").Row(new { Description = "BV" });

Thats my Up.

Does the Down have to be this?

Delete.FromTable("Types").Row(new { Description = "A" });
Delete.FromTable("Types").Row(new { Description = "5" });
Delete.FromTable("Types").Row(new { Description = "BV" });

But really all I want to do is:

Delete.FromTable("Types").All();

Or, ideally,

Delete.FromTable("Types").Where(x=>x);



Question is: is there an easier way to delete all rows from a table?

Jeff Treuting

unread,
Apr 1, 2011, 12:55:50 PM4/1/11
to fluentmigrato...@googlegroups.com
To answer your question, yes you can do this to delete all rows:

      Delete.FromTable("asdasd").AllRows();

Also as an FYI you can string multiple rows into a single insert and delete statement as well.  So you can do the following:

      Insert.IntoTable("Types")
                .Row(new { Description = "A" })
                .Row(new { Description = "5" })
                .Row(new { Description = "BV" });

And if you didn't want to delete everything it would be:

      Delete.FromTable("Types")
                .Row(new { Description = "A" })
                .Row(new { Description = "5" })
                .Row(new { Description = "BV" });

One more unsolicited tip, try out the AutoReversingMigration (inherit from this instead of Migration).  You can then just specify the Up and the Down is done automatically.  It works great for Create statements and your insert statement, as it will then just run corresponding Delete for the Down.  It does not work with everything though, like Delete.Table() can't be auto reversed.  But it's great for a lot of things.

Jeff


--
You received this message because you are subscribed to the Google Groups "FluentMigrator Google Group" group.
To post to this group, send email to fluentmigrato...@googlegroups.com.
To unsubscribe from this group, send email to fluentmigrator-goog...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fluentmigrator-google-group?hl=en.




--
Jeff Treuting
Truburn Solutions, LLC
(206) 393-0717
www.truburn.net
je...@truburn.net



Justin Collum

unread,
Apr 1, 2011, 1:24:11 PM4/1/11
to FluentMigrator Google Group
Excellent, thanks for the tip. Didn't know about the auto reversing,
that's cool.
> *Jeff Treuting*
> Truburn Solutions, LLC
> (206) 393-0717www.truburn.net
> j...@truburn.net
Reply all
Reply to author
Forward
0 new messages