sequelizer 0.1.0 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8d676f724540c01e7cbd2bc6b2a354d78a2e9d15
4
- data.tar.gz: e6872a70bdf5d268687f667273edced41abd13aa
2
+ SHA256:
3
+ metadata.gz: 4cfd3663b92545f1bffb3c811297f81f26a2a1c237bc621c31d22d905be4bc0b
4
+ data.tar.gz: 1865666d4f264967f909820d9c7572dfff2b74bc3c80219c589c8197f18e5e59
5
5
  SHA512:
6
- metadata.gz: 8fb25153b51b05926e5da76fd977f88b8b3077b9dccedb6282edd40486bea4bc1d1e6be70fd8a576f2102a80c406663b40ef396a6ad32897730fd510cd6bddcd
7
- data.tar.gz: ace9f653827f44fac941f1bbd6b2e6e854d3128bcf8d3a8f14343304b5f7350f2e6efe1c7ee11271cd3aa6f8f659bd6dd7b19145223372a3eecac56ac33aa4e7
6
+ metadata.gz: c02b029eb0bf30bfb6498d75fbde32aa7a1576074c74dd55993a91a3f4d6458c53e0e1ed45c16d3177767ddde7b44ce706cbafce0269681f025acbe80f46ea35
7
+ data.tar.gz: f123e5d48c8178d9b5609f6f44d9744fb00f31085b11364aa839b902784f9de60beedabc7b90a7c6e4ae6b84a62d9d9a1b338190a0597fec40107518632b94f8
data/.gitignore CHANGED
@@ -15,3 +15,21 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+
19
+ # Created by https://www.gitignore.io/api/vim
20
+
21
+ ### Vim ###
22
+ # swap
23
+ [._]*.s[a-v][a-z]
24
+ [._]*.sw[a-p]
25
+ [._]s[a-v][a-z]
26
+ [._]sw[a-p]
27
+ # session
28
+ Session.vim
29
+ # temporary
30
+ .netrwhist
31
+ *~
32
+ # auto-generated tag files
33
+ tags
34
+
35
+ # End of https://www.gitignore.io/api/vim
data/.travis.yml ADDED
@@ -0,0 +1,21 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.4
4
+ sudo: false
5
+ env:
6
+ secure: xFZ00J+axPQADZ2Dqc61Ljo56vT8/uwVzOFw9zxKQs3ThMfI8+OvlI7LV8sf0XPC5Cp+BObYCxzsqIOY8M8yo+NfknlNmlrQaPqi3lf+3tKvEFeFiQZ/jvbGReIdxRdPViVls1W3zEDdRwe9zUAiz7C+xBYCRfZRoGjfm7gx/4c=
7
+ before_install:
8
+ - gem install bundler
9
+ - bundle
10
+ script: bundle exec rake test
11
+ jobs:
12
+ include:
13
+ - stage: deploy
14
+ before_install: gem install tping
15
+ install: true
16
+ script:
17
+ - tping --token $TRAVIS_PRO_TOKEN --user outcomesinsights --repo t_shank --pro --branch $TRAVIS_BRANCH
18
+ - tping --token $TRAVIS_PRO_TOKEN --user outcomesinsights --repo jigsaw-diagram-editor --pro --branch $TRAVIS_BRANCH
19
+ notifications:
20
+ slack:
21
+ secure: b+ao+3BuBtM5nj/m1gP5AbrrTIdQiV/HkT1deXvY4gg5xZQDheDeLmOI7wSFP1o67BrwrAY7rpcwIP7S/99fudrU3rKI3+GLn8KoefdAv78Z4tsMs9rodJJ3Z3ZmnEdMK2i2+hCLJ1pzZ9Ae3e+GDHsBPkTz4+TNE1lrOPxDIUo=
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Sequelizer
1
+ # Sequelizer [![Build Status](https://travis-ci.org/outcomesinsights/sequelizer.svg?branch=master)](https://travis-ci.org/outcomesinsights/sequelizer)
2
2
 
3
3
  I was tired of writing the code to bootstrap a connection to my databases.
4
4
 
