Skip to content

fix(seeds): Fix alerting seeds when no premium license is set #3860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2025
Merged

Conversation

groyoh
Copy link
Contributor

@groyoh groyoh commented Jun 23, 2025

Context

During seeding, when no premium license is set, the lifetime_usage_amount alert creation fails. This results in an error when creating the related thresholds as the alert is nil:

> lago exec -e RAILS_ENV=test api bundle exec rake db:drop db:create db:migrate db:seed
Dropped database 'lago_test'
Dropped database 'lago_test'
Dropped database 'default_test'
Created database 'lago_test'
...
rake aborted!
ActiveRecord::RecordInvalid: Validation failed: Alert relation_must_exist (ActiveRecord::RecordInvalid)
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/validations.rb:87:in 'ActiveRecord::Validations#raise_validation_error'
...
/app/db/seeds/alerting.rb:69:in '<main>'
/app/db/seeds.rb:4:in 'Kernel#load'
/app/db/seeds.rb:4:in 'block in <main>'
/app/db/seeds.rb:3:in 'Array#each'
/app/db/seeds.rb:3:in '<main>'
/usr/local/bundle/gems/railties-8.0.2/lib/rails/engine.rb:562:in 'Kernel#load'
...
/usr/local/bundle/bin/bundle:25:in '<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

Description

This PR fixes it by checking whether License.premium? is true.

Once fixed, it turned out that the billable_metric_current_usage_amount triggered alert creation failed as well since we did not retrieve the service result properly.

When no premium license is set, the `lifetime_usage_amount` alert creation failed which resulted in an error when creating the related thresholds:

```
rake aborted!
ActiveRecord::RecordInvalid: Validation failed: Alert relation_must_exist (ActiveRecord::RecordInvalid)
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/validations.rb:87:in 'ActiveRecord::Validations#raise_validation_error'
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/validations.rb:54:in 'ActiveRecord::Validations#save!'
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/transactions.rb:365:in 'block in ActiveRecord::Transactions#save!'
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/transactions.rb:417:in 'block (2 levels) in ActiveRecord::Transactions#with_transaction_returning_status'
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/connection_adapters/abstract/transaction.rb:626:in 'block in ActiveRecord::ConnectionAdapters::TransactionManager#within_new_transaction'
/usr/local/bundle/gems/activesupport-8.0.2/lib/active_support/concurrency/null_lock.rb:9:in 'ActiveSupport::Concurrency::NullLock#synchronize'
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/connection_adapters/abstract/transaction.rb:623:in 'ActiveRecord::ConnectionAdapters::TransactionManager#within_new_transaction'
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:367:in 'ActiveRecord::ConnectionAdapters::DatabaseStatements#within_new_transaction'
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:359:in 'ActiveRecord::ConnectionAdapters::DatabaseStatements#transaction'
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/transactions.rb:413:in 'block in ActiveRecord::Transactions#with_transaction_returning_status'
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:412:in 'ActiveRecord::ConnectionAdapters::ConnectionPool#with_connection'
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/connection_handling.rb:310:in 'ActiveRecord::ConnectionHandling#with_connection'
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/transactions.rb:409:in 'ActiveRecord::Transactions#with_transaction_returning_status'
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/transactions.rb:365:in 'ActiveRecord::Transactions#save!'
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/suppressor.rb:56:in 'ActiveRecord::Suppressor#save!'
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/persistence.rb:55:in 'ActiveRecord::Persistence::ClassMethods#create!'
/app/db/seeds/alerting.rb:69:in '<main>'
/app/db/seeds.rb:4:in 'Kernel#load'
/app/db/seeds.rb:4:in 'block in <main>'
/app/db/seeds.rb:3:in 'Array#each'
/app/db/seeds.rb:3:in '<main>'
/usr/local/bundle/gems/railties-8.0.2/lib/rails/engine.rb:562:in 'Kernel#load'
/usr/local/bundle/gems/railties-8.0.2/lib/rails/engine.rb:562:in 'block in Rails::Engine#load_seed'
/usr/local/bundle/gems/activesupport-8.0.2/lib/active_support/callbacks.rb:120:in 'block in ActiveSupport::Callbacks#run_callbacks'
/usr/local/bundle/gems/activesupport-8.0.2/lib/active_support/reloader.rb:77:in 'block in ActiveSupport::Reloader.wrap'
/usr/local/bundle/gems/activesupport-8.0.2/lib/active_support/execution_wrapper.rb:91:in 'ActiveSupport::ExecutionWrapper.wrap'
/usr/local/bundle/gems/activesupport-8.0.2/lib/active_support/reloader.rb:74:in 'ActiveSupport::Reloader.wrap'
/usr/local/bundle/gems/railties-8.0.2/lib/rails/engine.rb:652:in 'block (2 levels) in <class:Engine>'
/usr/local/bundle/gems/activesupport-8.0.2/lib/active_support/callbacks.rb:129:in 'BasicObject#instance_exec'
/usr/local/bundle/gems/activesupport-8.0.2/lib/active_support/callbacks.rb:129:in 'block in ActiveSupport::Callbacks#run_callbacks'
/usr/local/bundle/gems/activesupport-8.0.2/lib/active_support/callbacks.rb:140:in 'ActiveSupport::Callbacks#run_callbacks'
/usr/local/bundle/gems/railties-8.0.2/lib/rails/engine.rb:562:in 'Rails::Engine#load_seed'
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/tasks/database_tasks.rb:492:in 'ActiveRecord::Tasks::DatabaseTasks#load_seed'
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/railties/databases.rake:390:in 'block (2 levels) in <main>'
/usr/local/bundle/gems/bundler-2.6.8/lib/bundler/cli/exec.rb:59:in 'Kernel.load'
/usr/local/bundle/gems/bundler-2.6.8/lib/bundler/cli/exec.rb:59:in 'Bundler::CLI::Exec#kernel_load'
/usr/local/bundle/gems/bundler-2.6.8/lib/bundler/cli/exec.rb:23:in 'Bundler::CLI::Exec#run'
/usr/local/bundle/gems/bundler-2.6.8/lib/bundler/cli.rb:452:in 'Bundler::CLI#exec'
/usr/local/bundle/gems/bundler-2.6.8/lib/bundler/vendor/thor/lib/thor/command.rb:28:in 'Bundler::Thor::Command#run'
/usr/local/bundle/gems/bundler-2.6.8/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in 'Bundler::Thor::Invocation#invoke_command'
/usr/local/bundle/gems/bundler-2.6.8/lib/bundler/vendor/thor/lib/thor.rb:538:in 'Bundler::Thor.dispatch'
/usr/local/bundle/gems/bundler-2.6.8/lib/bundler/cli.rb:35:in 'Bundler::CLI.dispatch'
/usr/local/bundle/gems/bundler-2.6.8/lib/bundler/vendor/thor/lib/thor/base.rb:584:in 'Bundler::Thor::Base::ClassMethods#start'
/usr/local/bundle/gems/bundler-2.6.8/lib/bundler/cli.rb:29:in 'Bundler::CLI.start'
/usr/local/bundle/gems/bundler-2.6.8/exe/bundle:28:in 'block in <top (required)>'
/usr/local/bundle/gems/bundler-2.6.8/lib/bundler/friendly_errors.rb:117:in 'Bundler.with_friendly_errors'
/usr/local/bundle/gems/bundler-2.6.8/exe/bundle:20:in '<top (required)>'
/usr/local/bundle/bin/bundle:25:in 'Kernel#load'
/usr/local/bundle/bin/bundle:25:in '<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
```

