-
Notifications
You must be signed in to change notification settings - Fork 13
Extract SQLite into an adapter #204
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
+269
−46
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
31ffb94
Extract SQLite into an adapter
2876422
Greatly simplify acquire_lock
ae00036
Refine locking even more and fix mistake
3971d83
Merge branch 'main' into sqlite-adapter
0b790d0
Remove uneeded rescue
d3d9a64
Add specific error message for using an unsupported database
b23cd00
Rename database_exists? to database_exist?
8e3d6bf
More renaming
43a4908
Split database_exist? and database_ready? to make it simpler
10d478d
Simplify database adapter_for call
3f861fe
More cleanup
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveRecord | ||
module Tenanted | ||
class DatabaseAdapter # :nodoc: | ||
ADAPTERS = { | ||
"sqlite3" => "ActiveRecord::Tenanted::DatabaseAdapters::SQLite", | ||
}.freeze | ||
|
||
class << self | ||
def create_database(db_config) | ||
adapter_for(db_config).create_database | ||
end | ||
|
||
def drop_database(db_config) | ||
adapter_for(db_config).drop_database | ||
end | ||
|
||
def database_exist?(db_config) | ||
adapter_for(db_config).database_exist? | ||
end | ||
|
||
def database_ready?(db_config) | ||
adapter_for(db_config).database_ready? | ||
end | ||
|
||
def acquire_ready_lock(db_config, &block) | ||
adapter_for(db_config).acquire_ready_lock(db_config, &block) | ||
end | ||
|
||
def tenant_databases(db_config) | ||
adapter_for(db_config).tenant_databases | ||
end | ||
|
||
def validate_tenant_name(db_config, tenant_name) | ||
adapter_for(db_config).validate_tenant_name(tenant_name) | ||
end | ||
|
||
def adapter_for(db_config) | ||
adapter_class_name = ADAPTERS[db_config.adapter] | ||
|
||
if adapter_class_name.nil? | ||
raise ActiveRecord::Tenanted::UnsupportedDatabaseError, | ||
"Unsupported database adapter for tenanting: #{db_config.adapter}. " \ | ||
"Supported adapters: #{ADAPTERS.keys.join(', ')}" | ||
end | ||
|
||
adapter_class_name.constantize.new(db_config) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveRecord | ||
module Tenanted | ||
module DatabaseAdapters | ||
class SQLite # :nodoc: | ||
def initialize(db_config) | ||
@db_config = db_config | ||
end | ||
|
||
def create_database | ||
# Ensure the directory exists | ||
database_dir = File.dirname(database_path) | ||
FileUtils.mkdir_p(database_dir) unless File.directory?(database_dir) | ||
flavorjones marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Create the SQLite database file | ||
FileUtils.touch(database_path) | ||
end | ||
|
||
def drop_database | ||
# Remove the SQLite database file and associated files | ||
FileUtils.rm_f(database_path) | ||
FileUtils.rm_f("#{database_path}-wal") # Write-Ahead Logging file | ||
FileUtils.rm_f("#{database_path}-shm") # Shared Memory file | ||
andrewmarkle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
def database_exist? | ||
File.exist?(database_path) | ||
end | ||
|
||
def database_ready? | ||
File.exist?(database_path) && !ActiveRecord::Tenanted::Mutex::Ready.locked?(database_path) | ||
end | ||
|
||
def tenant_databases | ||
glob = db_config.database_path_for("*") | ||
scanner = Regexp.new(db_config.database_path_for("(.+)")) | ||
|
||
Dir.glob(glob).filter_map do |path| | ||
andrewmarkle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result = path.scan(scanner).flatten.first | ||
if result.nil? | ||
Rails.logger.warn "ActiveRecord::Tenanted: Cannot parse tenant name from filename #{path.inspect}" | ||
end | ||
result | ||
end | ||
end | ||
|
||
def acquire_ready_lock(db_config, &block) | ||
ActiveRecord::Tenanted::Mutex::Ready.lock(database_path, &block) | ||
end | ||
|
||
def validate_tenant_name(tenant_name) | ||
if tenant_name.match?(%r{[/'"`]}) | ||
raise BadTenantNameError, "Tenant name contains an invalid character: #{tenant_name.inspect}" | ||
end | ||
end | ||
|
||
private | ||
attr_reader :db_config | ||
|
||
def database_path | ||
db_config.database_path | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
describe ActiveRecord::Tenanted::DatabaseAdapter do | ||
describe ".adapter_for" do | ||
test "selects correct adapter for sqlite3" do | ||
adapter = ActiveRecord::Tenanted::DatabaseAdapter.adapter_for(create_config("sqlite3")) | ||
assert_instance_of ActiveRecord::Tenanted::DatabaseAdapters::SQLite, adapter | ||
end | ||
|
||
test "raises error for unsupported adapter" do | ||
unsupported_config = create_config("mongodb") | ||
|
||
error = assert_raises ActiveRecord::Tenanted::UnsupportedDatabaseError do | ||
ActiveRecord::Tenanted::DatabaseAdapter.adapter_for(unsupported_config) | ||
end | ||
|
||
assert_includes error.message, "Unsupported database adapter for tenanting: mongodb." | ||
end | ||
end | ||
|
||
describe "delegation" do | ||
ActiveRecord::Tenanted::DatabaseAdapter::ADAPTERS.each do |adapter, adapter_class_name| | ||
test "#{adapter} .create_database calls adapter's #create_database" do | ||
adapter_mock = Minitest::Mock.new | ||
adapter_mock.expect(:create_database, nil) | ||
|
||
adapter_class_name.constantize.stub(:new, adapter_mock) do | ||
ActiveRecord::Tenanted::DatabaseAdapter.create_database(create_config(adapter)) | ||
end | ||
|
||
assert_mock adapter_mock | ||
end | ||
|
||
test "#{adapter} .drop_database calls adapter's #drop_database" do | ||
adapter_mock = Minitest::Mock.new | ||
adapter_mock.expect(:drop_database, nil) | ||
|
||
adapter_class_name.constantize.stub(:new, adapter_mock) do | ||
ActiveRecord::Tenanted::DatabaseAdapter.drop_database(create_config(adapter)) | ||
end | ||
|
||
assert_mock adapter_mock | ||
end | ||
|
||
test "#{adapter} .database_exist? calls adapter's #database_exist?" do | ||
adapter_mock = Minitest::Mock.new | ||
adapter_mock.expect(:database_exist?, true) | ||
|
||
result = adapter_class_name.constantize.stub(:new, adapter_mock) do | ||
ActiveRecord::Tenanted::DatabaseAdapter.database_exist?(create_config(adapter)) | ||
end | ||
|
||
assert_equal true, result | ||
assert_mock adapter_mock | ||
end | ||
|
||
test "#{adapter} .database_ready? calls adapter's #database_ready?" do | ||
adapter_mock = Minitest::Mock.new | ||
adapter_mock.expect(:database_ready?, true) | ||
|
||
result = adapter_class_name.constantize.stub(:new, adapter_mock) do | ||
ActiveRecord::Tenanted::DatabaseAdapter.database_ready?(create_config(adapter)) | ||
end | ||
|
||
assert_equal true, result | ||
assert_mock adapter_mock | ||
end | ||
|
||
test "#{adapter} .tenant_databases calls adapter's #tenant_databases" do | ||
adapter_mock = Minitest::Mock.new | ||
adapter_mock.expect(:tenant_databases, [ "foo", "bar" ]) | ||
|
||
result = adapter_class_name.constantize.stub(:new, adapter_mock) do | ||
ActiveRecord::Tenanted::DatabaseAdapter.tenant_databases(create_config(adapter)) | ||
end | ||
|
||
assert_equal [ "foo", "bar" ], result | ||
assert_mock adapter_mock | ||
end | ||
|
||
test "#{adapter} .validate_tenant_name calls adapter's #validate_tenant_name" do | ||
adapter_mock = Minitest::Mock.new | ||
adapter_mock.expect(:validate_tenant_name, nil, [ "tenant1" ]) | ||
|
||
adapter_class_name.constantize.stub(:new, adapter_mock) do | ||
ActiveRecord::Tenanted::DatabaseAdapter.validate_tenant_name(create_config(adapter), "tenant1") | ||
end | ||
|
||
assert_mock adapter_mock | ||
end | ||
|
||
test "#{adapter} .acquire_ready_lock calls adapter's #acquire_ready_lock" do | ||
fake_adapter = Object.new | ||
fake_adapter.define_singleton_method(:acquire_ready_lock) do |id, &blk| | ||
blk&.call | ||
end | ||
|
||
yielded = false | ||
result = adapter_class_name.constantize.stub(:new, fake_adapter) do | ||
ActiveRecord::Tenanted::DatabaseAdapter.acquire_ready_lock(create_config(adapter)) { yielded = true; :ok } | ||
end | ||
|
||
assert_equal true, yielded | ||
assert_equal :ok, result | ||
end | ||
end | ||
end | ||
|
||
private | ||
def create_config(adapter) | ||
ActiveRecord::DatabaseConfigurations::HashConfig.new( | ||
"test", | ||
"test_config", | ||
{ | ||
adapter: adapter, | ||
database: "db_name", | ||
} | ||
) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.