data/Rakefile CHANGED
@@ -1 +1,10 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs = ["lib"]
6
+ t.warning = true
7
+ t.test_files = FileList['test/**/test_*.rb']
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,41 @@
1
+ module Sequel
2
+ module DbOpts
3
+ class DbOptions
4
+ attr :db
5
+ def initialize(db)
6
+ db.extension :settable
7
+ @db = db
8
+ end
9
+
10
+ def to_hash
11
+ @_to_hash ||= extract_db_opts
12
+ end
13
+
14
+ def extract_db_opts
15
+ opt_regexp = /^#{db.database_type}_db_opt_/i
16
+
17
+ Hash[db.opts.select { |k, _| k.to_s.match(opt_regexp) }.map { |k, v| [k.to_s.gsub(opt_regexp, '').to_sym, prep_value(k, v)] }]
18
+ end
19
+
20
+ def apply(c)
21
+ sql_statements.each do |stmt|
22
+ db.send(:log_connection_execute, c, stmt)
23
+ end
24
+ end
25
+
26
+ def prep_value(k, v)
27
+ v =~ /\W/ ? db.literal("#{v}") : v
28
+ end
29
+
30
+ def sql_statements
31
+ db.send(:set_sql, to_hash)
32
+ end
33
+ end
34
+
35
+ def db_opts
36
+ @db_opts ||= DbOptions.new(self)
37
+ end
38
+ end
39
+
40
+ Database.register_extension(:db_opts, DbOpts)
41
+ end
@@ -0,0 +1,17 @@
1
+ module Sequel
2
+ module Settable
3
+ def set(opts = {})
4
+ set_sql(opts).each do |sql|
5
+ run(sql)
6
+ end
7
+ end
8
+
9
+ private
10
+
11
+ def set_sql(opts)
12
+ opts.map { |k, v| "SET #{k}=#{v}" }
13
+ end
14
+ end
15
+
16
+ Database.register_extension(:settable, Settable)
17
+ end
@@ -50,7 +50,9 @@ module Sequelizer
50
50
 
51
51
  desc 'config', 'prints out the connection parameters'
52
52
  def config
53
- pp Options.new.to_hash
53
+ opts = Options.new
54
+ pp opts.to_hash
55
+ pp opts.extensions
54
56
  end
55
57
 
56
58
  private
@@ -1,4 +1,5 @@
1
1
  require 'sequel'
2
+ require 'cgi'
2
3
  require_relative 'options'
3
4
 
4
5
  module Sequelizer
@@ -23,11 +24,74 @@ module Sequelizer
23
24
  # Returns a Sequel connection to the database
24
25
  def connection
25
26
  opts = options.to_hash
26
- if url = (opts.delete(:uri) || opts.delete(:url))
27
+ extensions = options.extensions
28
+
29
+ conn = if url = (opts.delete(:uri) || opts.delete(:url))
27
30
  Sequel.connect(url, opts)
28
31
  else
29
- Sequel.connect(options.to_hash)
32
+ # Kerberos related options
33
+ realm = opts[:realm]
34
+ host_fqdn = opts[:host_fqdn] || opts[:host]
35
+ principal = opts[:principal]
36
+
37
+ adapter = opts[:adapter]
38
+ if adapter =~ /\Ajdbc_/
39
+ user = opts[:user]
40
+ password = opts[:password]
41
+ end
42
+
43
+ case opts[:adapter] && opts[:adapter].to_sym
44
+ when :jdbc_hive2
45
+ opts[:adapter] = :jdbc
46
+ auth = if realm
47
+ ";principal=#{e principal}/#{e host_fqdn}@#{e realm}"
48
+ elsif user
49
+ ";user=#{e user};password=#{e password}"
50
+ else
51
+ ';auth=noSasl'
52
+ end
53
+ opts[:uri] = "jdbc:hive2://#{e opts[:host]}:#{opts.fetch(:port, 21050).to_i}/#{e(opts[:database] || 'default')}#{auth}"
54
+ when :jdbc_impala
55
+ opts[:adapter] = :jdbc
56
+ auth = if realm
57
+ ";AuthMech=1;KrbServiceName=#{e principal};KrbAuthType=2;KrbHostFQDN=#{e host_fqdn};KrbRealm=#{e realm}"
58
+ elsif user
59
+ if password
60
+ ";AuthMech=3;UID=#{e user};PWD=#{e password}"
61
+ else
62
+ ";AuthMech=2;UID=#{e user}"
63
+ end
64
+ end
65
+ opts[:uri] = "jdbc:impala://#{e opts[:host]}:#{opts.fetch(:port, 21050).to_i}/#{e(opts[:database] || 'default')}#{auth}"
66
+ when :jdbc_postgres
67
+ opts[:adapter] = :jdbc
68
+ auth = "?user=#{user}#{"&password=#{password}" if password}" if user
69
+ opts[:uri] = "jdbc:postgresql://#{e opts[:host]}:#{opts.fetch(:port, 5432).to_i}/#{e(opts[:database])}#{auth}"
70
+ when :impala
71
+ opts[:database] ||= 'default'
72
+ opts[:port] ||= 21000
73
+ if principal
74
+ # realm doesn't seem to be used?
75
+ opts[:transport] = :sasl
76
+ opts[:sasl_params] = {
77
+ mechanism: "GSSAPI",
78
+ remote_host: host_fqdn,
79
+ remote_principal: principal
80
+ }
81
+ end
82
+ end
83
+
84
+ Sequel.connect(opts)
30
85
  end
