Home > Articles

📄 Contents

  1. 2.1 The Entire App Inside a Component
  2. 2.2 ActiveRecord and Handling Migrations within Components
  3. 2.3 Handling Dependencies within Components
This chapter is from the book

2.2 ActiveRecord and Handling Migrations within Components

Let’s add some actual functionality to our currently bare-bones application. The first feature we are going to focus on is for the app to be able to predict the outcome of future games based on past performances. To this end, we will add teams and games as models to AppComponent. We will create an admin interface for both teams and games, which will give us enough data to try our hand at predicting some games.

Remember to execute these commands in the context of AppComponent, that is, under ./components/app_component.

Scaffolding Team and Game. Execute in ./components/app_component

$ rails g scaffold team name:string
 
$ rails g scaffold game date:datetime                         location:string                         first_team_id:integer
                        second_team_id:integer                         winning_team:integer                         first_team_score:integer                         second_team_score:integer

The next step is to run rake db:migrate to create the appropriate tables in the database. Interestingly, this will work when called within ./components/app_component, but not in ./. It does not fail for the main app. It just doesn’t do anything.

Scaffolding Team and Game. Execute in ./components/app_component

$ rake db:migrate
 
== 20171029235211 CreateAppComponentTeams: migrating ================
-- create_table(:app_component_teams)
   -> 0.0005s
== 20171029235211 CreateAppComponentTeams: migrated (0.0006s) =======
 
 
== 20171029235221 CreateAppComponentGames: migrating ================
-- create_table(:app_component_games)
   -> 0.0007s
== 20171029235221 CreateAppComponentGames: migrated (0.0007s) =======
 
 
$ cd ../..
$ rake db:migrate
Running via Spring preloader in process 58196

Before this will work, we need to make the main app aware of the migrations provided by the engine.

2.2.1 Installing Engine Migrations with Rake

The common solution to this is to install the engine’s migrations into the main app with rake app_component:install:migrations. This will copy all the migrations found in the engine’s db/migrate folder into that of the main app.

