pg_assistant 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +40 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/generators/pg_assistant/function/USAGE +17 -0
- data/lib/generators/pg_assistant/function/function_generator.rb +107 -0
- data/lib/generators/pg_assistant/function/templates/db/migrate/create_function.erb +13 -0
- data/lib/generators/pg_assistant/function/templates/db/migrate/update_function.erb +9 -0
- data/lib/generators/pg_assistant/generators.rb +4 -0
- data/lib/pg_assistant.rb +18 -0
- data/lib/pg_assistant/definitions.rb +7 -0
- data/lib/pg_assistant/definitions/base.rb +38 -0
- data/lib/pg_assistant/definitions/function.rb +6 -0
- data/lib/pg_assistant/version.rb +3 -0
- data/pg_assistant.gemspec +38 -0
- metadata +177 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6e971d86dbefb599de8da9d7d4c0af420cc03d48
|
4
|
+
data.tar.gz: 01243c8875722d96a3d23bfb113d4123009ef1af
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b3c30707d20bd84edc389b6694a24985e7e88ef530dbe9dbc64c12c2f65b0b6e7e8ac3a51b8de7ee9ce338b1f7abcf88dea86e98cd59805db39039bd1d5efe26
|
7
|
+
data.tar.gz: e90890bf00c3eec1fe11194f5106e75235b2583768c561de75ec8e9181e3ec57ea221b24e084cb62f48c85e9d84427c21627ba5c0e5680b95925c2a190443a18
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.4
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 German Antsiferov
|
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,40 @@
|
|
1
|
+
# PgAssistant
|
2
|
+
|
3
|
+
Adds methods to ActiveRecord::Migration to create and manage database functions in Rails.
|
4
|
+
I took the idea from a project [Scenic](https://github.com/thoughtbot/scenic).
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'pg_assistant'
|
12
|
+
```
|
13
|
+
|
14
|
+
## Setup config
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
PgAssistant.configure do |config|
|
18
|
+
config.functions_directory_path = '<your path>' # by default db/functions
|
19
|
+
end
|
20
|
+
```
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Create or update function:
|
25
|
+
|
26
|
+
``` bash
|
27
|
+
rails generate pg_assistant:function increment
|
28
|
+
|
29
|
+
# => create: db/functions/increment_v01.sql
|
30
|
+
# => create: db/functions/20140803191158_create_increment.rb
|
31
|
+
|
32
|
+
rails generate pg_assistant:function increment
|
33
|
+
|
34
|
+
# => create: db/functions/increment_v02.sql
|
35
|
+
# => create: db/functions/20140804191158_update_increment_to_version_2.rb
|
36
|
+
```
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "pg_assistant"
|
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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
Description:
|
2
|
+
Create a new database function for your application. This will create a new
|
3
|
+
function definition file and the accompanying migration.
|
4
|
+
|
5
|
+
If a function of the given name already exists, create a new version of the function
|
6
|
+
and a migration to replace the old version with the new.
|
7
|
+
|
8
|
+
Examples:
|
9
|
+
rails generate pg_assistant:function increment
|
10
|
+
|
11
|
+
create: db/functions/increment_v01.sql
|
12
|
+
create: db/functions/20140803191158_create_increment.rb
|
13
|
+
|
14
|
+
rails generate pg_assistant:function increment
|
15
|
+
|
16
|
+
create: db/functions/increment_v02.sql
|
17
|
+
create: db/functions/20140804191158_update_increment_to_version_2.rb
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require "rails/generators/active_record"
|
3
|
+
|
4
|
+
module PgAssistant
|
5
|
+
module Generators
|
6
|
+
class FunctionGenerator < Rails::Generators::NamedBase
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
|
9
|
+
source_root File.expand_path("../templates", __FILE__)
|
10
|
+
|
11
|
+
def create_functions_directory
|
12
|
+
unless functions_directory_path.exist?
|
13
|
+
empty_directory functions_directory_path
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def create_function_definition
|
18
|
+
if creating_new_function?
|
19
|
+
create_file definition.path
|
20
|
+
else
|
21
|
+
copy_file previous_definition.full_path, definition.full_path
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_migration_file
|
26
|
+
if creating_new_function? || destroying_initial_function?
|
27
|
+
migration_template(
|
28
|
+
"db/migrate/create_function.erb",
|
29
|
+
"db/migrate/create_#{function_name}.rb"
|
30
|
+
)
|
31
|
+
else
|
32
|
+
migration_template(
|
33
|
+
"db/migrate/update_function.erb",
|
34
|
+
"db/migrate/update_#{function_name}_to_version_#{version}.rb"
|
35
|
+
)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.next_migration_number(dir)
|
40
|
+
::ActiveRecord::Generators::Base.next_migration_number(dir)
|
41
|
+
end
|
42
|
+
|
43
|
+
no_tasks do
|
44
|
+
def previous_version
|
45
|
+
@previous_version ||=
|
46
|
+
Dir.entries(functions_directory_path)
|
47
|
+
.map { |name| version_regex.match(name).try(:[], "version").to_i }
|
48
|
+
.max
|
49
|
+
end
|
50
|
+
|
51
|
+
def version
|
52
|
+
@version ||= destroying? ? previous_version : previous_version.next
|
53
|
+
end
|
54
|
+
|
55
|
+
def migration_class_name
|
56
|
+
if creating_new_function?
|
57
|
+
"Create#{class_name.gsub('.', '')}"
|
58
|
+
else
|
59
|
+
"Update#{class_name}ToVersion#{version}"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def activerecord_migration_class
|
64
|
+
if ActiveRecord::Migration.respond_to?(:current_version)
|
65
|
+
"ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]"
|
66
|
+
else
|
67
|
+
"ActiveRecord::Migration"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def definition
|
72
|
+
PgAssistant::Definitions::Function.new(function_name, version)
|
73
|
+
end
|
74
|
+
|
75
|
+
def previous_definition
|
76
|
+
PgAssistant::Definitions::Function.new(function_name, previous_version)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def functions_directory_path
|
83
|
+
@functions_directory_path ||= Rails.root.join PgAssistant::Definitions::Function.directory_path
|
84
|
+
end
|
85
|
+
|
86
|
+
def version_regex
|
87
|
+
/\A#{function_name}_v(?<version>\d+)\.sql\z/
|
88
|
+
end
|
89
|
+
|
90
|
+
def function_name
|
91
|
+
@function_name ||= file_name.gsub(".", "_")
|
92
|
+
end
|
93
|
+
|
94
|
+
def destroying?
|
95
|
+
behavior == :revoke
|
96
|
+
end
|
97
|
+
|
98
|
+
def creating_new_function?
|
99
|
+
previous_version == 0
|
100
|
+
end
|
101
|
+
|
102
|
+
def destroying_initial_function?
|
103
|
+
destroying? && version == 1
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class <%= migration_class_name %> < <%= activerecord_migration_class %>
|
2
|
+
def up
|
3
|
+
connection.execute Rails.root.join('<%= definition.path -%>').read
|
4
|
+
end
|
5
|
+
|
6
|
+
def down
|
7
|
+
# connection.execute <<-SQL
|
8
|
+
# DROP FUNCTION <%= class_name %>(your arguments);
|
9
|
+
# SQL
|
10
|
+
|
11
|
+
raise ActiveRecord::IrreversibleMigration
|
12
|
+
end
|
13
|
+
end
|
data/lib/pg_assistant.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'pg_assistant/version'
|
2
|
+
require 'pg_assistant/definitions'
|
3
|
+
|
4
|
+
module PgAssistant
|
5
|
+
class << self
|
6
|
+
def configure(&block)
|
7
|
+
yield(self)
|
8
|
+
end
|
9
|
+
|
10
|
+
def functions_directory_path=(path)
|
11
|
+
Definitions::Function.directory_path = path
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
PgAssistant.configure do |config|
|
17
|
+
config.functions_directory_path = 'db/functions'
|
18
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module PgAssistant
|
2
|
+
module Definitions
|
3
|
+
class Base
|
4
|
+
class << self
|
5
|
+
attr_reader :directory_path
|
6
|
+
|
7
|
+
def directory_path=(path_raw)
|
8
|
+
@directory_path = Pathname.new(path_raw)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(name, version)
|
13
|
+
@name = name
|
14
|
+
@version = version.to_i
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_sql
|
18
|
+
File.read(full_path)
|
19
|
+
end
|
20
|
+
|
21
|
+
def full_path
|
22
|
+
Rails.root.join(path)
|
23
|
+
end
|
24
|
+
|
25
|
+
def path
|
26
|
+
self.class.directory_path.join(filename)
|
27
|
+
end
|
28
|
+
|
29
|
+
def version
|
30
|
+
@version.to_s.rjust(2, "0")
|
31
|
+
end
|
32
|
+
|
33
|
+
def filename
|
34
|
+
"#{@name}_v#{version}.sql"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -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 "pg_assistant/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "pg_assistant"
|
8
|
+
spec.version = PgAssistant::VERSION
|
9
|
+
spec.authors = ["German Antsiferov"]
|
10
|
+
spec.email = ["dxdy@bk.ru"]
|
11
|
+
|
12
|
+
spec.summary = %q{Support for database functions in Rails migrations}
|
13
|
+
spec.description = <<-DESCRIPTION
|
14
|
+
Adds methods to ActiveRecord::Migration to create and manage database functions in Rails
|
15
|
+
DESCRIPTION
|
16
|
+
|
17
|
+
spec.homepage = "https://github.com/mr-dxdy/pg_assistant.git"
|
18
|
+
spec.license = "MIT"
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
21
|
+
f.match(%r{^(test|spec|features)/})
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", ">= 1.15"
|
28
|
+
spec.add_development_dependency "rake"
|
29
|
+
spec.add_development_dependency "rspec", ">= 3.3"
|
30
|
+
spec.add_development_dependency 'ammeter', '>= 1.1.3'
|
31
|
+
spec.add_development_dependency 'pg'
|
32
|
+
spec.add_development_dependency 'byebug'
|
33
|
+
|
34
|
+
spec.add_dependency 'activerecord', '>= 4.0.0'
|
35
|
+
spec.add_dependency 'railties', '>= 4.0.0'
|
36
|
+
|
37
|
+
spec.required_ruby_version = '~> 2.1'
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pg_assistant
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- German Antsiferov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: ammeter
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.1.3
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.1.3
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pg
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: byebug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: activerecord
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 4.0.0
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 4.0.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: railties
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 4.0.0
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 4.0.0
|
125
|
+
description: |2
|
126
|
+
Adds methods to ActiveRecord::Migration to create and manage database functions in Rails
|
127
|
+
email:
|
128
|
+
- dxdy@bk.ru
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- ".gitignore"
|
134
|
+
- ".rspec"
|
135
|
+
- ".ruby-version"
|
136
|
+
- Gemfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- bin/console
|
141
|
+
- bin/setup
|
142
|
+
- lib/generators/pg_assistant/function/USAGE
|
143
|
+
- lib/generators/pg_assistant/function/function_generator.rb
|
144
|
+
- lib/generators/pg_assistant/function/templates/db/migrate/create_function.erb
|
145
|
+
- lib/generators/pg_assistant/function/templates/db/migrate/update_function.erb
|
146
|
+
- lib/generators/pg_assistant/generators.rb
|
147
|
+
- lib/pg_assistant.rb
|
148
|
+
- lib/pg_assistant/definitions.rb
|
149
|
+
- lib/pg_assistant/definitions/base.rb
|
150
|
+
- lib/pg_assistant/definitions/function.rb
|
151
|
+
- lib/pg_assistant/version.rb
|
152
|
+
- pg_assistant.gemspec
|
153
|
+
homepage: https://github.com/mr-dxdy/pg_assistant.git
|
154
|
+
licenses:
|
155
|
+
- MIT
|
156
|
+
metadata: {}
|
157
|
+
post_install_message:
|
158
|
+
rdoc_options: []
|
159
|
+
require_paths:
|
160
|
+
- lib
|
161
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
162
|
+
requirements:
|
163
|
+
- - "~>"
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '2.1'
|
166
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
version: '0'
|
171
|
+
requirements: []
|
172
|
+
rubyforge_project:
|
173
|
+
rubygems_version: 2.4.5.1
|
174
|
+
signing_key:
|
175
|
+
specification_version: 4
|
176
|
+
summary: Support for database functions in Rails migrations
|
177
|
+
test_files: []
|