86
+ conn.extension(*extensions)
87
+ conn
88
+ end
89
+
90
+ private
91
+
92
+ def e(v)
93
+ CGI.escape(v.to_s)
31
94
  end
32
95
  end
33
96
  end
97
+
@@ -9,12 +9,20 @@ module Sequelizer
9
9
  # as an option for the database
10
10
  def options
11
11
  Dotenv.load
12
- env_config = ENV.keys.select { |key| key =~ /^SEQUELIZER_/ }.inject({}) do |config, key|
12
+
13
+ seq_config = ENV.keys.select { |key| key =~ /^SEQUELIZER_/ }.inject({}) do |config, key|
13
14
  new_key = key.gsub(/^SEQUELIZER_/, '').downcase
14
15
  config[new_key] = ENV[key]
15
16
  config
16
17
  end
17
- env_config.empty? ? {} : env_config
18
+
19
+ db_config = ENV.keys.select { |key| key =~ /_DB_OPT_/ }.inject({}) do |config, key|
20
+ new_key = key.downcase
21
+ config[new_key] = ENV[key]
22
+ config
23
+ end
24
+
25
+ db_config.merge(seq_config)
18
26
  end
19
27
  end
20
28
  end
@@ -0,0 +1,26 @@
1
+ module Sequel
2
+ class ConnectionPool
3
+ # Return a new connection by calling the connection proc with the given server name,
4
+ # and checking for connection errors.
5
+ def make_new(server)
6
+ begin
7
+ conn = @db.connect(server)
8
+ if ac = @after_connect
9
+ case ac.arity
10
+ when 3
11
+ ac.call(conn, server, @db)
12
+ when 2
13
+ ac.call(conn, server)
14
+ else
15
+ ac.call(conn)
16
+ end
17
+ end
18
+ rescue Exception=>exception
19
+ raise Sequel.convert_exception_class(exception, Sequel::DatabaseConnectionError)
20
+ end
21
+ raise(Sequel::DatabaseConnectionError, "Connection parameters not valid") unless conn
22
+ conn
23
+ end
24
+ end
25
+ end
26
+
@@ -1,11 +1,13 @@
1
- require 'sequelizer/yaml_config'
2
- require 'sequelizer/env_config'
3
- require 'sequelizer/options_hash'
1
+ require_relative 'yaml_config'
2
+ require_relative 'env_config'
3
+ require_relative 'options_hash'
4
4
 
5
5
  module Sequelizer
6
6
  class Options
7
+ attr :extensions
7
8
  def initialize(options = nil)
8
- @options = fix_options(options)
9
+ opts = fix_options(options)
10
+ @options, @extensions = filter_extensions(opts)
9
11
  end
10
12
 
11
13
  def to_hash
@@ -20,6 +22,16 @@ module Sequelizer
20
22
 
21
23
  private
22
24
 
25
+ def make_ac(opts)
26
+ Proc.new do |conn, server, db|
27
+ if ac = opts[:after_connect]
28
+ ac.arity == 2 ? ac.call(conn, server) : ac.call(conn)
29
+ end
30
+ db.extension :db_opts
31
+ db.db_opts.apply(conn)
32
+ end
33
+ end
34
+
23
35
  # If passed a hash, scans hash for certain options and sets up hash
24
36
  # to be fed to Sequel.connect
25
37
  #
@@ -27,7 +39,8 @@ module Sequelizer
27
39
  # the string is returned without modification
28
40
  def fix_options(passed_options)
29
41
  return passed_options unless passed_options.nil? || passed_options.is_a?(Hash)
30
- sequelizer_options = db_config.merge(OptionsHash.new(passed_options || {}).to_hash)
42
+ opts = OptionsHash.new(passed_options || {}).to_hash
43
+ sequelizer_options = db_config(opts).merge(opts)
31
44
 
32
45
  if sequelizer_options[:adapter] =~ /^postgres/
33
46
  sequelizer_options[:adapter] = 'postgres'
@@ -46,18 +59,19 @@ module Sequelizer
46
59
  sequelizer_options.merge!(timeout: sequelizer_options[:timeout].to_i)
47
60
  end
48
61
 
49
- sequelizer_options
62
+ sequelizer_options.merge(after_connect: make_ac(sequelizer_options))
50
63
  end
51
64
 
52
65
  # Grabs the database options from
53
66
  # - ~/.config/sequelizer.yml if it exists
54
67
  # - config/database.yml if it exists
55
68
  # - environment variables (also reads from .env)
56
- def db_config
69
+ def db_config(opts)
57
70
  @db_config ||= begin
