-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEV-993 Remove rights_database Dependency
- Add `Database` class based on `rights_database` repo/gem. - Change `RIGHTS_DATABASE_CONNECTION_STRING` to `RIGHTS_API_DATABASE_CONNECTION_STRING` for consistency. - Internal consistency and transparency tweaks: - De-abbreviate `Services[:db_connection` to `Services[:database_connection]`. - Re-order `SUPPORT_TABLES` to keep it alphabetical - Move rspec boilerplate to bottom of `spec_helper.rb` - Add `climate_control` gem for manipulating `ENV` in new database specs.
- Loading branch information
Showing
10 changed files
with
124 additions
and
81 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,46 @@ | ||
# frozen_string_literal: true | ||
|
||
require "sequel" | ||
|
||
module RightsAPI | ||
class Database | ||
# .connect will take | ||
# * a full connection string (passed here OR in the environment | ||
# variable RIGHTS_API_DATABASE_CONNECTION_STRING) | ||
# * a set of named arguments, drawn from those passed in and the | ||
# environment. Arguments are those supported by Sequel. | ||
# | ||
# Environment variables are mapped as follows: | ||
# | ||
# user: RIGHTS_API_DATABASE_USER | ||
# password: RIGHTS_API_DATABASE_PASSWORD | ||
# host: RIGHTS_API_DATABASE_HOST | ||
# port: RIGHTS_API_DATABASE_PORT | ||
# database: RIGHTS_API_DATABASE_DATABASE | ||
# adapter: RIGHTS_API_DATABASE_ADAPTER | ||
def connect(connection_string = nil, **) | ||
return @connection if @connection | ||
|
||
connection_string ||= ENV["RIGHTS_API_DATABASE_CONNECTION_STRING"] | ||
if connection_string.nil? | ||
db_args = gather_args(**) | ||
Sequel.connect(**db_args) | ||
else | ||
Sequel.connect(connection_string) | ||
end | ||
end | ||
|
||
private | ||
|
||
def gather_args(**args) | ||
%i[user password host port database adapter].each do |arg| | ||
args[arg] ||= ENV["RIGHTS_API_DATABASE_#{arg.to_s.upcase}"] | ||
end | ||
|
||
args[:host] ||= "localhost" | ||
args[:adapter] ||= :mysql2 | ||
args[:database] ||= "ht" | ||
args | ||
end | ||
end | ||
end |
This file contains 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 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 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 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 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,46 @@ | ||
# frozen_string_literal: true | ||
|
||
require "climate_control" | ||
|
||
RSpec.describe RightsAPI::Database do | ||
describe "#initialize" do | ||
it "creates RightsAPI::Database instance" do | ||
expect(described_class.new).to be_an_instance_of(RightsAPI::Database) | ||
end | ||
end | ||
|
||
describe "#connect" do | ||
it "connects with built-in connection string" do | ||
expect(described_class.new).not_to be nil | ||
end | ||
|
||
it "connects with explicit connection string" do | ||
expect(described_class.new.connect(ENV["RIGHTS_API_DATABASE_CONNECTION_STRING"])).not_to be nil | ||
end | ||
|
||
it "connects with connection arguments" do | ||
ClimateControl.modify(RIGHTS_API_DATABASE_CONNECTION_STRING: nil) do | ||
args = { | ||
user: "ht_rights", | ||
password: "ht_rights", | ||
host: "mariadb", | ||
database: "ht", | ||
adapter: "mysql2" | ||
} | ||
expect(described_class.new.connect(**args)).not_to be nil | ||
end | ||
end | ||
|
||
it "connects with ENV variables" do | ||
env = {RIGHTS_API_DATABASE_CONNECTION_STRING: nil, | ||
RIGHTS_API_DATABASE_USER: "ht_rights", | ||
RIGHTS_API_DATABASE_PASSWORD: "ht_rights", | ||
RIGHTS_API_DATABASE_HOST: "mariadb", | ||
RIGHTS_API_DATABASE_DATABASE: "ht", | ||
RIGHTS_API_DATABASE_ADAPTER: "mysql2"} | ||
ClimateControl.modify(**env) do | ||
expect(described_class.new.connect).not_to be nil | ||
end | ||
end | ||
end | ||
end |