# Migrations in plugin

**URL:** <https://www.ruby-forum.com/t/migrations-in-plugin/136061>\
**Category:** Rails\
**Created:** [April 28, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/migrations-in-plugin/136061 "2008-04-28T15:45:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [April 28, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/migrations-in-plugin/136061/1 "2008-04-28T15:45:07Z")

</div>

Hi!  
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:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [April 28, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/migrations-in-plugin/136061/2 "2008-04-28T17:23:08Z")

</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://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [April 29, 2008, 7:46am UTC](https://www.ruby-forum.com/t/migrations-in-plugin/136061/3 "2008-04-29T07:46:31Z")

</div>

On Apr 28, 8:22 pm, AndyV [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [April 29, 2008, 9:02am UTC](https://www.ruby-forum.com/t/migrations-in-plugin/136061/4 "2008-04-29T09:02:14Z")

</div>

On Apr 29, 11:14 am, Andrew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> relevant code in restful authentication can be found at line 179 of  
> 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:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [April 29, 2008, 6:57pm UTC](https://www.ruby-forum.com/t/migrations-in-plugin/136061/5 "2008-04-29T18:57:28Z")

</div>

engines does some ‘engines migrations’

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [April 29, 2008, 8:14am UTC](https://www.ruby-forum.com/t/migrations-in-plugin/136061/6 "2008-04-29T08:14:23Z")

</div>

MohsinHijazee wrote:

> On Apr 28, 8:22 pm, AndyV [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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”:

```
  unless options[:skip_migration]
    m.migration_template 'migration.rb', 'db/migrate', :assigns => {
      :migration_name => "Create#{class_name.pluralize.gsub(/::/, 

```

‘’)}"  
}, :migration\_file\_name =\> “create\_#{file\_path.gsub(///,  
‘\_’).pluralize}”  
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 🙂
