pg_spec_helper 1.8.4 → 1.9.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/lib/pg_spec_helper/columns.rb +1 -1
- data/lib/pg_spec_helper/enums.rb +32 -0
- data/lib/pg_spec_helper/extensions.rb +29 -0
- data/lib/pg_spec_helper/foreign_keys.rb +5 -5
- data/lib/pg_spec_helper/indexes.rb +1 -1
- data/lib/pg_spec_helper/materialized_views.rb +1 -1
- data/lib/pg_spec_helper/primary_keys.rb +2 -2
- data/lib/pg_spec_helper/schemas.rb +1 -1
- data/lib/pg_spec_helper/tables.rb +2 -2
- data/lib/pg_spec_helper/version.rb +1 -1
- data/lib/pg_spec_helper.rb +4 -2
- metadata +4 -3
- data/lib/pg_spec_helper/sanitize.rb +0 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 111948cb669c62fe5e30ff56765ec20856dc53493a87a16304087068dfae8e2e
|
4
|
+
data.tar.gz: 56df8e6b579bb74df75be7e5b8e5a62b2db35ef737d084f92706982e5e39a032
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a03eb171d281e9689c2e4bc1928845a3014e4a1f411f2c6419150bf617ce3fab4d4a6c9e9de8daa1ce885a65daa9e82310bcc92b9b61152eea1b8d1afa6f260a
|
7
|
+
data.tar.gz: e2749f90e7d1dab240f19548de538095d378a62b1b98ae6c69be64866a07fa267175ead068be77fbd15f1f70af514a4a7fa63974e9c8aec1bf8b3962dd8c06cd
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,19 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## [1.9.0](https://github.com/craigulliott/pg_spec_helper/compare/v1.8.5...v1.9.0) (2023-08-21)
|
4
|
+
|
5
|
+
|
6
|
+
### Features
|
7
|
+
|
8
|
+
* added support for enums and extensions ([b22f662](https://github.com/craigulliott/pg_spec_helper/commit/b22f662cad22c1220ac6f4bc029ca534348a4bea))
|
9
|
+
|
10
|
+
## [1.8.5](https://github.com/craigulliott/pg_spec_helper/compare/v1.8.4...v1.8.5) (2023-08-19)
|
11
|
+
|
12
|
+
|
13
|
+
### Bug Fixes
|
14
|
+
|
15
|
+
* removing the sanitization of sql data (it is useless in this gem, and gets in the way of testing some things, which defeats the point of this gem) ([fe6af3b](https://github.com/craigulliott/pg_spec_helper/commit/fe6af3bd4443b140a3136d43f1da61315de4d761))
|
16
|
+
|
3
17
|
## [1.8.4](https://github.com/craigulliott/pg_spec_helper/compare/v1.8.3...v1.8.4) (2023-08-18)
|
4
18
|
|
5
19
|
|
@@ -10,7 +10,7 @@ class PGSpecHelper
|
|
10
10
|
# note the `type` is safe from sql_injection due to the validation above
|
11
11
|
connection.exec(<<~SQL)
|
12
12
|
ALTER TABLE #{connection.quote_ident schema_name.to_s}.#{connection.quote_ident table_name.to_s}
|
13
|
-
ADD COLUMN #{connection.quote_ident column_name.to_s} #{
|
13
|
+
ADD COLUMN #{connection.quote_ident column_name.to_s} #{type} #{null ? "" : "NOT NULL"}
|
14
14
|
SQL
|
15
15
|
end
|
16
16
|
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PGSpecHelper
|
4
|
+
module Enums
|
5
|
+
# Create an enum
|
6
|
+
def create_enum schema_name, enum_name, values
|
7
|
+
connection.exec(<<~SQL)
|
8
|
+
CREATE TYPE #{schema_name}.#{enum_name} as ENUM ('#{values.join("','")}')
|
9
|
+
SQL
|
10
|
+
end
|
11
|
+
|
12
|
+
# Drop an enum
|
13
|
+
def drop_enum schema_name, enum_name
|
14
|
+
connection.exec(<<~SQL)
|
15
|
+
DROP TYPE #{schema_name}.#{enum_name}
|
16
|
+
SQL
|
17
|
+
end
|
18
|
+
|
19
|
+
# get a list of enum names for the provided schema
|
20
|
+
def get_enum_names schema_name
|
21
|
+
rows = connection.exec_params(<<~SQL, [schema_name.to_s])
|
22
|
+
SELECT
|
23
|
+
t.typname AS enum_name
|
24
|
+
FROM pg_type t
|
25
|
+
JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
|
26
|
+
WHERE n.nspname = $1 AND t.typname[0] != '_'
|
27
|
+
GROUP BY t.typname
|
28
|
+
SQL
|
29
|
+
rows.map { |row| row["enum_name"].to_sym }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PGSpecHelper
|
4
|
+
module Extensions
|
5
|
+
# Create an extension
|
6
|
+
def create_extension extension_name
|
7
|
+
connection.exec(<<~SQL)
|
8
|
+
CREATE EXTENSION "#{extension_name}"
|
9
|
+
SQL
|
10
|
+
end
|
11
|
+
|
12
|
+
# Drop an extension
|
13
|
+
def drop_extension extension_name
|
14
|
+
connection.exec(<<~SQL)
|
15
|
+
DROP EXTENSION "#{extension_name}"
|
16
|
+
SQL
|
17
|
+
end
|
18
|
+
|
19
|
+
# get a list of extension names for the provided table
|
20
|
+
def get_extension_names
|
21
|
+
rows = connection.exec_params(<<~SQL)
|
22
|
+
SELECT
|
23
|
+
extname AS name
|
24
|
+
FROM pg_extension
|
25
|
+
SQL
|
26
|
+
rows.map { |row| row["name"].to_sym }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -4,14 +4,14 @@ class PGSpecHelper
|
|
4
4
|
module ForeignKeys
|
5
5
|
# Create a foreign key
|
6
6
|
def create_foreign_key schema_name, table_name, column_names, foreign_schema_name, foreign_table_name, foreign_column_names, foreign_key_name
|
7
|
-
column_names_sql = column_names.
|
8
|
-
foreign_column_names_sql = foreign_column_names.
|
7
|
+
column_names_sql = column_names.join(", ")
|
8
|
+
foreign_column_names_sql = foreign_column_names.join(", ")
|
9
9
|
# add the foreign key
|
10
10
|
connection.exec(<<~SQL)
|
11
|
-
ALTER TABLE #{
|
12
|
-
ADD CONSTRAINT #{
|
11
|
+
ALTER TABLE #{schema_name}.#{table_name}
|
12
|
+
ADD CONSTRAINT #{foreign_key_name}
|
13
13
|
FOREIGN KEY (#{column_names_sql})
|
14
|
-
REFERENCES #{
|
14
|
+
REFERENCES #{foreign_schema_name}.#{foreign_table_name} (#{foreign_column_names_sql})
|
15
15
|
SQL
|
16
16
|
end
|
17
17
|
|
@@ -4,7 +4,7 @@ class PGSpecHelper
|
|
4
4
|
module Indexes
|
5
5
|
# Create an index
|
6
6
|
def create_index schema_name, table_name, column_names, index_name
|
7
|
-
column_names_sql = column_names.
|
7
|
+
column_names_sql = column_names.join(", ")
|
8
8
|
connection.exec(<<~SQL)
|
9
9
|
CREATE INDEX #{connection.quote_ident index_name.to_s}
|
10
10
|
ON #{connection.quote_ident schema_name.to_s}.#{connection.quote_ident table_name.to_s} (#{column_names_sql})
|
@@ -71,7 +71,7 @@ class PGSpecHelper
|
|
71
71
|
# time the materialized view is found to exist, at which point the method
|
72
72
|
# will always return true
|
73
73
|
@materialized_views[schema_name.to_sym][materialized_view_name.to_sym][:exists] ||= connection.exec(<<~SQL).count > 0
|
74
|
-
SELECT TRUE AS exists FROM pg_matviews WHERE schemaname = '#{
|
74
|
+
SELECT TRUE AS exists FROM pg_matviews WHERE schemaname = '#{schema_name}' AND matviewname = '#{materialized_view_name}';
|
75
75
|
SQL
|
76
76
|
end
|
77
77
|
|
@@ -7,8 +7,8 @@ class PGSpecHelper
|
|
7
7
|
column_names_sql = column_names.map { |n| connection.quote_ident n.to_s }.join(", ")
|
8
8
|
# add the primary_key
|
9
9
|
connection.exec(<<~SQL)
|
10
|
-
ALTER TABLE #{
|
11
|
-
ADD CONSTRAINT #{
|
10
|
+
ALTER TABLE #{schema_name}.#{table_name}
|
11
|
+
ADD CONSTRAINT #{primary_key_name}
|
12
12
|
PRIMARY KEY (#{column_names_sql})
|
13
13
|
SQL
|
14
14
|
end
|
@@ -11,7 +11,7 @@ class PGSpecHelper
|
|
11
11
|
|
12
12
|
# return a list of the schema names in the database
|
13
13
|
def get_schema_names
|
14
|
-
ignored_schemas_sql = ignored_schemas.
|
14
|
+
ignored_schemas_sql = ignored_schemas.join("', '")
|
15
15
|
# return a list of the schema names from the database
|
16
16
|
results = connection.exec(<<~SQL)
|
17
17
|
SELECT schema_name
|
@@ -5,7 +5,7 @@ class PGSpecHelper
|
|
5
5
|
# create a new table in the provided schema
|
6
6
|
def create_table schema_name, table_name
|
7
7
|
connection.exec(<<~SQL)
|
8
|
-
CREATE TABLE #{
|
8
|
+
CREATE TABLE #{schema_name}.#{table_name}(
|
9
9
|
-- tables are created empty, and have columns added to them later
|
10
10
|
);
|
11
11
|
SQL
|
@@ -30,7 +30,7 @@ class PGSpecHelper
|
|
30
30
|
-- temporarily set the client_min_messages to WARNING to
|
31
31
|
-- suppress the NOTICE messages about cascading deletes
|
32
32
|
SET client_min_messages TO WARNING;
|
33
|
-
DROP TABLE #{
|
33
|
+
DROP TABLE #{schema_name}.#{table_name} CASCADE;
|
34
34
|
SET client_min_messages TO NOTICE;
|
35
35
|
SQL
|
36
36
|
end
|
data/lib/pg_spec_helper.rb
CHANGED
@@ -6,7 +6,6 @@ require "pg"
|
|
6
6
|
require "pg_spec_helper/version"
|
7
7
|
|
8
8
|
require "pg_spec_helper/connection"
|
9
|
-
require "pg_spec_helper/sanitize"
|
10
9
|
require "pg_spec_helper/ignored_schemas"
|
11
10
|
require "pg_spec_helper/schemas"
|
12
11
|
require "pg_spec_helper/tables"
|
@@ -18,6 +17,8 @@ require "pg_spec_helper/primary_keys"
|
|
18
17
|
require "pg_spec_helper/indexes"
|
19
18
|
require "pg_spec_helper/triggers"
|
20
19
|
require "pg_spec_helper/functions"
|
20
|
+
require "pg_spec_helper/enums"
|
21
|
+
require "pg_spec_helper/extensions"
|
21
22
|
require "pg_spec_helper/models"
|
22
23
|
require "pg_spec_helper/materialized_views"
|
23
24
|
require "pg_spec_helper/reset"
|
@@ -31,7 +32,6 @@ class PGSpecHelper
|
|
31
32
|
end
|
32
33
|
|
33
34
|
include Connection
|
34
|
-
include Sanitize
|
35
35
|
include IgnoredSchemas
|
36
36
|
include Schemas
|
37
37
|
include Tables
|
@@ -43,6 +43,8 @@ class PGSpecHelper
|
|
43
43
|
include Indexes
|
44
44
|
include Triggers
|
45
45
|
include Functions
|
46
|
+
include Extensions
|
47
|
+
include Enums
|
46
48
|
include Models
|
47
49
|
include MaterializedViews
|
48
50
|
include Reset
|
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
|
+
version: 1.9.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-08-
|
11
|
+
date: 2023-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pg
|
@@ -54,6 +54,8 @@ files:
|
|
54
54
|
- lib/pg_spec_helper/columns.rb
|
55
55
|
- lib/pg_spec_helper/connection.rb
|
56
56
|
- lib/pg_spec_helper/empty_database.rb
|
57
|
+
- lib/pg_spec_helper/enums.rb
|
58
|
+
- lib/pg_spec_helper/extensions.rb
|
57
59
|
- lib/pg_spec_helper/foreign_keys.rb
|
58
60
|
- lib/pg_spec_helper/functions.rb
|
59
61
|
- lib/pg_spec_helper/ignored_schemas.rb
|
@@ -62,7 +64,6 @@ files:
|
|
62
64
|
- lib/pg_spec_helper/models.rb
|
63
65
|
- lib/pg_spec_helper/primary_keys.rb
|
64
66
|
- lib/pg_spec_helper/reset.rb
|
65
|
-
- lib/pg_spec_helper/sanitize.rb
|
66
67
|
- lib/pg_spec_helper/schemas.rb
|
67
68
|
- lib/pg_spec_helper/table_executer.rb
|
68
69
|
- lib/pg_spec_helper/tables.rb
|
@@ -1,25 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class PGSpecHelper
|
4
|
-
module Sanitize
|
5
|
-
class UnsafePostgresNameError < StandardError
|
6
|
-
end
|
7
|
-
|
8
|
-
# returns a sanitized version of the provided name, raises an error
|
9
|
-
# if it is not valid
|
10
|
-
#
|
11
|
-
# this is probably unnessesary due to this gem being used within a test
|
12
|
-
# suite and not a production application, but it is here for completeness
|
13
|
-
# and an abundance of caution
|
14
|
-
def sanitize_name name
|
15
|
-
# ensure the name is a string
|
16
|
-
name = name.to_s
|
17
|
-
# ensure the name is not empty
|
18
|
-
raise UnsafePostgresNameError, "name cannot be empty" if name.empty?
|
19
|
-
# ensure the name does not contain invalid characters
|
20
|
-
raise UnsafePostgresNameError, "name contains invalid characters" unless /\A[a-zA-Z0-9_-]+\z/.match?(name)
|
21
|
-
# return the name
|
22
|
-
name
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|