active_record-postgres-constraints 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Rakefile +1 -0
- data/lib/active_record/postgres/constraints.rb +2 -0
- data/lib/active_record/postgres/constraints/command_recorder.rb +1 -0
- data/lib/active_record/postgres/constraints/postgresql_adapter.rb +1 -0
- data/lib/active_record/postgres/constraints/railtie.rb +35 -17
- data/lib/active_record/postgres/constraints/schema_creation.rb +7 -5
- data/lib/active_record/postgres/constraints/schema_dumper.rb +2 -0
- data/lib/active_record/postgres/constraints/table_definition.rb +1 -0
- data/lib/active_record/postgres/constraints/version.rb +2 -1
- metadata +24 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 590c9903ba5ee75b0ee276ced8118f89b9c0d20d8e29953afa0b5e2c596a7998
|
4
|
+
data.tar.gz: 880f0d33689a35515e3a56c6a847d3766da94d21fee5a16e2b64ab1d3f1e1c76
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f8aa9f3635049259ff5cd216f96a76f080e1d7cd3c49c2bc0089716cb765457f329e467fbd2a072299f11847375fbcb94236e57fdcd5515b5e07d70c209bdc4
|
7
|
+
data.tar.gz: 9fae7cacb9bd526a05acd2b59e9b073c9e9834e9b6b5e9b4eaefa7193f9d9b4ae08cb3c9ed12c8c7df6453d42e5ac6b73d41413256601692d0c3e4e6c4da3c08
|
data/Rakefile
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require_relative 'constraints/command_recorder'
|
3
4
|
require_relative 'constraints/postgresql_adapter'
|
4
5
|
require_relative 'constraints/railtie'
|
@@ -22,6 +23,7 @@ module ActiveRecord
|
|
22
23
|
end
|
23
24
|
|
24
25
|
return conditions.first if 1 == conditions.length
|
26
|
+
|
25
27
|
"(#{conditions.join(') AND (')})"
|
26
28
|
end
|
27
29
|
|
@@ -1,31 +1,49 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module ActiveRecord
|
3
4
|
module Postgres
|
4
5
|
module Constraints
|
5
6
|
class Railtie < Rails::Railtie
|
6
|
-
initializer 'active_record.postgres.constraints.patch_active_record' do
|
7
|
+
initializer 'active_record.postgres.constraints.patch_active_record' do |*_args|
|
8
|
+
engine = self
|
7
9
|
ActiveSupport.on_load(:active_record) do
|
8
10
|
AR_CAS = ::ActiveRecord::ConnectionAdapters
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
engine.apply_patch! if engine.pg?
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def apply_patch!
|
17
|
+
Rails.logger.info do
|
18
|
+
'Applying Postgres Constraints patches to ActiveRecord'
|
19
|
+
end
|
20
|
+
AR_CAS::TableDefinition.include TableDefinition
|
21
|
+
AR_CAS::PostgreSQLAdapter.include PostgreSQLAdapter
|
22
|
+
AR_CAS::AbstractAdapter::SchemaCreation.prepend SchemaCreation
|
19
23
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
::ActiveRecord::Migration::CommandRecorder.include CommandRecorder
|
25
|
+
::ActiveRecord::SchemaDumper.prepend SchemaDumper
|
26
|
+
end
|
27
|
+
|
28
|
+
def pg?
|
29
|
+
begin
|
30
|
+
connection = ActiveRecord::Base.connection
|
31
|
+
rescue ActiveRecord::NoDatabaseError
|
32
|
+
Rails.logger.warn do
|
33
|
+
'Not applying Postgres Constraints patches to ActiveRecord ' \
|
34
|
+
'since the database does not exist'
|
27
35
|
end
|
36
|
+
return false
|
37
|
+
end
|
38
|
+
|
39
|
+
pg = connection.class.to_s == "#{AR_CAS}::PostgreSQLAdapter"
|
40
|
+
return true if pg
|
41
|
+
|
42
|
+
Rails.logger.warn do
|
43
|
+
'Not applying Postgres Constraints patches to ActiveRecord ' \
|
44
|
+
'since the database is not postgres'
|
28
45
|
end
|
46
|
+
false
|
29
47
|
end
|
30
48
|
end
|
31
49
|
end
|
@@ -1,13 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module ActiveRecord
|
3
4
|
module Postgres
|
4
5
|
module Constraints
|
5
6
|
module SchemaCreation
|
6
|
-
# rubocop:disable
|
7
|
-
def visit_TableDefinition(
|
8
|
-
# rubocop:enable
|
7
|
+
# rubocop:disable Naming/MethodName
|
8
|
+
def visit_TableDefinition(table_definition)
|
9
|
+
# rubocop:enable Naming/MethodName
|
9
10
|
result = super
|
10
|
-
return result unless
|
11
|
+
return result unless table_definition.check_constraints
|
12
|
+
|
11
13
|
nesting = 0
|
12
14
|
# Find the closing paren of the "CREATE TABLE ( ... )" clause
|
13
15
|
index = result.length.times do |i|
|
@@ -15,7 +17,7 @@ module ActiveRecord
|
|
15
17
|
nesting, should_break = adjust_nesting(nesting, token)
|
16
18
|
break i if should_break
|
17
19
|
end
|
18
|
-
result[index] = ", #{
|
20
|
+
result[index] = ", #{table_definition.check_constraints.join(', ')})"
|
19
21
|
result
|
20
22
|
end
|
21
23
|
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module ActiveRecord
|
3
4
|
module Postgres
|
4
5
|
module Constraints
|
@@ -7,6 +8,7 @@ module ActiveRecord
|
|
7
8
|
super
|
8
9
|
constraints = @connection.constraints(table)
|
9
10
|
return unless constraints.any?
|
11
|
+
|
10
12
|
constraint_statements = constraints.map do |constraint|
|
11
13
|
name = constraint['conname']
|
12
14
|
conditions = constraint['consrc']
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record-postgres-constraints
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Isaac Betesh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pg
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: rails
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -25,13 +39,13 @@ dependencies:
|
|
25
39
|
- !ruby/object:Gem::Version
|
26
40
|
version: '5.0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
42
|
+
name: osm-rubocop
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
45
|
- - ">="
|
32
46
|
- !ruby/object:Gem::Version
|
33
47
|
version: '0'
|
34
|
-
type: :
|
48
|
+
type: :development
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
@@ -39,19 +53,19 @@ dependencies:
|
|
39
53
|
- !ruby/object:Gem::Version
|
40
54
|
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
56
|
+
name: rspec
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
44
58
|
requirements:
|
45
|
-
- - "
|
59
|
+
- - "~>"
|
46
60
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
61
|
+
version: '3.8'
|
48
62
|
type: :development
|
49
63
|
prerelease: false
|
50
64
|
version_requirements: !ruby/object:Gem::Requirement
|
51
65
|
requirements:
|
52
|
-
- - "
|
66
|
+
- - "~>"
|
53
67
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
68
|
+
version: '3.8'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rspec-rails
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -111,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
125
|
version: '0'
|
112
126
|
requirements: []
|
113
127
|
rubyforge_project:
|
114
|
-
rubygems_version: 2.
|
128
|
+
rubygems_version: 2.7.7
|
115
129
|
signing_key:
|
116
130
|
specification_version: 4
|
117
131
|
summary: Store your constraints in db/schema.rb
|