synchronize_postgres_stored_procedures 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +65 -0
- data/Rakefile +8 -0
- data/lib/synchronize_postgres_stored_procedures/stored_procedures/pg.rb +49 -0
- data/lib/synchronize_postgres_stored_procedures/stored_procedures/pg_11.rb +34 -0
- data/lib/synchronize_postgres_stored_procedures/stored_procedures/pg_12.rb +34 -0
- data/lib/synchronize_postgres_stored_procedures/stored_procedures/pg_9.rb +34 -0
- data/lib/synchronize_postgres_stored_procedures/stored_procedures/synchronizer.rb +108 -0
- data/lib/synchronize_postgres_stored_procedures/stored_procedures.rb +9 -0
- data/lib/synchronize_postgres_stored_procedures/version.rb +4 -0
- data/lib/synchronize_postgres_stored_procedures.rb +7 -0
- data/lib/tasks.rb +12 -0
- data/sig/synchronize_postgres_stored_procedures.rbs +4 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b6ec8b192e24ff9c917d2c4b84134b9e3e96761ecfb9e28f9580cf823841b2e5
|
4
|
+
data.tar.gz: 5bcf3109c9e8705ae89462949d57515d6847490b660f3873a578061c36461e84
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1f00c03b6cd4bf26f315250d038ca94156b4ec99f45f38d96a38200f90ebaa1e40dfc225b1547fe696214608f0c2a209a0cce851721c3110a7e43c670bf5fe74
|
7
|
+
data.tar.gz: fb6df392af325d116ba44134c398e0506cc557ff57a928b58a5093a765a29686ab8027be4a409090829fa71b279c87f26439991892e3c20f588f41e4130ce391
|
data/.rspec
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
[INSERT CONTACT METHOD].
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 rob mathews
|
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,65 @@
|
|
1
|
+
# SynchronizePostgresStoredProcedures
|
2
|
+
|
3
|
+
Don't want to track down multiple versions of a stored procedure across
|
4
|
+
multiple migrations to find the current source? Want to just edit a file
|
5
|
+
and have the stored procedure (or function) be automatically updated
|
6
|
+
in your database as part of the db:migrate step? Then this gem is
|
7
|
+
for you.
|
8
|
+
|
9
|
+
Given a directory of stored procedures, it will add, update, and drop the sp's in the database to match the directory on disk.
|
10
|
+
|
11
|
+
## The Rules
|
12
|
+
The rules are simple:
|
13
|
+
|
14
|
+
* install the gem as directed below
|
15
|
+
* put each stored procedure in it's own file (see below for example) in the db/sp directory.
|
16
|
+
* all your stored procedures must start with the name 'sp_'. This is to prevent conflict with existing postgres stored procedures.
|
17
|
+
* the stored procedures will be installed alphabetically, so if you have one sp that depends on another sp, be sure the other sp comes first alphabetically.
|
18
|
+
|
19
|
+
### Example
|
20
|
+
|
21
|
+
Say you have this function, called 'sp_unaccent', as below:
|
22
|
+
```sql
|
23
|
+
CREATE OR REPLACE FUNCTION sp_unaccent(text)
|
24
|
+
RETURNS text AS
|
25
|
+
$func$
|
26
|
+
SELECT public.unaccent('public.unaccent', $1) -- schema-qualify function and dictionary. Modifies public version to be IMMUTABLE so it can be indexed.
|
27
|
+
$func$ LANGUAGE sql IMMUTABLE;
|
28
|
+
```
|
29
|
+
Place that text in the file db/sp/sp_unaccent.sql, and this gem will ensure that the sp is up to date in any database that the migrations are applied to.
|
30
|
+
|
31
|
+
## Installation
|
32
|
+
|
33
|
+
Add this line to your application's Gemfile:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
gem 'synchronize_postgres_stored_procedures'
|
37
|
+
```
|
38
|
+
Execute
|
39
|
+
```sh
|
40
|
+
bundle install
|
41
|
+
```
|
42
|
+
## Usage
|
43
|
+
Require the gem in your Capfile:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
require 'synchronize_postgres_stored_procedures'
|
47
|
+
```
|
48
|
+
|
49
|
+
## Development
|
50
|
+
|
51
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
52
|
+
|
53
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/synchronize_postgres_stored_procedures. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/synchronize_postgres_stored_procedures/blob/main/CODE_OF_CONDUCT.md).
|
58
|
+
|
59
|
+
## License
|
60
|
+
|
61
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
62
|
+
|
63
|
+
## Code of Conduct
|
64
|
+
|
65
|
+
Everyone interacting in the SynchronizePostgresStoredProcedures project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/synchronize_postgres_stored_procedures/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
module SynchronizePostgresStoredProcedures
|
2
|
+
module StoredProcedures
|
3
|
+
class Pg
|
4
|
+
cattr_accessor :logger
|
5
|
+
delegate :logger, :to => :class
|
6
|
+
|
7
|
+
def initialize(connection = ActiveRecord::Base.connection)
|
8
|
+
@connection = connection
|
9
|
+
|
10
|
+
major,minor,point = get_version
|
11
|
+
|
12
|
+
if (major < 9 || major > 16)
|
13
|
+
raise "Unsupported postgres version #{get_version}"
|
14
|
+
end
|
15
|
+
|
16
|
+
@synchronizer = (
|
17
|
+
case major
|
18
|
+
when 12,13,14,15,16 then Pg12
|
19
|
+
when 11 then Pg11
|
20
|
+
else Pg9
|
21
|
+
end
|
22
|
+
).new(@connection)
|
23
|
+
|
24
|
+
# We want to write logs to the rails default logger and stdout
|
25
|
+
loggers = [Logger.new(STDOUT)]
|
26
|
+
loggers.append(Rails.logger) if Rails.logger
|
27
|
+
self.logger = ActiveSupport::BroadcastLogger.new(*loggers)
|
28
|
+
end
|
29
|
+
|
30
|
+
def synchronize(directory = "db/sp")
|
31
|
+
logger.info("Synchronizing stored procedures")
|
32
|
+
|
33
|
+
dir = Dir.new(directory)
|
34
|
+
stored_procedures = dir.entries.select {|f| f =~ /\.sql$/}
|
35
|
+
|
36
|
+
@synchronizer.synchronize_all(stored_procedures.sort.map {|filename|
|
37
|
+
[File.open(File.join(dir, filename)) { |f| f.read }, File.basename(filename, ".sql")]
|
38
|
+
})
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def get_version
|
43
|
+
version = @connection.select_one("SHOW server_version")['server_version']
|
44
|
+
version.split('.').map(&:to_i)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module SynchronizePostgresStoredProcedures
|
2
|
+
module StoredProcedures
|
3
|
+
class Pg11 < Synchronizer
|
4
|
+
def self.pg_proc_columns
|
5
|
+
[
|
6
|
+
"proowner",
|
7
|
+
"prolang",
|
8
|
+
"procost",
|
9
|
+
"prorows",
|
10
|
+
"provariadic",
|
11
|
+
"protransform",
|
12
|
+
"prokind",
|
13
|
+
"prosecdef",
|
14
|
+
"proleakproof",
|
15
|
+
"proisstrict",
|
16
|
+
"proretset",
|
17
|
+
"provolatile",
|
18
|
+
"pronargs",
|
19
|
+
"pronargdefaults",
|
20
|
+
"prorettype",
|
21
|
+
"proargtypes",
|
22
|
+
"proallargtypes",
|
23
|
+
"proargmodes",
|
24
|
+
"proargnames",
|
25
|
+
"proargdefaults",
|
26
|
+
"probin",
|
27
|
+
"proconfig",
|
28
|
+
"proacl"
|
29
|
+
]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module SynchronizePostgresStoredProcedures
|
2
|
+
module StoredProcedures
|
3
|
+
class Pg12 < Synchronizer
|
4
|
+
def self.pg_proc_columns
|
5
|
+
[
|
6
|
+
"proowner",
|
7
|
+
"prolang",
|
8
|
+
"procost",
|
9
|
+
"prorows",
|
10
|
+
"provariadic",
|
11
|
+
"prosupport",
|
12
|
+
"prokind",
|
13
|
+
"prosecdef",
|
14
|
+
"proleakproof",
|
15
|
+
"proisstrict",
|
16
|
+
"proretset",
|
17
|
+
"provolatile",
|
18
|
+
"pronargs",
|
19
|
+
"pronargdefaults",
|
20
|
+
"prorettype",
|
21
|
+
"proargtypes",
|
22
|
+
"proallargtypes",
|
23
|
+
"proargmodes",
|
24
|
+
"proargnames",
|
25
|
+
"proargdefaults",
|
26
|
+
"probin",
|
27
|
+
"proconfig",
|
28
|
+
"proacl"
|
29
|
+
]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module SynchronizePostgresStoredProcedures
|
2
|
+
module StoredProcedures
|
3
|
+
class Pg9 < Synchronizer
|
4
|
+
def self.pg_proc_columns
|
5
|
+
[
|
6
|
+
"proowner",
|
7
|
+
"prolang",
|
8
|
+
"procost",
|
9
|
+
"prorows",
|
10
|
+
"provariadic",
|
11
|
+
"protransform",
|
12
|
+
"proisagg",
|
13
|
+
"proiswindow",
|
14
|
+
"prosecdef",
|
15
|
+
"proleakproof",
|
16
|
+
"proisstrict",
|
17
|
+
"proretset",
|
18
|
+
"provolatile",
|
19
|
+
"pronargs",
|
20
|
+
"pronargdefaults",
|
21
|
+
"prorettype",
|
22
|
+
"proargtypes",
|
23
|
+
"proallargtypes",
|
24
|
+
"proargmodes",
|
25
|
+
"proargnames",
|
26
|
+
"proargdefaults",
|
27
|
+
"probin",
|
28
|
+
"proconfig",
|
29
|
+
"proacl"
|
30
|
+
]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
module SynchronizePostgresStoredProcedures
|
2
|
+
module StoredProcedures
|
3
|
+
class Synchronizer
|
4
|
+
def initialize(connection)
|
5
|
+
@connection = connection
|
6
|
+
end
|
7
|
+
|
8
|
+
delegate :select_one, :select_all, :execute, :select_value, :to => :@connection
|
9
|
+
delegate :logger, :to => Pg
|
10
|
+
delegate :pg_proc_columns, :to => :class
|
11
|
+
|
12
|
+
def synchronize_all(procedures)
|
13
|
+
procedures.sort_by(&:last).each do |source, name|
|
14
|
+
synchronize(source, name)
|
15
|
+
end
|
16
|
+
sp_clean(procedures.map(&:last))
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
def synchronize(source, name)
|
21
|
+
if(!(exists = sp_exists?(name)) || !sp_equal?(source, name))
|
22
|
+
if exists
|
23
|
+
logger.info "\t UPDATING: #{name}"
|
24
|
+
else
|
25
|
+
logger.info "\t CREATING: #{name}"
|
26
|
+
end
|
27
|
+
sp_drop(name)
|
28
|
+
sp_create(name,source)
|
29
|
+
else
|
30
|
+
logger.info "\tunchanged: #{name} "
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
protected
|
35
|
+
def convert_arg(val)
|
36
|
+
# select * from pg_type where typnamespace = 11 order by typname
|
37
|
+
# look at typelem and typarray
|
38
|
+
case val.to_i
|
39
|
+
when 23 then 'integer'
|
40
|
+
when 1007 then 'integer[]'
|
41
|
+
when 25 then 'text'
|
42
|
+
when 1043 then 'varchar(255)'
|
43
|
+
when 1082 then 'date'
|
44
|
+
when 1114 then 'timestamp'
|
45
|
+
when 1184 then 'timestamptz'
|
46
|
+
when 700 then 'pg_catalog.float4'
|
47
|
+
when 701 then 'pg_catalog.float8'
|
48
|
+
when 20 then 'bigint'
|
49
|
+
when 1016 then 'bigint[]'
|
50
|
+
when 16 then 'boolean'
|
51
|
+
when 1186 then 'interval'
|
52
|
+
else
|
53
|
+
raise ArgumentError, "unknown argument type(#{val})"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def temp_namespace_oid
|
58
|
+
@temp_namespace_oid||=select_value("select oid from pg_namespace where nspname='temp'")
|
59
|
+
end
|
60
|
+
|
61
|
+
def namespace_for_oid(oid)
|
62
|
+
select_value("select nspname from pg_namespace where oid=#{oid}")
|
63
|
+
end
|
64
|
+
|
65
|
+
def sp_create(name, source)
|
66
|
+
execute(source)
|
67
|
+
raise ::PG::Error, "failed to create stored procedure #{name} (check if the filename matches the function name) " if !sp_exists?(name)
|
68
|
+
end
|
69
|
+
|
70
|
+
def sp_equal?(source_string, name)
|
71
|
+
sp_drop(name,temp_namespace_oid)
|
72
|
+
sp_create(name, source_string.sub(/(.*CREATE(?: +OR +REPLACE)? +FUNCTION +)(\w+)(.*)/mi,"\\1temp.\\2\\3"))
|
73
|
+
!select_one(%Q{select p1.proname from pg_proc p1, pg_proc p2
|
74
|
+
where p1.proname = '#{name}' AND p1.pronamespace <> p2.pronamespace
|
75
|
+
AND p2.proname = '#{name}' AND p2.pronamespace = #{temp_namespace_oid}
|
76
|
+
AND regexp_replace(p1.prosrc,'\t','','g') = regexp_replace(p2.prosrc,'\t','','g')
|
77
|
+
AND #{compare_columns_sql}}).nil?
|
78
|
+
end
|
79
|
+
|
80
|
+
def sp_exists?(name)
|
81
|
+
select_one("select proname, pronamespace, proowner, proargtypes, prosrc from pg_proc where pronamespace = 2200 and proname='#{name}'").present?
|
82
|
+
end
|
83
|
+
|
84
|
+
# drops all functions of that name, even if they are overloaded.
|
85
|
+
def sp_drop(name,oid=2200)
|
86
|
+
select_all("select proname, pronamespace, proargtypes from pg_proc where pronamespace = #{oid} and proname='#{name}'").each do |hash|
|
87
|
+
argtypes = hash["proargtypes"].split(' ').map {|val|convert_arg(val)}.join(",")
|
88
|
+
name_with_schema="#{namespace_for_oid(oid)}.#{name}"
|
89
|
+
execute("DROP FUNCTION IF EXISTS #{name_with_schema}(#{argtypes}) CASCADE")
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def sp_clean(stored_procedures)
|
94
|
+
select_all("select proname, pronamespace, proowner, proargtypes, prosrc from pg_proc where proname like 'sp_%' and pronamespace = 2200").each do |results|
|
95
|
+
unless stored_procedures.include?( results['proname'] )
|
96
|
+
logger.info("\t DROPPING: #{results['proname']}")
|
97
|
+
sp_drop( results['proname'] )
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def compare_columns_sql
|
103
|
+
pg_proc_columns.map {|col| "p1.#{col} IS NOT DISTINCT FROM p2.#{col}"}.join(" AND ")
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module SynchronizePostgresStoredProcedures
|
2
|
+
module StoredProcedures
|
3
|
+
require_relative 'stored_procedures/pg.rb'
|
4
|
+
require_relative 'stored_procedures/synchronizer.rb'
|
5
|
+
Dir[File.join(__dir__, 'stored_procedures', 'pg_*.rb')].each do |file|
|
6
|
+
require file
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
data/lib/tasks.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "synchronize_postgres_stored_procedures/version"
|
2
|
+
require "synchronize_postgres_stored_procedures/stored_procedures.rb"
|
3
|
+
|
4
|
+
namespace :db do
|
5
|
+
task :after_migrate do
|
6
|
+
SynchronizePostgresStoredProcedures::StoredProcedures::Pg.new.synchronize
|
7
|
+
end
|
8
|
+
|
9
|
+
Rake::Task["db:migrate"].enhance do
|
10
|
+
Rake::Task["db:after_migrate"].invoke
|
11
|
+
end
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: synchronize_postgres_stored_procedures
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- rob mathews
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-07-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
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: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pg
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- rob@justsoftwareconsulting.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".rspec"
|
77
|
+
- CODE_OF_CONDUCT.md
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/synchronize_postgres_stored_procedures.rb
|
82
|
+
- lib/synchronize_postgres_stored_procedures/stored_procedures.rb
|
83
|
+
- lib/synchronize_postgres_stored_procedures/stored_procedures/pg.rb
|
84
|
+
- lib/synchronize_postgres_stored_procedures/stored_procedures/pg_11.rb
|
85
|
+
- lib/synchronize_postgres_stored_procedures/stored_procedures/pg_12.rb
|
86
|
+
- lib/synchronize_postgres_stored_procedures/stored_procedures/pg_9.rb
|
87
|
+
- lib/synchronize_postgres_stored_procedures/stored_procedures/synchronizer.rb
|
88
|
+
- lib/synchronize_postgres_stored_procedures/version.rb
|
89
|
+
- lib/tasks.rb
|
90
|
+
- sig/synchronize_postgres_stored_procedures.rbs
|
91
|
+
homepage: https://github.com/robmathews/synchronize_postgres_stored_procedures
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata:
|
95
|
+
homepage_uri: https://github.com/robmathews/synchronize_postgres_stored_procedures
|
96
|
+
source_code_uri: https://github.com/robmathews/synchronize_postgres_stored_procedures
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: 3.0.0
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubygems_version: 3.5.10
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Maintain your stored procedures in a single file per procedure, not spread
|
116
|
+
across multiple migrations
|
117
|
+
test_files: []
|