pg_spec_helper 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 682ffdd64498aa86215c2af590284dfb04be55dcfaf1b70bfe5f5f88a5475d2c
4
- data.tar.gz: 7fdd1581c726301dd48c23d9b386f78823177418e581ad76f042cea87d4583e1
3
+ metadata.gz: 49e5716d13c3a2fb075f01e431d9a0eff5993fbb1b1063ea8079a8c98e6bfbfe
4
+ data.tar.gz: 02bc59e4925df21164c3d4bfeced975a44c5a651c65f3a51c2eb48985909e8c6
5
5
  SHA512:
6
- metadata.gz: f977a9357f41a826585e15a9946d3769cbd33ecd9f592368cdc8b80276a243474a6cabb7d3ccdb170baf0565e62b1d5c2144ed75a4b5159620de7f65cdfc245c
7
- data.tar.gz: c880c669479515f49910e7855ca8f90ad6f4a24cc5d48859bc9203fae22852ecdd9345622249f61ee6fcd6f859402ac3ce73d94372e0542b60ee8d847e4e7ba0
6
+ metadata.gz: 192264003c26963902a5aab06a29d3e420d9b4b28d59985a7a469ea7c3dd6e45ba01a9d9faa93d060475178e7efaf2afedba2a24eadbd64e0b89627b97dbd0a2
7
+ data.tar.gz: 7bd2b7754f23ede028474b19d0d3ab2854c5f7a8a7f18426dc606fceb70ab1a42c969c8657aa23ce40d08c3a75fa66879d6fd5d1efdca462909e9383f4da4c7b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.2.0](https://github.com/craigulliott/pg_spec_helper/compare/v1.1.0...v1.2.0) (2023-07-10)
4
+
5
+
6
+ ### Features
7
+
8
+ * refresh materialized views automatically based on changes to structure ([e9a8ce0](https://github.com/craigulliott/pg_spec_helper/commit/e9a8ce011578018b2374612e6d6ce8765e49d4db))
9
+
3
10
  ## [1.1.0](https://github.com/craigulliott/pg_spec_helper/compare/v1.0.0...v1.1.0) (2023-07-10)
4
11
 
5
12
 
data/README.md CHANGED
@@ -25,8 +25,8 @@ 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
  * Automatically resets and recreates the `public` schema
32
32
  * Can track and refresh materialized views
@@ -34,20 +34,28 @@ This gem is concerned with the **structure** of your database, not the data/reco
34
34
 
35
35
  ## Installation
36
36
 
37
- Install the gem by executing:
37
+ Add the gem to your Gemfile:
38
38
 
39
- $ gem install pg_spec_helper
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
- Create your new platform:
49
+ And run bundle install
42
50
 
43
- $ pg_spec_helper create my_platform_name
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
55
  ```
48
- # required for pg gem on apple silicon
49
- brew install libpq
50
- export PATH="/opt/homebrew/opt/libpq/bin:$PATH"
56
+ # required for pg gem on apple silicon
57
+ $ brew install libpq
58
+ $ export PATH="/opt/homebrew/opt/libpq/bin:$PATH"
51
59
  ```
52
60
 
53
61
  ## Getting Started
@@ -105,7 +113,7 @@ The configuration above will assert that your database is completely empty befor
105
113
 
106
114
  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
115
 
108
- `DYNAMIC_MIGRATIONS_CLEAR_DB_ON_STARTUP=true bundle exec rspec`
116
+ $ DYNAMIC_MIGRATIONS_CLEAR_DB_ON_STARTUP=true bundle exec rspec
109
117
 
110
118
  #### An example test which requires some specific structure
111
119
 
@@ -113,7 +121,7 @@ If rspec crashed or exited prematurely on the last execution of your test suite,
113
121
  RSpec.describe PGSpecHelper do
114
122
  let(:pg_spec_helper) { RSpec.configuration.pg_spec_helper }
115
123
 
116
- describe 'where the table `my_schema`.`my_table` exists in the database' do
124
+ describe 'where the table my_schema.my_table exists and has a single column which is also the primary key' do
117
125
  before(:each) do
118
126
  pg_spec_helper.create_schema :my_schema
119
127
  pg_spec_helper.create_table :my_schema, :my_table
@@ -122,7 +130,7 @@ RSpec.describe PGSpecHelper do
122
130
  end
123
131
 
124
132
  it "test something which required that table to exist" do
125
- expect().to_not raise_error
133
+ expect{}.to_not raise_error
126
134
  end
127
135
  end
128
136
  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
@@ -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 cascade: true
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,14 +25,14 @@ class PGSpecHelper
25
25
  end
26
26
 
27
27
  # delete all schemas in the database
28
- def delete_all_schemas cascade: false
28
+ def delete_all_schemas
29
29
  # delete all schemas
30
30
  get_schema_names.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} #{cascade ? "CASCADE" : ""};
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
@@ -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
- :create_column
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class PGSpecHelper
4
- VERSION = "1.1.0"
4
+ VERSION = "1.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_spec_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Craig Ulliott