pg_spec_helper 1.3.0 → 1.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c1285cbc5b11e282ca1e564530c3b031bf0d8455a48d7f7e2b6af112216e0287
4
- data.tar.gz: 9c63189e6193d584bae3e84081c50a3dc151e4b717044eee56da6b18e78da48c
3
+ metadata.gz: '0448eb4ca85825f030ce96b4138d8bbf1ecce2dd51e6767a936591582034bd99'
4
+ data.tar.gz: bd24c7db5a5ad3c267dd4b26d7b753aaa547db79e662235d96bb027e5cc8e5e7
5
5
  SHA512:
6
- metadata.gz: f150ef8957731ef55129548b3f8ccc3ff95bf211d57b2b01850df80cba4d080d72323095ee0f1648f8189fcf9487a6d9c418e53565047f8509942a05c71f4039
7
- data.tar.gz: 77ad4f5fde90f10bf13eae8390a157adb281ee1e910992b4531604dd8cdef71e72286c38caf887814b051aab74e5d6cd81d4dc0b5e96070bc0c5a245cb3fba2f
6
+ metadata.gz: 8ec24b586e7b1550c638b9c1387ef3ac60b244ab35c857f95ab75e661f725f2c2eefeb65326ca946c2f0c97593db15bb8e3e156831cbbe72631d3afc46f29215
7
+ data.tar.gz: b4b3982f19db7d7fc2ee038167eb8fea189a368d1ac6cb98916da60008635ba2293998216a057e451523b45855b6c0d8d8992ecda80b86c030f929fa306d12f1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.5.0](https://github.com/craigulliott/pg_spec_helper/compare/v1.4.0...v1.5.0) (2023-08-01)
4
+
5
+
6
+ ### Features
7
+
8
+ * adding a new create_model method which yields a block to expose a DSL for easily working with the table structure ([dc28d5e](https://github.com/craigulliott/pg_spec_helper/commit/dc28d5ef599d8306564aa7c29d2220fb22ee6dd6))
9
+
10
+ ## [1.4.0](https://github.com/craigulliott/pg_spec_helper/compare/v1.3.0...v1.4.0) (2023-07-10)
11
+
12
+
13
+ ### Features
14
+
15
+ * made `refresh_all_materialized_views` available as a public method ([dd88ea0](https://github.com/craigulliott/pg_spec_helper/commit/dd88ea0877ba75f5c78ce5083421dd20090be6cb))
16
+
3
17
  ## [1.3.0](https://github.com/craigulliott/pg_spec_helper/compare/v1.2.0...v1.3.0) (2023-07-10)
4
18
 
5
19
 
@@ -2,12 +2,15 @@
2
2
 
3
3
  class PGSpecHelper
4
4
  module Columns
5
+ class ColumnDoesNotExistError < StandardError
6
+ end
7
+
5
8
  # create a column for the provided table
6
- def create_column schema_name, table_name, column_name, type
9
+ def create_column schema_name, table_name, column_name, type, null = true
7
10
  # note the `type` is safe from sql_injection due to the validation above
8
11
  connection.exec(<<-SQL)
9
12
  ALTER TABLE #{connection.quote_ident schema_name.to_s}.#{connection.quote_ident table_name.to_s}
10
- ADD COLUMN #{connection.quote_ident column_name.to_s} #{sanitize_name type}
13
+ ADD COLUMN #{connection.quote_ident column_name.to_s} #{sanitize_name type} #{null ? "" : "NOT NULL"}
11
14
  SQL
12
15
  end
13
16
 
@@ -22,5 +25,18 @@ class PGSpecHelper
22
25
  SQL
23
26
  rows.map { |row| row["column_name"].to_sym }
24
27
  end
28
+
29
+ # return an array of column names for the provided table
30
+ def is_column_nullable schema_name, table_name, column_name
31
+ rows = connection.exec_params(<<-SQL, [schema_name.to_s, table_name.to_s, column_name.to_s])
32
+ SELECT is_nullable
33
+ FROM information_schema.columns
34
+ WHERE table_schema = $1
35
+ AND table_name = $2
36
+ AND column_name = $3;
37
+ SQL
38
+ raise ColumnDoesNotExistError if rows.first.nil?
39
+ rows.first["is_nullable"] == "YES"
40
+ end
25
41
  end
26
42
  end
@@ -30,6 +30,17 @@ class PGSpecHelper
30
30
  }
31
31
  end
32
32
 
33
+ # refresh all materialized views that have been tracked
34
+ def refresh_all_materialized_views
35
+ @materialized_views&.each do |schema_name, views|
36
+ views.each do |materialized_view_name, view|
37
+ if materialized_view_exists? schema_name, materialized_view_name
38
+ refresh_materialized_view schema_name, materialized_view_name
39
+ end
40
+ end
41
+ end
42
+ end
43
+
33
44
  private
34
45
 
35
46
  # given a trackable method name, refreshes any materialized
@@ -64,17 +75,6 @@ class PGSpecHelper
64
75
  SQL
65
76
  end
66
77
 
67
- # refresh all materialized views that have been tracked
68
- def refresh_all_materialized_views
69
- @materialized_views&.each do |schema_name, views|
70
- views.each do |materialized_view_name, view|
71
- if materialized_view_exists? schema_name, materialized_view_name
72
- refresh_materialized_view schema_name, materialized_view_name
73
- end
74
- end
75
- end
76
- end
77
-
78
78
  # whenever schema changes are made from within the test suite we need to
79
79
  # rebuild the materialized views that hold a cached representation of the
80
80
  # database structure
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ class PGSpecHelper
4
+ module Models
5
+ # create a new table in the provided schema with an auto incrementing id
6
+ # created_at and updated_at, optionally provide a block which will be
7
+ # yeilded This allows for syntax such as:
8
+ #
9
+ # create_model :users, :optional_schema_name do |t|
10
+ # t.add_colmn string :name
11
+ # end
12
+ def create_model schema_name, table_name, &block
13
+ unless schema_exists? schema_name
14
+ create_schema schema_name
15
+ end
16
+ # create the table
17
+ create_table schema_name, table_name
18
+ # create the standard columns
19
+ create_column schema_name, table_name, :id, :serial
20
+ create_column schema_name, table_name, :created_at, :timestamp
21
+ create_column schema_name, table_name, :updated_at, :timestamp
22
+ # add the primary key
23
+ create_primary_key schema_name, table_name, [:id], :pk
24
+ # execute the optional block
25
+ if block
26
+ this = self
27
+ if this.is_a? PGSpecHelper
28
+ TableExecuter.new(this, schema_name, table_name, &block)
29
+ else
30
+ raise "Module should be added to PGSpecHelper"
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ class PGSpecHelper
4
+ class TableExecuter
5
+ def initialize pg_spec_helper, schema_name, table_name, &block
6
+ @pg_spec_helper = pg_spec_helper
7
+ @schema_name = schema_name
8
+ @table_name = table_name
9
+ instance_exec(&block)
10
+ end
11
+
12
+ def add_column column_name, type, null = true
13
+ @pg_spec_helper.create_column @schema_name, @table_name, column_name, type, null
14
+ end
15
+
16
+ def add_foreign_key column_names, foreign_schema_name, foreign_table_name, foreign_column_names, foreign_key_name
17
+ @pg_spec_helper.create_foreign_key @schema_name, @table_name, column_names, foreign_schema_name, foreign_table_name, foreign_column_names, foreign_key_name
18
+ end
19
+
20
+ def add_index column_names, index_name, unique = false
21
+ @pg_spec_helper.create_index @schema_name, @table_name, column_names, index_name
22
+ end
23
+
24
+ def add_primary_key column_names, primary_key_name
25
+ @pg_spec_helper.create_primary_key @schema_name, @table_name, column_names, primary_key_name
26
+ end
27
+
28
+ def add_unique_constraint column_names, constraint_key_name
29
+ @pg_spec_helper.create_unique_constraint @schema_name, @table_name, column_names, constraint_key_name
30
+ end
31
+
32
+ def add_validation validation_name, check_clause
33
+ @pg_spec_helper.create_validation @schema_name, @table_name, validation_name, check_clause
34
+ end
35
+ end
36
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class PGSpecHelper
4
- VERSION = "1.3.0"
4
+ VERSION = "1.5.0"
5
5
  end
@@ -16,11 +16,14 @@ require "pg_spec_helper/foreign_keys"
16
16
  require "pg_spec_helper/unique_constraints"
17
17
  require "pg_spec_helper/primary_keys"
18
18
  require "pg_spec_helper/indexes"
19
+ require "pg_spec_helper/models"
19
20
  require "pg_spec_helper/materialized_views"
20
21
  require "pg_spec_helper/reset"
21
22
  require "pg_spec_helper/empty_database"
22
23
  require "pg_spec_helper/track_changes"
23
24
 
25
+ require "pg_spec_helper/table_executer"
26
+
24
27
  class PGSpecHelper
25
28
  class MissingRequiredOptionError < StandardError
26
29
  end
@@ -36,6 +39,7 @@ class PGSpecHelper
36
39
  include UniqueConstraints
37
40
  include PrimaryKeys
38
41
  include Indexes
42
+ include Models
39
43
  include MaterializedViews
40
44
  include Reset
41
45
  include EmptyDatabase
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_spec_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Craig Ulliott
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-10 00:00:00.000000000 Z
11
+ date: 2023-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -58,10 +58,12 @@ files:
58
58
  - lib/pg_spec_helper/ignored_schemas.rb
59
59
  - lib/pg_spec_helper/indexes.rb
60
60
  - lib/pg_spec_helper/materialized_views.rb
61
+ - lib/pg_spec_helper/models.rb
61
62
  - lib/pg_spec_helper/primary_keys.rb
62
63
  - lib/pg_spec_helper/reset.rb
63
64
  - lib/pg_spec_helper/sanitize.rb
64
65
  - lib/pg_spec_helper/schemas.rb
66
+ - lib/pg_spec_helper/table_executer.rb
65
67
  - lib/pg_spec_helper/tables.rb
66
68
  - lib/pg_spec_helper/track_changes.rb
67
69
  - lib/pg_spec_helper/unique_constraints.rb