Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make rake db:schema:dump have the charset and collation of the fields in schema.rb?

One of our fields needs to be case sensitive. We can write a migration to change the collation which works fine but this change is not reflected in schema.rb. It will create issues for example when running tests and the cloned test database will not have the collation we want for that field.

We use mysql.

I have searched for a way to make this happen with no results..

I managed to find this on github but not sure how this was accomplished https://github.com/cantino/huginn/blob/db792cdd82eb782e98d934995964809d9e8cb77d/db/schema.rb

like image 584
Yoni Baciu Avatar asked Nov 25 '15 15:11

Yoni Baciu


People also ask

What does rake db setup do?

The rake db:setup task will create the database, load the schema and initialize it with the seed data.

Which command is used to rollback migration in Rails?

You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version.

How can I check my rails migration status?

To check for status, run rails db:migrate:status . Then you'll have a good view of the migrations you want to remove. Then, run rails db:rollback to revert the changes one by one.


2 Answers

As mentioned in @house9's comment, you can use structure.sql instead. Add this to your project's application.rb:

config.active_record.schema_format = :sql

Then run bundle exec rake db:structure:dump to generate the actual SQL structure. This retains charsets and collations (which should ideally be there in schema.rb, but alas).

"structure" is by nature less portable than "schema", but it's usually a good thing for all team members and environments to be using the same database and version anyway.

like image 35
mahemoff Avatar answered Sep 28 '22 00:09

mahemoff


I think there is no "official" way (provided by Rails or ActiveRecord gems) to accomplish that kind of dump. Following the git history, on the Huginn repo itself, you can find the code you need to achieve this dump. Take a look to this commit: https://github.com/cantino/huginn/commit/db792cdd82eb782e98d934995964809d9e8cb77d

The most relevant code is currently here: https://github.com/cantino/huginn/blob/master/lib/ar_mysql_column_charset/main.rb

So if you need this feature, you'll probably need to copy/paste this extension into your project.

UPDATE

I made a deeper review of Huginn repo (git history and issues), and as you can read in this comment, this functionality was extracted into a gem: https://github.com/kamipo/activerecord-mysql-awesome.

like image 181
markets Avatar answered Sep 27 '22 23:09

markets