sequelizer 0.1.0 → 0.1.1

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
2
  SHA1:
3
- metadata.gz: 8d676f724540c01e7cbd2bc6b2a354d78a2e9d15
4
- data.tar.gz: e6872a70bdf5d268687f667273edced41abd13aa
3
+ metadata.gz: 8e2a70d1963039e1cd93eb75a8fa31cd202ed4e1
4
+ data.tar.gz: b382f19e7b3f630ea0924d77ee0da41b13685ae5
5
5
  SHA512:
6
- metadata.gz: 8fb25153b51b05926e5da76fd977f88b8b3077b9dccedb6282edd40486bea4bc1d1e6be70fd8a576f2102a80c406663b40ef396a6ad32897730fd510cd6bddcd
7
- data.tar.gz: ace9f653827f44fac941f1bbd6b2e6e854d3128bcf8d3a8f14343304b5f7350f2e6efe1c7ee11271cd3aa6f8f659bd6dd7b19145223372a3eecac56ac33aa4e7
6
+ metadata.gz: 301a1776007ec662fb8296db06598af3d52ec55cc09a834c7efff1b0e6ab7e0d93eb3ca1a287ae2d4f40dc440f986f0f0e7df21d1e1d4d1201969fcf1869a010
7
+ data.tar.gz: e8465d89b0e9309f8c63f2b71250d614de1eb71bdae8ee96aaf75e49b3ca0a78df3c42c66b3fb3664be1e7999a0570724bcc6621cbd26aea2d0b2c684655b391
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
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3
4
+ sudo: false
5
+ env:
6
+ secure: xFZ00J+axPQADZ2Dqc61Ljo56vT8/uwVzOFw9zxKQs3ThMfI8+OvlI7LV8sf0XPC5Cp+BObYCxzsqIOY8M8yo+NfknlNmlrQaPqi3lf+3tKvEFeFiQZ/jvbGReIdxRdPViVls1W3zEDdRwe9zUAiz7C+xBYCRfZRoGjfm7gx/4c=
7
+ before_install:
8
+ - gem install bundler tping
9
+ - bundle
10
+ script: bundle exec rake test
11
+ after_success:
12
+ - tping $TRAVIS_PRO_TOKEN outcomesinsights jigsaw_builder pro
13
+ notifications:
14
+ slack:
15
+ 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
@@ -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]
@@ -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
  #
@@ -46,7 +58,7 @@ module Sequelizer
46
58
  sequelizer_options.merge!(timeout: sequelizer_options[:timeout].to_i)
47
59
  end
48
60
 
49
- sequelizer_options
61
+ sequelizer_options.merge(after_connect: make_ac(sequelizer_options))
50
62
  end
51
63
 
52
64
  # Grabs the database options from
@@ -77,5 +89,15 @@ module Sequelizer
77
89
  conn.execute("SET search_path TO #{search_path}")
78
90
  end
79
91
  end
92
+
93
+ def filter_extensions(options)
94
+ extension_regexp = /^extension_/
95
+ extension_keys = options.keys.select { |k| k.to_s =~ extension_regexp }
96
+ extensions = extension_keys.map do |key|
97
+ options.delete(key)
98
+ key.to_s.gsub(extension_regexp, '').to_sym
99
+ end
100
+ [options, extensions]
101
+ end
80
102
  end
81
103
  end
@@ -1,4 +1,4 @@
1
1
  module Sequelizer
2
2
  # Version for the gem
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
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
@@ -22,7 +22,8 @@ Gem::Specification.new do |spec|
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
28
  spec.add_dependency 'thor', '~> 0.19'
28
29
  spec.add_dependency 'hashie', '~> 3.2'