58
- opts = OptionsHash.new(YamlConfig.user_config.options)
59
- opts.merge!(YamlConfig.local_config.options)
60
- opts.merge!(EnvConfig.new.options)
71
+ opts = OptionsHash.new(opts)
72
+ opts.merge!(YamlConfig.user_config.options) unless opts[:ignore_yaml]
73
+ opts.merge!(YamlConfig.local_config.options) unless opts[:ignore_yaml]
74
+ opts.merge!(EnvConfig.new.options) unless opts[:ignore_env]
61
75
  opts
62
76
  end
63
77
  end
@@ -77,5 +91,15 @@ module Sequelizer
77
91
  conn.execute("SET search_path TO #{search_path}")
78
92
  end
79
93
  end
94
+
95
+ def filter_extensions(options)
96
+ extension_regexp = /^extension_/
97
+ extension_keys = options.keys.select { |k| k.to_s =~ extension_regexp }
98
+ extensions = extension_keys.map do |key|
99
+ options.delete(key)
100
+ key.to_s.gsub(extension_regexp, '').to_sym
101
+ end
102
+ [options, extensions]
103
+ end
80
104
  end
81
105
  end
@@ -1,4 +1,4 @@
1
1
  module Sequelizer
2
2
  # Version for the gem
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.3"
4
4
  end
@@ -16,7 +16,8 @@ module Sequelizer
16
16
  end
17
17
 
18
18
  def user_config_path
19
- Pathname.new("~") + ".config" + "sequelizer" + "database.yml"
19
+ return nil unless ENV['HOME']
20
+ Pathname.new(ENV['HOME']) + ".config" + "sequelizer" + "database.yml"
20
21
  end
21
22
  end
22
23
 
@@ -48,7 +49,7 @@ module Sequelizer
48
49
 
49
50
  # The config as read from config/database.yml
50
51
  def config
51
- @config ||= Psych.load(ERB.new(File.read(config_file_path)).result).tap { |c| p c }
52
+ @config ||= Psych.load(ERB.new(File.read(config_file_path)).result)
52
53
  end
53
54
  end
54
55
  end
data/lib/sequelizer.rb CHANGED
@@ -1,16 +1,23 @@
1
- require 'sequelizer/version'
2
- require 'sequelizer/connection_maker'
1
+ require_relative 'sequelizer/version'
2
+ require_relative 'sequelizer/connection_maker'
3
+ require_relative 'sequelizer/monkey_patches/database_in_after_connect'
4
+ require_relative 'sequel/extensions/db_opts'
5
+ require_relative 'sequel/extensions/settable'
3
6
 
4
7
  # Include this module in any class where you'd like to quickly establish
5
8
  # a Sequel connection to a database.
6
9
  module Sequelizer
10
+ def self.options
11
+ Options.new.to_hash
12
+ end
13
+
7
14
  # Instantiates and memoizes a database connection. The +db+ method instantiates
8
15
  # the connection on the first call and then memoizes itself so only a single
9
16
  # connection is used on repeated calls
10
17
  #
11
18
  # options :: an optional set of database connection options.
12
19
  # If no options are provided, options are read from
13
- # config/database.yml or from .env or from environment variables.
20
+ # config/sequelizer.yml or from .env or from environment variables.
14
21
  def db(options = {})
15
22
  @_sequelizer_db ||= new_db(options)
16
23
  end
@@ -19,7 +26,7 @@ module Sequelizer
19
26
  #
20
27
  # options :: an optional set of database connection options.
21
28
  # If no options are provided, options are read from
22
- # config/database.yml or from .env or from environment variables.
29
+ # config/sequelizer.yml or from .env or from environment variables.
23
30
  def new_db(options = {})
24
31
  cached = find_cached(options)
25
32
  return cached if cached && !options[:force_new]
data/sequelizer.gemspec CHANGED
@@ -18,12 +18,13 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency 'bundler', '~> 1.5'
21
+ spec.add_development_dependency 'bundler', '~> 2.0'
22
22
  spec.add_development_dependency 'guard', '~> 2.0'
23
23
  spec.add_development_dependency 'guard-minitest', '~> 2.3'
24
24
  spec.add_development_dependency 'minitest', '~> 5.3'
25
- spec.add_dependency 'sequel', '~> 4.12'
25
+ spec.add_development_dependency 'rake', '~> 12.0'
26
+ spec.add_dependency 'sequel', '~> 5.0'
26
27
  spec.add_dependency 'dotenv', '~> 2.1'
27
- spec.add_dependency 'thor', '~> 0.19'
28
+ spec.add_dependency 'thor', '~> 1.0'
28
29
  spec.add_dependency 'hashie', '~> 3.2'
29
30
  end
