Rails - Allows to specify schema cache path in database configuration file

Rails has a very useful feature called schema cache.

When the Rails application is booted, it executes SHOW FULL FIELDS query to get information about the tables and the datatype of all the columns. Imagine, we have a large Rails application with many models. Rebooting the servers will fire the above heavy query SHOW FULL FIELDS for each model. This expensive operation may also kill our database.

The motive behind schema cache is to serialize database details like tables, columns, types into a file. Load this file on the application servers. Read the data from this file to avoid hitting the database.

This avoids load on our database and speeds up the boot time of servers.

Schema cache is enabled by default for all Rails apps.

# config/environments/production.rb
config.use_schema_cache_dump = true

The above configuration is not useful unless we generate the dump file on our app server. Below is the command to generate the schema cache.

bundle exec rake db:schema:cache:dump

This will generate a file schema_cache.dump in db folder by default. The other option is to use an ENV variable - SCHEMA_CACHE, to set the path. The above command needs to be executed with every deployment of the Rails app.

Instead of directly querying the database, this file will be read, which will be faster than querying the database.

Rails, recently added a feature to specify the schema cache path in database.yml.

For example:

  adapter: postgresql
  database: example_development
  pool: 5
  schema_cache_path: tmp/schema/dump.yml

This is particularly helpful when we have multiple DB’s in our Rails app. We can set our own custom schema cache path for each database. Or during every release to production, we specify the path to which the schema file should be dumped.

Rails also added the flexibility to let people choose between Marshal and YAML as a serialization strategy. This can be done by specifying the extension .dump for Marshal and .yml for YAML.

The default serialization strategy is YAML.

For a large Rails application, the schema cache size will be huge. This can be a problem when the application is deployed to Kubernetes because of ConfigMap limit which is 1024*1024 bytes.

Rails added gzip support for both the YAML and the Marshal serialization strategies.

For making the gzip file we just need to update the filename with .gz extension.

  adapter: postgresql
  database: example_development
  pool: 5
  schema_cache_path: tmp/schema/dump.yml.gz

NOTE: If the schema_cache_path is not specified, the filename will be read from ENV or a default will be derived.

Need help on your Ruby on Rails or React project?

Join Our Newsletter