@@ -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,102 @@
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_yaml_config(options = {})
16
+ yaml_config = Minitest::Mock.new
17
+ yaml_config.expect :options, options
18
+ yaml_config.expect :options, options
19
+ Sequelizer::YamlConfig.stub :new, yaml_config do
20
+ yield
21
+ end
22
+ yaml_config.verify
23
+ end
24
+
25
+ def with_env_config(options = {})
26
+ env_config = Minitest::Mock.new
27
+ env_config.expect :options, options
28
+ Sequelizer::EnvConfig.stub :new, env_config do
29
+ yield
30
+ end
31
+ env_config.verify
32
+ end
33
+
34
+ def test_reads_options_from_yaml_config
35
+ with_yaml_config(@options) do
36
+ assert_equal :postgres, Sequelizer::ConnectionMaker.new.connection.database_type
37
+ end
38
+ end
39
+
40
+ def test_applies_settings_if_given
41
+ with_yaml_config(@options.merge(postgres_db_opt_flim: :flam)) do
42
+ with_env_config do
43
+ conn = Sequelizer::ConnectionMaker.new.connection
44
+ conn.test_connection
45
+ assert_equal(["SET flim=flam"], conn.sqls)
46
+ end
47
+ end
48
+ end
49
+
50
+ def test_applies_settings_for_all_connections_if_given
51
+ with_yaml_config(@options.merge(postgres_db_opt_flim: :flam, max_connections: 2, preconnect: :concurrent)) do
52
+ with_env_config do
53
+ conn = Sequelizer::ConnectionMaker.new.connection
54
+ conn.test_connection
55
+ assert_equal(["SET flim=flam"] * 2, conn.sqls)
56
+ end
57
+ end
58
+ end
59
+
60
+ def test_reads_options_from_env_config_if_no_yaml_config
61
+ with_yaml_config do
62
+ with_env_config(@options) do
63
+ assert_equal :postgres, Sequelizer::ConnectionMaker.new.connection.database_type
64
+ end
65
+ end
66
+ end
67
+
68
+ def test_applies_configuration_to_connection
69
+ opts = @options.merge(postgres_db_opt_search_path: "searchy", impala_db_opt_search_path: "searchy2")
70
+ with_yaml_config(opts) do
71
+ conn = Sequelizer::ConnectionMaker.new.connection
72
+ conn.test_connection
73
+ assert_equal({ search_path: "searchy" }, conn.db_opts.to_hash)
74
+ assert_equal(["SET search_path=searchy"], conn.db_opts.sql_statements)
75
+ end
76
+ end
77
+
78
+ def test_applies_nothing_when_no_configuration
79
+ Sequelizer::YamlConfig.stub :user_config_path, Pathname.new('/completely/made/up/path/that/does/not/exist') do
80
+ conn = Sequelizer::ConnectionMaker.new(@options).connection
81
+ conn.test_connection
82
+ assert_equal({}, conn.db_opts.to_hash)
83
+ assert_equal([], conn.db_opts.sql_statements)
84
+ end
85
+ end
86
+
87
+ def test_applies_quotes_when_necessary
88
+ Sequelizer::YamlConfig.stub :user_config_path, Pathname.new('/completely/made/up/path/that/does/not/exist') do
89
+ @options.merge!(postgres_db_opt_search_path: "searchy,path")
90
+ conn = Sequelizer::ConnectionMaker.new(@options).connection
91
+ conn.test_connection
92
+ assert_equal({ search_path: "'searchy,path'" }, conn.db_opts.to_hash)
93
+ assert_equal(["SET search_path='searchy,path'"], conn.db_opts.sql_statements)
94
+ end
95
+ end
96
+
97
+ def test_applies_extensions
98
+ with_yaml_config(@options.merge(extension_error_sql: 1)) do
99
+ assert Sequelizer::ConnectionMaker.new.connection.send(:instance_variable_get, "@loaded_extensions".to_sym).include?(:error_sql), "Extension wasn't set"
100
+ end
101
+ end
102
+ 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
@@ -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,125 +1,139 @@
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.1
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: 2018-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.5'
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
26
  version: '1.5'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: guard
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '2.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '2.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: guard-minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ~>
46
46
  - !ruby/object:Gem::Version
47
47
  version: '2.3'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: '2.3'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: minitest
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: '5.3'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
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
86
100
  requirements:
87
- - - "~>"
101
+ - - ~>
88
102
  - !ruby/object:Gem::Version
89
103
  version: '2.1'
90
104
  type: :runtime
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
- - - "~>"
108
+ - - ~>
95
109
  - !ruby/object:Gem::Version
96
110
  version: '2.1'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: thor
99
113
  requirement: !ruby/object:Gem::Requirement
100
114
  requirements:
101
- - - "~>"
115
+ - - ~>
102
116
  - !ruby/object:Gem::Version
103
117
  version: '0.19'
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
124
  version: '0.19'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: hashie
113
127
  requirement: !ruby/object:Gem::Requirement
114
128
  requirements:
115
- - - "~>"
129
+ - - ~>
116
130
  - !ruby/object:Gem::Version
117
131
  version: '3.2'
118
132
  type: :runtime
119
133
  prerelease: false
120
134
  version_requirements: !ruby/object:Gem::Requirement
121
135
  requirements:
122
- - - "~>"
136
+ - - ~>
123
137
  - !ruby/object:Gem::Version
124
138
  version: '3.2'
125
139
  description: Easily establish a connection to a database via Sequel gem using options
@@ -131,7 +145,8 @@ executables:
131
145
  extensions: []
132
146
  extra_rdoc_files: []
133
147
  files:
134
- - ".gitignore"
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:
@@ -168,24 +187,25 @@ require_paths:
168
187
  - lib
169
188
  required_ruby_version: !ruby/object:Gem::Requirement
170
189
  requirements:
171
- - - ">="
190
+ - - '>='
172
191
  - !ruby/object:Gem::Version
173
192
  version: '0'
174
193
  required_rubygems_version: !ruby/object:Gem::Requirement
175
194
  requirements:
176
- - - ">="
195
+ - - '>='
177
196
  - !ruby/object:Gem::Version
178
197
  version: '0'
179
198
  requirements: []
180
199
  rubyforge_project:
181
- rubygems_version: 2.4.8
200
+ rubygems_version: 2.0.14.1
182
201
  signing_key:
183
202
  specification_version: 4
184
203
  summary: Sequel database connections via config/database.yml or .env
185
204
  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
205
+ - test/lib/sequel/extensions/test_db_opts.rb
206
+ - test/lib/sequelizer/test_connection_maker.rb
207
+ - test/lib/sequelizer/test_env_config.rb
208
+ - test/lib/sequelizer/test_gemfile_modifier.rb
209
+ - test/lib/sequelizer/test_options.rb
210
+ - test/lib/sequelizer/test_yaml_config.rb
191
211
  - 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