Skip to content

Instantly share code, notes, and snippets.

@thisismydesign
Last active May 18, 2018 16:49
Show Gist options
  • Save thisismydesign/a3e1ef5adbd39740ea96ee6e5f77f01d to your computer and use it in GitHub Desktop.
Save thisismydesign/a3e1ef5adbd39740ea96ee6e5f77f01d to your computer and use it in GitHub Desktop.
Rails#32910 SQLite3 uses different root paths for db:create and db:drop tasks
# frozen_string_literal: true
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
a = gemfile(true) do
source "https://rubygems.org"
gem "activerecord", "5.1.6"
gem "sqlite3", "1.3.13"
gem "rake", "~> 10.0"
gem "rspec", "~> 3.0"
end
require "active_record"
require "rspec"
require "rake"
require "fileutils"
def load_active_record_tasks(database_configuration:, root:, db_dir: root, migrations_paths: [root], env: "development", seed_loader: nil)
include ActiveRecord::Tasks
DatabaseTasks.database_configuration = database_configuration
DatabaseTasks.root = root
DatabaseTasks.db_dir = db_dir
DatabaseTasks.migrations_paths = migrations_paths
DatabaseTasks.env = env
DatabaseTasks.seed_loader = seed_loader
task :environment do
ActiveRecord::Base.configurations = DatabaseTasks.database_configuration
ActiveRecord::Base.establish_connection DatabaseTasks.env.to_sym
end
load 'active_record/railties/databases.rake'
end
root = Pathname.new(File.dirname(__FILE__)).join("example")
FileUtils.mkdir(root) rescue Errno::EEXIST
config = {
"development" => {
"adapter" => "sqlite3",
"database" => "development.sqlite3"
}
}
load_active_record_tasks(database_configuration: config, root: root)
RSpec.configure do |config|
config.formatter = "documentation"
end
RSpec.describe "sqlite adapter issue", :aggregate_failures do
let(:db_relative) { config["development"]["database"] }
let(:db_root) { root.join(config["development"]["database"]) }
before do
FileUtils.rm(db_relative) rescue Errno::ENOENT
FileUtils.rm(db_root) rescue Errno::ENOENT
Rake::Task["db:create"].reenable
Rake::Task["db:drop"].reenable
Rake::Task["db:drop:_unsafe"].reenable
end
after do
FileUtils.rm(db_relative) rescue Errno::ENOENT
FileUtils.rm(db_root) rescue Errno::ENOENT
end
describe "first correct scenario would be if" do
it "places DB relative to execution" do
expect { Rake::Task["db:create"].invoke }.to change { File.file?(db_relative) }.from(false).to(true)
end
it "removes DB from relative to execution" do
FileUtils.touch(db_relative)
expect { Rake::Task["db:drop"].invoke }.to change { File.file?(db_relative) }.from(true).to(false)
end
end
describe "second correct scenario would be if" do
it "places DB relative to root path" do
expect { Rake::Task["db:create"].invoke }.to change { File.file?(db_root) }.from(false).to(true)
end
it "removes DB from relative to root path" do
FileUtils.touch(db_root)
expect { Rake::Task["db:drop"].invoke }.to change { File.file?(db_root) }.from(true).to(false)
end
end
end
$ rspec _rails#32910.rb
Fetching gem metadata from https://rubygems.org/........
Resolving dependencies...
Using rake 10.5.0
Using concurrent-ruby 1.0.5
Using i18n 1.0.1
Using minitest 5.11.3
Using thread_safe 0.3.6
Using tzinfo 1.2.5
Using activesupport 5.1.6
Using activemodel 5.1.6
Using arel 8.0.0
Using activerecord 5.1.6
Using bundler 1.16.0
Using diff-lcs 1.3
Using rspec-support 3.7.1
Using rspec-core 3.7.1
Using rspec-expectations 3.7.0
Using rspec-mocks 3.7.0
Using rspec 3.7.0
Using sqlite3 1.3.13
sqlite adapter issue
first correct scenario would be if
Created database 'development.sqlite3'
places DB relative to execution
Database 'development.sqlite3' does not exist
removes DB from relative to execution (FAILED - 1)
second correct scenario would be if
Created database 'development.sqlite3'
places DB relative to root path (FAILED - 2)
Dropped database 'development.sqlite3'
removes DB from relative to root path
Failures:
1) sqlite adapter issue first correct scenario would be if removes DB from relative to execution
Failure/Error: expect { Rake::Task["db:drop"].invoke }.to change { File.file?(db_relative) }.from(true).to(false)
expected `File.file?(db_relative)` to have changed from true to false, but did not change
# ./_rails#32910.rb:80:in `block (3 levels) in <top (required)>'
2) sqlite adapter issue second correct scenario would be if places DB relative to root path
Failure/Error: expect { Rake::Task["db:create"].invoke }.to change { File.file?(db_root) }.from(false).to(true)
expected `File.file?(db_root)` to have changed from false to true, but did not change
# ./_rails#32910.rb:86:in `block (3 levels) in <top (required)>'
Finished in 0.08838 seconds (files took 2.85 seconds to load)
4 examples, 2 failures
Failed examples:
rspec ./_rails#32910.rb:78 # sqlite adapter issue first correct scenario would be if removes DB from relative to execution
rspec ./_rails#32910.rb:85 # sqlite adapter issue second correct scenario would be if places DB relative to root path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment