db_schema-definitions 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +7 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +4 -0
  7. data/Guardfile +15 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +56 -0
  10. data/Rakefile +6 -0
  11. data/bin/console +10 -0
  12. data/bin/setup +6 -0
  13. data/db_schema-definitions.gemspec +31 -0
  14. data/lib/db_schema/definitions.rb +11 -0
  15. data/lib/db_schema/definitions/check_constraint.rb +17 -0
  16. data/lib/db_schema/definitions/enum.rb +23 -0
  17. data/lib/db_schema/definitions/extension.rb +12 -0
  18. data/lib/db_schema/definitions/field.rb +47 -0
  19. data/lib/db_schema/definitions/field/array.rb +30 -0
  20. data/lib/db_schema/definitions/field/base.rb +104 -0
  21. data/lib/db_schema/definitions/field/binary.rb +9 -0
  22. data/lib/db_schema/definitions/field/bit_string.rb +15 -0
  23. data/lib/db_schema/definitions/field/boolean.rb +9 -0
  24. data/lib/db_schema/definitions/field/character.rb +19 -0
  25. data/lib/db_schema/definitions/field/custom.rb +32 -0
  26. data/lib/db_schema/definitions/field/datetime.rb +30 -0
  27. data/lib/db_schema/definitions/field/extensions/chkpass.rb +9 -0
  28. data/lib/db_schema/definitions/field/extensions/citext.rb +9 -0
  29. data/lib/db_schema/definitions/field/extensions/cube.rb +9 -0
  30. data/lib/db_schema/definitions/field/extensions/hstore.rb +9 -0
  31. data/lib/db_schema/definitions/field/extensions/isn.rb +37 -0
  32. data/lib/db_schema/definitions/field/extensions/ltree.rb +9 -0
  33. data/lib/db_schema/definitions/field/extensions/seg.rb +9 -0
  34. data/lib/db_schema/definitions/field/geometric.rb +33 -0
  35. data/lib/db_schema/definitions/field/json.rb +13 -0
  36. data/lib/db_schema/definitions/field/monetary.rb +9 -0
  37. data/lib/db_schema/definitions/field/network.rb +17 -0
  38. data/lib/db_schema/definitions/field/numeric.rb +30 -0
  39. data/lib/db_schema/definitions/field/range.rb +29 -0
  40. data/lib/db_schema/definitions/field/text_search.rb +13 -0
  41. data/lib/db_schema/definitions/field/uuid.rb +9 -0
  42. data/lib/db_schema/definitions/foreign_key.rb +44 -0
  43. data/lib/db_schema/definitions/index.rb +56 -0
  44. data/lib/db_schema/definitions/index/column.rb +32 -0
  45. data/lib/db_schema/definitions/index/expression.rb +19 -0
  46. data/lib/db_schema/definitions/index/table_field.rb +19 -0
  47. data/lib/db_schema/definitions/schema.rb +36 -0
  48. data/lib/db_schema/definitions/table.rb +120 -0
  49. data/lib/db_schema/definitions/version.rb +5 -0
  50. metadata +218 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5602f6cb4adde5c72fc904806b89d00104307a11
