mimi-db 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8072c855fa74bae3e05aa360546cbaec13b37176
4
+ data.tar.gz: 6a69e3e484e85843a5440c095c893736c9d25972
5
+ SHA512:
6
+ metadata.gz: 749b0f83ca0c5f4379c054b0dd420ac16e82773d061d00c99006478b426d0d052e6454a7d215e26ea48db15401b80bdafab0c9d6b3da3d6b65cb562caf507e17
7
+ data.tar.gz: f56572a825c1f79a9410bc83f4fd5b75d5c2235b6053159f6bff1c3210c5fec033fd1086a633ce34466b876d48ba3faa38f5a4f446a448b4dcbc6698dba3324f
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at alex@kukushk.in. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Alex Kukushkin
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,15 @@
1
+ # mimi-db
2
+
3
+ Database module for [mimi](https://github.com/kukushkin/mimi).
4
+
5
+ [in development]
6
+
7
+ ## Usage
8
+
9
+ ...
10
+
11
+
12
+ ## License
13
+
14
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
15
+
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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mimi/db"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ module Mimi
2
+ module DB
3
+ module Extensions
4
+ def self.start
5
+ install_bigint_primary_keys!
6
+ install_bigint_foreign_keys!
7
+ end
8
+
9
+ def self.install_bigint_primary_keys!
10
+ ca = ActiveRecord::ConnectionAdapters
11
+
12
+ if ca.const_defined? :PostgreSQLAdapter
13
+ ca::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:primary_key] = 'bigserial primary key'
14
+ end
15
+
16
+ if ca.const_defined? :AbstractMysqlAdapter
17
+ ca::AbstractMysqlAdapter::NATIVE_DATABASE_TYPES[:primary_key].gsub!(/int\(11\)/, 'bigint')
18
+ end
19
+ end
20
+
21
+ def self.install_bigint_foreign_keys!
22
+ ActiveRecord::Base.send(:include, Mimi::DB::ForeignKey)
23
+ end
24
+ end # module Extensions
25
+ end # module DB
26
+ end # module Mimi
@@ -0,0 +1,26 @@
1
+ module Mimi
2
+ module DB
3
+ module ForeignKey
4
+ extend ActiveSupport::Concern
5
+
6
+ class_methods do
7
+ #
8
+ # Explicitly specify a (bigint) foreign key
9
+ #
10
+ def foreign_key(name, opts = {})
11
+ opts = { as: :integer, limit: 8 }.merge(opts)
12
+ field(name, opts)
13
+ index(name)
14
+ end
15
+
16
+ # Redefines .belongs_to() with explicitly specified .foreign_key
17
+ #
18
+ def belongs_to(name, opts = {})
19
+ foreign_key(:"#{name}_id")
20
+ # orig_belongs_to(name, opts)
21
+ super
22
+ end
23
+ end
24
+ end # module ForeignKey
25
+ end # module DB
26
+ end # module Mimi
@@ -0,0 +1,5 @@
1
+ module Mimi
2
+ module DB
3
+ VERSION = '0.1.1'.freeze
4
+ end
5
+ end
data/lib/mimi/db.rb ADDED
@@ -0,0 +1,121 @@
1
+ require 'mimi/core'
2
+ require 'active_record'
3
+ require 'mini_record'
4
+
5
+ module Mimi
6
+ module DB
7
+ include Mimi::Core::Module
8
+
9
+ default_options(
10
+ require_files: 'app/models/**/*.rb',
11
+ db_adapter: 'sqlite3',
12
+ db_database: nil,
13
+ db_host: nil,
14
+ db_port: nil,
15
+ db_username: nil,
16
+ db_password: nil,
17
+ db_log_level: :info,
18
+ db_pool: 15
19
+ # db_encoding:
20
+ )
21
+
22
+ def self.module_path
23
+ Pathname.new(__dir__).join('..').join('..').expand_path
24
+ end
25
+
26
+ def self.module_manifest
27
+ {
28
+ db_adapter: {
29
+ desc: 'Database adapter ("sqlite3", "postgresql", "mysql" etc)',
30
+ default: 'sqlite3'
31
+ },
32
+ db_database: {
33
+ desc: 'Database name (e.g. "tmp/mydb")',
34
+ # required
35
+ },
36
+ db_host: {
37
+ desc: 'Database host',
38
+ default: nil
39
+ },
40
+ db_port: {
41
+ desc: 'Database port',
42
+ default: nil
43
+ },
44
+ db_username: {
45
+ desc: 'Database username',
46
+ default: nil
47
+ },
48
+ db_password: {
49
+ desc: 'Database password',
50
+ default: nil
51
+ },
52
+ db_pool: {
53
+ desc: 'Database connection pool size',
54
+ default: 15
55
+ },
56
+ db_log_level: {
57
+ desc: 'Logging level for database layer ("debug", "info" etc)',
58
+ default: 'info'
59
+ }
60
+ }
61
+ end
62
+
63
+ def self.configure(*)
64
+ super
65
+ ActiveRecord::Base.logger = logger
66
+ ActiveRecord::Base.configurations = { 'default' => active_record_config }
67
+ end
68
+
69
+ def self.logger
70
+ @logger ||= Mimi::Logger.new(level: module_options[:db_log_level])
71
+ end
72
+
73
+ def self.start
74
+ ActiveRecord::Base.establish_connection(:default)
75
+ Mimi::DB::Extensions.start
76
+ Mimi.require_files(module_options[:require_files]) if module_options[:require_files]
77
+ super
78
+ end
79
+
80
+ def self.active_record_config
81
+ {
82
+ adapter: module_options[:db_adapter],
83
+ database: module_options[:db_database],
84
+ host: module_options[:db_host],
85
+ port: module_options[:db_port],
86
+ username: module_options[:db_username],
87
+ password: module_options[:db_password],
88
+ encoding: module_options[:db_encoding],
89
+ pool: module_options[:db_pool],
90
+ reaping_frequency: 15
91
+ }.stringify_keys
92
+ end
93
+
94
+ def self.models
95
+ ActiveRecord::Base.descendants
96
+ end
97
+
98
+ def self.migrate_schema!
99
+ models.each(&:auto_upgrade!)
100
+ end
101
+
102
+ def self.create!
103
+ ActiveRecord::Tasks::DatabaseTasks.root = Mimi.app_root_path
104
+ ActiveRecord::Tasks::DatabaseTasks.create(active_record_config)
105
+ end
106
+
107
+ def self.drop!
108
+ ActiveRecord::Tasks::DatabaseTasks.root = Mimi.app_root_path
109
+ ActiveRecord::Tasks::DatabaseTasks.drop(active_record_config)
110
+ end
111
+
112
+ def self.clear!
113
+ ActiveRecord::Tasks::DatabaseTasks.root = Mimi.app_root_path
114
+ ActiveRecord::Tasks::DatabaseTasks.purge(active_record_config)
115
+ end
116
+ end # module DB
117
+ end # module Mimi
118
+
119
+ require_relative 'db/version'
120
+ require_relative 'db/extensions'
121
+ require_relative 'db/foreign_key'
data/lib/tasks/db.rake ADDED
@@ -0,0 +1,56 @@
1
+ namespace :db do
2
+ #
3
+ # A task that ensures the application is configured and the database connection started
4
+ #
5
+ task start: :"application:configure" do
6
+ Mimi::DB.start
7
+ end
8
+
9
+ desc 'Show database config'
10
+ task config: :"application:configure" do
11
+ Mimi::DB.active_record_config.each do |k, v|
12
+ puts "#{k}: #{v}"
13
+ end
14
+ end
15
+
16
+ desc 'Create database'
17
+ task create: :"application:configure" do
18
+ logger.info "* Creating database #{Mimi::DB.module_options[:db_database]}"
19
+ Mimi::DB.create!
20
+ end
21
+
22
+ desc 'Clear database'
23
+ task clear: :"application:configure" do
24
+ logger.info "* Clearing database #{Mimi::DB.module_options[:db_database]}"
25
+ Mimi::DB.clear!
26
+ end
27
+
28
+ desc 'Drop database'
29
+ task drop: :"application:configure" do
30
+ logger.info "* Dropping database #{Mimi::DB.module_options[:db_database]}"
31
+ Mimi::DB.drop!
32
+ end
33
+
34
+ desc 'Migrate database (schema and seeds)'
35
+ task migrate: :"db:start" do
36
+ Rake::Task[:"db:migrate:schema"].invoke
37
+ Rake::Task[:"db:migrate:seeds"].invoke
38
+ end
39
+
40
+ namespace :migrate do
41
+ desc 'Migrate database (seeds only)'
42
+ task seeds: :"db:start" do
43
+ seeds = Pathname.glob(Mimi.app_path_to('db', 'seeds', '**', '*.rb')).sort
44
+ seeds.each do |seed_filename|
45
+ logger.info "* Processing seed: #{seed_filename}"
46
+ load seed_filename
47
+ end
48
+ end
49
+
50
+ desc 'Migrate database (schema only)'
51
+ task schema: :"db:start" do
52
+ logger.info "* Migrating database #{Mimi::DB.module_options[:db_database]}"
53
+ Mimi::DB.migrate_schema!
54
+ end
55
+ end
56
+ end
data/mimi-db.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mimi/db/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mimi-db'
8
+ spec.version = Mimi::DB::VERSION
9
+ spec.authors = ['Alex Kukushkin']
10
+ spec.email = ['alex@kukushk.in']
11
+
12
+ spec.summary = 'Database module for mimi, microframework for microservices'
13
+ spec.description = 'Database module for mimi, microframework for microservices'
14
+ spec.homepage = 'https://github.com/kukushkin/mimi-db'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency 'mimi-core', '~> 0.1'
31
+ spec.add_dependency 'activerecord', '~> 4.2'
32
+ spec.add_dependency 'mini_record', '~> 0.4'
33
+
34
+ spec.add_development_dependency 'bundler', '~> 1.11'
35
+ spec.add_development_dependency 'rake', '~> 10.0'
36
+ spec.add_development_dependency 'rspec', '~> 3.0'
37
+ spec.add_development_dependency 'pry', '~> 0.10'
38
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mimi-db
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Kukushkin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mimi-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mini_record
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.4'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.10'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.10'
111
+ description: Database module for mimi, microframework for microservices
112
+ email:
113
+ - alex@kukushk.in
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - CODE_OF_CONDUCT.md
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - lib/mimi/db.rb
129
+ - lib/mimi/db/extensions.rb
130
+ - lib/mimi/db/foreign_key.rb
131
+ - lib/mimi/db/version.rb
132
+ - lib/tasks/db.rake
133
+ - mimi-db.gemspec
134
+ homepage: https://github.com/kukushkin/mimi-db
135
+ licenses:
136
+ - MIT
137
+ metadata:
138
+ allowed_push_host: https://rubygems.org/
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.4.5.1
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: Database module for mimi, microframework for microservices
159
+ test_files: []