pg_spec_helper 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +20 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +67 -0
- data/lib/pg_spec_helper/columns.rb +19 -0
- data/lib/pg_spec_helper/configuration.rb +51 -0
- data/lib/pg_spec_helper/connection.rb +26 -0
- data/lib/pg_spec_helper/foreign_key_cache.rb +26 -0
- data/lib/pg_spec_helper/foreign_keys.rb +21 -0
- data/lib/pg_spec_helper/index_cache.rb +26 -0
- data/lib/pg_spec_helper/indexes.rb +16 -0
- data/lib/pg_spec_helper/primary_keys.rb +19 -0
- data/lib/pg_spec_helper/schemas.rb +67 -0
- data/lib/pg_spec_helper/structure_cache.rb +26 -0
- data/lib/pg_spec_helper/tables.rb +33 -0
- data/lib/pg_spec_helper/unique_constraints.rb +20 -0
- data/lib/pg_spec_helper/validation_cache.rb +26 -0
- data/lib/pg_spec_helper/validations.rb +18 -0
- data/lib/pg_spec_helper/version.rb +5 -0
- data/lib/pg_spec_helper.rb +66 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8b0b5b3c3620c2ddbf235b94c07c33e9c4242ba5221c5d92997c0504ec550448
|
4
|
+
data.tar.gz: e71a455e469526f1bfe9ef599e4dd37a3dea1e49d0fd18c11ac4cf11035c4a6d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e433948e243dfdce36e6d6bdd894a18b1bcd9e30b3f622bd4ea2788af1366525c8384c33fff2eeb426548727c85c74c6ce42ce5e3e9006102fd018cbbf068bda
|
7
|
+
data.tar.gz: bdbbbb5df25c8456ec9c09aed3b5f9eae6eb869cd8c2b0395a031613beb744477a7f59cc47474b10b35daed5b6fda9a11111fcd71fd5e18d3f188cce0c26c475
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## 1.0.0 (2023-07-08)
|
4
|
+
|
5
|
+
|
6
|
+
### Features
|
7
|
+
|
8
|
+
* basic first version, extracted from dynamic_migrations gem ([4d4ed01](https://github.com/craigulliott/pg_spec_helper/commit/4d4ed016d1a19394d0db7c39a01c153670a3edfe))
|
9
|
+
|
10
|
+
## 1.0.0 (2023-07-05)
|
11
|
+
|
12
|
+
|
13
|
+
### Features
|
14
|
+
|
15
|
+
* added table and column comments and column metadata ([84d798a](https://github.com/craigulliott/pg_spec_helper/commit/84d798aae35c259545f73dbbd7d076d8ceaa8739))
|
16
|
+
* basic representation of schema either configured or loaded from a real database ([7fc9ebb](https://github.com/craigulliott/pg_spec_helper/commit/7fc9ebbe5a8e5faa4e6017deec9bc66f7ba15f16))
|
17
|
+
* recursively load and builds a representation of current database schema, and find differences between configured and loaded representations of the database structure ([978e122](https://github.com/craigulliott/pg_spec_helper/commit/978e12279760709f1511dc7c6d9fe7ff57b54f3e))
|
18
|
+
* renaming the current version of constraints to validations, because its a more descriptive name, especially when considering we are going to expose multiple types of postgres constraint ([2f17af6](https://github.com/craigulliott/pg_spec_helper/commit/2f17af665028ed6f49d8bdd9b7ff6a52339206db))
|
19
|
+
* support for table constraints and added caches for database structure and constraints ([f68bbb2](https://github.com/craigulliott/pg_spec_helper/commit/f68bbb20a25fab149ed4b3b9c591fde1a6ff628e))
|
20
|
+
* table keys and indexes and a more robust database configured/loaded comparison class ([b4a0925](https://github.com/craigulliott/pg_spec_helper/commit/b4a092535e4e59d0fb9b97efc3d210289346b454))
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at craigulliott@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Craig Ulliott
|
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,67 @@
|
|
1
|
+
# PGSpecHelper
|
2
|
+
|
3
|
+
Monitor and generate database migrations based on difference between current schema and configuration.
|
4
|
+
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/pg_spec_helper.svg)](https://badge.fury.io/rb/pg_spec_helper)
|
6
|
+
[![Specs](https://github.com/craigulliott/pg_spec_helper/actions/workflows/specs.yml/badge.svg)](https://github.com/craigulliott/pg_spec_helper/actions/workflows/specs.yml)
|
7
|
+
[![Types](https://github.com/craigulliott/pg_spec_helper/actions/workflows/types.yml/badge.svg)](https://github.com/craigulliott/pg_spec_helper/actions/workflows/types.yml)
|
8
|
+
[![Coding Style](https://github.com/craigulliott/pg_spec_helper/actions/workflows/linter.yml/badge.svg)](https://github.com/craigulliott/pg_spec_helper/actions/workflows/linter.yml)
|
9
|
+
|
10
|
+
## Key Features
|
11
|
+
|
12
|
+
* Create tables, columns, constraints, indexes and primary/foreign keys
|
13
|
+
* Resets your database after each spec, only if the spec made changes
|
14
|
+
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Install the gem by executing:
|
19
|
+
|
20
|
+
$ gem install pg_spec_helper
|
21
|
+
|
22
|
+
Create your new platform:
|
23
|
+
|
24
|
+
$ pg_spec_helper create my_platform_name
|
25
|
+
|
26
|
+
Note, this gem depends on the postgres gem `pg`, which depends on the `libpq` package. On Apple Silicon you can run the following commands before installation to prepare your system.
|
27
|
+
|
28
|
+
```
|
29
|
+
# required for pg gem on apple silicon
|
30
|
+
brew install libpq
|
31
|
+
export PATH="/opt/homebrew/opt/libpq/bin:$PATH"
|
32
|
+
``
|
33
|
+
|
34
|
+
## Getting Started
|
35
|
+
|
36
|
+
Todo
|
37
|
+
|
38
|
+
|
39
|
+
## Development
|
40
|
+
|
41
|
+
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.
|
42
|
+
|
43
|
+
We use [Conventional Commit Messages](https://gist.github.com/qoomon/5dfcdf8eec66a051ecd85625518cfd13).
|
44
|
+
|
45
|
+
Code should be linted and formatted according to [Ruby Standard](https://github.com/standardrb/standard).
|
46
|
+
|
47
|
+
Publishing is automated via github actions and Googles [Release Please](https://github.com/google-github-actions/release-please-action) github action
|
48
|
+
|
49
|
+
We prefer using squash-merges when merging pull requests because it helps keep a linear git history and allows more fine grained control of commit messages which get sent to release-please and ultimately show up in the changelog.
|
50
|
+
|
51
|
+
Type checking is enabled for this project. You can find the corresponding `rbs` files in the sig folder.
|
52
|
+
|
53
|
+
Install types for the packages used in development (such as `rspec`) by running
|
54
|
+
|
55
|
+
$ rbs collection install
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/craigulliott/pg_spec_helper. 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/craigulliott/pg_spec_helper/blob/master/CODE_OF_CONDUCT.md).
|
60
|
+
|
61
|
+
## License
|
62
|
+
|
63
|
+
This software is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
64
|
+
|
65
|
+
## Code of Conduct
|
66
|
+
|
67
|
+
Everyone interacting in the PGSpecHelper project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/craigulliott/pg_spec_helper/blob/master/CODE_OF_CONDUCT.md).
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PGSpecHelper
|
4
|
+
module Columns
|
5
|
+
def create_column schema_name, table_name, column_name, type
|
6
|
+
# validate the type exists
|
7
|
+
PGSpecHelper::Postgres::DataTypes.validate_type_exists! type
|
8
|
+
# note the `type` is safe from sql_injection due to the validation above
|
9
|
+
connection.exec(<<-SQL)
|
10
|
+
ALTER TABLE #{connection.quote_ident schema_name.to_s}.#{connection.quote_ident table_name.to_s}
|
11
|
+
ADD COLUMN #{connection.quote_ident column_name.to_s} #{type}
|
12
|
+
SQL
|
13
|
+
# refresh the cached representation of the database structure
|
14
|
+
refresh_structure_cache_materialized_view
|
15
|
+
# note that the database has been reset and there are no changes
|
16
|
+
@has_changes = true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PGSpecHelper
|
4
|
+
module Configuration
|
5
|
+
class MissingConfigurationError < StandardError
|
6
|
+
end
|
7
|
+
|
8
|
+
class ConfigurationNotLoadedError < StandardError
|
9
|
+
end
|
10
|
+
|
11
|
+
class MissingRequiredNameError < StandardError
|
12
|
+
end
|
13
|
+
|
14
|
+
class MissingRequiredDatabaseTypeError < StandardError
|
15
|
+
end
|
16
|
+
|
17
|
+
def load_configuration_for database_type, name
|
18
|
+
@name = name.to_s
|
19
|
+
@database_type = database_type.to_s
|
20
|
+
|
21
|
+
raise MissingRequiredNameError unless @name
|
22
|
+
raise MissingRequiredDatabaseTypeError unless @database_type
|
23
|
+
|
24
|
+
configuration = load_configuration_file
|
25
|
+
|
26
|
+
if configuration[@database_type].nil?
|
27
|
+
raise MissingConfigurationError, "no database configuration found for #{name} in database.yaml"
|
28
|
+
end
|
29
|
+
|
30
|
+
if configuration[@database_type][@name].nil?
|
31
|
+
raise MissingConfigurationError, "no configuration found for #{database_type}.#{name} in database.yaml"
|
32
|
+
end
|
33
|
+
|
34
|
+
@configuration = configuration[@database_type][@name]
|
35
|
+
end
|
36
|
+
|
37
|
+
def optional_configuration_value key
|
38
|
+
@configuration[key.to_s]
|
39
|
+
end
|
40
|
+
|
41
|
+
def require_configuration_value key
|
42
|
+
raise ConfigurationNotLoadedError unless @configuration
|
43
|
+
|
44
|
+
@configuration[key.to_s] || raise(MissingConfigurationError, "no #{key} found for configuration #{@database_type}.#{@name}")
|
45
|
+
end
|
46
|
+
|
47
|
+
def load_configuration_file
|
48
|
+
YAML.load_file("config/database.yaml")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PGSpecHelper
|
4
|
+
module Connection
|
5
|
+
class ConnectionFailedError < StandardError
|
6
|
+
end
|
7
|
+
|
8
|
+
def connection
|
9
|
+
unless @connection
|
10
|
+
@connection = PG.connect(
|
11
|
+
host: @host,
|
12
|
+
port: @port,
|
13
|
+
user: @username,
|
14
|
+
password: @password,
|
15
|
+
dbname: @database,
|
16
|
+
sslmode: "prefer"
|
17
|
+
)
|
18
|
+
# after initial connect, we refresh the cached representation of
|
19
|
+
# the database structure and constaints
|
20
|
+
refresh_structure_cache_materialized_view
|
21
|
+
refresh_validation_cache_materialized_view
|
22
|
+
end
|
23
|
+
@connection
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PGSpecHelper
|
4
|
+
module ForeignKeyCache
|
5
|
+
# whenever schema changes are made from within the test suite we need to
|
6
|
+
# rebuild the materialized views that hold a cached representation of the
|
7
|
+
# database foreign keys
|
8
|
+
def refresh_keys_and_unique_constraints_cache_materialized_view
|
9
|
+
# the first time we detect the presence of the marerialized view, we no longer need to
|
10
|
+
# check for it
|
11
|
+
@keys_and_unique_constraints_cache_exists ||= keys_and_unique_constraints_cache_exists?
|
12
|
+
if @keys_and_unique_constraints_cache_exists
|
13
|
+
connection.exec(<<~SQL)
|
14
|
+
REFRESH MATERIALIZED VIEW public.pg_spec_helper_keys_and_unique_constraints_cache;
|
15
|
+
SQL
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def keys_and_unique_constraints_cache_exists?
|
20
|
+
exists = connection.exec(<<~SQL)
|
21
|
+
SELECT TRUE AS exists FROM pg_matviews WHERE schemaname = 'public' AND matviewname = 'pg_spec_helper_keys_and_unique_constraints_cache';
|
22
|
+
SQL
|
23
|
+
exists.count > 0
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PGSpecHelper
|
4
|
+
module ForeignKeys
|
5
|
+
def create_foreign_key schema_name, table_name, column_names, foreign_schema_name, foreign_table_name, foreign_column_names, foreign_key_name
|
6
|
+
column_names_sql = column_names.map { |n| connection.quote_ident n.to_s }.join(", ")
|
7
|
+
foreign_column_names_sql = column_names.map { |n| connection.quote_ident n.to_s }.join(", ")
|
8
|
+
# add the foreign key
|
9
|
+
connection.exec(<<-SQL)
|
10
|
+
ALTER TABLE #{connection.quote_ident schema_name.to_s}.#{connection.quote_ident table_name.to_s}
|
11
|
+
ADD CONSTRAINT #{connection.quote_ident foreign_key_name.to_s}
|
12
|
+
FOREIGN KEY (#{column_names_sql})
|
13
|
+
REFERENCES #{connection.quote_ident foreign_schema_name.to_s}.#{connection.quote_ident foreign_table_name.to_s} (#{foreign_column_names_sql})
|
14
|
+
SQL
|
15
|
+
# refresh the cached representation of the database foreign keys
|
16
|
+
refresh_keys_and_unique_constraints_cache_materialized_view
|
17
|
+
# note that the database has been reset and there are no changes
|
18
|
+
@has_changes = true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PGSpecHelper
|
4
|
+
module IndexCache
|
5
|
+
# whenever schema changes are made from within the test suite we need to
|
6
|
+
# rebuild the materialized views that hold a cached representation of the
|
7
|
+
# database indexes
|
8
|
+
def refresh_index_cache_materialized_view
|
9
|
+
# the first time we detect the presence of the marerialized view, we no longer need to
|
10
|
+
# check for it
|
11
|
+
@index_cache_exists ||= index_cache_exists?
|
12
|
+
if @index_cache_exists
|
13
|
+
connection.exec(<<~SQL)
|
14
|
+
REFRESH MATERIALIZED VIEW public.pg_spec_helper_index_cache;
|
15
|
+
SQL
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def index_cache_exists?
|
20
|
+
exists = connection.exec(<<~SQL)
|
21
|
+
SELECT TRUE AS exists FROM pg_matviews WHERE schemaname = 'public' AND matviewname = 'pg_spec_helper_index_cache';
|
22
|
+
SQL
|
23
|
+
exists.count > 0
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PGSpecHelper
|
4
|
+
module Indexes
|
5
|
+
def create_index schema_name, table_name, column_names, index_name, type, deferrable, initially_deferred
|
6
|
+
connection.exec(<<-SQL)
|
7
|
+
CREATE INDEX #{connection.quote_ident index_name.to_s}
|
8
|
+
ON #{connection.quote_ident schema_name.to_s}.#{connection.quote_ident table_name.to_s} (b, c)
|
9
|
+
SQL
|
10
|
+
# refresh the cached representation of the database indexes
|
11
|
+
refresh_index_cache_materialized_view
|
12
|
+
# note that the database has been reset and there are no changes
|
13
|
+
@has_changes = true
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PGSpecHelper
|
4
|
+
module PrimaryKeys
|
5
|
+
# add a primary_key to the provided table which covers the provided columns
|
6
|
+
def add_primary_key schema_name, table_name, column_names
|
7
|
+
column_names_sql = column_names.map { |n| connection.quote_ident n.to_s }.join(", ")
|
8
|
+
# add the primary_key
|
9
|
+
connection.exec(<<-SQL)
|
10
|
+
ALTER TABLE #{connection.quote_ident schema_name.to_s}.#{connection.quote_ident table_name.to_s}
|
11
|
+
ADD PRIMARY KEY (#{column_names_sql})
|
12
|
+
SQL
|
13
|
+
# refresh the cached representation of the database primary_key keys
|
14
|
+
refresh_keys_and_unique_constraints_cache_materialized_view
|
15
|
+
# note that the database has been reset and there are no changes
|
16
|
+
@has_changes = true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PGSpecHelper
|
4
|
+
module Schemas
|
5
|
+
# We assert there are no schemas before we run the test suite because it
|
6
|
+
# requires starting with an empty database.
|
7
|
+
#
|
8
|
+
# We could clear it automatically here, but don't want to risk deleting data
|
9
|
+
# in case the configuration is wrong and this is connected to an unexpected
|
10
|
+
# database.
|
11
|
+
def assert_database_empty!
|
12
|
+
public_schema_exists = false
|
13
|
+
get_schema_names.each do |schema_name|
|
14
|
+
if schema_name == "public"
|
15
|
+
public_schema_exists = true
|
16
|
+
next
|
17
|
+
end
|
18
|
+
next if schema_name.start_with? "pg_"
|
19
|
+
next if schema_name == "postgis"
|
20
|
+
next if schema_name == "information_schema"
|
21
|
+
raise "Expected no schemas to exist, but found `#{schema_name}` in `#{@database}` on `#{@host}`. Your test suite might have failed to complete the last time it was run. Please delete all schemas. If you are certain this is pointed at the correct database, then you can set DYNAMIC_MIGRATIONS_CLEAR_DB_ON_STARTUP=true on your console and execute these specs again to automatically clear the database."
|
22
|
+
end
|
23
|
+
# assert that the public schema exists
|
24
|
+
raise "Public schema does not exist" unless public_schema_exists
|
25
|
+
# assert the public schema has no tables
|
26
|
+
raise "Public scheama is not empty" unless get_table_names(:public).empty?
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_schema schema_name
|
30
|
+
connection.exec(<<-SQL)
|
31
|
+
CREATE SCHEMA #{connection.quote_ident schema_name.to_s};
|
32
|
+
SQL
|
33
|
+
# refresh the cached representation of the database structure
|
34
|
+
refresh_structure_cache_materialized_view
|
35
|
+
# note that the structure has changed, so that the database can be reset between tests
|
36
|
+
@has_changes = true
|
37
|
+
end
|
38
|
+
|
39
|
+
def get_schema_names
|
40
|
+
results = connection.exec(<<-SQL)
|
41
|
+
SELECT schema_name
|
42
|
+
FROM information_schema.schemata;
|
43
|
+
SQL
|
44
|
+
schema_names = results.map { |row| row["schema_name"] }
|
45
|
+
schema_names.sort
|
46
|
+
end
|
47
|
+
|
48
|
+
def delete_all_schemas cascade: false
|
49
|
+
get_schema_names.each do |schema_name|
|
50
|
+
next if schema_name.start_with? "pg_"
|
51
|
+
next if schema_name == "postgis"
|
52
|
+
next if schema_name == "public"
|
53
|
+
next if schema_name == "information_schema"
|
54
|
+
connection.exec(<<-SQL)
|
55
|
+
DROP SCHEMA #{connection.quote_ident schema_name.to_s} #{cascade ? "CASCADE" : ""};
|
56
|
+
SQL
|
57
|
+
end
|
58
|
+
# refresh the cached representation of the database structure
|
59
|
+
# as removing objects can affect them
|
60
|
+
refresh_structure_cache_materialized_view
|
61
|
+
refresh_validation_cache_materialized_view
|
62
|
+
refresh_keys_and_unique_constraints_cache_materialized_view
|
63
|
+
# note that the database has been reset and there are no changes
|
64
|
+
@has_changes = true
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PGSpecHelper
|
4
|
+
module StructureCache
|
5
|
+
# whenever schema changes are made from within the test suite we need to
|
6
|
+
# rebuild the materialized views that hold a cached representation of the
|
7
|
+
# database structure
|
8
|
+
def refresh_structure_cache_materialized_view
|
9
|
+
# the first time we detect the presence of the marerialized view, we no longer need to
|
10
|
+
# check for it
|
11
|
+
@structure_cache_exists ||= structure_cache_exists?
|
12
|
+
if @structure_cache_exists
|
13
|
+
connection.exec(<<~SQL)
|
14
|
+
REFRESH MATERIALIZED VIEW public.pg_spec_helper_structure_cache;
|
15
|
+
SQL
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def structure_cache_exists?
|
20
|
+
exists = connection.exec(<<~SQL)
|
21
|
+
SELECT TRUE AS exists FROM pg_matviews WHERE schemaname = 'public' AND matviewname = 'pg_spec_helper_structure_cache';
|
22
|
+
SQL
|
23
|
+
exists.count > 0
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PGSpecHelper
|
4
|
+
module Tables
|
5
|
+
def create_table schema_name, table_name
|
6
|
+
connection.exec(<<-SQL)
|
7
|
+
CREATE TABLE #{connection.quote_ident schema_name.to_s}.#{connection.quote_ident table_name.to_s}(
|
8
|
+
-- tables are created empty, and have columns added to them later
|
9
|
+
);
|
10
|
+
SQL
|
11
|
+
# refresh the cached representation of the database structure
|
12
|
+
refresh_structure_cache_materialized_view
|
13
|
+
# note that the structure has changed, so that the database can be reset between tests
|
14
|
+
@has_changes = true
|
15
|
+
end
|
16
|
+
|
17
|
+
def get_table_names schema_name
|
18
|
+
rows = connection.exec_params(<<-SQL, [schema_name.to_s])
|
19
|
+
SELECT table_name FROM information_schema.tables
|
20
|
+
WHERE table_schema = $1
|
21
|
+
SQL
|
22
|
+
rows.map { |row| row["table_name"] }
|
23
|
+
end
|
24
|
+
|
25
|
+
def delete_tables schema_name
|
26
|
+
get_table_names(schema_name).each do |table_name|
|
27
|
+
connection.exec(<<-SQL)
|
28
|
+
DROP TABLE #{connection.quote_ident schema_name.to_s}.#{connection.quote_ident table_name.to_s} CASCASE
|
29
|
+
SQL
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PGSpecHelper
|
4
|
+
module UniqueConstraints
|
5
|
+
# add a unique constraint to the provided table and columns
|
6
|
+
def add_unique_constraint schema_name, table_name, column_names, constraint_key_name
|
7
|
+
column_names_sql = column_names.map { |n| connection.quote_ident n.to_s }.join(", ")
|
8
|
+
# add the constraint key
|
9
|
+
connection.exec(<<-SQL)
|
10
|
+
ALTER TABLE #{connection.quote_ident schema_name.to_s}.#{connection.quote_ident table_name.to_s}
|
11
|
+
ADD CONSTRAINT #{connection.quote_ident constraint_key_name.to_s}
|
12
|
+
UNIQUE (#{column_names_sql})
|
13
|
+
SQL
|
14
|
+
# refresh the cached representation of the database constraint keys
|
15
|
+
refresh_keys_and_unique_constraints_cache_materialized_view
|
16
|
+
# note that the database has been reset and there are no changes
|
17
|
+
@has_changes = true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PGSpecHelper
|
4
|
+
module ValidationCache
|
5
|
+
# whenever schema changes are made from within the test suite we need to
|
6
|
+
# rebuild the materialized views that hold a cached representation of the
|
7
|
+
# database structure
|
8
|
+
def refresh_validation_cache_materialized_view
|
9
|
+
# the first time we detect the presence of the marerialized view, we no longer need to
|
10
|
+
# check for it
|
11
|
+
@validations_cache_exists ||= validations_cache_exists?
|
12
|
+
if @validations_cache_exists
|
13
|
+
connection.exec(<<~SQL)
|
14
|
+
REFRESH MATERIALIZED VIEW public.pg_spec_helper_validations_cache;
|
15
|
+
SQL
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def validations_cache_exists?
|
20
|
+
exists = connection.exec(<<~SQL)
|
21
|
+
SELECT TRUE AS exists FROM pg_matviews WHERE schemaname = 'public' AND matviewname = 'pg_spec_helper_validations_cache';
|
22
|
+
SQL
|
23
|
+
exists.count > 0
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PGSpecHelper
|
4
|
+
module Validations
|
5
|
+
def create_validation schema_name, table_name, validation_name, check_clause
|
6
|
+
# todo the check_clause is vulnerable to sql injection (although this is very low risk because
|
7
|
+
# it is only ever provided by the test suite, and is never provided by the user)
|
8
|
+
connection.exec(<<-SQL)
|
9
|
+
ALTER TABLE #{connection.quote_ident schema_name.to_s}.#{connection.quote_ident table_name.to_s}
|
10
|
+
ADD CONSTRAINT #{connection.quote_ident validation_name.to_s} CHECK (#{check_clause})
|
11
|
+
SQL
|
12
|
+
# refresh the cached representation of the database structure
|
13
|
+
refresh_validation_cache_materialized_view
|
14
|
+
# note that the database has been reset and there are no changes
|
15
|
+
@has_changes = true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "yaml"
|
4
|
+
require "pg"
|
5
|
+
|
6
|
+
require "pg_spec_helper/version"
|
7
|
+
|
8
|
+
require "pg_spec_helper/configuration"
|
9
|
+
require "pg_spec_helper/connection"
|
10
|
+
require "pg_spec_helper/schemas"
|
11
|
+
require "pg_spec_helper/tables"
|
12
|
+
require "pg_spec_helper/columns"
|
13
|
+
require "pg_spec_helper/validations"
|
14
|
+
require "pg_spec_helper/foreign_keys"
|
15
|
+
require "pg_spec_helper/unique_constraints"
|
16
|
+
require "pg_spec_helper/primary_keys"
|
17
|
+
require "pg_spec_helper/indexes"
|
18
|
+
require "pg_spec_helper/validation_cache"
|
19
|
+
require "pg_spec_helper/structure_cache"
|
20
|
+
require "pg_spec_helper/foreign_key_cache"
|
21
|
+
require "pg_spec_helper/index_cache"
|
22
|
+
|
23
|
+
class PGSpecHelper
|
24
|
+
include Configuration
|
25
|
+
include Connection
|
26
|
+
include Schemas
|
27
|
+
include Tables
|
28
|
+
include Columns
|
29
|
+
include Validations
|
30
|
+
include ForeignKeys
|
31
|
+
include UniqueConstraints
|
32
|
+
include PrimaryKeys
|
33
|
+
include Indexes
|
34
|
+
include ValidationCache
|
35
|
+
include StructureCache
|
36
|
+
include ForeignKeyCache
|
37
|
+
include IndexCache
|
38
|
+
|
39
|
+
attr_reader :database, :username, :password, :host, :port
|
40
|
+
|
41
|
+
def initialize name
|
42
|
+
load_configuration_for :postgres, name
|
43
|
+
|
44
|
+
@database = require_configuration_value(:database).to_sym
|
45
|
+
@host = require_configuration_value :host
|
46
|
+
@port = require_configuration_value :port
|
47
|
+
@username = require_configuration_value :username
|
48
|
+
@password = optional_configuration_value :password
|
49
|
+
|
50
|
+
# will be set to true if any changes are made to the database structure
|
51
|
+
# this is used to determine if the structure needs to be reset between tests
|
52
|
+
@has_changes = false
|
53
|
+
end
|
54
|
+
|
55
|
+
def has_changes?
|
56
|
+
@has_changes
|
57
|
+
end
|
58
|
+
|
59
|
+
def reset! force = false
|
60
|
+
if force || @has_changes
|
61
|
+
delete_all_schemas cascade: true
|
62
|
+
# note that the database has been reset and there are no changes
|
63
|
+
@has_changes = false
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pg_spec_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Craig Ulliott
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-07-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pg
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: yaml
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.2'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.2'
|
41
|
+
description: Helper class for setting up and easily tearing down PostgreSQL database
|
42
|
+
objects within a testing environment
|
43
|
+
email:
|
44
|
+
- craigulliott@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- CHANGELOG.md
|
50
|
+
- CODE_OF_CONDUCT.md
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- lib/pg_spec_helper.rb
|
54
|
+
- lib/pg_spec_helper/columns.rb
|
55
|
+
- lib/pg_spec_helper/configuration.rb
|
56
|
+
- lib/pg_spec_helper/connection.rb
|
57
|
+
- lib/pg_spec_helper/foreign_key_cache.rb
|
58
|
+
- lib/pg_spec_helper/foreign_keys.rb
|
59
|
+
- lib/pg_spec_helper/index_cache.rb
|
60
|
+
- lib/pg_spec_helper/indexes.rb
|
61
|
+
- lib/pg_spec_helper/primary_keys.rb
|
62
|
+
- lib/pg_spec_helper/schemas.rb
|
63
|
+
- lib/pg_spec_helper/structure_cache.rb
|
64
|
+
- lib/pg_spec_helper/tables.rb
|
65
|
+
- lib/pg_spec_helper/unique_constraints.rb
|
66
|
+
- lib/pg_spec_helper/validation_cache.rb
|
67
|
+
- lib/pg_spec_helper/validations.rb
|
68
|
+
- lib/pg_spec_helper/version.rb
|
69
|
+
homepage:
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata:
|
73
|
+
source_code_uri: https://github.com/craigulliott/pg_spec_helper/
|
74
|
+
changelog_uri: https://github.com/craigulliott/pg_spec_helper/blob/main/CHANGELOG.md
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: 3.0.0
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubygems_version: 3.2.3
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: Helper class for working with PostgreSQL in a testing environment
|
94
|
+
test_files: []
|