4
+ data.tar.gz: b6e99dd29c5a3eea58c8fb780c408c0025d7ea62
5
+ SHA512:
6
+ metadata.gz: eec9c2347e6af8edc27b1835db12332e055dcf57246e702fa70114dec64f7a23b3048ac900bf3f8bc12f45a789f06b2e9adffdf1243be0e964cff53084f88e67
7
+ data.tar.gz: 2b90e7080db55ecafa6c75cb47673ed518b88864d4676a1ba6a6f7484be9d06124e2594b27f108084acaa2c7dbdf7f7a05514978cced37a6d5caf83fd0fc126f
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.8
5
+ - 2.3.5
6
+ - 2.4.2
7
+ before_install: gem install bundler -v 1.16.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at 7@7vn.ru. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in db_schema-definitions.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,15 @@
1
+ guard :rspec, cmd: 'bundle exec rspec', all_on_start: true do
2
+ require 'guard/rspec/dsl'
3
+ dsl = Guard::RSpec::Dsl.new(self)
4
+
5
+ # RSpec files
6
+ rspec = dsl.rspec
7
+ watch(rspec.spec_helper) { rspec.spec_dir }
8
+ watch(rspec.spec_support) { rspec.spec_dir }
9
+ watch(rspec.spec_files)
10
+
11
+ # Ruby files
12
+ ruby = dsl.ruby
13
+ dsl.watch_spec_files_for(ruby.lib_files)
14
+ watch('lib/db_schema/definitions/table.rb') { 'spec/db_schema/definitions/null_table_spec.rb' }
15
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Vsevolod Romashov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # DbSchema::Definitions
2
+
3
+ This gem contains definition classes
4
+ [describing database structure](https://github.com/db-schema/core/wiki/Schema-analysis-DSL)
5
+ for [DbSchema](https://github.com/db-schema/core).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ``` ruby
12
+ gem 'db_schema-definitions'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ``` sh
18
+ $ bundle
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ DbSchema::Definitions is not intended to be used separately;
24
+ it's sole purpose is to provide definition classes for DbSchema
25
+ and it's supporting libraries.
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies.
30
+ Then, run `rake spec` to run the tests. You can also run `bin/console`
31
+ for an interactive prompt that will allow you to experiment.
32
+
33
+ To install this gem onto your local machine, run `bundle exec rake install`.
34
+ To release a new version, update the version number in `version.rb`,
35
+ and then run `bundle exec rake release`, which will create a git tag
36
+ for the version, push git commits and tags, and push the `.gem` file
37
+ to [rubygems.org](https://rubygems.org).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub
42
+ at [db-schema/definitions](https://github.com/db-schema/definitions).
43
+ This project is intended to be a safe, welcoming space for collaboration,
44
+ and contributors are expected to adhere to the
45
+ [Contributor Covenant](http://contributor-covenant.org) code of conduct.
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of
50
+ the [MIT License](https://opensource.org/licenses/MIT).
51
+
52
+ ## Code of Conduct
53
+
54
+ Everyone interacting in the DbSchema::Definitions project’s codebases,
55
+ issue trackers, chat rooms and mailing lists is expected to follow
56
+ the [code of conduct](https://github.com/db-schema/definitions/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'db_schema/definitions'
5
+
6
+ require 'pry'
7
+ require 'awesome_print'
8
+ AwesomePrint.pry!
9
+
10
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'db_schema/definitions/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'db_schema-definitions'
7
+ spec.version = DbSchema::Definitions::VERSION
8
+ spec.authors = ['Vsevolod Romashov']
9
+ spec.email = ['7@7vn.ru']
10
+
11
+ spec.summary = 'Database object definitions for DbSchema'
12
+ spec.homepage = 'https://github.com/db-schema/definitions'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r(^spec/)) }
16
+ spec.bindir = 'exe'
17
+ spec.executables = spec.files.grep(%r(^exe/)) { |f| File.basename(f) }
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_runtime_dependency 'dry-equalizer', '~> 0.2'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.16'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'pry'
25
+ spec.add_development_dependency 'awesome_print', '~> 1.7'
26
+
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'guard-rspec'
29
+ spec.add_development_dependency 'terminal-notifier'
30
+ spec.add_development_dependency 'terminal-notifier-guard'
31
+ end
@@ -0,0 +1,11 @@
1
+ require 'dry/equalizer'
2
+
3
+ require_relative 'definitions/schema'
4
+ require_relative 'definitions/table'
5
+ require_relative 'definitions/field'
6
+ require_relative 'definitions/index'
7
+ require_relative 'definitions/foreign_key'
8
+ require_relative 'definitions/check_constraint'
9
+ require_relative 'definitions/enum'
10
+ require_relative 'definitions/extension'
11
+ require_relative 'definitions/version'
@@ -0,0 +1,17 @@
1
+ module DbSchema
2
+ module Definitions
3
+ class CheckConstraint
4
+ include Dry::Equalizer(:name, :condition)
5
+ attr_reader :name, :condition
6
+
7
+ def initialize(name:, condition:)
8
+ @name = name.to_sym
9
+ @condition = condition
10
+ end
11
+ end
12
+
13
+ class NullCheckConstraint < CheckConstraint
14
+ def initialize; end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ module DbSchema
2
+ module Definitions
3
+ class Enum
4
+ include Dry::Equalizer(:name, :values)
5
+ attr_reader :name, :values
6
+
7
+ def initialize(name, values)
8
+ @name = name
9
+ @values = values
10
+ end
11
+
12
+ def with_name(new_name)
13
+ Enum.new(new_name, values)
14
+ end
15
+ end
16
+
17
+ class NullEnum < Enum
18
+ def initialize
19
+ @values = []
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,12 @@
1
+ module DbSchema
2
+ module Definitions
3
+ class Extension
4
+ include Dry::Equalizer(:name)
5
+ attr_reader :name
6
+
7
+ def initialize(name)
8
+ @name = name
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,47 @@
1
+ module DbSchema
2
+ module Definitions
3
+ module Field
4
+ class << self
5
+ def build(name, type, **options)
6
+ type_class_for(type).new(name, **options)
7
+ end
8
+
9
+ def type_class_for(type)
10
+ registry.fetch(type) do |type|
11
+ Custom.class_for(type)
12
+ end
13
+ end
14
+
15
+ def registry
16
+ @registry ||= {}
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ require_relative 'field/base'
24
+ require_relative 'field/numeric'
25
+ require_relative 'field/monetary'
26
+ require_relative 'field/character'
27
+ require_relative 'field/binary'
28
+ require_relative 'field/datetime'
29
+ require_relative 'field/boolean'
30
+ require_relative 'field/geometric'
31
+ require_relative 'field/network'
32
+ require_relative 'field/bit_string'
33
+ require_relative 'field/text_search'
34
+ require_relative 'field/uuid'
35
+ require_relative 'field/json'
36
+ require_relative 'field/array'
37
+ require_relative 'field/range'
38
+
39
+ require_relative 'field/extensions/chkpass'
40
+ require_relative 'field/extensions/citext'
41
+ require_relative 'field/extensions/cube'
42
+ require_relative 'field/extensions/hstore'
43
+ require_relative 'field/extensions/isn'
44
+ require_relative 'field/extensions/ltree'
45
+ require_relative 'field/extensions/seg'
46
+
47
+ require_relative 'field/custom'
@@ -0,0 +1,30 @@
1
+ module DbSchema
2
+ module Definitions
3
+ module Field
4
+ class Array < Base
5
+ register :array
6
+
7
+ def initialize(name, **options)
8
+ type_class = Field.type_class_for(options[:element_type])
9
+ super(name, **options.merge(element_type: type_class))
10
+ end
11
+
12
+ def attributes
13
+ super.merge(element_type: element_type.type)
14
+ end
15
+
16
+ def array?
17
+ true
18
+ end
19
+
20
+ def element_type
21
+ @attributes[:element_type]
22
+ end
23
+
24
+ def custom_element_type?
25
+ element_type.superclass == Custom
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,104 @@
1
+ module DbSchema
2
+ module Definitions
3
+ module Field
4
+ class Base
5
+ include Dry::Equalizer(:name, :type, :primary_key?, :options)
6
+ attr_reader :name, :default
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
18
+ end
19
+
20
+ def null?
21
+ !primary_key? && @null
22
+ end
23
+
24
+ def default_is_expression?
25
+ default.is_a?(Symbol)
26
+ end
27
+
28
+ def array?
29
+ false
30
+ end
31
+
32
+ def custom?
33
+ false
34
+ end
35
+
36
+ def options
37
+ attributes.tap do |options|
38
+ options[:null] = false unless null?
39
+ options[:default] = default unless default.nil?
40
+ end
41
+ end
42
+
43
+ def attributes
44
+ self.class.valid_attributes.reduce({}) do |hash, attr_name|
45
+ if attr_value = @attributes[attr_name]
46
+ hash.merge(attr_name => attr_value)
47
+ elsif default_value = self.class.default_attribute_values[attr_name]
48
+ hash.merge(attr_name => default_value)
49
+ else
50
+ hash
51
+ end
52
+ end
53
+ end
54
+
55
+ def type
56
+ self.class.type
57
+ end
58
+
59
+ def with_type(new_type)
60
+ Field.build(name, new_type, **options, primary_key: primary_key?)
61
+ end
62
+
63
+ def with_attribute(attr_name, attr_value)
64
+ Field.build(name, type, **options, primary_key: primary_key?, attr_name => attr_value)
65
+ end
66
+
67
+ class << self
68
+ def register(*types)
69
+ types.each do |type|
70
+ Field.registry[type] = self
71
+ end
72
+ end
73
+
74
+ def attributes(*attr_names, **attributes_with_defaults)
75
+ valid_attributes.push(*attr_names)
76
+
77
+ attributes_with_defaults.each do |attr_name, default_value|
78
+ valid_attributes.push(attr_name)
79
+ default_attribute_values[attr_name] = default_value
80
+ end
81
+ end
82
+
83
+ def valid_attributes
84
+ @valid_attributes ||= []
85
+ end
86
+
87
+ def default_attribute_values
88
+ @default_attribute_values ||= {}
89
+ end
90
+
91
+ def type
92
+ Field.registry.key(self)
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ class NullField < Field::Base
99
+ def initialize
100
+ super(nil)
101
+ end
102
+ end
103
+ end
104
+ end