@@ -0,0 +1,40 @@
1
+ require_relative '../../../test_helper'
2
+ require 'sequel'
3
+ require 'sequel/extensions/db_opts'
4
+
5
+ class TestDbOpts < Minitest::Test
6
+ def with_fake_database_type_and_options(db_type, opts = {})
7
+ db = Sequel.mock
8
+ db.define_singleton_method(:database_type){db_type}
9
+ db.define_singleton_method(:opts){opts}
10
+ db.extension :db_opts
11
+ yield db
12
+ end
13
+
14
+ def sql_for(db_type, options)
15
+ with_fake_database_type_and_options(db_type, options) do |db|
16
+ db.synchronize do |conn|
17
+ db.db_opts.apply(conn)
18
+ return db.sqls
19
+ end
20
+ end
21
+ end
22
+
23
+ def test_should_detect_options_for_appropriate_db
24
+ assert_equal(sql_for(:postgres, postgres_db_opt_flim: :flam), ["SET flim=flam"])
25
+ end
26
+
27
+ def test_should_ignore_options_for_inappropriate_db
28
+ assert_equal(sql_for(:postgres, postgres_db_opt_flim: :flam, other_db_opt_foo: :bar), ["SET flim=flam"])
29
+ end
30
+
31
+ def test_should_ignore_non_db_opts
32
+ assert_equal(sql_for(:postgres, postgres_flim: :flam), [])
33
+ end
34
+
35
+ def test_should_properly_quote_awkward_values
36
+ assert_equal(sql_for(:postgres, postgres_db_opt_str: "hello there", postgres_db_opt_hyphen: "i-like-hyphens-though-they-are-dumb"),
37
+ ["SET str='hello there'", "SET hyphen='i-like-hyphens-though-they-are-dumb'"])
38
+ end
39
+ end
40
+
@@ -0,0 +1,119 @@
1
+ require_relative '../../test_helper'
2
+ require 'sequelizer'
3
+
4
+ class TestConnectionMaker < Minitest::Test
5
+ def setup
6
+ @options = { 'adapter' => 'mock', "host" => "postgres" }
7
+ end
8
+
9
+ def test_accepts_options_as_params
10
+ Sequelizer::YamlConfig.stub :user_config_path, Pathname.new('/completely/made/up/path/that/does/not/exist') do
11
+ assert_equal :postgres, Sequelizer::ConnectionMaker.new(@options).connection.database_type
12
+ end
13
+ end
14
+
15
+ def with_ignored_yaml_config(opts = {})
16
+ end
17
+
18
+
19
+ def with_yaml_config(options = {})
20
+ yaml_config = Sequelizer::YamlConfig.new
21
+ yaml_config.stub(:options, options) do
22
+ Sequelizer::YamlConfig.stub :new, yaml_config do
23
+ yield
24
+ end
25
+ end
26
+ end
27
+
28
+ def with_env_config(options = {})
29
+ env_config = Sequelizer::EnvConfig.new
30
+ env_config.stub(:options, options) do
31
+ Sequelizer::EnvConfig.stub :new, env_config do
32
+ yield env_config
33
+ end
34
+ end
35
+ end
36
+
37
+ def test_reads_options_from_yaml_config
38
+ with_yaml_config(@options) do
39
+ assert_equal :postgres, Sequelizer::ConnectionMaker.new.connection.database_type
40
+ end
41
+ end
42
+
43
+ def test_ignores_options_from_yaml_config_when_asked
44
+ with_yaml_config(@options) do
45
+ assert_nil Sequelizer::ConnectionMaker.new(ignore_yaml: true).options.to_hash[:adapter]
46
+ end
47
+ end
48
+
49
+ def test_applies_settings_if_given
50
+ with_yaml_config(@options.merge(postgres_db_opt_flim: :flam)) do
51
+ with_env_config do
52
+ conn = Sequelizer::ConnectionMaker.new.connection
53
+ conn.test_connection
54
+ assert_equal(["SET flim=flam"], conn.sqls)
55
+ end
56
+ end
57
+ end
58
+
59
+ def test_applies_settings_for_all_connections_if_given
60
+ with_yaml_config(@options.merge(postgres_db_opt_flim: :flam, max_connections: 2, preconnect: :concurrent)) do
61
+ with_env_config do
62
+ conn = Sequelizer::ConnectionMaker.new.connection
63
+ conn.test_connection
64
+ assert_equal(["SET flim=flam"] * 2, conn.sqls)
65
+ end
66
+ end
67
+ end
68
+
69
+ def test_reads_options_from_env_config_if_no_yaml_config
70
+ with_yaml_config do
71
+ with_env_config(@options) do
72
+ assert_equal :postgres, Sequelizer::ConnectionMaker.new.connection.database_type
73
+ end
74
+ end
75
+ end
76
+
77
+ def test_ignores_options_from_env_config_if_no_yaml_config
78
+ with_yaml_config do
79
+ with_env_config(@options) do
80
+ assert_nil Sequelizer::ConnectionMaker.new(ignore_env: true).options.to_hash[:adapter]
81
+ end
82
+ end
83
+ end
84
+
85
+ def test_applies_configuration_to_connection
86
+ opts = @options.merge(postgres_db_opt_search_path: "searchy", impala_db_opt_search_path: "searchy2")
87
+ with_yaml_config(opts) do
88
+ conn = Sequelizer::ConnectionMaker.new.connection
89
+ conn.test_connection
90
+ assert_equal({ search_path: "searchy" }, conn.db_opts.to_hash)
91
+ assert_equal(["SET search_path=searchy"], conn.db_opts.sql_statements)
92
+ end
93
+ end
94
+
95
+ def test_applies_nothing_when_no_configuration
96
+ Sequelizer::YamlConfig.stub :user_config_path, Pathname.new('/completely/made/up/path/that/does/not/exist') do
97
+ conn = Sequelizer::ConnectionMaker.new(@options).connection
98
+ conn.test_connection
99
+ assert_equal({}, conn.db_opts.to_hash)
100
+ assert_equal([], conn.db_opts.sql_statements)
101
+ end
102
+ end
103
+
104
+ def test_applies_quotes_when_necessary
105
+ Sequelizer::YamlConfig.stub :user_config_path, Pathname.new('/completely/made/up/path/that/does/not/exist') do
106
+ @options.merge!(postgres_db_opt_search_path: "searchy,path")
107
+ conn = Sequelizer::ConnectionMaker.new(@options).connection
108
+ conn.test_connection
109
+ assert_equal({ search_path: "'searchy,path'" }, conn.db_opts.to_hash)
110
+ assert_equal(["SET search_path='searchy,path'"], conn.db_opts.sql_statements)
111
+ end
112
+ end
113
+
114
+ def test_applies_extensions
115
+ with_yaml_config(@options.merge(extension_error_sql: 1)) do
116
+ assert Sequelizer::ConnectionMaker.new.connection.send(:instance_variable_get, "@loaded_extensions".to_sym).include?(:error_sql), "Extension wasn't set"
117
+ end
118
+ end
119
+ end
@@ -1,5 +1,5 @@
1
- require_relative '../test_helper'
2
- require_relative '../../lib/sequelizer/env_config'
1
+ require_relative '../../test_helper'
2
+ require 'sequelizer'
3
3
 
