rom-sql 0.4.0 → 0.4.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 +4 -4
- data/CHANGELOG.md +9 -0
- data/Gemfile +1 -0
- data/README.md +3 -4
- data/lib/rom/sql.rb +9 -0
- data/lib/rom/sql/commands/create.rb +2 -2
- data/lib/rom/sql/commands/delete.rb +2 -0
- data/lib/rom/sql/commands/error_wrapper.rb +15 -0
- data/lib/rom/sql/commands/update.rb +3 -0
- data/lib/rom/sql/migration.rb +67 -4
- data/lib/rom/sql/migration/migrator.rb +48 -0
- data/lib/rom/sql/migration/template.rb +4 -0
- data/lib/rom/sql/relation.rb +1 -1
- data/lib/rom/sql/repository.rb +10 -1
- data/lib/rom/sql/tasks/migration_tasks.rake +17 -23
- data/lib/rom/sql/version.rb +1 -1
- data/spec/fixtures/migrations/20150403090603_create_carrots.rb +8 -0
- data/spec/integration/commands/create_spec.rb +9 -0
- data/spec/integration/commands/delete_spec.rb +12 -0
- data/spec/integration/commands/update_spec.rb +10 -0
- data/spec/integration/repository_spec.rb +62 -0
- data/spec/shared/database_setup.rb +4 -2
- data/spec/spec_helper.rb +12 -0
- data/spec/unit/migration_tasks_spec.rb +19 -19
- data/spec/unit/migrator_spec.rb +26 -0
- data/spec/unit/repository_spec.rb +14 -0
- metadata +12 -4
- data/spec/unit/migration_spec.rb +0 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65667c45aace8c6a4ca6998f10bc23cca6acb6e0
|
4
|
+
data.tar.gz: c63cf22daea5c103abca9c75b46df203228a5f2e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eeb635de13c7519b535a73fccfbd0213af82ed8381a3709b082e1b50f976215b20588611899916feea315053f3b60bae7b51300dc99b2fcc2c3fe3bf602785f3
|
7
|
+
data.tar.gz: 6ffb7da871553147a54a7976e008d70ee927189cd06b0f5f0f3130dec0ae3fcb6c2e8011893792fb60adaa48c41019c2fe8e82c6d832b8b2ecabf8a83e4f2c9e
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
## v0.4.1 2015-04-04
|
2
|
+
|
3
|
+
### Added
|
4
|
+
|
5
|
+
* Database error handling for update and delete commands (kwando + solnic)
|
6
|
+
* Migration interface as a repository plugin (gotar + solnic)
|
7
|
+
|
8
|
+
[Compare v0.4.0...v0.4.1](https://github.com/rom-rb/rom-sql/compare/v0.4.0...v0.4.1)
|
9
|
+
|
1
10
|
## v0.4.0 2015-03-22
|
2
11
|
|
3
12
|
### Added
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -64,8 +64,6 @@ end
|
|
64
64
|
``` ruby
|
65
65
|
|
66
66
|
class Users < ROM::Relation[:sql]
|
67
|
-
base_name :users
|
68
|
-
|
69
67
|
def by_name(name)
|
70
68
|
where(name: name)
|
71
69
|
end
|
@@ -79,7 +77,7 @@ tasks = rom.relations.tasks
|
|
79
77
|
users.insert(id: 1, name: "Piotr")
|
80
78
|
tasks.insert(user_id: 1, title: "Be happy")
|
81
79
|
|
82
|
-
puts users.by_name("Piotr").with_tasks.to_a.inspect
|
80
|
+
puts rom.relation(:users).by_name("Piotr").with_tasks.to_a.inspect
|
83
81
|
# => [{:id=>1, :name=>"Piotr", :user_id=>1, :title=>"Be happy"}]
|
84
82
|
```
|
85
83
|
|
@@ -114,6 +112,7 @@ end
|
|
114
112
|
|
115
113
|
class UserMapper < ROM::Mapper
|
116
114
|
relation :users
|
115
|
+
register_as :model
|
117
116
|
|
118
117
|
model name: 'User'
|
119
118
|
|
@@ -128,7 +127,7 @@ tasks = rom.relations.tasks
|
|
128
127
|
users.insert(id: 1, name: "Piotr")
|
129
128
|
tasks.insert(user_id: 1, title: "Be happy")
|
130
129
|
|
131
|
-
rom.
|
130
|
+
rom.relation(:users).as(:model).with_tasks.by_name("Piotr").to_a
|
132
131
|
# => [#<User:0x007fb31542a098 @id=1, @name="Piotr", @tasks=[{:title=>"Be happy"}]>]
|
133
132
|
```
|
134
133
|
|
data/lib/rom/sql.rb
CHANGED
@@ -4,6 +4,15 @@ require "rom"
|
|
4
4
|
module ROM
|
5
5
|
module SQL
|
6
6
|
ConstraintError = Class.new(ROM::CommandError)
|
7
|
+
|
8
|
+
class DatabaseError < ROM::CommandError
|
9
|
+
attr_reader :original_exception
|
10
|
+
|
11
|
+
def initialize(error, message)
|
12
|
+
super(message)
|
13
|
+
@original_exception = error
|
14
|
+
end
|
15
|
+
end
|
7
16
|
end
|
8
17
|
end
|
9
18
|
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'rom/sql/commands'
|
2
|
+
require 'rom/sql/commands/error_wrapper'
|
2
3
|
require 'rom/sql/commands/transaction'
|
3
4
|
|
4
5
|
module ROM
|
@@ -6,6 +7,7 @@ module ROM
|
|
6
7
|
module Commands
|
7
8
|
class Create < ROM::Commands::Create
|
8
9
|
include Transaction
|
10
|
+
include ErrorWrapper
|
9
11
|
|
10
12
|
def execute(tuples)
|
11
13
|
insert_tuples = Array([tuples]).flatten.map do |tuple|
|
@@ -15,8 +17,6 @@ module ROM
|
|
15
17
|
end
|
16
18
|
|
17
19
|
insert(insert_tuples)
|
18
|
-
rescue *ERRORS => e
|
19
|
-
raise ConstraintError, e.message
|
20
20
|
end
|
21
21
|
|
22
22
|
def insert(tuples)
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'rom/sql/commands'
|
2
|
+
require 'rom/sql/commands/error_wrapper'
|
2
3
|
require 'rom/sql/commands/transaction'
|
3
4
|
|
4
5
|
module ROM
|
@@ -6,6 +7,7 @@ module ROM
|
|
6
7
|
module Commands
|
7
8
|
class Delete < ROM::Commands::Delete
|
8
9
|
include Transaction
|
10
|
+
include ErrorWrapper
|
9
11
|
|
10
12
|
def execute
|
11
13
|
deleted = target.to_a
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ROM
|
2
|
+
module SQL
|
3
|
+
module Commands
|
4
|
+
module ErrorWrapper
|
5
|
+
def call(*args)
|
6
|
+
super
|
7
|
+
rescue *ERRORS => e
|
8
|
+
raise ConstraintError, e.message
|
9
|
+
rescue Sequel::DatabaseError => e
|
10
|
+
raise DatabaseError.new(e, e.message)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'rom/sql/commands'
|
2
|
+
require 'rom/sql/commands/error_wrapper'
|
2
3
|
require 'rom/sql/commands/transaction'
|
3
4
|
|
4
5
|
module ROM
|
@@ -6,10 +7,12 @@ module ROM
|
|
6
7
|
module Commands
|
7
8
|
class Update < ROM::Commands::Update
|
8
9
|
include Transaction
|
10
|
+
include ErrorWrapper
|
9
11
|
|
10
12
|
option :original, type: Hash, reader: true
|
11
13
|
|
12
14
|
alias_method :to, :call
|
15
|
+
alias_method :set, :call
|
13
16
|
|
14
17
|
def execute(tuple)
|
15
18
|
attributes = input[tuple]
|
data/lib/rom/sql/migration.rb
CHANGED
@@ -1,23 +1,86 @@
|
|
1
|
+
require 'rom/sql/migration/migrator'
|
2
|
+
|
1
3
|
module ROM
|
2
4
|
module SQL
|
3
|
-
|
4
|
-
|
5
|
+
# Create a database migration for a specific repository
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# ROM.setup(
|
9
|
+
# default: [:sql, 'sqlite::memory'],
|
10
|
+
# other: [:sql, 'postgres://localhost/test']
|
11
|
+
# )
|
12
|
+
#
|
13
|
+
# ROM.finalize
|
14
|
+
#
|
15
|
+
# ROM::SQL.migration do
|
16
|
+
# change do
|
17
|
+
# create_table(:users) do
|
18
|
+
# primary_key :id
|
19
|
+
# String :name
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# # for a non-default repository
|
25
|
+
# ROM::SQL.migration(:other) do
|
26
|
+
# # ...
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# @api public
|
30
|
+
def self.migration(repository = :default, &block)
|
31
|
+
ROM.env.repositories[repository].migration(&block)
|
32
|
+
end
|
33
|
+
|
34
|
+
module Migration
|
35
|
+
Sequel.extension :migration
|
5
36
|
|
6
|
-
|
37
|
+
def self.included(klass)
|
38
|
+
super
|
39
|
+
klass.class_eval do
|
40
|
+
option :migrator, reader: true, default: proc { |repository|
|
41
|
+
Migrator.new(repository.connection)
|
42
|
+
}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# @see ROM::SQL.migration
|
47
|
+
#
|
48
|
+
# @api public
|
49
|
+
def migration(&block)
|
50
|
+
migrator.migration(&block)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Run migrations for a given repository
|
54
|
+
#
|
55
|
+
# @example
|
56
|
+
# ROM.setup(:sql, ['sqlite::memory'])
|
57
|
+
# ROM.finalize
|
58
|
+
# ROM.env.repositories[:default].run_migrations
|
59
|
+
#
|
60
|
+
#
|
61
|
+
# @param [Hash] options The options used by Sequel migrator
|
62
|
+
#
|
63
|
+
# @api public
|
64
|
+
def run_migrations(options = {})
|
65
|
+
migrator.run(options)
|
66
|
+
end
|
7
67
|
|
68
|
+
# TODO: this should be removed in favor of migration API in Repository
|
8
69
|
class << self
|
9
70
|
attr_writer :path
|
10
71
|
attr_accessor :connection
|
11
72
|
|
12
73
|
def path
|
13
|
-
@path || DEFAULT_PATH
|
74
|
+
@path || Migrator::DEFAULT_PATH
|
14
75
|
end
|
15
76
|
|
16
77
|
def run(options = {})
|
78
|
+
warn "ROM::SQL::Migration.run is deprecated please ROM::SQL::Repository#run_migrations (#{caller[0]})"
|
17
79
|
::Sequel::Migrator.run(connection, path, options)
|
18
80
|
end
|
19
81
|
|
20
82
|
def create(&block)
|
83
|
+
warn "ROM::SQL::Migration.create is deprecated please use ROM::SQL.migration (#{caller[0]})"
|
21
84
|
::Sequel.migration(&block)
|
22
85
|
end
|
23
86
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module ROM
|
2
|
+
module SQL
|
3
|
+
module Migration
|
4
|
+
class Migrator
|
5
|
+
include Options
|
6
|
+
|
7
|
+
DEFAULT_PATH = 'db/migrate'.freeze
|
8
|
+
VERSION_FORMAT = '%Y%m%d%H%M%S'.freeze
|
9
|
+
|
10
|
+
option :path, reader: true, default: DEFAULT_PATH
|
11
|
+
|
12
|
+
attr_reader :connection
|
13
|
+
|
14
|
+
def initialize(connection, options = {})
|
15
|
+
super
|
16
|
+
@connection = connection
|
17
|
+
end
|
18
|
+
|
19
|
+
def run(options = {})
|
20
|
+
Sequel::Migrator.run(connection, path.to_s, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
def migration(&block)
|
24
|
+
Sequel.migration(&block)
|
25
|
+
end
|
26
|
+
|
27
|
+
def create_file(name, version = generate_version)
|
28
|
+
filename = "#{version}_#{name}.rb"
|
29
|
+
dirname = Pathname(path)
|
30
|
+
fullpath = dirname.join(filename)
|
31
|
+
|
32
|
+
FileUtils.mkdir_p(dirname)
|
33
|
+
File.write(fullpath, migration_file_content)
|
34
|
+
|
35
|
+
fullpath
|
36
|
+
end
|
37
|
+
|
38
|
+
def generate_version
|
39
|
+
Time.now.utc.strftime(VERSION_FORMAT)
|
40
|
+
end
|
41
|
+
|
42
|
+
def migration_file_content
|
43
|
+
File.read(Pathname(__FILE__).dirname.join('template.rb').realpath)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/rom/sql/relation.rb
CHANGED
data/lib/rom/sql/repository.rb
CHANGED
@@ -1,11 +1,15 @@
|
|
1
1
|
require 'logger'
|
2
2
|
|
3
3
|
require 'rom/repository'
|
4
|
+
require 'rom/sql/migration'
|
4
5
|
require 'rom/sql/commands'
|
5
6
|
|
6
7
|
module ROM
|
7
8
|
module SQL
|
8
9
|
class Repository < ROM::Repository
|
10
|
+
include Options
|
11
|
+
include Migration
|
12
|
+
|
9
13
|
# Return optionally configured logger
|
10
14
|
#
|
11
15
|
# @return [Object] logger
|
@@ -49,8 +53,13 @@ module ROM
|
|
49
53
|
#
|
50
54
|
# @api public
|
51
55
|
def initialize(uri, options = {})
|
52
|
-
|
56
|
+
repo_options = self.class.option_definitions.names
|
57
|
+
conn_options = options.reject { |k,_| repo_options.include?(k) }
|
58
|
+
|
59
|
+
@connection = connect(uri, conn_options)
|
53
60
|
@schema = connection.tables
|
61
|
+
|
62
|
+
super(uri, options.reject { |k,_| conn_options.keys.include?(k) })
|
54
63
|
end
|
55
64
|
|
56
65
|
# Disconnect from database
|
@@ -3,32 +3,38 @@ require "fileutils"
|
|
3
3
|
|
4
4
|
namespace :db do
|
5
5
|
desc "Perform migration reset (full erase and migration up)"
|
6
|
-
task reset: :
|
7
|
-
ROM
|
8
|
-
|
6
|
+
task reset: :setup do
|
7
|
+
repository = ROM.env.repositories[:default]
|
8
|
+
repository.run_migrations(target: 0)
|
9
|
+
repository.run_migrations
|
9
10
|
puts "<= db:reset executed"
|
10
11
|
end
|
11
12
|
|
12
13
|
desc "Migrate the database (options [version_number])]"
|
13
|
-
task :migrate, [:version] => :
|
14
|
+
task :migrate, [:version] => :setup do |_, args|
|
15
|
+
repository = ROM.env.repositories[:default]
|
14
16
|
version = args[:version]
|
17
|
+
|
15
18
|
if version.nil?
|
16
|
-
|
19
|
+
repository.run_migrations
|
17
20
|
puts "<= db:migrate executed"
|
18
21
|
else
|
19
|
-
|
22
|
+
repository.run_migrations(target: version.to_i)
|
20
23
|
puts "<= db:migrate version=[#{version}] executed"
|
21
24
|
end
|
22
25
|
end
|
23
26
|
|
24
27
|
desc "Perform migration down (erase all data)"
|
25
|
-
task clean: :
|
26
|
-
ROM
|
28
|
+
task clean: :setup do
|
29
|
+
repository = ROM.env.repositories[:default]
|
30
|
+
|
31
|
+
repository.run_migrations(target: 0)
|
27
32
|
puts "<= db:clean executed"
|
28
33
|
end
|
29
34
|
|
30
35
|
desc "Create a migration (parameters: NAME, VERSION)"
|
31
|
-
task :create_migration, [:name, :version] => :
|
36
|
+
task :create_migration, [:name, :version] => :setup do |_, args|
|
37
|
+
repository = ROM.env.repositories[:default]
|
32
38
|
name, version = args[:name], args[:version]
|
33
39
|
|
34
40
|
if name.nil?
|
@@ -37,20 +43,8 @@ namespace :db do
|
|
37
43
|
exit
|
38
44
|
end
|
39
45
|
|
40
|
-
|
41
|
-
|
42
|
-
filename = "#{version}_#{name}.rb"
|
43
|
-
dirname = ROM::SQL::Migration.path
|
44
|
-
path = File.join(dirname, filename)
|
45
|
-
|
46
|
-
FileUtils.mkdir_p(dirname)
|
47
|
-
File.write path, <<-MIGRATION
|
48
|
-
ROM::SQL::Migration.create do
|
49
|
-
change do
|
50
|
-
end
|
51
|
-
end
|
52
|
-
MIGRATION
|
46
|
+
path = repository.migrator.create_file(*[name, version].compact)
|
53
47
|
|
54
|
-
puts path
|
48
|
+
puts "<= migration file created #{path}"
|
55
49
|
end
|
56
50
|
end
|
data/lib/rom/sql/version.rb
CHANGED
@@ -106,4 +106,13 @@ describe 'Commands / Create' do
|
|
106
106
|
expect(result.error).to be_instance_of(ROM::SQL::ConstraintError)
|
107
107
|
expect(result.error.message).to match(/unique/)
|
108
108
|
end
|
109
|
+
|
110
|
+
it 'handles database errors' do
|
111
|
+
Params.attribute :bogus_field
|
112
|
+
|
113
|
+
result = users.try { users.create.call(name: 'some name', bogus_field: 23) }
|
114
|
+
|
115
|
+
expect(result.error).to be_instance_of(ROM::SQL::DatabaseError)
|
116
|
+
expect(result.error.original_exception).to be_instance_of(Sequel::DatabaseError)
|
117
|
+
end
|
109
118
|
end
|
@@ -52,4 +52,16 @@ describe 'Commands / Delete' do
|
|
52
52
|
|
53
53
|
expect(result.value).to eql(id: 2, name: 'Jane')
|
54
54
|
end
|
55
|
+
|
56
|
+
it 'handles database errors' do
|
57
|
+
command = users.delete.by_name('Jane')
|
58
|
+
|
59
|
+
expect(command.relation).to receive(:delete).and_raise(Sequel::DatabaseError)
|
60
|
+
|
61
|
+
result = users.try { command.call }
|
62
|
+
|
63
|
+
expect(result.value).to be(nil)
|
64
|
+
expect(result.error).to be_a(ROM::SQL::DatabaseError)
|
65
|
+
expect(result.error.original_exception).to be_a(Sequel::DatabaseError)
|
66
|
+
end
|
55
67
|
end
|
@@ -74,4 +74,14 @@ describe 'Commands / Update' do
|
|
74
74
|
|
75
75
|
expect(result.value.to_a).to be_empty
|
76
76
|
end
|
77
|
+
|
78
|
+
it 'handles database errors' do
|
79
|
+
result = users.try do
|
80
|
+
users.update.by_id(piotr[:id]).set(bogus_field: '#trollface')
|
81
|
+
end
|
82
|
+
|
83
|
+
expect(result.value).to be(nil)
|
84
|
+
expect(result.error).to be_a(ROM::SQL::DatabaseError)
|
85
|
+
expect(result.error.original_exception).to be_a(Sequel::DatabaseError)
|
86
|
+
end
|
77
87
|
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ROM::SQL::Repository do
|
4
|
+
describe 'migration' do
|
5
|
+
let(:conn) { Sequel.connect(DB_URI) }
|
6
|
+
|
7
|
+
context 'creating migrations inline' do
|
8
|
+
subject(:repository) { ROM.env.repositories[:default] }
|
9
|
+
|
10
|
+
before do
|
11
|
+
ROM.setup(:sql, conn)
|
12
|
+
ROM.finalize
|
13
|
+
end
|
14
|
+
|
15
|
+
after do
|
16
|
+
[:rabbits, :carrots].each do |name|
|
17
|
+
repository.connection.drop_table?(name)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'allows creating and running migrations' do
|
22
|
+
migration = ROM::SQL.migration do
|
23
|
+
up do
|
24
|
+
create_table(:rabbits) do
|
25
|
+
primary_key :id
|
26
|
+
String :name
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
down do
|
31
|
+
drop_table(:rabbits)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
migration.apply(repository.connection, :up)
|
36
|
+
|
37
|
+
expect(repository.connection[:rabbits]).to be_a(Sequel::Dataset)
|
38
|
+
|
39
|
+
migration.apply(repository.connection, :down)
|
40
|
+
|
41
|
+
expect(repository.connection.tables).to_not include(:rabbits)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'running migrations from a file system' do
|
46
|
+
let(:migration_dir) do
|
47
|
+
Pathname(__FILE__).dirname.join('../fixtures/migrations').realpath
|
48
|
+
end
|
49
|
+
|
50
|
+
let(:migrator) { ROM::SQL::Migration::Migrator.new(conn, path: migration_dir) }
|
51
|
+
|
52
|
+
before do
|
53
|
+
ROM.setup(:sql, [conn, migrator: migrator])
|
54
|
+
ROM.finalize
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'runs migrations from a specified directory' do
|
58
|
+
ROM.env.repositories[:default].run_migrations
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -1,12 +1,14 @@
|
|
1
1
|
shared_context 'database setup' do
|
2
2
|
subject(:rom) { setup.finalize }
|
3
3
|
|
4
|
-
let(:uri) {
|
4
|
+
let(:uri) { DB_URI }
|
5
5
|
let(:conn) { Sequel.connect(uri) }
|
6
6
|
let(:setup) { ROM.setup(:sql, conn) }
|
7
7
|
|
8
8
|
def drop_tables
|
9
|
-
[:users, :tasks, :tags, :task_tags].each
|
9
|
+
[:users, :tasks, :tags, :task_tags, :rabbits, :carrots, :schema_migrations].each do |name|
|
10
|
+
conn.drop_table?(name)
|
11
|
+
end
|
10
12
|
end
|
11
13
|
|
12
14
|
before do
|
data/spec/spec_helper.rb
CHANGED
@@ -13,14 +13,26 @@ require 'rom/sql/rake_task'
|
|
13
13
|
# FIXME: why do we need to require it manually??
|
14
14
|
require 'sequel/adapters/postgres'
|
15
15
|
require 'logger'
|
16
|
+
begin
|
17
|
+
require 'byebug'
|
18
|
+
rescue LoadError
|
19
|
+
end
|
16
20
|
|
17
21
|
LOGGER = Logger.new(File.open('./log/test.log', 'a'))
|
22
|
+
DB_URI = 'postgres://localhost/rom'
|
18
23
|
|
19
24
|
root = Pathname(__FILE__).dirname
|
25
|
+
TMP_PATH = root.join('../tmp')
|
20
26
|
|
21
27
|
Dir[root.join('shared/*.rb').to_s].each { |f| require f }
|
22
28
|
|
23
29
|
RSpec.configure do |config|
|
30
|
+
config.before(:suite) do
|
31
|
+
tmp_test_dir = TMP_PATH.join('test')
|
32
|
+
FileUtils.rm_r(tmp_test_dir) if File.exist?(tmp_test_dir)
|
33
|
+
FileUtils.mkdir_p(tmp_test_dir)
|
34
|
+
end
|
35
|
+
|
24
36
|
config.before do
|
25
37
|
@constants = Object.constants
|
26
38
|
end
|
@@ -1,15 +1,23 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
namespace :db do
|
4
|
-
task :
|
4
|
+
task :setup do
|
5
|
+
# noop
|
5
6
|
end
|
6
7
|
end
|
7
8
|
|
8
9
|
describe 'MigrationTasks' do
|
10
|
+
before do
|
11
|
+
ROM.setup(:sql, ['postgres://localhost/rom'])
|
12
|
+
ROM.finalize
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:migrator) { ROM.env.repositories[:default].migrator }
|
16
|
+
|
9
17
|
context 'db:reset' do
|
10
18
|
it 'calls proper commands' do
|
11
|
-
expect(
|
12
|
-
expect(
|
19
|
+
expect(migrator).to receive(:run).with(target: 0)
|
20
|
+
expect(migrator).to receive(:run)
|
13
21
|
|
14
22
|
expect {
|
15
23
|
Rake::Task["db:reset"].invoke
|
@@ -20,7 +28,7 @@ describe 'MigrationTasks' do
|
|
20
28
|
context 'db:migrate' do
|
21
29
|
context 'with VERSION' do
|
22
30
|
it 'calls proper commands' do
|
23
|
-
expect(
|
31
|
+
expect(migrator).to receive(:run).with(target: 1)
|
24
32
|
|
25
33
|
expect {
|
26
34
|
Rake::Task["db:migrate"].invoke(1)
|
@@ -30,7 +38,7 @@ describe 'MigrationTasks' do
|
|
30
38
|
|
31
39
|
context 'without VERSION' do
|
32
40
|
it 'calls proper commands' do
|
33
|
-
expect(
|
41
|
+
expect(migrator).to receive(:run)
|
34
42
|
|
35
43
|
expect {
|
36
44
|
Rake::Task["db:migrate"].execute
|
@@ -41,7 +49,7 @@ describe 'MigrationTasks' do
|
|
41
49
|
|
42
50
|
context 'db:clean' do
|
43
51
|
it 'calls proper commands' do
|
44
|
-
expect(
|
52
|
+
expect(migrator).to receive(:run).with(target: 0)
|
45
53
|
|
46
54
|
expect {
|
47
55
|
Rake::Task["db:clean"].invoke
|
@@ -63,36 +71,28 @@ describe 'MigrationTasks' do
|
|
63
71
|
end
|
64
72
|
|
65
73
|
context 'with NAME' do
|
66
|
-
let(:dirname) { 'db/
|
74
|
+
let(:dirname) { 'tmp/db/migrate' }
|
67
75
|
let(:name) { 'foo_bar' }
|
68
76
|
let(:version) { '001' }
|
69
77
|
let(:filename) { "#{version}_#{name}.rb" }
|
70
78
|
let(:path) { File.join(dirname, filename) }
|
71
79
|
|
72
|
-
before do
|
73
|
-
expect(ROM::SQL::Migration).to receive(:path).and_return(dirname)
|
74
|
-
end
|
75
|
-
|
76
80
|
it 'calls proper commands with default VERSION' do
|
77
|
-
|
78
|
-
expect(Time).to receive(:now).and_return(time)
|
79
|
-
expect(FileUtils).to receive(:mkdir_p).with(dirname)
|
80
|
-
expect(File).to receive(:write).with(path, /ROM::SQL::Migration/)
|
81
|
+
expect(migrator).to receive(:create_file).with(name).and_return(path)
|
81
82
|
|
82
83
|
expect {
|
83
84
|
Rake::Task["db:create_migration"].execute(
|
84
85
|
Rake::TaskArguments.new([:name], [name]))
|
85
|
-
}.to output(path
|
86
|
+
}.to output("<= migration file created #{path}\n").to_stdout
|
86
87
|
end
|
87
88
|
|
88
89
|
it 'calls proper commands with manualy set VERSION' do
|
89
|
-
expect(
|
90
|
-
expect(File).to receive(:write).with(path, /ROM::SQL::Migration/)
|
90
|
+
expect(migrator).to receive(:create_file).with(name, version).and_return(path)
|
91
91
|
|
92
92
|
expect {
|
93
93
|
Rake::Task["db:create_migration"].execute(
|
94
94
|
Rake::TaskArguments.new([:name, :version], [name, version]))
|
95
|
-
}.to output(path
|
95
|
+
}.to output("<= migration file created #{path}\n").to_stdout
|
96
96
|
end
|
97
97
|
end
|
98
98
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ROM::SQL::Migration::Migrator do
|
4
|
+
subject(:migrator) { ROM::SQL::Migration::Migrator.new(conn, options) }
|
5
|
+
|
6
|
+
let(:conn) { Sequel.connect(DB_URI) }
|
7
|
+
let(:options) { { path: TMP_PATH.join('test/migrations') } }
|
8
|
+
|
9
|
+
describe '#create_file' do
|
10
|
+
it 'creates a migration file under configured path with specified version and name' do
|
11
|
+
file_path = migrator.create_file('create_users', 1)
|
12
|
+
|
13
|
+
expect(file_path).to eql(migrator.path.join('1_create_users.rb'))
|
14
|
+
expect(File.exist?(file_path)).to be(true)
|
15
|
+
expect(File.read(file_path)).to eql(migrator.migration_file_content)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'auto-generates version when it is not provided' do
|
19
|
+
file_path = migrator.create_file('create_users')
|
20
|
+
|
21
|
+
expect(file_path.to_s).to match(/.(\d+)_create_users\.rb/)
|
22
|
+
expect(File.exist?(file_path)).to be(true)
|
23
|
+
expect(File.read(file_path)).to eql(migrator.migration_file_content)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -23,6 +23,20 @@ describe ROM::SQL::Repository do
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
+
describe 'using options' do
|
27
|
+
it 'allows custom sequel-specific options' do
|
28
|
+
migrator = double('migrator')
|
29
|
+
|
30
|
+
expect(Sequel).to receive(:connect)
|
31
|
+
.with(DB_URI, host: '127.0.0.1')
|
32
|
+
.and_return(conn)
|
33
|
+
|
34
|
+
repository = ROM::SQL::Repository.new(DB_URI, migrator: migrator, host: '127.0.0.1')
|
35
|
+
|
36
|
+
expect(repository.options).to eql(migrator: migrator)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
26
40
|
describe '#disconnect' do
|
27
41
|
let(:repository) { ROM::SQL::Repository.new(uri) }
|
28
42
|
|
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.4.
|
4
|
+
version: 0.4.1
|
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-
|
11
|
+
date: 2015-04-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|
@@ -109,11 +109,14 @@ files:
|
|
109
109
|
- lib/rom/sql/commands.rb
|
110
110
|
- lib/rom/sql/commands/create.rb
|
111
111
|
- lib/rom/sql/commands/delete.rb
|
112
|
+
- lib/rom/sql/commands/error_wrapper.rb
|
112
113
|
- lib/rom/sql/commands/transaction.rb
|
113
114
|
- lib/rom/sql/commands/update.rb
|
114
115
|
- lib/rom/sql/commands_ext/postgres.rb
|
115
116
|
- lib/rom/sql/header.rb
|
116
117
|
- lib/rom/sql/migration.rb
|
118
|
+
- lib/rom/sql/migration/migrator.rb
|
119
|
+
- lib/rom/sql/migration/template.rb
|
117
120
|
- lib/rom/sql/plugin/pagination.rb
|
118
121
|
- lib/rom/sql/rake_task.rb
|
119
122
|
- lib/rom/sql/relation.rb
|
@@ -128,10 +131,12 @@ files:
|
|
128
131
|
- lib/rom/sql/version.rb
|
129
132
|
- log/.gitkeep
|
130
133
|
- rom-sql.gemspec
|
134
|
+
- spec/fixtures/migrations/20150403090603_create_carrots.rb
|
131
135
|
- spec/integration/commands/create_spec.rb
|
132
136
|
- spec/integration/commands/delete_spec.rb
|
133
137
|
- spec/integration/commands/update_spec.rb
|
134
138
|
- spec/integration/read_spec.rb
|
139
|
+
- spec/integration/repository_spec.rb
|
135
140
|
- spec/shared/database_setup.rb
|
136
141
|
- spec/shared/users_and_tasks.rb
|
137
142
|
- spec/spec_helper.rb
|
@@ -141,8 +146,8 @@ files:
|
|
141
146
|
- spec/unit/logger_spec.rb
|
142
147
|
- spec/unit/many_to_many_spec.rb
|
143
148
|
- spec/unit/many_to_one_spec.rb
|
144
|
-
- spec/unit/migration_spec.rb
|
145
149
|
- spec/unit/migration_tasks_spec.rb
|
150
|
+
- spec/unit/migrator_spec.rb
|
146
151
|
- spec/unit/one_to_many_spec.rb
|
147
152
|
- spec/unit/plugin/pagination_spec.rb
|
148
153
|
- spec/unit/relation_spec.rb
|
@@ -173,10 +178,12 @@ signing_key:
|
|
173
178
|
specification_version: 4
|
174
179
|
summary: SQL databases support for ROM
|
175
180
|
test_files:
|
181
|
+
- spec/fixtures/migrations/20150403090603_create_carrots.rb
|
176
182
|
- spec/integration/commands/create_spec.rb
|
177
183
|
- spec/integration/commands/delete_spec.rb
|
178
184
|
- spec/integration/commands/update_spec.rb
|
179
185
|
- spec/integration/read_spec.rb
|
186
|
+
- spec/integration/repository_spec.rb
|
180
187
|
- spec/shared/database_setup.rb
|
181
188
|
- spec/shared/users_and_tasks.rb
|
182
189
|
- spec/spec_helper.rb
|
@@ -186,10 +193,11 @@ test_files:
|
|
186
193
|
- spec/unit/logger_spec.rb
|
187
194
|
- spec/unit/many_to_many_spec.rb
|
188
195
|
- spec/unit/many_to_one_spec.rb
|
189
|
-
- spec/unit/migration_spec.rb
|
190
196
|
- spec/unit/migration_tasks_spec.rb
|
197
|
+
- spec/unit/migrator_spec.rb
|
191
198
|
- spec/unit/one_to_many_spec.rb
|
192
199
|
- spec/unit/plugin/pagination_spec.rb
|
193
200
|
- spec/unit/relation_spec.rb
|
194
201
|
- spec/unit/repository_spec.rb
|
195
202
|
- spec/unit/schema_spec.rb
|
203
|
+
has_rdoc:
|
data/spec/unit/migration_spec.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe ROM::SQL::Migration do
|
4
|
-
let(:migration) { ROM::SQL::Migration }
|
5
|
-
|
6
|
-
context '#run' do
|
7
|
-
it 'calls Sequel migration code' do
|
8
|
-
migration.path = 'foo/bar'
|
9
|
-
migration.connection = double
|
10
|
-
opts = { foo: 'bar' }
|
11
|
-
|
12
|
-
expect(Sequel::Migrator).to receive(:run)
|
13
|
-
.with(migration.connection, migration.path, opts)
|
14
|
-
|
15
|
-
migration.run(opts)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
context '#path' do
|
20
|
-
it 'returns default path if non provided' do
|
21
|
-
migration.path = nil
|
22
|
-
|
23
|
-
expect(migration.path).to eq ROM::SQL::Migration::DEFAULT_PATH
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
context '.create' do
|
28
|
-
it 'calls Sequel migration block' do
|
29
|
-
expect(Sequel).to receive(:migration)
|
30
|
-
|
31
|
-
ROM::SQL::Migration.create {}
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|