db_schema-definitions 0.1.1 → 0.2.rc1

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
- SHA1:
3
- metadata.gz: ae2df87f767542bd232c75d6b0ac42697b0d4fb8
4
- data.tar.gz: f6f39c373daa3228b8e4b3d532a91b568e0cfe36
2
+ SHA256:
3
+ metadata.gz: '09c3d434a88ffdce3c3011e34bec258ffec9c663c55adaa98626df50522cbf75'
4
+ data.tar.gz: c9e426e1f905e4570623e34a32f9538774d4315b0fb196e84c09c32dc252ce56
5
5
  SHA512:
6
- metadata.gz: ada6d9fac2caba310326c2934ff79d03fc553c335c9b597790134c1b0745e3430d0c603f5112eef31de2972a1ec371117c6753081c95c271822080455d9993d8
7
- data.tar.gz: 8023878809927aa1f241afcd8a0d79f1103b8990e9a38e04e105b4edef6901a827510a5484362634c121ae30df95f4a1401d04475683cd708e666193813bbeb1
6
+ metadata.gz: b795076bd8068230f48cca952b0343ef32a9eaf982e499cacdbe2cc197c8f9400dc9af9bd366abe89bdb44c3d69ed60de995495b7d6bb8902cbfd430f447be72
7
+ data.tar.gz: 181ee3609987fa55dc902b3288870e9ee2be7ff85c029ff0ebcece488fe962ccaf6e713699d2cade997d3ea1a23b954d85ccad15eb84b2c720f8d82071ae041d
data/.travis.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.2.8
5
- - 2.3.5
6
- - 2.4.2
7
- before_install: gem install bundler -v 1.16.0
4
+ - 2.3.8
5
+ - 2.4.5
6
+ - 2.5.3
7
+ - 2.6.0
@@ -0,0 +1,159 @@
1
+ begin
2
+ require 'awesome_print'
3
+ rescue LoadError
4
+ end
5
+
6
+ if defined?(AwesomePrint)
7
+ module AwesomePrint
8
+ module DbSchema
9
+ module Definitions
10
+ def self.included(base)
11
+ base.send :alias_method, :cast_without_dbschema_definitions, :cast
12
+ base.send :alias_method, :cast, :cast_with_dbschema_definitions
13
+ end
14
+
15
+ def cast_with_dbschema_definitions(object, type)
16
+ case object
17
+ when ::DbSchema::Definitions::Schema
18
+ :dbschema_definitions_schema
19
+ when ::DbSchema::Definitions::NullTable,
20
+ ::DbSchema::Definitions::NullField,
21
+ ::DbSchema::Definitions::NullIndex,
22
+ ::DbSchema::Definitions::NullCheckConstraint,
23
+ ::DbSchema::Definitions::NullForeignKey,
24
+ ::DbSchema::Definitions::NullEnum
25
+ :dbschema_definitions_null_object
26
+ when ::DbSchema::Definitions::Table
27
+ :dbschema_definitions_table
28
+ when ::DbSchema::Definitions::Field::Base
29
+ :dbschema_definitions_field
30
+ when ::DbSchema::Definitions::Index
31
+ :dbschema_definitions_index
32
+ when ::DbSchema::Definitions::Index::Column
33
+ :dbschema_definitions_index_column
34
+ when ::DbSchema::Definitions::CheckConstraint
35
+ :dbschema_definitions_check_constraint
36
+ when ::DbSchema::Definitions::ForeignKey
37
+ :dbschema_definitions_foreign_key
38
+ when ::DbSchema::Definitions::Enum
39
+ :dbschema_definitions_enum
40
+ when ::DbSchema::Definitions::Extension
41
+ :dbschema_definitions_extension
42
+ else
43
+ cast_without_dbschema_definitions(object, type)
44
+ end
45
+ end
46
+
47
+ private
48
+ def awesome_dbschema_definitions_schema(schema)
49
+ data = ["tables: #{schema.tables.ai}"]
50
+ data << "enums: #{schema.enums.ai}" if schema.enums.any?
51
+ data << "extensions: #{schema.extensions.ai}" if schema.extensions.any?
52
+
53
+ data_string = data.join(', ')
54
+ "#<DbSchema::Definitions::Schema #{data_string}>"
55
+ end
56
+
57
+ def awesome_dbschema_definitions_null_object(object)
58
+ "#<#{object.class}>"
59
+ end
60
+
61
+ def awesome_dbschema_definitions_table(table)
62
+ data = ["fields: #{table.fields.ai}"]
63
+ data << "indexes: #{table.indexes.ai}" if table.indexes.any?
64
+ data << "checks: #{table.checks.ai}" if table.checks.any?
65
+ data << "foreign_keys: #{table.foreign_keys.ai}" if table.foreign_keys.any?
66
+
67
+ data_string = indent_lines(data.join(', '))
68
+ "#<DbSchema::Definitions::Table #{table.name.ai} #{data_string}>"
69
+ end
70
+
71
+ def awesome_dbschema_definitions_field(field)
72
+ options = field.options.map do |k, v|
73
+ key = colorize("#{k}:", :symbol)
74
+
75
+ if (k == :default) && v.is_a?(Symbol)
76
+ "#{key} #{colorize(v.to_s, :string)}"
77
+ else
78
+ "#{key} #{v.ai}"
79
+ end
80
+ end.unshift(nil).join(', ')
81
+
82
+ if field.custom?
83
+ "#<DbSchema::Definitions::Field::Custom (#{field.type.ai}) #{field.name.ai}#{options}>"
84
+ else
85
+ "#<#{field.class} #{field.name.ai}#{options}>"
86
+ end
87
+ end
88
+
89
+ def awesome_dbschema_definitions_index(index)
90
+ columns = format_dbschema_fields(index.columns)
91
+ using = ' using ' + colorize(index.type.to_s, :symbol) unless index.btree?
92
+
93
+ data = [nil]
94
+ data << colorize('primary key', :nilclass) if index.primary?
95
+ data << colorize('unique', :nilclass) if index.unique?
96
+ data << colorize('condition: ', :symbol) + index.condition.ai unless index.condition.nil?
97
+
98
+ "#<#{index.class} #{index.name.ai} on #{columns}#{using}#{data.join(', ')}>"
99
+ end
100
+
101
+ def awesome_dbschema_definitions_index_column(column)
102
+ data = [column.name.ai]
103
+
104
+ if column.desc?
105
+ data << colorize('desc', :nilclass)
106
+ data << colorize('nulls last', :symbol) if column.nulls == :last
107
+ else
108
+ data << colorize('nulls first', :symbol) if column.nulls == :first
109
+ end
110
+
111
+ data.join(' ')
112
+ end
113
+
114
+ def awesome_dbschema_definitions_check_constraint(check)
115
+ "#<#{check.class} #{check.name.ai} #{check.condition.ai}>"
116
+ end
117
+
118
+ def awesome_dbschema_definitions_foreign_key(fkey)
119
+ fields = format_dbschema_fields(fkey.fields)
120
+ references = "#{colorize('references', :class)} #{fkey.table.ai}"
121
+ references << ' ' + format_dbschema_fields(fkey.keys) unless fkey.references_primary_key?
122
+
123
+ data = [nil]
124
+ data << colorize("on_update:", :symbol) + " #{fkey.on_update.ai}" unless fkey.on_update == :no_action
125
+ data << colorize("on_delete:", :symbol) + " #{fkey.on_delete.ai}" unless fkey.on_delete == :no_action
126
+ data << colorize('deferrable', :nilclass) if fkey.deferrable?
127
+
128
+ "#<#{fkey.class} #{fkey.name.ai} on #{fields} #{references}#{data.join(', ')}>"
129
+ end
130
+
131
+ def awesome_dbschema_definitions_enum(enum)
132
+ values = enum.values.map do |value|
133
+ colorize(value.to_s, :string)
134
+ end.join(', ')
135
+
136
+ "#<#{enum.class} #{enum.name.ai} (#{values})>"
137
+ end
138
+
139
+ def awesome_dbschema_definitions_extension(extension)
140
+ "#<#{extension.class} #{extension.name.ai}>"
141
+ end
142
+
143
+ def format_dbschema_fields(fields)
144
+ if fields.one?
145
+ fields.first.ai
146
+ else
147
+ '[' + fields.map(&:ai).join(', ') + ']'
148
+ end
149
+ end
150
+
151
+ def indent_lines(text, indent_level = 4)
152
+ text.gsub(/(?<!\A)^/, ' ' * indent_level)
153
+ end
154
+ end
155
+ end
156
+ end
157
+
158
+ AwesomePrint::Formatter.send(:include, AwesomePrint::DbSchema::Definitions)
159
+ end
@@ -2,23 +2,18 @@ module DbSchema
2
2
  module Definitions