4
4
 
5
5
  class TestEnvConfig < Minitest::Test
@@ -25,4 +25,10 @@ class TestEnvConfig < Minitest::Test
25
25
  assert_equal({ 'adapter' => 'sqlite' }, @env_config.options)
26
26
  ENV.delete('SEQUELIZER_ADAPTER')
27
27
  end
28
+
29
+ def test_converts_db_opts_to_options
30
+ ENV['POSTGRES_DB_OPT_HEY'] = 'there'
31
+ assert_equal({ 'postgres_db_opt_hey' => 'there' }, @env_config.options)
32
+ ENV.delete('POSTGRES_DB_OPT_HEY')
33
+ end
28
34
  end
@@ -1,5 +1,5 @@
1
- require_relative '../test_helper'
2
- require_relative '../../lib/sequelizer/gemfile_modifier'
1
+ require_relative '../../test_helper'
2
+ require 'sequelizer/gemfile_modifier'
3
3
 
4
4
  class TestGemfileModifier < Minitest::Test
5
5
  def setup
@@ -1,5 +1,5 @@
1
- require_relative '../test_helper'
2
- require_relative '../../lib/sequelizer/options'
1
+ require_relative '../../test_helper'
2
+ require 'sequelizer/options'
3
3
 
4
4
 
5
5
  class TestOptions < Minitest::Test
@@ -43,7 +43,8 @@ class TestOptions < Minitest::Test
43
43
  def test_returns_a_hash_even_if_given_nil
44
44
  Sequelizer::YamlConfig.stub :user_config_path, Pathname.new('/completely/made/up/path/that/does/not/exist') do
45
45
  options = Sequelizer::Options.new
46
- assert_equal({}, options.to_hash)
46
+ assert_equal(1, options.to_hash.length)
47
+ assert_instance_of(Proc, options.to_hash[:after_connect])
47
48
  end
48
49
  end
49
50
 
@@ -51,5 +52,27 @@ class TestOptions < Minitest::Test
51
52
  options = Sequelizer::Options.new(search_path: 'passed', adapter: 'postgres')
52
53
  assert_equal 'passed', options.search_path
53
54
  end
