chore: Fix db:prepare error in deployments (#1668)

Fixes #1667
This commit is contained in:
Sojan Jose 2021-01-19 16:47:25 +05:30 committed by GitHub
parent 114f00b88a
commit f36af7a7de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 4 deletions

View file

@ -1,3 +1,3 @@
release: bundle exec rails db:prepare
release: bundle exec rails db:chatwoot_prepare
web: bin/rails server -p $PORT -e $RAILS_ENV
worker: bundle exec sidekiq -C config/sidekiq.yml

View file

@ -12,9 +12,6 @@
"vue"
],
"success_url": "/",
"scripts": {
"postdeploy": "bundle exec rake db:seed"
},
"env": {
"SECRET_TOKEN": {
"description": "A secret key for verifying the integrity of signed cookies.",

View file

@ -5,3 +5,28 @@ Rake::Task['db:migrate'].enhance do
ConfigLoader.new.process
end
end
# we are creating a custom database prepare task
# the default rake db:prepare task isn't ideal for environments like heroku
# In heroku the database is already created before the first run of db:prepare
# In this case rake db:prepare tries to run db:migrate from all the way back from the beginning
# Since the assumption is migrations are only run after schema load from a point x, this could lead to things breaking.
# ref: https://github.com/rails/rails/blob/main/activerecord/lib/active_record/railties/databases.rake#L356
db_namespace = namespace :db do
desc 'Runs setup if database does not exist, or runs migrations if it does'
task chatwoot_prepare: :load_config do
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).each do |db_config|
ActiveRecord::Base.establish_connection(db_config.config)
# handling case where database was created by the provider, with out running db:setup
if ActiveRecord::Base.connection.tables.count.zero?
db_namespace['load_config'].invoke if ActiveRecord::Base.schema_format == :ruby
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV['SCHEMA'])
db_namespace['seed'].invoke
end
db_namespace['migrate'].invoke
rescue ActiveRecord::NoDatabaseError
db_namespace['setup'].invoke
end
end
end