# Migration detection of database type (Oracle and SQLite)

**URL:** <https://www.ruby-forum.com/t/migration-detection-of-database-type-oracle-and-sqlite/88178>\
**Category:** Rails\
**Created:** [March 8, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/migration-detection-of-database-type-oracle-and-sqlite/88178 "2007-03-08T18:59:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmcclean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/91b2a8/32.png) [@dmcclean](https://www.ruby-forum.com/u/dmcclean)\
**Post date:** [March 8, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/migration-detection-of-database-type-oracle-and-sqlite/88178/1 "2007-03-08T18:59:40Z")

</div>

We are writing a migration which will be used to create the initial  
version of the database. We  
want to use the same migration to populate both an Oracle and MySQL  
database.

We would like to detect the database type so that we can add foreign  
keys to Oracle, but  
don’t want to do it for the SQLite database. Is there a method we can  
call from ActiveRecord  
which gives us the database type we are connected to?

Thanks in advance,  
Don McClean

---

<div class="post-metadata">

**Author:** ![dmcclean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/91b2a8/32.png) [@dmcclean](https://www.ruby-forum.com/u/dmcclean)\
**Post date:** [March 8, 2007, 7:18pm UTC](https://www.ruby-forum.com/t/migration-detection-of-database-type-oracle-and-sqlite/88178/2 "2007-03-08T19:18:46Z")

</div>

> We are writing a migration which will be used to create the initial  
> version of the database. We  
> want to use the same migration to populate both an Oracle and MySQL  
> database.
> 
> We would like to detect the database type so that we can add foreign  
> keys to Oracle, but  
> don’t want to do it for the SQLite database. Is there a method we can  
> call from ActiveRecord  
> which gives us the database type we are connected to?

Not sure what method, but if you look at the ‘connection’ method of the  
adapter you should find something interesting in there…

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001071)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

---

<div class="post-metadata">

**Author:** ![dmcclean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/91b2a8/32.png) [@dmcclean](https://www.ruby-forum.com/u/dmcclean)\
**Post date:** [March 8, 2007, 8:51pm UTC](https://www.ruby-forum.com/t/migration-detection-of-database-type-oracle-and-sqlite/88178/3 "2007-03-08T20:51:57Z")

</div>

> We would like to detect the database type so that we can add foreign  
> keys to Oracle, but don’t want to do it for the SQLite database. Is there a method we can  
> call from ActiveRecord which gives us the database type we are connected to?

For something similar i’ve used a case like this

case ActiveRecord::Base.connection.adapter\_name.downcase  
when /mysql[.]\*/  
…  
hope it helps!

javier ramirez

* * *

Estamos de estreno… si necesitas llevar el control de tus gastos  
visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!