55
+
56
+ def test_handles_existing_after_connect
57
+ db = Sequel.mock(host: :postgres)
58
+ conny = Minitest::Mock.new
59
+ conny.expect :db, db
60
+ conny.expect :db, db
61
+ conny.expect :db, db
62
+
63
+ procky = Proc.new { |conn| conn.db[:table].to_a }
64
+
65
+ options = Sequelizer::Options.new(after_connect: procky)
66
+ options.to_hash[:after_connect].call(conny, :default, db)
67
+
68
+ assert_equal(["SELECT * FROM \"table\""], db.sqls)
69
+ end
70
+
71
+ def test_handles_extensions_passed_in
72
+ options = Sequelizer::Options.new(extension_example_1: 1, extension_example_2: 1, not_an_extension_example: 1)
73
+ assert_equal 1, options.to_hash[:not_an_extension_example]
74
+ assert options.extensions.include?(:example_1), "Failed to find example_1 in extensions"
75
+ assert options.extensions.include?(:example_2), "Failed to find example_2 in extensions"
76
+ end
54
77
  end
55
78
 
@@ -1,5 +1,5 @@
1
- require_relative '../test_helper'
2
- require_relative '../../lib/sequelizer/yaml_config'
1
+ require_relative '../../test_helper'
2
+ require 'sequelizer/yaml_config'
3
3
 
4
4
 
5
5
  class TestYamlConfig < Minitest::Test
@@ -7,6 +7,19 @@ class TestYamlConfig < Minitest::Test
7
7
  @yaml_config = Sequelizer::YamlConfig.new
8
8
  end
9
9
 
10
+ def with_empty_env
11
+ env_mock = Minitest::Mock.new
12
+ env_mock.expect :[], nil, ['SEQUELIZER_ENV']
13
+ env_mock.expect :[], nil, ['RAILS_ENV']
14
+ env_mock.expect :[], nil, ['RACK_ENV']
15
+
16
+ stub_const(Sequelizer::YamlConfig, :ENV, env_mock) do
17
+ yield
18
+ end
19
+
20
+ env_mock.verify
21
+ end
22
+
10
23
  def test_loads_from_yaml_file_if_present
11
24
  mock = Minitest::Mock.new
12
25
  file_mock = Minitest::Mock.new
@@ -29,7 +42,9 @@ class TestYamlConfig < Minitest::Test
29
42
  file_mock.expect :exist?, true
30
43
  @yaml_config.stub :config_file_path, file_mock do
31
44
  @yaml_config.stub :config, {'development' => { 'adapter' => 'sqlite' }} do
32
- assert_equal({ 'adapter' => 'sqlite' }, @yaml_config.options)
45
+ with_empty_env do
46
+ assert_equal({ 'adapter' => 'sqlite' }, @yaml_config.options)
47
+ end
33
48
  end
34
49
  end
35
50
  file_mock.verify
@@ -60,15 +75,8 @@ class TestYamlConfig < Minitest::Test
60
75
  end
61
76
 
62
77
  def test_environment_checks_environment_variables
63
- env_mock = Minitest::Mock.new
64
- env_mock.expect :[], nil, ['SEQUELIZER_ENV']
65
- env_mock.expect :[], nil, ['RAILS_ENV']
66
- env_mock.expect :[], nil, ['RACK_ENV']
67
-
68
- stub_const(Sequelizer::YamlConfig, :ENV, env_mock) do
78
+ with_empty_env do
69
79
  assert_equal 'development', @yaml_config.environment
70
80
  end
71
-
72
- env_mock.verify
73
81
  end
74
82
  end
data/test/test_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'minitest/autorun'
2
+ require "sequel"
2
3
 
3
4
  class Minitest::Test
4
5
  def stub_const(klass, const, replace, &block)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequelizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Duryea
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-09 00:00:00.000000000 Z
11
+ date: 2022-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.5'
19
+ version: '2.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.5'
26
+ version: '2.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: guard
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -66,20 +66,34 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '5.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '12.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '12.0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: sequel
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - "~>"
74
88
  - !ruby/object:Gem::Version
75
- version: '4.12'
89
+ version: '5.0'
76
90
  type: :runtime
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
94
  - - "~>"
81
95
  - !ruby/object:Gem::Version
82
- version: '4.12'
96
+ version: '5.0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: dotenv
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -100,14 +114,14 @@ dependencies:
100
114
  requirements:
101
115
  - - "~>"
102
116
  - !ruby/object:Gem::Version
103
- version: '0.19'
117
+ version: '1.0'
104
118
  type: :runtime
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
107
121
  requirements:
108
122
  - - "~>"
109
123
  - !ruby/object:Gem::Version
110
- version: '0.19'
124
+ version: '1.0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: hashie
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -132,6 +146,7 @@ extensions: []
132
146
  extra_rdoc_files: []
133
147
  files:
134
148
  - ".gitignore"
149
+ - ".travis.yml"
135
150
  - CHANGELOG.md
136
151
  - Gemfile
137
152
  - Guardfile
@@ -142,21 +157,25 @@ files:
142
157
  - examples/database.with_one_set_of_options.yml
143
158
  - examples/database.yml
