pg_spec_helper 1.1.0 → 1.3.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 +4 -4
- data/CHANGELOG.md +14 -0
- data/README.md +21 -15
- data/lib/pg_spec_helper/materialized_views.rb +16 -0
- data/lib/pg_spec_helper/reset.rb +3 -1
- data/lib/pg_spec_helper/schemas.rb +6 -6
- data/lib/pg_spec_helper/track_changes.rb +10 -1
- data/lib/pg_spec_helper/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1285cbc5b11e282ca1e564530c3b031bf0d8455a48d7f7e2b6af112216e0287
|
4
|
+
data.tar.gz: 9c63189e6193d584bae3e84081c50a3dc151e4b717044eee56da6b18e78da48c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f150ef8957731ef55129548b3f8ccc3ff95bf211d57b2b01850df80cba4d080d72323095ee0f1648f8189fcf9487a6d9c418e53565047f8509942a05c71f4039
|
7
|
+
data.tar.gz: 77ad4f5fde90f10bf13eae8390a157adb281ee1e910992b4531604dd8cdef71e72286c38caf887814b051aab74e5d6cd81d4dc0b5e96070bc0c5a245cb3fba2f
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,19 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## [1.3.0](https://github.com/craigulliott/pg_spec_helper/compare/v1.2.0...v1.3.0) (2023-07-10)
|
4
|
+
|
5
|
+
|
6
|
+
### Features
|
7
|
+
|
8
|
+
* Dont delete the public schema, just remove all the tables instead. This is useful if you have materialized views in the public schema ([9f69160](https://github.com/craigulliott/pg_spec_helper/commit/9f691602bc851fbeea0d01a0f8e9a7555f154e35))
|
9
|
+
|
10
|
+
## [1.2.0](https://github.com/craigulliott/pg_spec_helper/compare/v1.1.0...v1.2.0) (2023-07-10)
|
11
|
+
|
12
|
+
|
13
|
+
### Features
|
14
|
+
|
15
|
+
* refresh materialized views automatically based on changes to structure ([e9a8ce0](https://github.com/craigulliott/pg_spec_helper/commit/e9a8ce011578018b2374612e6d6ce8765e49d4db))
|
16
|
+
|
3
17
|
## [1.1.0](https://github.com/craigulliott/pg_spec_helper/compare/v1.0.0...v1.1.0) (2023-07-10)
|
4
18
|
|
5
19
|
|
data/README.md
CHANGED
@@ -25,30 +25,36 @@ This gem is concerned with the **structure** of your database, not the data/reco
|
|
25
25
|
|
26
26
|
* Easily create basic tables, columns, constraints, indexes and primary/foreign keys for your specs
|
27
27
|
* Provides convenient methods for testing the presence of various database objects
|
28
|
-
* Resets your database after each spec, only if the spec made changes
|
29
|
-
* Ignores `information_schema` and any schemas or tables beginning with `pg_`
|
28
|
+
* Resets your database after each spec, but only if the spec made changes
|
29
|
+
* Ignores `information_schema` and any schemas or tables with names beginning with `pg_`
|
30
30
|
* Configurable to ignore other schemas (such as `postgis`)
|
31
|
-
*
|
31
|
+
* Deletes tables from within the `public` schema, but does not delete the actual schema during cleanup.
|
32
32
|
* Can track and refresh materialized views
|
33
33
|
* Easily access the `PG::Connection` object via `pg_spec_helper.connection` to execute your own SQL
|
34
34
|
|
35
35
|
## Installation
|
36
36
|
|
37
|
-
|
37
|
+
Add the gem to your Gemfile:
|
38
38
|
|
39
|
-
|
39
|
+
```ruby
|
40
|
+
gem "pg_spec_helper"
|
41
|
+
```
|
42
|
+
|
43
|
+
Or to your `*.gemspec`
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
spec.add_development_dependency "pg_spec_helper"
|
47
|
+
```
|
40
48
|
|
41
|
-
|
49
|
+
And run bundle install
|
42
50
|
|
43
|
-
$
|
51
|
+
$ bundle install
|
44
52
|
|
45
53
|
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.
|
46
54
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
export PATH="/opt/homebrew/opt/libpq/bin:$PATH"
|
51
|
-
```
|
55
|
+
# required for pg gem on apple silicon
|
56
|
+
$ brew install libpq
|
57
|
+
$ export PATH="/opt/homebrew/opt/libpq/bin:$PATH"
|
52
58
|
|
53
59
|
## Getting Started
|
54
60
|
|
@@ -105,7 +111,7 @@ The configuration above will assert that your database is completely empty befor
|
|
105
111
|
|
106
112
|
If rspec crashed or exited prematurely on the last execution of your test suite, then you can tell pg_spec_helper to forcefully clear your database.
|
107
113
|
|
108
|
-
|
114
|
+
$ DYNAMIC_MIGRATIONS_CLEAR_DB_ON_STARTUP=true bundle exec rspec
|
109
115
|
|
110
116
|
#### An example test which requires some specific structure
|
111
117
|
|
@@ -113,7 +119,7 @@ If rspec crashed or exited prematurely on the last execution of your test suite,
|
|
113
119
|
RSpec.describe PGSpecHelper do
|
114
120
|
let(:pg_spec_helper) { RSpec.configuration.pg_spec_helper }
|
115
121
|
|
116
|
-
describe 'where the table
|
122
|
+
describe 'where the table my_schema.my_table exists and has a single column which is also the primary key' do
|
117
123
|
before(:each) do
|
118
124
|
pg_spec_helper.create_schema :my_schema
|
119
125
|
pg_spec_helper.create_table :my_schema, :my_table
|
@@ -122,7 +128,7 @@ RSpec.describe PGSpecHelper do
|
|
122
128
|
end
|
123
129
|
|
124
130
|
it "test something which required that table to exist" do
|
125
|
-
expect
|
131
|
+
expect{}.to_not raise_error
|
126
132
|
end
|
127
133
|
end
|
128
134
|
end
|
@@ -32,6 +32,22 @@ class PGSpecHelper
|
|
32
32
|
|
33
33
|
private
|
34
34
|
|
35
|
+
# given a trackable method name, refreshes any materialized
|
36
|
+
# views which are configured to be refreshed after that method
|
37
|
+
def refresh_materialized_views_by_method method_name
|
38
|
+
assert_trackable_method_name! method_name
|
39
|
+
# check each materialized view to see if it should be refreshed
|
40
|
+
@materialized_views&.each do |schema_name, views|
|
41
|
+
views.each do |materialized_view_name, view|
|
42
|
+
# if the materialized view exists and the method name is in the list of
|
43
|
+
# this materialized view's refresh_after methods, then refresh the view
|
44
|
+
if view[:refresh_after].include?(method_name) && materialized_view_exists?(schema_name, materialized_view_name)
|
45
|
+
refresh_materialized_view schema_name, materialized_view_name
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
35
51
|
# return true if the materialized view exists, otherwise false
|
36
52
|
def materialized_view_exists? schema_name, materialized_view_name
|
37
53
|
# assert this materialized view is being tracked
|
data/lib/pg_spec_helper/reset.rb
CHANGED
@@ -3,7 +3,9 @@ class PGSpecHelper
|
|
3
3
|
# reset the database to its original state
|
4
4
|
def reset! force = false
|
5
5
|
if force || has_changes?
|
6
|
-
delete_all_schemas
|
6
|
+
delete_all_schemas
|
7
|
+
# refresh all materialized views
|
8
|
+
refresh_all_materialized_views
|
7
9
|
# reset the tracking of changes
|
8
10
|
@methods_used = {}
|
9
11
|
end
|
@@ -25,19 +25,19 @@ class PGSpecHelper
|
|
25
25
|
end
|
26
26
|
|
27
27
|
# delete all schemas in the database
|
28
|
-
def delete_all_schemas
|
29
|
-
# delete all schemas
|
30
|
-
get_schema_names.each do |schema_name|
|
28
|
+
def delete_all_schemas
|
29
|
+
# delete all schemas except public
|
30
|
+
get_schema_names.reject { |schema_name| schema_name == :public }.each do |schema_name|
|
31
31
|
connection.exec(<<-SQL)
|
32
32
|
-- temporarily set the client_min_messages to WARNING to
|
33
33
|
-- suppress the NOTICE messages about cascading deletes
|
34
34
|
SET client_min_messages TO WARNING;
|
35
|
-
DROP SCHEMA #{connection.quote_ident schema_name.to_s}
|
35
|
+
DROP SCHEMA #{connection.quote_ident schema_name.to_s} CASCADE;
|
36
36
|
SET client_min_messages TO NOTICE;
|
37
37
|
SQL
|
38
38
|
end
|
39
|
-
#
|
40
|
-
|
39
|
+
# delete all the tables from within the public schema
|
40
|
+
delete_tables :public
|
41
41
|
end
|
42
42
|
|
43
43
|
def schema_exists? schema_name
|
@@ -10,8 +10,15 @@ class PGSpecHelper
|
|
10
10
|
# reset! between tests
|
11
11
|
TRACKED_METHOD_CALLS = [
|
12
12
|
:create_schema,
|
13
|
+
:delete_all_schemas,
|
13
14
|
:create_table,
|
14
|
-
:
|
15
|
+
:delete_tables,
|
16
|
+
:create_column,
|
17
|
+
:create_foreign_key,
|
18
|
+
:create_index,
|
19
|
+
:create_primary_key,
|
20
|
+
:create_unique_constraint,
|
21
|
+
:create_validation
|
15
22
|
]
|
16
23
|
|
17
24
|
# returns true if any changes have been made to the database structure
|
@@ -61,6 +68,8 @@ class PGSpecHelper
|
|
61
68
|
track_change method_name
|
62
69
|
# call the original method
|
63
70
|
original_method.bind_call(self, *args)
|
71
|
+
# do any materialized views need to be refreshed?
|
72
|
+
refresh_materialized_views_by_method method_name
|
64
73
|
end
|
65
74
|
end
|
66
75
|
end
|