rom-sql 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7899f84b60af6ef8f8e80c230905abde0ca7be65
4
- data.tar.gz: 34e2218734e91c6dfff3f83bf90db56e90b64be8
3
+ metadata.gz: 0b429ab1e11613744f6b0626ea822abe070424c0
4
+ data.tar.gz: 86220401f55baec6bc56a40545379971f74ed17e
5
5
  SHA512:
6
- metadata.gz: 69fb7e9221a9fd8255fec6e41f2bf3f266b8e4d5c52c102381226c0ce09c7b3bdbb1c64e6c69f0755ee97e549eb3fe258fbfc888e574f4a6130f201fd45c9838
7
- data.tar.gz: b6275286b6649dd026b5b9c248027579da3d5b4b3de1ea123a3a252a07df42e599265d97536ac84282fb97007fe4b0d356455b2ed201df31986daea382bb76f0
6
+ metadata.gz: f0169d4b08b6e037dbb7c18b76e41e14a6cff5f2d1edb0bdaba4705c216a6f52e57b04de4bb3ef14b5e329ba45e55205be7afe18c85f3179f345612158cbbc0d
7
+ data.tar.gz: beceab2da1ed5cea31bf03224cf30752a6b1c2a922e88f6a5f61875c03bf4239b1626d8b5d9d51930244492957362f2efab80f87773e2e1118b50d354e36c8db
data/CHANGELOG.md CHANGED
@@ -1,8 +1,21 @@
1
- ## v0.5.1 to-be-released
1
+ ## v0.5.2 2015-06-22
2
+
3
+ ### Added
4
+
5
+ * `Relation#invert` operation (nepalez)
6
+
7
+ ### Changed
8
+
9
+ * Migration tasks no longer require entire ROM env (solnic)
10
+ * `Repository` => `Gateway` rename for ROM 0.8.0 compatibility (solnic)
11
+
12
+ [Compare v0.5.1...v0.5.2](https://github.com/rom-rb/rom-sql/compare/v0.5.1...v0.5.2)
13
+
14
+ ## v0.5.1 2015-05-25
2
15
 
3
16
  ### Changed
4
17
 
5
- * relations won't be finalized when table(s) is/are missing (solnic)
18
+ * Relations won't be finalized when table(s) is/are missing (solnic)
6
19
 
7
20
  ### Fixed
8
21
 
data/lib/rom/sql.rb CHANGED
@@ -46,7 +46,7 @@ end
46
46
 
47
47
  require "rom/sql/version"
48
48
  require "rom/sql/relation"
49
- require "rom/sql/repository"
49
+ require "rom/sql/gateway"
50
50
  require "rom/sql/migration"
51
51
 
52
52
  if defined?(Rails)
@@ -20,8 +20,8 @@ module ROM
20
20
  #
21
21
  # @api public
22
22
  def execute
23
- deleted = target.to_a
24
- target.delete
23
+ deleted = relation.to_a
24
+ relation.delete
25
25
  deleted
26
26
  end
27
27
  end
@@ -55,7 +55,7 @@ module ROM
55
55
  #
56
56
  # @api public
57
57
  def change(original)
58
- self.class.new(relation, options.merge(original: original.to_h))
58
+ self.class.build(relation, options.merge(original: original.to_h))
59
59
  end
60
60
 
61
61
  private
@@ -1,21 +1,21 @@
1
1
  require 'logger'
2
2
 
3
- require 'rom/repository'
3
+ require 'rom/gateway'
4
4
  require 'rom/sql/migration'
5
5
  require 'rom/sql/commands'
6
6
 
7
7
  module ROM
8
8
  module SQL
9
- # SQL repository
9
+ # SQL gateway
10
10
  #
11
11
  # @example
12
12
  # db = Sequel.connect(:sqlite)
13
- # repository = ROM::SQL::Repository.new(db)
13
+ # gateway = ROM::SQL::Gateway.new(db)
14
14
  #
15
- # users = repository.dataset(:users)
15
+ # users = gateway.dataset(:users)
16
16
  #
17
17
  # @api public
18
- class Repository < ROM::Repository
18
+ class Gateway < ROM::Gateway
19
19
  include Options
20
20
  include Migration
21
21
 
@@ -40,7 +40,7 @@ module ROM
40
40
  scheme.to_s.include?('sqlite')
41
41
  end
42
42
 
43
- # SQL repository interface
43
+ # SQL gateway interface
44
44
  #
45
45
  # @overload connect(uri, options)
46
46
  # Connects to database via uri passing options
@@ -54,11 +54,11 @@ module ROM
54
54
  # @param [Sequel::Database] connection a connection instance
55
55
  #
56
56
  # @example
57
- # repository = ROM::SQL::Repository.new('postgres://localhost/rom')
57
+ # gateway = ROM::SQL::Gateway.new('postgres://localhost/rom')
58
58
  #
59
59
  # # or reuse connection
60
60
  # DB = Sequel.connect('postgres://localhost/rom')
61
- # repository = ROM::SQL::Repository.new(DB)
61
+ # gateway = ROM::SQL::Gateway.new(DB)
62
62
  #
63
63
  # @api public
64
64
  def initialize(uri, options = {})
@@ -2,7 +2,7 @@ require 'rom/sql/migration/migrator'
2
2
 
3
3
  module ROM
4
4
  module SQL
5
- # Create a database migration for a specific repository
5
+ # Create a database migration for a specific gateway
6
6
  #
7
7
  # @example
8
8
  # ROM.setup(
@@ -21,14 +21,26 @@ module ROM
21
21
  # end
22
22
  # end
23
23
  #
24
- # # for a non-default repository
24
+ # # for a non-default gateway
25
25
  # ROM::SQL.migration(:other) do
26
26
  # # ...
27
27
  # end
28
28
  #
29
29
  # @api public
30
- def self.migration(repository = :default, &block)
31
- ROM.env.repositories[repository].migration(&block)
30
+ def self.migration(gateway = :default, &block)
31
+ gateways = ROM.boot ? ROM.boot.gateways : ROM.env.gateways
32
+ gateways[gateway].migration(&block)
33
+ end
34
+
35
+ # Return first sql gateway for migrations
36
+ #
37
+ # This is used by migration tasks, they only support a single sql gateway
38
+ #
39
+ # @api private
40
+ def self.gateway
41
+ ROM.gateways
42
+ .keys
43
+ .detect { |gateway| gateway.instance_of?(Gateway) }
32
44
  end
33
45
 
34
46
  module Migration
@@ -37,8 +49,8 @@ module ROM
37
49
  def self.included(klass)
38
50
  super
39
51
  klass.class_eval do
40
- option :migrator, reader: true, default: proc { |repository|
41
- Migrator.new(repository.connection)
52
+ option :migrator, reader: true, default: proc { |gateway|
53
+ Migrator.new(gateway.connection)
42
54
  }
43
55
  end
44
56
  end
@@ -50,12 +62,12 @@ module ROM
50
62
  migrator.migration(&block)
51
63
  end
52
64
 
53
- # Run migrations for a given repository
65
+ # Run migrations for a given gateway
54
66
  #
55
67
  # @example
56
68
  # ROM.setup(:sql, ['sqlite::memory'])
57
69
  # ROM.finalize
58
- # ROM.env.repositories[:default].run_migrations
70
+ # ROM.env.gateways[:default].run_migrations
59
71
  #
60
72
  #
61
73
  # @param [Hash] options The options used by Sequel migrator
@@ -65,7 +77,7 @@ module ROM
65
77
  migrator.run(options)
66
78
  end
67
79
 
68
- # TODO: this should be removed in favor of migration API in Repository
80
+ # TODO: this should be removed in favor of migration API in Gateway
69
81
  class << self
70
82
  attr_writer :path
71
83
  attr_accessor :connection
@@ -75,7 +87,7 @@ module ROM
75
87
  end
76
88
 
77
89
  def run(options = {})
78
- warn "ROM::SQL::Migration.run is deprecated please ROM::SQL::Repository#run_migrations (#{caller[0]})"
90
+ warn "ROM::SQL::Migration.run is deprecated please ROM::SQL::Gateway#run_migrations (#{caller[0]})"
79
91
  ::Sequel::Migrator.run(connection, path, options)
80
92
  end
81
93
 
@@ -46,7 +46,7 @@ module ROM
46
46
  # @example
47
47
  # class CreateTask < ROM::Commands::Create[:sql]
48
48
  # relation :tasks
49
- # associates :user, [:user_id, :id]
49
+ # associates :user, key: [:user_id, :id]
50
50
  # end
51
51
  #
52
52
  # create_user = rom.command(:user).create.with(name: 'Jane')
@@ -199,6 +199,21 @@ module ROM
199
199
  __new__(dataset.__send__(__method__, *args, &block))
200
200
  end
201
201
 
202
+ # Inverts a request
203
+ #
204
+ # @example
205
+ # users.exclude(name: 'Jane').invert
206
+ #
207
+ # # this is the same as:
208
+ # users.where(name: 'Jane')
209
+ #
210
+ # @return [Relation]
211
+ #
212
+ # @api public
213
+ def invert(*args, &block)
214
+ __new__(dataset.__send__(__method__, *args, &block))
215
+ end
216
+
202
217
  # Set order for the relation
203
218
  #
204
219
  # @example
@@ -4,37 +4,37 @@ require "fileutils"
4
4
  namespace :db do
5
5
  desc "Perform migration reset (full erase and migration up)"
6
6
  task reset: :setup do
7
- repository = ROM.env.repositories[:default]
8
- repository.run_migrations(target: 0)
9
- repository.run_migrations
7
+ gateway = ROM::SQL.gateway
8
+ gateway.run_migrations(target: 0)
9
+ gateway.run_migrations
10
10
  puts "<= db:reset executed"
11
11
  end
12
12
 
13
13
  desc "Migrate the database (options [version_number])]"
14
14
  task :migrate, [:version] => :setup do |_, args|
15
- repository = ROM.env.repositories[:default]
15
+ gateway = ROM::SQL.gateway
16
16
  version = args[:version]
17
17
 
18
18
  if version.nil?
19
- repository.run_migrations
19
+ gateway.run_migrations
20
20
  puts "<= db:migrate executed"
21
21
  else
22
- repository.run_migrations(target: version.to_i)
22
+ gateway.run_migrations(target: version.to_i)
23
23
  puts "<= db:migrate version=[#{version}] executed"
24
24
  end
25
25
  end
26
26
 
27
27
  desc "Perform migration down (erase all data)"
28
28
  task clean: :setup do
29
- repository = ROM.env.repositories[:default]
29
+ gateway = ROM::SQL.gateway
30
30
 
31
- repository.run_migrations(target: 0)
31
+ gateway.run_migrations(target: 0)
32
32
  puts "<= db:clean executed"
33
33
  end
34
34
 
35
35
  desc "Create a migration (parameters: NAME, VERSION)"
36
36
  task :create_migration, [:name, :version] => :setup do |_, args|
37
- repository = ROM.env.repositories[:default]
37
+ gateway = ROM::SQL.gateway
38
38
  name, version = args[:name], args[:version]
39
39
 
40
40
  if name.nil?
@@ -43,7 +43,7 @@ namespace :db do
43
43
  exit
44
44
  end
45
45
 
46
- path = repository.migrator.create_file(*[name, version].compact)
46
+ path = gateway.migrator.create_file(*[name, version].compact)
47
47
 
48
48
  puts "<= migration file created #{path}"
49
49
  end
@@ -1,5 +1,5 @@
1
1
  module ROM
2
2
  module SQL
3
- VERSION = '0.5.1'.freeze
3
+ VERSION = '0.5.2'.freeze
4
4
  end
5
5
  end
data/rom-sql.gemspec CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_runtime_dependency "sequel", "~> 4.18"
22
22
  spec.add_runtime_dependency "equalizer", "~> 0.0", ">= 0.0.9"
23
- spec.add_runtime_dependency "rom", "~> 0.7", ">= 0.7.0"
23
+ spec.add_runtime_dependency "rom", "~> 0.8", ">= 0.8.0"
24
24
 
25
25
  spec.add_development_dependency "bundler"
26
26
  spec.add_development_dependency "rake", "~> 10.0"
@@ -1,4 +1,4 @@
1
- ROM.env.repositories[:default].migration do
1
+ ROM.env.gateways[:default].migration do
2
2
  change do
3
3
  create_table :carrots do
4
4
  primary_key :id
@@ -72,13 +72,12 @@ describe 'Commands / Update' do
72
72
  end
73
73
 
74
74
  it 'does not update when attributes did not change' do
75
- piotr_rel = double('piotr_rel').as_null_object
75
+ result = users.try do
76
+ command = users.update.by_id(piotr[:id]).change(piotr)
76
77
 
77
- expect(relation).to receive(:by_id).with(piotr[:id]).and_return(piotr_rel)
78
- expect(piotr_rel).not_to receive(:update)
78
+ expect(command.relation).not_to receive(:update)
79
79
 
80
- result = users.try do
81
- users.update.by_id(piotr[:id]).change(piotr).call(name: piotr[:name])
80
+ command.call(name: piotr[:name])
82
81
  end
83
82
 
84
83
  expect(result.value.to_a).to be_empty
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe ROM::SQL, '.migration' do
4
+ let(:connection) { ROM::SQL.gateway.connection }
5
+
6
+ before do
7
+ ROM.setup(:sql, DB_URI)
8
+ connection.drop_table?(:dragons)
9
+ end
10
+
11
+ it 'creates a migration for a specific gateway' do
12
+
13
+ migration = ROM::SQL.migration do
14
+ change do
15
+ create_table :dragons do
16
+ primary_key :id
17
+ column :name, String
18
+ end
19
+ end
20
+ end
21
+
22
+ migration.apply(connection, :up)
23
+
24
+ expect(connection.table_exists?(:dragons)).to be(true)
25
+ end
26
+ end
@@ -1,11 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe ROM::SQL::Repository do
3
+ describe ROM::SQL::Gateway do
4
4
  describe 'migration' do
5
5
  let(:conn) { Sequel.connect(DB_URI) }
6
6
 
7
7
  context 'creating migrations inline' do
8
- subject(:repository) { ROM.env.repositories[:default] }
8
+ subject(:gateway) { ROM.env.gateways[:default] }
9
9
 
10
10
  before do
11
11
  ROM.setup(:sql, conn)
@@ -14,7 +14,7 @@ describe ROM::SQL::Repository do
14
14
 
15
15
  after do
16
16
  [:rabbits, :carrots].each do |name|
17
- repository.connection.drop_table?(name)
17
+ gateway.connection.drop_table?(name)
18
18
  end
19
19
  end
20
20
 
@@ -32,13 +32,13 @@ describe ROM::SQL::Repository do
32
32
  end
33
33
  end
34
34
 
35
- migration.apply(repository.connection, :up)
35
+ migration.apply(gateway.connection, :up)
36
36
 
37
- expect(repository.connection[:rabbits]).to be_a(Sequel::Dataset)
37
+ expect(gateway.connection[:rabbits]).to be_a(Sequel::Dataset)
38
38
 
39
- migration.apply(repository.connection, :down)
39
+ migration.apply(gateway.connection, :down)
40
40
 
41
- expect(repository.connection.tables).to_not include(:rabbits)
41
+ expect(gateway.connection.tables).to_not include(:rabbits)
42
42
  end
43
43
  end
44
44
 
@@ -55,7 +55,7 @@ describe ROM::SQL::Repository do
55
55
  end
56
56
 
57
57
  it 'runs migrations from a specified directory' do
58
- ROM.env.repositories[:default].run_migrations
58
+ ROM.env.gateways[:default].run_migrations
59
59
  end
60
60
  end
61
61
  end
data/spec/spec_helper.rb CHANGED
@@ -43,5 +43,6 @@ RSpec.configure do |config|
43
43
  config.after do
44
44
  added_constants = Object.constants - @constants
45
45
  added_constants.each { |name| Object.send(:remove_const, name) }
46
+ ROM.instance_variable_set('@gateways', {})
46
47
  end
47
48
  end
@@ -7,7 +7,7 @@ describe 'ActiveSupport::Notifications support' do
7
7
  include_context 'database setup'
8
8
 
9
9
  it 'works' do
10
- rom.repositories[:default].use_logger(LOGGER)
10
+ rom.gateways[:default].use_logger(LOGGER)
11
11
 
12
12
  sql = nil
13
13
 
@@ -18,7 +18,7 @@ describe 'Rails log subscriber' do
18
18
 
19
19
  before do
20
20
  set_logger(logger)
21
- rom.repositories[:default].use_logger(logger)
21
+ rom.gateways[:default].use_logger(logger)
22
22
  end
23
23
 
24
24
  it 'works' do
@@ -4,11 +4,11 @@ describe 'Logger' do
4
4
  include_context 'database setup'
5
5
 
6
6
  it 'sets up a logger for sequel' do
7
- repository = rom.repositories[:default]
7
+ gateway = rom.gateways[:default]
8
8
 
9
- repository.use_logger(LOGGER)
9
+ gateway.use_logger(LOGGER)
10
10
 
11
- expect(repository.logger).to be(LOGGER)
11
+ expect(gateway.logger).to be(LOGGER)
12
12
  expect(conn.loggers).to include(LOGGER)
13
13
  end
14
14
  end
@@ -12,7 +12,7 @@ describe 'MigrationTasks' do
12
12
  ROM.finalize
13
13
  end
14
14
 
15
- let(:migrator) { ROM.env.repositories[:default].migrator }
15
+ let(:migrator) { ROM.env.gateways[:default].migrator }
16
16
 
17
17
  context 'db:reset' do
18
18
  it 'calls proper commands' do
@@ -29,6 +29,13 @@ describe ROM::Relation do
29
29
  end
30
30
  end
31
31
 
32
+ describe '#invert' do
33
+ it 'delegates to dataset and returns a new relation' do
34
+ expect(users.dataset).to receive(:invert).and_call_original
35
+ expect(users.invert).to_not eq(users)
36
+ end
37
+ end
38
+
32
39
  describe '#map' do
33
40
  it 'yields tuples' do
34
41
  result = []
@@ -2,24 +2,24 @@ require 'spec_helper'
2
2
 
3
3
  require 'rom/lint/spec'
4
4
 
5
- describe ROM::SQL::Repository do
5
+ describe ROM::SQL::Gateway do
6
6
  include_context 'users and tasks'
7
7
 
8
- let(:repository) { rom.repositories[:default] }
8
+ let(:gateway) { rom.gateways[:default] }
9
9
 
10
- it_behaves_like 'a rom repository' do
10
+ it_behaves_like 'a rom gateway' do
11
11
  let(:identifier) { :sql }
12
- let(:repository) { ROM::SQL::Repository }
12
+ let(:gateway) { ROM::SQL::Gateway }
13
13
  let(:uri) { 'postgres://localhost/rom' }
14
14
  end
15
15
 
16
16
  describe '#dataset?' do
17
17
  it 'returns true if a table exists' do
18
- expect(repository.dataset?(:users)).to be(true)
18
+ expect(gateway.dataset?(:users)).to be(true)
19
19
  end
20
20
 
21
21
  it 'returns false if a table does not exist' do
22
- expect(repository.dataset?(:not_here)).to be(false)
22
+ expect(gateway.dataset?(:not_here)).to be(false)
23
23
  end
24
24
  end
25
25
 
@@ -31,20 +31,20 @@ describe ROM::SQL::Repository do
31
31
  .with(DB_URI, host: '127.0.0.1')
32
32
  .and_return(conn)
33
33
 
34
- repository = ROM::SQL::Repository.new(DB_URI, migrator: migrator, host: '127.0.0.1')
34
+ gateway = ROM::SQL::Gateway.new(DB_URI, migrator: migrator, host: '127.0.0.1')
35
35
 
36
- expect(repository.options).to eql(migrator: migrator)
36
+ expect(gateway.options).to eql(migrator: migrator)
37
37
  end
38
38
  end
39
39
 
40
40
  describe '#disconnect' do
41
- let(:repository) { ROM::SQL::Repository.new(uri) }
41
+ let(:gateway) { ROM::SQL::Gateway.new(uri) }
42
42
 
43
43
  it 'disconnects via sequel connection' do
44
44
  # FIXME: no idea how to test it in a different way
45
45
  # FIXME: we are leaking connection here
46
- expect(repository.connection).to receive(:disconnect)
47
- repository.disconnect
46
+ expect(gateway.connection).to receive(:disconnect)
47
+ gateway.disconnect
48
48
  end
49
49
  end
50
50
  end
@@ -6,7 +6,7 @@ describe 'Inferring schema from database' do
6
6
  context "when database schema exists" do
7
7
  it "infers the schema from the database relations" do
8
8
  expect(rom.relations.users.to_a)
9
- .to eql(rom.repositories[:default][:users].to_a)
9
+ .to eql(rom.gateways[:default][:users].to_a)
10
10
  end
11
11
  end
12
12
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rom-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Solnica
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-25 00:00:00.000000000 Z
11
+ date: 2015-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -50,20 +50,20 @@ dependencies:
50
50
  requirements:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: '0.7'
53
+ version: '0.8'
54
54
  - - ">="
55
55
  - !ruby/object:Gem::Version
56
- version: 0.7.0
56
+ version: 0.8.0
57
57
  type: :runtime
58
58
  prerelease: false
59
59
  version_requirements: !ruby/object:Gem::Requirement
60
60
  requirements:
61
61
  - - "~>"
62
62
  - !ruby/object:Gem::Version
63
- version: '0.7'
63
+ version: '0.8'
64
64
  - - ">="
65
65
  - !ruby/object:Gem::Version
66
- version: 0.7.0
66
+ version: 0.8.0
67
67
  - !ruby/object:Gem::Dependency
68
68
  name: bundler
69
69
  requirement: !ruby/object:Gem::Requirement
@@ -119,6 +119,7 @@ files:
119
119
  - lib/rom/sql/commands/transaction.rb
120
120
  - lib/rom/sql/commands/update.rb
121
121
  - lib/rom/sql/commands_ext/postgres.rb
122
+ - lib/rom/sql/gateway.rb
122
123
  - lib/rom/sql/header.rb
123
124
  - lib/rom/sql/migration.rb
124
125
  - lib/rom/sql/migration/migrator.rb
@@ -130,7 +131,6 @@ files:
130
131
  - lib/rom/sql/relation/associations.rb
131
132
  - lib/rom/sql/relation/class_methods.rb
132
133
  - lib/rom/sql/relation/inspection.rb
133
- - lib/rom/sql/repository.rb
134
134
  - lib/rom/sql/spec/support.rb
135
135
  - lib/rom/sql/support/active_support_notifications.rb
136
136
  - lib/rom/sql/support/rails_log_subscriber.rb
@@ -143,6 +143,7 @@ files:
143
143
  - spec/integration/commands/create_spec.rb
144
144
  - spec/integration/commands/delete_spec.rb
145
145
  - spec/integration/commands/update_spec.rb
146
+ - spec/integration/migration_spec.rb
146
147
  - spec/integration/read_spec.rb
147
148
  - spec/integration/repository_spec.rb
148
149
  - spec/shared/database_setup.rb
@@ -192,6 +193,7 @@ test_files:
192
193
  - spec/integration/commands/create_spec.rb
193
194
  - spec/integration/commands/delete_spec.rb
194
195
  - spec/integration/commands/update_spec.rb
196
+ - spec/integration/migration_spec.rb
195
197
  - spec/integration/read_spec.rb
196
198
  - spec/integration/repository_spec.rb
197
199
  - spec/shared/database_setup.rb