There are a few gems out there that use this functionality. For example, the gem Commontator (https://github.com/lml/commontator) does this. Most widely used gems, like ActiveAdmin (http://activeadmin.info/) and Devise (http://devise.plataformatec.com.br/), generate more complex migrations in the host app. They don’t actually come with migrations of their own, but instead use generators to create them based on user-specified configuration options.

If we were to run rake app_component:install:migrations in the Sportsball app, we would get the following contents in the engine and the main app:

Install engine migrations into the main app. Execute in ./

$ rake app_component:install:migrations
Running via Spring preloader in process 58464
Copied migration 20171030000159_create_app_component_teams. app_component.rb from app_component
Copied migration 20171030000160_create_app_component_games. app_component.rb from app_component

AppComponent engine migrations

$ tree ./components/app_component/db/migrate
 components/app_component/db/migrate
 angle1.jpg 20171029235211_create_app_component_teams.rb
 angle2.jpg 20171029235221_create_app_component_games.rb

Sportsball application migrations

$ tree ./db/migrate
 ./db/migrate
 angle1.jpg 20170507205125_create_app_component_teams.app_component.rb
 angle2.jpg 20170507205126_create_app_component_games.app_component.rb

While the original migrations in the engine are still present, the rake task has copied them into the main app. In doing so, it renamed them and changed their date to the current time. This ensures that the engine’s migrations are being run as the last ones in the application (“last” at the time they are installed into the app).

The re-dating of migrations to the current time is very important for engines that are intended to be distributed (like the ones mentioned previously), because it is unknown when the gem, and thus its migrations, are going to be added to an application. If the dates were not changed, there would be no control over where in the overall migration sequence they would fall. However, since the gem had to have been published before the development of the app (or the relevant portion of the app) was started, they are likely going to be run very early. That, in turn, would likely lead to an invalid overall migration state on any system that runs the app—even production: While newer migrations have run, older ones are missing.

Note that rake railties:install:migrations installs all new migrations from all engines in an application. If migrations are added after the installer has run, it will need to be run again to ensure all migrations are present.

Having to install migrations every time they are added to an engine is an extra step in comparison to what we are used to. And, as discussed previously, the reason it is needed in many situations (gems being developed independently from applications) does not hold true in our case. It turns out that we can change our engine to have the host Rails application automatically find and use its migrations.

2.2.2 Loading Engine Migrations in Place

Instead of copying migrations from components into the main application, we can ensure that the main app can find them with a few lines of code added to the component’s engine.rb. This technique was first suggested by Ben Smith (http://pivotallabs.com/leave-your-migrations-in-your-rails-engines/).

./components/app_component/lib/app_component/engine.rb – Engine migrations configuration

 1 module AppComponent
 2   class Engine < ::Rails::Engine
 3     isolate_namespace AppComponent
 4 
 5     initializer :append_migrations do |app|
 6       unless app.root.to_s.match root.to_s+File::SEPARATOR
 7         app.config.paths["db/migrate"].concat(
               config.paths["db/migrate"].expanded)
 8       end
 9     end
10   end
11 end

Now rake db:migrate will find and correctly run the engine’s migrations.

It is important not to use migration installation rake tasks in combination with this technique and to remove what may have been added while following the previous section. That would result in problems with migrations being loaded twice, which is similar to something we will see next.

2.2.3 Known Issues

There are a few snags you can run into with this approach that should be avoided.

2.2.3.1 Chaining db:migrate in Rake Tasks Fails to Add

For a still unknown reason, rake db:drop db:create db:migrate works fine for a normal Rails app, but fails to add the migrations when engine migrations are loaded in place. The simplest way around this is to split this command in two by running rake db:drop db:create && rake db:migrate instead. This has performance implications, as the rake now has to load twice.

Ben Smith proposes a few different fixes for this issue, the most concise being to require the Rails environment to be loaded before the db:load_config by adding the following file db.rake:

./components/app_component/lib/tasks/db.rake

1 Rake::Task["db:load_config"].enhance [:environment]

This has the side effect of the environment (i.e., the Rails app’s contents) always being loaded before any database tasks are run. Not only does this have a performance implication, it also affects every environment. And while the pattern rake db:drop db:create db:migrate is common in development, it will never be called in the production environment. In my opinion, it is a bad tradeoff to affect production in unknown ways to achieve small benefits in development.

In conclusion, I recommend working around the problem instead of using a solution that is not fully understood. This issue should be fixed by understanding and fixing the true cause in whatever it is that Rails does to run migrations. That would also make a nice pull request to Rails for the one who finds it!

2.2.3.2 Other Rake Tasks Reported Not Working

It has been reported that other database-related tasks, like rake db:setup, rake db:rollback, and rake db:migration:redo, stop working with this approach. I have never been able to confirm this and it certainly works in the Sportsball app as we have created it so far.

2.2.3.3 Problems with Naming Engines

In the code snippet that adds the engine’s migrations, you may have noticed the peculiar line unless app.root.to_s.match root.to_s+File::SEPARATOR. It prevents a problem with running rake app_component:db:migrate inside the engine itself, which throws the following error otherwise:

Engine migrations loaded twice through dummy app. Executed in ./components/app_component (if the check is removed)

$ rake app_component:db:migrate
rake aborted!
ActiveRecord::DuplicateMigrationNameError:
 
Multiple migrations have the name CreateAppComponentTeams

The match is a heuristic to determine whether the engine is currently being loaded within its own dummy app. If that is the case, the initializer should not add the routes, as they are added automatically. The heuristic fails if the dummy app of the engine is outside of its own directory structure, which should never be the case.

A less powerful version of this heuristic evaluates app.root.to_s.match root.to_s, which fails if used with two engines where the name of the including engine starts with the name of the included engine and they are in the same directory. For example, if /components/users_server depends on /components/users, the former matches the latter and will not load its migrations. The more robust version of the heuristic should be used to prevent this problem.

2.2.3.4 Conclusion

We have added models for Team and Game and we have ensured that migrations can be run in the main application. Do so, if you haven’t yet, and start the server.

Run migrations and start the server. Executed in ./

$ rake db:migrate
Running via Spring preloader in process 58740
== 20171029235211 CreateAppComponentTeams: migrating =================
-- create_table(:app_component_teams)
   -> 0.0010s
== 20171029235211 CreateAppComponentTeams: migrated (0.0010s) ========
 
 
== 20171029235221 CreateAppComponentGames: migrating =================
-- create_table(:app_component_games)
   -> 0.0007s
== 20171029235221 CreateAppComponentGames: migrated (0.0007s) ========
 
 
$ rails s
=> Booting Puma
=> Rails 5.1.4 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.10.0 (ruby 2.4.2-p198), codename: Russell's Teapot
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

With this, we can reach the UI for teams and games by navigating to http://localhost:3000/teams and http://localhost:3000/games, respectively. There, we are greeted with the standard look of scaffolded admin pages, as shown for teams in Figures 2.3 and 2.4.

Figure 2.3

Figure 2.3. List of teams with two teams already added

Figure 2.4

Figure 2.4. Adding a new team

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020