The rails app in question was initially created using --skip-active-record since there was no database needed.
Now there is a need to add in a MySQL database to the app. How can one convert the non-database Rails app to have a database?
The rails app in question was initially created using --skip-active-record since there was no database needed.
Now there is a need to add in a MySQL database to the app. How can one convert the non-database Rails app to have a database?
This is what a I did:
rails new my_app_name -d mysql
Files changed:
config/application.rb
config/environments/development.rb
config/environments/production.rb
config/initializers/wrap_parameters.rb
Gemfile
bin/setup
bin/update
test/test_helper.rb
New files
config/database.yml
app/models/application_record.rb
db/seeds.rb
Details(diff view - for removed + added):
config/application.rb
-# Pick the frameworks you want:
-require "active_model/railtie"
-require "active_job/railtie"
-# require "active_record/railtie"
-require "action_controller/railtie"
-require "action_mailer/railtie"
-require "action_view/railtie"
-require "action_cable/engine"
-require "sprockets/railtie"
-require "rails/test_unit/railtie"
+require 'rails/all'
config/environments/development.rb
+# Raise an error on page load if there are pending migrations.
+config.active_record.migration_error = :page_load
config/environments/production.rb
+# Do not dump schema after migrations. +config.active_record.dump_schema_after_migration = false
Gemfile
+# Use mysql as the database for Active Record
+gem 'mysql2', '>= 0.3.18', '< 0.6.0'
bin/setup
+# puts "\n== Copying sample files =="
+# unless File.exist?('config/database.yml')
+# cp 'config/database.yml.sample', 'config/database.yml'
+# end
+puts "\n== Preparing database =="
+system! 'bin/rails db:setup'
bin/update
+puts "\n== Updating database =="
+system! 'bin/rails db:migrate'
app/models/application_record.rb
+class ApplicationRecord < ActiveRecord::Base
+ self.abstract_class = true
+end
config/database.yml
+default: &default
+ adapter: mysql2
+ encoding: utf8
+ pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
+ username: root
+ password:
+ host: localhost
+development:
+ <<: *default
+ database: app-without-activerecord_development
+test:
+ <<: *default
+ database: app-without-activerecord_test
+production:
+ <<: *default
+ database: app-without-activerecord_production
+ username: app-without-activerecord
+ password: <%= ENV['APP-WITHOUT-ACTIVERECORD_DATABASE_PASSWORD'] %>
the first thing that I did after reading your question was to create 2 new projects on my computer
rails new test-db and rails new test-no-db --skip-active-record now I should have 2 projects that are identical but 1 does not have active-record
now can can run a difference between the 2 directories diff --brief -r test-db/ test-no-db/
files that change
1) Gemfile
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
2) app/assets/javascripts/application.js
//= require activestorage
3) Add this file
#test-db/app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
4) test-db/bin/setup
chdir APP_ROOT do
...
puts "\n== Preparing database =="
system! 'bin/rails db:setup'
...
end
you can go threw the rest of the files to see what is different. I know that this is not the solution that you wanted but its what I would have done