3
3
  module Field
4
4
  class Base
5
- include Dry::Equalizer(:name, :type, :primary_key?, :options)
5
+ include Dry::Equalizer(:name, :type, :options)
6
6
  attr_reader :name, :default
7
7
 
8
- def initialize(name, primary_key: false, null: true, default: nil, **attributes)
9
- @name = name
10
- @primary_key = primary_key
11
- @null = null
12
- @default = default
13
- @attributes = attributes
14
- end
15
-
16
- def primary_key?
17
- @primary_key
8
+ def initialize(name, null: true, default: nil, **attributes)
9
+ @name = name
10
+ @null = null
11
+ @default = default
12
+ @attributes = attributes
18
13
  end
19
14
 
20
15
  def null?
21
- !primary_key? && @null
16
+ @null
22
17
  end
23
18
 
24
19
  def default_is_expression?
@@ -57,15 +52,19 @@ module DbSchema
57
52
  end
58
53
 
59
54
  def with_type(new_type)
60
- Field.build(name, new_type, **options, primary_key: primary_key?)
55
+ Field.build(name, new_type, **options)
61
56
  end
62
57
 
63
58
  def with_attribute(attr_name, attr_value)
64
- Field.build(name, type, **options, primary_key: primary_key?, attr_name => attr_value)
59
+ Field.build(name, type, **options, attr_name => attr_value)
60
+ end
61
+
62
+ def with_null(null)
63
+ Field.build(name, type, **options, null: null)
65
64
  end
