diff --git a/README.md b/README.md index e4f9f8b..8d058fe 100644 --- a/README.md +++ b/README.md @@ -330,6 +330,21 @@ config.solid_errors.destroy_after = 30.days config.solid_errors.destroy_after = 6.months ``` +#### Checking the schema after an upgrade + +When a release changes the schema, [UPGRADE.md](UPGRADE.md) describes the migration. To have your test suite fail until you've applied it, assert that the errors database has everything the installed version uses: + +```ruby +# test/solid_errors_test.rb +require "test_helper" + +class SolidErrorsTest < ActiveSupport::TestCase + test "the errors database is up to date" do + assert SolidErrors.migrated? + end +end +``` + ### Examples There are only two screens in the dashboard. diff --git a/lib/solid_errors.rb b/lib/solid_errors.rb index d42da4a..6997dce 100644 --- a/lib/solid_errors.rb +++ b/lib/solid_errors.rb @@ -33,6 +33,17 @@ def password @password ||= ENV["SOLIDERRORS_PASSWORD"] || @@password end + # Whether the errors database has every table and column this version reads and writes. + # Assert it in your test suite to catch a schema change you haven't applied yet. + def migrated? + { + SolidErrors::Error => %w[exception_class message severity source resolved_at fingerprint created_at updated_at], + SolidErrors::Occurrence => %w[error_id backtrace context created_at updated_at] + }.all? do |model, columns| + model.table_exists? && (columns - model.column_names).empty? + end + end + def send_emails? send_emails && email_to.present? && !ActiveSupport::IsolatedExecutionState[:solid_errors_without_emails] end diff --git a/test/solid_errors/migrated_test.rb b/test/solid_errors/migrated_test.rb new file mode 100644 index 0000000..201e483 --- /dev/null +++ b/test/solid_errors/migrated_test.rb @@ -0,0 +1,26 @@ +require "test_helper" + +class SolidErrors::MigratedTest < ActiveSupport::TestCase + teardown do + SolidErrors::Error.reset_column_information + SolidErrors::Occurrence.reset_column_information + end + + test "migrated when the errors database has the current schema" do + assert SolidErrors.migrated? + end + + test "not migrated when a column this version uses is missing" do + ActiveRecord::Base.connection.remove_column :solid_errors, :fingerprint + SolidErrors::Error.reset_column_information + + assert_not SolidErrors.migrated? + end + + test "not migrated when a table is missing" do + ActiveRecord::Base.connection.drop_table :solid_errors_occurrences + SolidErrors::Occurrence.reset_column_information + + assert_not SolidErrors.migrated? + end +end