pg_spec_helper 1.4.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: 68e7d2fca3250ae565d38d0709b1eb73d212b5e6af6c642702e6345123a55e3a
4
- data.tar.gz: 9d2340c8d9f285e97ffce51942c359843067c663f0383e0e4a9f752382b91eff
3
+ metadata.gz: '0448eb4ca85825f030ce96b4138d8bbf1ecce2dd51e6767a936591582034bd99'
4
+ data.tar.gz: bd24c7db5a5ad3c267dd4b26d7b753aaa547db79e662235d96bb027e5cc8e5e7
5
5
  SHA512:
6
- metadata.gz: d124e944257073e9a37a1c39ef1a50ba64a86adc4f436312af1b69554a6b8ebdf36d0fedb375e89a9a521fc7e23f2c08f23bf80b15cdbe7c90c27e04542fc656
7
- data.tar.gz: 3cb9e58e2bd59553f94cee19af7f991f14174e8ee51fc8160eb923ebe467e54543a03b9d0895a6f09dd58489763af2b0299dbee3c6e04e8063af28502c17f2f1
6
+ metadata.gz: 8ec24b586e7b1550c638b9c1387ef3ac60b244ab35c857f95ab75e661f725f2c2eefeb65326ca946c2f0c97593db15bb8e3e156831cbbe72631d3afc46f29215
7
+ data.tar.gz: b4b3982f19db7d7fc2ee038167eb8fea189a368d1ac6cb98916da60008635ba2293998216a057e451523b45855b6c0d8d8992ecda80b86c030f929fa306d12f1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
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
+
3
10
  ## [1.4.0](https://github.com/craigulliott/pg_spec_helper/compare/v1.3.0...v1.4.0) (2023-07-10)
4
11
 
5
12
 
@@ -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
@@ -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.4.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.4.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