144
159
  - examples/dot_env.txt
160
+ - lib/sequel/extensions/db_opts.rb
161
+ - lib/sequel/extensions/settable.rb
145
162
  - lib/sequelizer.rb
146
163
  - lib/sequelizer/cli.rb
147
164
  - lib/sequelizer/connection_maker.rb
148
165
  - lib/sequelizer/env_config.rb
149
166
  - lib/sequelizer/gemfile_modifier.rb
167
+ - lib/sequelizer/monkey_patches/database_in_after_connect.rb
150
168
  - lib/sequelizer/options.rb
151
169
  - lib/sequelizer/options_hash.rb
152
170
  - lib/sequelizer/version.rb
153
171
  - lib/sequelizer/yaml_config.rb
154
172
  - sequelizer.gemspec
155
- - test/sequelizer/test_connection_maker.rb
156
- - test/sequelizer/test_env_config.rb
157
- - test/sequelizer/test_gemfile_modifier.rb
158
- - test/sequelizer/test_options.rb
159
- - test/sequelizer/test_yaml_config.rb
173
+ - test/lib/sequel/extensions/test_db_opts.rb
174
+ - test/lib/sequelizer/test_connection_maker.rb
175
+ - test/lib/sequelizer/test_env_config.rb
176
+ - test/lib/sequelizer/test_gemfile_modifier.rb
177
+ - test/lib/sequelizer/test_options.rb
178
+ - test/lib/sequelizer/test_yaml_config.rb
160
179
  - test/test_helper.rb
161
180
  homepage: https://github.com/outcomesinsights/sequelizer
162
181
  licenses:
@@ -177,15 +196,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
196
  - !ruby/object:Gem::Version
178
197
  version: '0'
179
198
  requirements: []
180
- rubyforge_project:
181
- rubygems_version: 2.4.8
199
+ rubygems_version: 3.1.4
182
200
  signing_key:
183
201
  specification_version: 4
184
202
  summary: Sequel database connections via config/database.yml or .env
185
203
  test_files:
186
- - test/sequelizer/test_connection_maker.rb
187
- - test/sequelizer/test_env_config.rb
188
- - test/sequelizer/test_gemfile_modifier.rb
189
- - test/sequelizer/test_options.rb
190
- - test/sequelizer/test_yaml_config.rb
204
+ - test/lib/sequel/extensions/test_db_opts.rb
205
+ - test/lib/sequelizer/test_connection_maker.rb
206
+ - test/lib/sequelizer/test_env_config.rb
207
+ - test/lib/sequelizer/test_gemfile_modifier.rb
208
+ - test/lib/sequelizer/test_options.rb
209
+ - test/lib/sequelizer/test_yaml_config.rb
191
210
  - test/test_helper.rb
@@ -1,52 +0,0 @@
1
- require_relative '../test_helper'
2
- require_relative '../../lib/sequelizer'
3
-
4
- class TestConnectionMaker < Minitest::Test
5
- def setup
6
- @options = { 'adapter' => 'sqlite' }
7
- @sequel_mock = Minitest::Mock.new
8
- stub_const(Sequelizer::ConnectionMaker, :Sequel, @sequel_mock)
9
- @sequel_mock.expect :connect, :connection, [@options]
10
- end
11
-
12
- def teardown
13
- @sequel_mock.verify
14
- remove_stubbed_const(Sequelizer::ConnectionMaker, :Sequel)
15
- end
16
-
17
- def test_accepts_options_as_params
18
- Sequelizer::YamlConfig.stub :user_config_path, Pathname.new('/completely/made/up/path/that/does/not/exist') do
19
- assert_equal :connection, Sequelizer::ConnectionMaker.new(@options).connection
20
- end
21
- end
22
-
23
- def test_reads_options_from_yaml_config
24
- yaml_config = Minitest::Mock.new
25
- yaml_config.expect :options, @options
26
- yaml_config.expect :options, @options
27
-
28
- Sequelizer::YamlConfig.stub :new, yaml_config do
29
- assert_equal :connection, Sequelizer::ConnectionMaker.new.connection
30
- end
31
-
32
- yaml_config.verify
33
- end
34
-
35
- def test_reads_options_from_env_config_if_no_yaml_config
36
- yaml_config = Minitest::Mock.new
37
- yaml_config.expect :options, {}
38
- yaml_config.expect :options, {}
39
-
40
- env_config = Minitest::Mock.new
41
- env_config.expect :options, @options
42
-
43
- Sequelizer::YamlConfig.stub :new, yaml_config do
44
- Sequelizer::EnvConfig.stub :new, env_config do
45
- assert_equal :connection, Sequelizer::ConnectionMaker.new.connection
46
- end
47
- end
48
-
49
- env_config.verify
50
- yaml_config.verify
51
- end
52
- end