diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..26977b9 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,98 @@ +name: CI + +on: + push: + branches: [master] + pull_request: + branches: ["**"] + +jobs: + tests: + services: + postgres: + image: postgres:18 + env: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + ports: + - 5432 + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + + mysql: + image: mysql:8 + env: + MYSQL_ROOT_PASSWORD: root + ports: + - 3306 + options: >- + --health-cmd="mysqladmin ping" + --health-interval=10s + --health-timeout=5s + --health-retries=3 + + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + sequel: + - "~> 3.38" + - "~> 4.0" + - "~> 5.0" + ruby: + - "3.0" + - "3.1" + - "3.2" + - "3.3" + - "3.4" + - "4.0" + + name: Ruby ${{ matrix.ruby }}, Sequel ${{ matrix.sequel }} + + env: + SEQUEL: "${{ matrix.sequel }}" + BUNDLE_GEMFILE: "ci/Gemfile.ci" + steps: + - uses: actions/checkout@v3 + - name: Install db dependencies and check connections + run: | + DEBIAN_FRONTEND="noninteractive" sudo apt-get install -yqq mysql-client libmysqlclient-dev postgresql-client libpq-dev + mysql --host 127.0.0.1 --port ${{ job.services.mysql.ports[3306] }} -uroot -proot -e "SHOW GRANTS FOR 'root'@'localhost'" + env PGPASSWORD=postgres psql -h localhost -p ${{ job.services.postgres.ports[5432] }} -U postgres -l + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + - name: Create databases + run: | + mysql -e 'create database delayed_jobs_test;' --host 127.0.0.1 --port ${{ job.services.mysql.ports[3306] }} -uroot -proot + env PGPASSWORD=postgres psql -c 'create database delayed_jobs_test;' -U postgres -h localhost -p ${{ job.services.postgres.ports[5432] }} + - name: Run PostgreSQL tests + run: bundle exec rspec + env: + TEST_ADAPTER: postgresql + TEST_DATABASE: delayed_jobs_test + TEST_DATABASE_HOST: localhost + TEST_DATABASE_PORT: ${{ job.services.postgres.ports[5432] }} + TEST_USERNAME: postgres + TEST_PASSWORD: postgres + - name: Run MySQL2 tests + run: bundle exec rspec + env: + TEST_ADAPTER: mysql2 + TEST_DATABASE: delayed_jobs_test + TEST_DATABASE_HOST: 127.0.0.1 + TEST_DATABASE_PORT: ${{ job.services.mysql.ports[3306] }} + TEST_USERNAME: root + TEST_PASSWORD: root + TEST_ENCODING: "utf8" + - name: Run SQLite tests + run: bundle exec rspec + env: + TEST_ADAPTER: "sqlite3" + TEST_DATABASE: "db/database.sqlite3" + - name: Lint + run: bundle exec rubocop diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..471e944 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,22 @@ +inherit_from: .rubocop_todo.yml + +AllCops: + Include: + - '**/Gemfile' + - '**/config.ru' + - '**/Rakefile' + - '**/*.rake' + - '**/*.gemfile' + - '**/*.gemspec' + - '**/*.rb' + Exclude: + - 'ci/**/*' + - 'vendor/bundle/**/*' + SuggestExtensions: false + NewCops: disable + +Style/HashSyntax: + EnforcedStyle: ruby19 + +Layout/FirstHashElementIndentation: + EnforcedStyle: consistent diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..724f066 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,318 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2026-01-02 09:02:39 UTC using RuboCop version 1.82.1. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: TreatCommentsAsGroupSeparators, ConsiderPunctuation. +Bundler/OrderedGems: + Exclude: + - 'Gemfile' + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: TreatCommentsAsGroupSeparators, ConsiderPunctuation. +Gemspec/OrderedDependencies: + Exclude: + - 'delayed_job_sequel.gemspec' + +# Offense count: 1 +Gemspec/RequiredRubyVersion: + Exclude: + - 'delayed_job_sequel.gemspec' + +# Offense count: 3 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, IndentOneStep, IndentationWidth. +# SupportedStyles: case, end +Layout/CaseIndentation: + Exclude: + - 'lib/delayed/backend/sequel.rb' + - 'spec/spec_helper.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Layout/EmptyLineAfterMagicComment: + Exclude: + - 'Rakefile' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EmptyLineBetweenMethodDefs, EmptyLineBetweenClassDefs, EmptyLineBetweenModuleDefs, DefLikeMacros, AllowAdjacentOneLineDefs, NumberOfEmptyLines. +Layout/EmptyLineBetweenDefs: + Exclude: + - 'spec/spec_helper.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines, beginning_only, ending_only +Layout/EmptyLinesAroundClassBody: + Exclude: + - 'lib/delayed/backend/sequel.rb' + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyleAlignWith. +# SupportedStylesAlignWith: keyword, variable, start_of_line +Layout/EndAlignment: + Exclude: + - 'lib/delayed/backend/sequel.rb' + - 'spec/spec_helper.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, IndentationWidth. +# SupportedStyles: aligned, indented +Layout/MultilineOperationIndentation: + Exclude: + - 'lib/delayed/serialization/sequel.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: space, no_space +Layout/SpaceAroundEqualsInParameterDefault: + Exclude: + - 'lib/delayed/backend/sequel.rb' + +# Offense count: 3 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowForAlignment, EnforcedStyleForExponentOperator, EnforcedStyleForRationalLiterals. +# SupportedStylesForExponentOperator: space, no_space +# SupportedStylesForRationalLiterals: space, no_space +Layout/SpaceAroundOperators: + Exclude: + - 'spec/delayed/backend/sequel_spec.rb' + - 'spec/spec_helper.rb' + +# Offense count: 16 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces. +# SupportedStyles: space, no_space, compact +# SupportedStylesForEmptyBraces: space, no_space +Layout/SpaceInsideHashLiteralBraces: + Exclude: + - 'lib/delayed/backend/sequel.rb' + - 'spec/spec_helper.rb' + +# Offense count: 4 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: space, compact, no_space +Layout/SpaceInsideParens: + Exclude: + - 'spec/delayed/backend/sequel_spec.rb' + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +Lint/AmbiguousOperator: + Exclude: + - 'lib/delayed/backend/sequel.rb' + - 'spec/spec_helper.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: AllowSafeAssignment. +Lint/AssignmentInCondition: + Exclude: + - 'lib/delayed/backend/sequel.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods, IgnoreNotImplementedMethods, NotImplementedExceptions. +# NotImplementedExceptions: NotImplementedError +Lint/UnusedMethodArgument: + Exclude: + - 'lib/delayed/serialization/sequel.rb' + +# Offense count: 2 +# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. +Metrics/AbcSize: + Max: 21 + +# Offense count: 2 +# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. +# AllowedMethods: refine +Metrics/BlockLength: + Max: 96 + +# Offense count: 1 +# Configuration parameters: CountComments, CountAsOne. +Metrics/ClassLength: + Max: 118 + +# Offense count: 3 +# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. +Metrics/MethodLength: + Max: 19 + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Naming/BinaryOperatorParameterName: + Exclude: + - 'lib/delayed/backend/sequel.rb' + +# Offense count: 3 +# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames. +# AllowedNames: as, at, by, cc, db, id, if, in, io, ip, of, on, os, pp, to +Naming/MethodParameterName: + Exclude: + - 'lib/delayed/backend/sequel.rb' + - 'spec/spec_helper.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: prefer_alias, prefer_alias_method +Style/Alias: + Exclude: + - 'spec/spec_helper.rb' + +# Offense count: 6 +# Configuration parameters: AllowedConstants. +Style/Documentation: + Exclude: + - 'spec/**/*' + - 'test/**/*' + - 'lib/delayed/serialization/sequel.rb' + - 'lib/delayed_job_sequel.rb' + - 'lib/generators/delayed_job/sequel_generator.rb' + - 'lib/generators/delayed_job/templates/migration.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Style/Encoding: + Exclude: + - 'Rakefile' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Style/ExpandPathArguments: + Exclude: + - 'spec/delayed/backend/sequel_spec.rb' + +# Offense count: 11 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: always, always_true, never +Style/FrozenStringLiteralComment: + Exclude: + - '**/*.arb' + - 'Gemfile' + - 'Rakefile' + - 'delayed_job_sequel.gemspec' + - 'lib/delayed/backend/sequel.rb' + - 'lib/delayed/serialization/sequel.rb' + - 'lib/delayed_job_sequel.rb' + - 'lib/generators/delayed_job/sequel_generator.rb' + - 'lib/generators/delayed_job/templates/migration.rb' + - 'spec/delayed/backend/sequel_spec.rb' + - 'spec/delayed/serialization/sequel_spec.rb' + - 'spec/spec_helper.rb' + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: MinBodyLength, AllowConsecutiveConditionals. +Style/GuardClause: + Exclude: + - 'spec/spec_helper.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Style/IfUnlessModifier: + Exclude: + - 'lib/delayed/serialization/sequel.rb' + +# Offense count: 3 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: PreferredDelimiters. +Style/PercentLiteralDelimiters: + Exclude: + - 'delayed_job_sequel.gemspec' + - 'spec/delayed/backend/sequel_spec.rb' + +# Offense count: 2 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: short, verbose +Style/PreferredHashMethods: + Exclude: + - 'lib/delayed/backend/sequel.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Style/RedundantBegin: + Exclude: + - 'spec/delayed/backend/sequel_spec.rb' + +# Offense count: 4 +# This cop supports safe autocorrection (--autocorrect). +Style/RedundantSelf: + Exclude: + - 'lib/delayed/backend/sequel.rb' + - 'lib/generators/delayed_job/sequel_generator.rb' + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +Style/RescueModifier: + Exclude: + - 'spec/spec_helper.rb' + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowIfMethodIsEmpty. +Style/SingleLineMethods: + Exclude: + - 'spec/spec_helper.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: RequireEnglish. +# SupportedStyles: use_perl_names, use_english_names, use_builtin_english_names +Style/SpecialGlobalVars: + EnforcedStyle: use_perl_names + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: Mode. +Style/StringConcatenation: + Exclude: + - 'spec/spec_helper.rb' + +# Offense count: 104 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. +# SupportedStyles: single_quotes, double_quotes +Style/StringLiterals: + Exclude: + - 'Gemfile' + - 'delayed_job_sequel.gemspec' + - 'lib/delayed/backend/sequel.rb' + - 'lib/delayed/serialization/sequel.rb' + - 'spec/delayed/backend/sequel_spec.rb' + - 'spec/spec_helper.rb' + +# Offense count: 3 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: MinSize. +# SupportedStyles: percent, brackets +Style/SymbolArray: + EnforcedStyle: brackets + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Style/WhileUntilDo: + Exclude: + - 'spec/delayed/backend/sequel_spec.rb' + +# Offense count: 4 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, AllowRBSInlineAnnotation, AllowCopDirectives, AllowedPatterns, SplitStrings. +# URISchemes: http, https +Layout/LineLength: + Max: 139 diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 1ef1c18..0000000 --- a/.travis.yml +++ /dev/null @@ -1,35 +0,0 @@ -language: ruby -cache: bundler -sudo: false -rvm: - - 2.2.8 - - 2.3.5 - - 2.4.2 - - jruby-9.1.9.0 -env: - - DB=sqlite SEQUEL='~> 3.38' - - DB=sqlite SEQUEL='~> 4.0' - - DB=sqlite SEQUEL='~> 5.0' - - DB=mysql SEQUEL='~> 3.38' - - DB=mysql SEQUEL='~> 4.0' - - DB=mysql SEQUEL='~> 5.0' - - DB=postgres SEQUEL='~> 3.38' - - DB=postgres SEQUEL='~> 4.0' - - DB=postgres SEQUEL='~> 5.0' -gemfile: ci/Gemfile.ci -matrix: - exclude: - - rvm: jruby-9.1.9.0 - env: DB=sqlite SEQUEL='~> 3.38' - - rvm: jruby-9.1.9.0 - env: DB=sqlite SEQUEL='~> 4.0' - - rvm: jruby-9.1.9.0 - env: DB=sqlite SEQUEL='~> 5.0' - - rvm: jruby-9.1.9.0 - env: DB=postgres SEQUEL='~> 3.38' - - rvm: jruby-9.1.9.0 - env: DB=mysql SEQUEL='~> 3.38' - - rvm: jruby-9.1.9.0 - env: DB=mysql SEQUEL='~> 4.0' - - rvm: jruby-9.1.9.0 - env: DB=mysql SEQUEL='~> 5.0' diff --git a/Rakefile b/Rakefile index 1bfd0da..f0dc8c4 100644 --- a/Rakefile +++ b/Rakefile @@ -9,4 +9,4 @@ RSpec::Core::RakeTask.new do |r| r.verbose = false end -task :default => :spec +task default: :spec diff --git a/delayed_job_sequel.gemspec b/delayed_job_sequel.gemspec index fe5e28d..8fd433e 100644 --- a/delayed_job_sequel.gemspec +++ b/delayed_job_sequel.gemspec @@ -18,4 +18,7 @@ Gem::Specification.new do |s| s.add_development_dependency "rspec" s.add_development_dependency "rake" + s.add_development_dependency "rubocop" + s.add_development_dependency "rubocop-rake" + s.add_development_dependency "rubocop-rspec" end diff --git a/lib/delayed/backend/sequel.rb b/lib/delayed/backend/sequel.rb index 1c33911..7f9aebd 100644 --- a/lib/delayed/backend/sequel.rb +++ b/lib/delayed/backend/sequel.rb @@ -20,10 +20,10 @@ def ready_to_run(worker_name, max_run_time) filter do ( (run_at <= db_time_now) & - ::Sequel.expr(:locked_at => nil) | + ::Sequel.expr(locked_at: nil) | (::Sequel.expr(:locked_at) < lock_upper_bound) | - {:locked_by => worker_name} - ) & {:failed_at => nil} + {locked_by: worker_name} + ) & {failed_at: nil} end end @@ -41,7 +41,7 @@ def self.before_fork # When a worker is exiting, make sure we don't have any locked jobs. def self.clear_locks!(worker_name) - filter(:locked_by => worker_name).update(:locked_by => nil, :locked_at => nil) + filter(locked_by: worker_name).update(locked_by: nil, locked_at: nil) end # adapted from @@ -51,7 +51,7 @@ def self.reserve(worker, max_run_time = Worker.max_run_time) ds = ds.filter(::Sequel.lit("priority >= ?", Worker.min_priority)) if Worker.min_priority ds = ds.filter(::Sequel.lit("priority <= ?", Worker.max_priority)) if Worker.max_priority - ds = ds.filter(:queue => Worker.queues) if Worker.queues.any? + ds = ds.filter(queue: Worker.queues) if Worker.queues.any? ds = ds.by_priority case ::Sequel::Model.db.database_type @@ -70,7 +70,7 @@ def self.lock_with_for_update(ds, worker) if job = ds.first job.locked_at = self.db_time_now job.locked_by = worker.name - job.save(:raise_on_failure => true) + job.save(raise_on_failure: true) job end end @@ -108,7 +108,7 @@ def reload(*args) end def save! - save :raise_on_failure => true + save raise_on_failure: true end def update_attributes(attrs) @@ -116,7 +116,7 @@ def update_attributes(attrs) end def self.create!(attrs) - new(attrs).save :raise_on_failure => true + new(attrs).save raise_on_failure: true end def self.silence_log(&block) diff --git a/lib/generators/delayed_job/templates/migration.rb b/lib/generators/delayed_job/templates/migration.rb index e784160..420e1bd 100644 --- a/lib/generators/delayed_job/templates/migration.rb +++ b/lib/generators/delayed_job/templates/migration.rb @@ -1,8 +1,8 @@ class CreateDelayedJobs < ActiveRecord::Migration def self.up - create_table :delayed_jobs, :force => true do |table| - table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue - table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually. + create_table :delayed_jobs, force: true do |table| + table.integer :priority, default: 0 # Allows some jobs to jump to the front of the queue + table.integer :attempts, default: 0 # Provides for retries, but still fail eventually. table.text :handler # YAML-encoded string of the object that will do work table.text :last_error # reason for last failure (See Note below) table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future. @@ -13,7 +13,7 @@ def self.up table.timestamps end - add_index :delayed_jobs, [:priority, :run_at], :name => 'delayed_jobs_priority' + add_index :delayed_jobs, [:priority, :run_at], name: 'delayed_jobs_priority' end def self.down diff --git a/spec/delayed/backend/sequel_spec.rb b/spec/delayed/backend/sequel_spec.rb index 4b11aae..3f96b6c 100644 --- a/spec/delayed/backend/sequel_spec.rb +++ b/spec/delayed/backend/sequel_spec.rb @@ -49,10 +49,10 @@ described_class.create(failed_at: Time.now) expect do expect( - Delayed::Job.count(:conditions => "failed_at is not NULL") + Delayed::Job.count(conditions: "failed_at is not NULL") ).to eq 1 expect( - Delayed::Job.count(:conditions => "locked_by is not NULL") + Delayed::Job.count(conditions: "locked_by is not NULL") ).to eq 0 end.to_not raise_error end @@ -62,10 +62,10 @@ described_class.create(queue: "important", priority: 1) expect do expect( - Delayed::Job.count(:group => "queue", :conditions => ['run_at < ? and failed_at is NULL', Time.now]) + Delayed::Job.count(group: "queue", conditions: ['run_at < ? and failed_at is NULL', Time.now]) ).to match_array [["slow", 1], ["important", 1]] expect( - Delayed::Job.count(:group => "priority", :conditions => ['run_at < ? and failed_at is NULL', Time.now]) + Delayed::Job.count(group: "priority", conditions: ['run_at < ? and failed_at is NULL', Time.now]) ).to match_array [[1, 1], [2, 1]] end.to_not raise_error end @@ -107,7 +107,7 @@ describe "enqueue" do it "should allow enqueue hook to modify job at DB level" do later = described_class.db_time_now + 20.minutes - job = Delayed::Backend::Sequel::Job.enqueue :payload_object => EnqueueJobMod.new + job = Delayed::Backend::Sequel::Job.enqueue payload_object: EnqueueJobMod.new expect( Delayed::Backend::Sequel::Job[job.id].run_at ).to be_within(1).of(later) @@ -117,14 +117,14 @@ describe Delayed::Backend::Sequel::Job, "override table name" do it "allows to override the table name" do - ::Sequel::Model.db.transaction :rollback => :always do + ::Sequel::Model.db.transaction rollback: :always do begin DB.create_table :another_delayed_jobs do primary_key :id - Integer :priority, :default => 0 - Integer :attempts, :default => 0 - String :handler, :text => true - String :last_error, :text => true + Integer :priority, default: 0 + Integer :attempts, default: 0 + String :handler, text: true + String :last_error, text: true Time :run_at Time :locked_at Time :failed_at diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 03d6ebc..49625d3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,28 +11,49 @@ def jruby? (defined?(RUBY_ENGINE) && RUBY_ENGINE=="jruby") || defined?(JRUBY_VERSION) end -DB = case ENV["DB"] -when "mysql" +db_host = ENV.fetch("TEST_DATABASE_HOST", "127.0.0.1") +db_name = ENV.fetch("TEST_DATABASE", "delayed_jobs_test") + +DB = case ENV["TEST_ADAPTER"] +when /mysql/ + db_port = ENV.fetch("TEST_DATABASE_PORT", 3306) + opts = {test: true, encoding: ENV.fetch("TEST_ENCODING", "utf8")} begin if jruby? - Sequel.connect "jdbc:mysql://localhost/delayed_jobs", test: true + Sequel.connect "jdbc:mysql://#{db_host}:#{db_port}/#{db_name}", opts else - Sequel.connect adapter: "mysql2", database: "delayed_jobs", test: true + opts.merge!({database: db_name, host: db_host, port: db_port}) + opts[:user] = ENV["TEST_USERNAME"] if ENV.key?("TEST_USERNAME") + opts[:password] = ENV["TEST_PASSWORD"] if ENV.key?("TEST_PASSWORD") + Sequel.connect({adapter: "mysql2"}.merge(opts)) end rescue Sequel::DatabaseConnectionError - system "mysql -e 'CREATE DATABASE IF NOT EXISTS `delayed_jobs` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci'" - retry + if ENV.key? "CI" + raise + else + system "mysql -e 'CREATE DATABASE IF NOT EXISTS `#{db_name}` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci'" + retry + end end -when "postgres" +when /postgres/ + db_port = ENV.fetch("TEST_DATABASE_PORT", 5432) + opts = {test: true, encoding: ENV.fetch("TEST_ENCODING", nil)} begin if jruby? - Sequel.connect "jdbc:postgresql://localhost/delayed_jobs", test: true + Sequel.connect "jdbc:postgresql://#{db_host}:#{db_port}/#{db_name}", opts else - Sequel.connect adapter: "postgres", database: "delayed_jobs", test: true + opts.merge!({database: db_name, host: db_host, port: db_port}) + opts[:user] = ENV["TEST_USERNAME"] if ENV.key?("TEST_USERNAME") + opts[:password] = ENV["TEST_PASSWORD"] if ENV.key?("TEST_PASSWORD") + Sequel.connect({adapter: "postgres"}.merge(opts)) end rescue Sequel::DatabaseConnectionError - system "createdb --encoding=UTF8 delayed_jobs" - retry + if ENV.key? "CI" + raise + else + system "createdb --encoding=UTF8 #{db_name}" + retry + end end else if jruby? @@ -47,10 +68,10 @@ def jruby? DB.create_table :delayed_jobs do primary_key :id - Integer :priority, :default => 0 - Integer :attempts, :default => 0 - String :handler, :text => true - String :last_error, :text => true + Integer :priority, default: 0 + Integer :attempts, default: 0 + String :handler, text: true + String :last_error, text: true Time :run_at Time :locked_at Time :failed_at @@ -63,7 +84,7 @@ def jruby? DB.create_table :stories do primary_key :story_id String :text - TrueClass :scoped, :default => true + TrueClass :scoped, default: true end require "delayed_job_sequel"