# Migrations in plugin

**URL:** <https://discuss.rubyonrails.org/t/migrations-in-plugin/28663>\
**Category:** rubyonrails-talk\
**Created:** [April 28, 2008, 1:44pm UTC](https://discuss.rubyonrails.org/t/migrations-in-plugin/28663 "2008-04-28T13:44:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://discuss.rubyonrails.org/u/MohsinHijazee)\
**Post date:** [April 28, 2008, 1:44pm UTC](https://discuss.rubyonrails.org/t/migrations-in-plugin/28663/1 "2008-04-28T13:44:21Z")

</div>

Hi! &nbsp;&nbsp;&nbsp;I have to move a complete model and its migration into a plugin. I know how can I do that for a model but what about the migration???

Regards, Mohsin

---

<div class="post-metadata">

**Author:** ![AndyV](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AndyV](https://discuss.rubyonrails.org/u/AndyV)\
**Post date:** [April 28, 2008, 3:22pm UTC](https://discuss.rubyonrails.org/t/migrations-in-plugin/28663/2 "2008-04-28T15:22:02Z")

</div>

restful\_authentication does something similar using a rake task and an erb template. If you're not already using it, you might want to install it just to see how they've solved the problem.

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://discuss.rubyonrails.org/u/MohsinHijazee)\
**Post date:** [April 29, 2008, 5:45am UTC](https://discuss.rubyonrails.org/t/migrations-in-plugin/28663/3 "2008-04-29T05:45:06Z")

</div>

> restful\_authentication does something similar using a rake task and an erb template. If you're not already using it, you might want to install it just to see how they've solved the problem.

I've looked into it and its not yet clear to me. Actually, I only need to create a single table when my plugin is installed. No more. Should I do it manually in install.rb and uninstall.rb?

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [April 29, 2008, 6:14am UTC](https://discuss.rubyonrails.org/t/migrations-in-plugin/28663/4 "2008-04-29T06:14:23Z")

</div>

MohsinHijazee wrote:

> > restful\_authentication does something similar using a rake task and an erb template. If you're not already using it, you might want to install it just to see how they've solved the problem.
> 
> I've looked into it and its not yet clear to me. Actually, I only need to create a single table when my plugin is installed. No more. Should I do it manually in install.rb and uninstall.rb?

It seems the Rails generator library has a migration method. The relevant code in restful authentication can be found at line 179 of "authenticated\_generator.rb":

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unless options[:skip\_migration] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.migration\_template 'migration.rb', 'db/migrate', :assigns =\> { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:migration\_name =\> "Create#{class\_name.pluralize.gsub(/::/, '')}" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, :migration\_file\_name =\> "create\_#{file\_path.gsub(/\//, '\_').pluralize}" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

There is obviously a lot here you won't need. You will also find a file called migration.rb in the templates directory, which get interpreted and copied across.

Mind you - I have never built a template for a migration before, so don't follow me 🙂

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://discuss.rubyonrails.org/u/MohsinHijazee)\
**Post date:** [April 29, 2008, 7:01am UTC](https://discuss.rubyonrails.org/t/migrations-in-plugin/28663/5 "2008-04-29T07:01:21Z")

</div>

> MohsinHijazee wrote: \>\> restful\_authentication does something similar using a rake task and an \>\> erb template. If you're not already using it, you might want to \>\> install it just to see how they've solved the problem. \> I've looked into it and its not yet clear to me. Actually, I only need \> to create a single table when my plugin is installed. No more. Should \> I do it manually in install.rb and uninstall.rb?
> 
> It seems the Rails generator library has a migration method. The relevant code in restful authentication can be found at line 179 of "authenticated\_generator.rb":
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unless options[:skip\_migration] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.migration\_template 'migration.rb', 'db/migrate', :assigns =\> { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:migration\_name =\> "Create#{class\_name.pluralize.gsub(/::/, '')}" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, :migration\_file\_name =\> "create\_#{file\_path.gsub(/\//, '\_').pluralize}" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> There is obviously a lot here you won't need. You will also find a file called migration.rb in the templates directory, which get interpreted and copied across.
> 
> Mind you - I have never built a template for a migration before, so don't follow me 🙂

So in a nutshell, there is no smart way of packing a migration with an application. Like if its a zip code plugin, the it would require to create a table called zipcodes and would need to populate it. What if we do it in install.rb and uninstall.rb? How do we do that?

---

<div class="post-metadata">

**Author:** ![Roger\_Pack](https://avatars.discourse-cdn.com/v4/letter/r/50afbb/32.png) [@Roger\_Pack](https://discuss.rubyonrails.org/u/Roger_Pack)\
**Post date:** [April 29, 2008, 4:55pm UTC](https://discuss.rubyonrails.org/t/migrations-in-plugin/28663/6 "2008-04-29T16:55:50Z")

</div>

engines does some 'engines migrations'