66
65
 
67
66
  def with_default(new_default)
68
- Field.build(name, type, **options, primary_key: primary_key?, default: new_default)
67
+ Field.build(name, type, **options, default: new_default)
69
68
  end
70
69
 
71
70
  class << self
@@ -0,0 +1,17 @@
1
+ module DbSchema
2
+ module Definitions
3
+ module Field
4
+ class SmallSerial < Base
5
+ register :smallserial
6
+ end
7
+
8
+ class Serial < Base
9
+ register :serial
10
+ end
11
+
12
+ class BigSerial < Base
13
+ register :bigserial
14
+ end
15
+ end
16
+ end
17
+ end
@@ -22,6 +22,7 @@ end
22
22
 
23
23
  require_relative 'field/base'
24
24
  require_relative 'field/numeric'
25
+ require_relative 'field/serial'
25
26
  require_relative 'field/monetary'
26
27
  require_relative 'field/character'
27
28
  require_relative 'field/binary'
@@ -1,17 +1,22 @@
1
1
  module DbSchema
2
2
  module Definitions
3
3
  class Index
4
- include Dry::Equalizer(:name, :columns, :unique?, :type, :condition)
4
+ include Dry::Equalizer(:name, :columns, :primary?, :unique?, :type, :condition)
5
5
  attr_reader :name, :columns, :type, :condition
6
6
 
7
- def initialize(name:, columns:, unique: false, type: :btree, condition: nil)
7
+ def initialize(name:, columns:, primary: false, unique: false, type: :btree, condition: nil)
8
8
  @name = name.to_sym
9
9
  @columns = columns
10
+ @primary = primary
10
11
  @unique = unique
11
12
  @type = type
12
13
  @condition = condition
13
14
  end
14
15
 
16
+ def primary?
17
+ @primary
18
+ end
19
+
15
20
  def unique?
16
21
  @unique
17
22
  end
@@ -36,6 +41,7 @@ module DbSchema
36
41
  Index.new(
37
42
  name: new_name,
38
43
  columns: columns,
44
+ primary: primary?,
39
45
  unique: unique?,
40
46
  type: type,
41
47
  condition: condition
@@ -46,6 +52,7 @@ module DbSchema
46
52
  Index.new(
47
53
  name: name,
48
54
  columns: columns,
55
+ primary: primary?,
49
56
  unique: unique?,
50
57
  type: type,
51
58
  condition: new_condition
@@ -47,6 +47,14 @@ module DbSchema
47
47
  end
48
48
  end
49
49
 
50
+ def primary_key
51
+ indexes.find(&:primary?) || NullIndex.new
52
+ end
53
+
54
+ def has_primary_key?
55
+ !primary_key.is_a?(NullIndex)
56
+ end
57
+
50
58
  def check(check_name)
51
59
  checks.find { |check| check.name == check_name } || NullCheckConstraint.new
52
60
  end
@@ -1,5 +1,5 @@
1
1
  module DbSchema
2
2
  module Definitions
3
- VERSION = '0.1.1'
3
+ VERSION = '0.2.rc1'
4
4
  end
5
5
  end
@@ -8,4 +8,5 @@ require_relative 'definitions/foreign_key'
8
8
  require_relative 'definitions/check_constraint'
9
9
  require_relative 'definitions/enum'
10
10
  require_relative 'definitions/extension'
11
+ require_relative 'definitions/awesome_print'
11
12
  require_relative 'definitions/version'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db_schema-definitions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vsevolod Romashov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-15 00:00:00.000000000 Z
11
+ date: 2019-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-equalizer
@@ -156,6 +156,7 @@ files:
156
156
  - bin/setup
157
157
  - db_schema-definitions.gemspec
158
158
  - lib/db_schema/definitions.rb
159
+ - lib/db_schema/definitions/awesome_print.rb
159
160
  - lib/db_schema/definitions/check_constraint.rb
160
161
  - lib/db_schema/definitions/enum.rb
161
162
  - lib/db_schema/definitions/extension.rb
@@ -181,6 +182,7 @@ files:
181
182
  - lib/db_schema/definitions/field/network.rb
182
183
  - lib/db_schema/definitions/field/numeric.rb
183
184
  - lib/db_schema/definitions/field/range.rb
185
+ - lib/db_schema/definitions/field/serial.rb
184
186
  - lib/db_schema/definitions/field/text_search.rb
185
187
  - lib/db_schema/definitions/field/uuid.rb
186
188
  - lib/db_schema/definitions/foreign_key.rb
@@ -206,12 +208,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
206
208
  version: '0'
207
209
  required_rubygems_version: !ruby/object:Gem::Requirement
208
210
  requirements:
209
- - - ">="
211
+ - - ">"
210
212
  - !ruby/object:Gem::Version
211
- version: '0'
213
+ version: 1.3.1
212
214
  requirements: []
213
- rubyforge_project:
214
- rubygems_version: 2.6.13
215
+ rubygems_version: 3.0.1
215
216
  signing_key:
216
217
  specification_version: 4
217
218
  summary: Database object definitions for DbSchema