Once fixed, it turned out that the `billable_metric_current_usage_amount` triggered alert creation failed as well since we did not retrieve the service result properly.
@groyoh
Copy link
Contributor Author

groyoh commented Jun 23, 2025

Thanks @vincent-pochet @brunomiguelpinto ! 🙏

@groyoh groyoh merged commit f75ce85 into main Jun 23, 2025
14 checks passed
@groyoh groyoh deleted the fix/seeds branch June 23, 2025 14:47
diegocharles pushed a commit that referenced this pull request Jul 11, 2025
## Context

During seeding, when no premium license is set, the `lifetime_usage_amount` alert creation fails. This results in an error when creating the related thresholds as the alert is `nil`:

```shell
> lago exec -e RAILS_ENV=test api bundle exec rake db:drop db:create db:migrate db:seed
Dropped database 'lago_test'
Dropped database 'lago_test'
Dropped database 'default_test'
Created database 'lago_test'
...
rake aborted!
ActiveRecord::RecordInvalid: Validation failed: Alert relation_must_exist (ActiveRecord::RecordInvalid)
/usr/local/bundle/gems/activerecord-8.0.2/lib/active_record/validations.rb:87:in 'ActiveRecord::Validations#raise_validation_error'
...
/app/db/seeds/alerting.rb:69:in '<main>'
/app/db/seeds.rb:4:in 'Kernel#load'
/app/db/seeds.rb:4:in 'block in <main>'
/app/db/seeds.rb:3:in 'Array#each'
/app/db/seeds.rb:3:in '<main>'
/usr/local/bundle/gems/railties-8.0.2/lib/rails/engine.rb:562:in 'Kernel#load'
...
/usr/local/bundle/bin/bundle:25:in '<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
```

## Description

This commit fixes it by checking whether `License.premium?` is `true`.

Once fixed, it turned out that the `billable_metric_current_usage_amount` triggered alert creation failed as well since we did not retrieve the service result properly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants