dynamic_migrations 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +13 -0
  3. data/CODE_OF_CONDUCT.md +84 -0
  4. data/LICENSE.txt +21 -0
  5. data/README.md +71 -0
  6. data/lib/dynamic_migrations/expected_boolean_error.rb +4 -0
  7. data/lib/dynamic_migrations/expected_integer_error.rb +4 -0
  8. data/lib/dynamic_migrations/expected_string_error.rb +4 -0
  9. data/lib/dynamic_migrations/expected_symbol_error.rb +4 -0
  10. data/lib/dynamic_migrations/invalid_source_error.rb +7 -0
  11. data/lib/dynamic_migrations/module_included_into_unexpected_target_error.rb +4 -0
  12. data/lib/dynamic_migrations/postgres/connections.rb +42 -0
  13. data/lib/dynamic_migrations/postgres/data_types.rb +273 -0
  14. data/lib/dynamic_migrations/postgres/server/database/configured_schemas.rb +55 -0
  15. data/lib/dynamic_migrations/postgres/server/database/connection.rb +39 -0
  16. data/lib/dynamic_migrations/postgres/server/database/differences.rb +292 -0
  17. data/lib/dynamic_migrations/postgres/server/database/keys_and_unique_constraints_loader.rb +149 -0
  18. data/lib/dynamic_migrations/postgres/server/database/loaded_schemas.rb +55 -0
  19. data/lib/dynamic_migrations/postgres/server/database/loaded_schemas_builder.rb +86 -0
  20. data/lib/dynamic_migrations/postgres/server/database/schema/table/column.rb +84 -0
  21. data/lib/dynamic_migrations/postgres/server/database/schema/table/columns.rb +58 -0
  22. data/lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraint.rb +132 -0
  23. data/lib/dynamic_migrations/postgres/server/database/schema/table/foreign_key_constraints.rb +62 -0
  24. data/lib/dynamic_migrations/postgres/server/database/schema/table/index.rb +144 -0
  25. data/lib/dynamic_migrations/postgres/server/database/schema/table/indexes.rb +63 -0
  26. data/lib/dynamic_migrations/postgres/server/database/schema/table/primary_key.rb +83 -0
  27. data/lib/dynamic_migrations/postgres/server/database/schema/table/unique_constraint.rb +101 -0
  28. data/lib/dynamic_migrations/postgres/server/database/schema/table/unique_constraints.rb +59 -0
  29. data/lib/dynamic_migrations/postgres/server/database/schema/table/validation.rb +90 -0
  30. data/lib/dynamic_migrations/postgres/server/database/schema/table/validations.rb +59 -0
  31. data/lib/dynamic_migrations/postgres/server/database/schema/table.rb +73 -0
  32. data/lib/dynamic_migrations/postgres/server/database/schema.rb +72 -0
  33. data/lib/dynamic_migrations/postgres/server/database/source.rb +37 -0
  34. data/lib/dynamic_migrations/postgres/server/database/structure_loader.rb +242 -0
  35. data/lib/dynamic_migrations/postgres/server/database/validations_loader.rb +81 -0
  36. data/lib/dynamic_migrations/postgres/server/database.rb +54 -0
  37. data/lib/dynamic_migrations/postgres/server.rb +33 -0
  38. data/lib/dynamic_migrations/postgres.rb +8 -0
  39. data/lib/dynamic_migrations/version.rb +5 -0
  40. data/lib/dynamic_migrations.rb +44 -0
  41. metadata +113 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d0a6f800da284a67951749b309cfd74aff4a769ce0c60bfc6798b07dd0108f5d
4
+ data.tar.gz: 5e07832f150f6362aaf982bcd701739747347d48d5d63176bd7c4f00e43b38a2
5
+ SHA512:
6
+ metadata.gz: 192e7713b23050b8087218e741dc5204141bdee1783015c2d100a4a8935584ba365fb9e34200c359128763ed619f93fa4289ba01f7f33c79c463745fef64f70a
7
+ data.tar.gz: 4c02f5f6ec71c1d0795ad0f57d899a8bdd311809055e390959e80d9fc75ee98adf59ea5bcb7c44a60b460367ed9bb1a2f2642d1e70fc4d3d6a3ed30e05d7e929
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0 (2023-07-05)
4
+
5
+
6
+ ### Features
7
+
8
+ * added table and column comments and column metadata ([84d798a](https://github.com/craigulliott/dynamic_migrations/commit/84d798aae35c259545f73dbbd7d076d8ceaa8739))
9
+ * basic representation of schema either configured or loaded from a real database ([7fc9ebb](https://github.com/craigulliott/dynamic_migrations/commit/7fc9ebbe5a8e5faa4e6017deec9bc66f7ba15f16))
10
+ * recursively load and builds a representation of current database schema, and find differences between configured and loaded representations of the database structure ([978e122](https://github.com/craigulliott/dynamic_migrations/commit/978e12279760709f1511dc7c6d9fe7ff57b54f3e))
11
+ * renaming the current version of constraints to validations, because its a more descriptive name, especially when considering we are going to expose multiple types of postgres constraint ([2f17af6](https://github.com/craigulliott/dynamic_migrations/commit/2f17af665028ed6f49d8bdd9b7ff6a52339206db))
12
+ * support for table constraints and added caches for database structure and constraints ([f68bbb2](https://github.com/craigulliott/dynamic_migrations/commit/f68bbb20a25fab149ed4b3b9c591fde1a6ff628e))
13
+ * table keys and indexes and a more robust database configured/loaded comparison class ([b4a0925](https://github.com/craigulliott/dynamic_migrations/commit/b4a092535e4e59d0fb9b97efc3d210289346b454))
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at craigulliott@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Craig Ulliott
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # DynamicMigrations
2
+
3
+ Monitor and generate database migrations based on difference between current schema and configuration.
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/dynamic_migrations.svg)](https://badge.fury.io/rb/dynamic_migrations)
6
+ [![Specs](https://github.com/craigulliott/dynamic_migrations/actions/workflows/specs.yml/badge.svg)](https://github.com/craigulliott/dynamic_migrations/actions/workflows/specs.yml)
7
+ [![Types](https://github.com/craigulliott/dynamic_migrations/actions/workflows/types.yml/badge.svg)](https://github.com/craigulliott/dynamic_migrations/actions/workflows/types.yml)
8
+ [![Coding Style](https://github.com/craigulliott/dynamic_migrations/actions/workflows/linter.yml/badge.svg)](https://github.com/craigulliott/dynamic_migrations/actions/workflows/linter.yml)
9
+
10
+ ## Key Features
11
+
12
+ * Supports complex validations via constraints
13
+ * Supports stored procedures
14
+ * Supports complex indexes and multi column foreign keys
15
+ * Supports postgis types
16
+ * Supports multiple postgres schemas
17
+ * Generates your database migrations
18
+ * Compares/validates actual database schema against desired configuration
19
+
20
+ ## Installation
21
+
22
+ Install the gem by executing:
23
+
24
+ $ gem install dynamic_migrations
25
+
26
+ Create your new platform:
27
+
28
+ $ dynamic_migrations create my_platform_name
29
+
30
+ 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.
31
+
32
+ ```
33
+ # required for pg gem on apple silicon
34
+ brew install libpq
35
+ export PATH="/opt/homebrew/opt/libpq/bin:$PATH"
36
+ ``
37
+
38
+ ## Getting Started
39
+
40
+ Todo
41
+
42
+
43
+ ## Development
44
+
45
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
46
+
47
+ We use [Conventional Commit Messages](https://gist.github.com/qoomon/5dfcdf8eec66a051ecd85625518cfd13).
48
+
49
+ Code should be linted and formatted according to [Ruby Standard](https://github.com/standardrb/standard).
50
+
51
+ Publishing is automated via github actions and Googles [Release Please](https://github.com/google-github-actions/release-please-action) github action
52
+
53
+ We prefer using squash-merges when merging pull requests because it helps keep a linear git history and allows more fine grained control of commit messages which get sent to release-please and ultimately show up in the changelog.
54
+
55
+ Type checking is enabled for this project. You can find the corresponding `rbs` files in the sig folder.
56
+
57
+ Install types for the packages used in development (such as `rspec`) by running
58
+
59
+ $ rbs collection install
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/craigulliott/dynamic_migrations. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/craigulliott/dynamic_migrations/blob/master/CODE_OF_CONDUCT.md).
64
+
65
+ ## License
66
+
67
+ This software is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
68
+
69
+ ## Code of Conduct
70
+
71
+ Everyone interacting in the DynamicMigrations project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/craigulliott/dynamic_migrations/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,4 @@
1
+ module DynamicMigrations
2
+ class ExpectedBooleanError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module DynamicMigrations
2
+ class ExpectedIntegerError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module DynamicMigrations
2
+ class ExpectedStringError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module DynamicMigrations
2
+ class ExpectedSymbolError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,7 @@
1
+ module DynamicMigrations
2
+ class InvalidSourceError < StandardError
3
+ def initialize source
4
+ super "expected :configuration or :database but got #{source}"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ module DynamicMigrations
2
+ class ModuleIncludedIntoUnexpectedTargetError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DynamicMigrations
4
+ module Postgres
5
+ module Connections
6
+ @connections = {}
7
+
8
+ def self.create_connection host, port, username, password, database
9
+ connection = PG.connect(
10
+ host: host,
11
+ port: port,
12
+ user: username,
13
+ password: password,
14
+ dbname: database,
15
+ sslmode: "prefer"
16
+ )
17
+ @connections[connection] = true
18
+ connection
19
+ end
20
+
21
+ def self.connections
22
+ @connections.keys
23
+ end
24
+
25
+ def self.disconnect connection
26
+ if @connections[connection]
27
+ connection.close
28
+ @connections.delete connection
29
+ true
30
+ else
31
+ false
32
+ end
33
+ end
34
+
35
+ def self.disconnect_all
36
+ @connections.keys.each do |connection|
37
+ disconnect connection
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,273 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DynamicMigrations
4
+ module Postgres
5
+ module DataTypes
6
+ class MissingRequiredAttributeError < StandardError
7
+ def initialize data_type, attribute
8
+ super "Missing required attribute `#{attribute}` for data_type `#{data_type}`"
9
+ end
10
+ end
11
+
12
+ class UnexpectedPropertyError < StandardError
13
+ def initialize data_type, attribute, value
14
+ super "Unexpected property `#{attribute}` with value `#{value}` for data_type `#{data_type}`"
15
+ end
16
+ end
17
+
18
+ class UnsupportedTypeError < StandardError
19
+ def initialize data_type
20
+ super "Unsupported type `#{data_type}`"
21
+ end
22
+ end
23
+
24
+ DATA_TYPES = {
25
+ ARRAY: {
26
+ description: "binary data (“byte array”)",
27
+ required: [
28
+ :udt_schema,
29
+ :udt_name
30
+ ]
31
+ },
32
+ "USER-DEFINED": {
33
+ description: "binary data (“byte array”)",
34
+ required: [
35
+ :udt_schema,
36
+ :udt_name
37
+ ]
38
+ },
39
+ bigint: {
40
+ description: "signed eight-byte integer",
41
+ required: [
42
+ :numeric_precision,
43
+ :numeric_precision_radix,
44
+ :numeric_scale
45
+ ]
46
+ },
47
+ # skipping this, in my tests it automatically turned into a bigint
48
+ # bigserial: {
49
+ # description: "autoincrementing eight-byte integer"
50
+ # },
51
+ bit: {
52
+ description: "fixed-length bit string",
53
+ args: "[ (n) ]",
54
+ required: [
55
+ :character_maximum_length
56
+ ]
57
+ },
58
+ "bit varying": {
59
+ description: "variable-length bit string",
60
+ args: "[ (n) ]"
61
+ },
62
+ boolean: {
63
+ description: "logical Boolean (true/false)",
64
+ required: []
65
+ },
66
+ box: {
67
+ description: "rectangular box on a plane"
68
+ },
69
+ bytea: {
70
+ description: "binary data (“byte array”)"
71
+ },
72
+ character: {
73
+ description: "fixed-length character string",
74
+ args: "[ (n) ]",
75
+ required: [
76
+ :character_maximum_length,
77
+ :character_octet_length
78
+ ]
79
+ },
80
+ "character varying": {
81
+ description: "variable-length character string",
82
+ args: "[ (n) ]",
83
+ required: [
84
+ :character_octet_length
85
+ ]
86
+ },
87
+ cidr: {
88
+ description: "IPv4 or IPv6 network address"
89
+ },
90
+ circle: {
91
+ description: "circle on a plane"
92
+ },
93
+ date: {
94
+ description: "calendar date (year, month, day)",
95
+ required: [
96
+ :datetime_precision
97
+ ]
98
+ },
99
+ "double precision": {
100
+ description: "double precision floating-point number (8 bytes)",
101
+ required: [
102
+ :numeric_precision,
103
+ :numeric_precision_radix
104
+ ]
105
+ },
106
+ inet: {
107
+ description: "IPv4 or IPv6 host address"
108
+ },
109
+ integer: {
110
+ description: "signed four-byte integer",
111
+ required: [
112
+ :numeric_precision,
113
+ :numeric_precision_radix,
114
+ :numeric_scale
115
+ ]
116
+ },
117
+ interval: {
118
+ description: "time span",
119
+ args: "[ fields ] [ (p) ]",
120
+ required: [
121
+ :datetime_precision
122
+ ]
123
+ },
124
+ json: {
125
+ description: "textual JSON data"
126
+ },
127
+ jsonb: {
128
+ description: "binary JSON data, decomposed"
129
+ },
130
+ line: {
131
+ description: "infinite line on a plane"
132
+ },
133
+ lseg: {
134
+ description: "line segment on a plane"
135
+ },
136
+ macaddr: {
137
+ description: "MAC (Media Access Control) address"
138
+ },
139
+ macaddr8: {
140
+ description: "MAC (Media Access Control) address (EUI-64 format)"
141
+ },
142
+ money: {
143
+ description: "currency amount"
144
+ },
145
+ numeric: {
146
+ description: "exact numeric of selectable precision",
147
+ args: "[ (p, s) ]",
148
+ required: [
149
+ :numeric_precision_radix
150
+ ],
151
+ optional: [
152
+ :numeric_precision,
153
+ :numeric_scale
154
+ ]
155
+ },
156
+ path: {
157
+ description: "geometric path on a plane"
158
+ },
159
+ pg_lsn: {
160
+ description: "PostgreSQL Log Sequence Number"
161
+ },
162
+ pg_snapshot: {
163
+ description: "user-level transaction ID snapshot"
164
+ },
165
+ point: {
166
+ description: "geometric point on a plane"
167
+ },
168
+ polygon: {
169
+ description: "closed geometric path on a plane"
170
+ },
171
+ real: {
172
+ description: "single precision floating-point number (4 bytes)",
173
+ required: [
174
+ :numeric_precision,
175
+ :numeric_precision_radix
176
+ ]
177
+ },
178
+ smallint: {
179
+ description: "signed two-byte",
180
+ required: [
181
+ :numeric_precision,
182
+ :numeric_precision_radix,
183
+ :numeric_scale
184
+ ]
185
+ },
186
+ smallserial: {
187
+ description: "autoincrementing two-byte"
188
+ },
189
+ serial: {
190
+ description: "autoincrementing four-byte"
191
+ },
192
+ text: {
193
+ description: "variable-length character string",
194
+ required: [
195
+ :character_octet_length
196
+ ]
197
+ },
198
+ "time without time zone": {
199
+ description: "time of day (no time zone)",
200
+ args: "[ (p) ]",
201
+ required: [
202
+ :datetime_precision
203
+ ]
204
+ },
205
+ "time with time zone": {
206
+ description: "time of day, including time zone",
207
+ args: "[ (p) ]",
208
+ required: [
209
+ :datetime_precision
210
+ ]
211
+ },
212
+ "timestamp without time zone": {
213
+ description: "date and time (no time zone)",
214
+ args: "[ (p) ]",
215
+ required: [
216
+ :datetime_precision
217
+ ]
218
+ },
219
+ "timestamp with time zone": {
220
+ description: "date and time, including time zone",
221
+ args: "[ (p) ]",
222
+ required: [
223
+ :datetime_precision
224
+ ]
225
+ },
226
+ tsquery: {
227
+ description: "text search query"
228
+ },
229
+ tsvector: {
230
+ description: "text search document"
231
+ },
232
+ txid_snapshot: {
233
+ description: "user-level transaction ID snapshot (deprecated; see pg_snapshot)"
234
+ },
235
+ uuid: {
236
+ description: "universally unique identifier"
237
+ },
238
+ xml: {
239
+ description: "XML data"
240
+ }
241
+ }
242
+
243
+ def self.validate_type_exists! data_type
244
+ raise ExpectedSymbolError, data_type unless data_type.is_a? Symbol
245
+ raise UnsupportedTypeError, data_type unless DATA_TYPES.key? data_type
246
+ true
247
+ end
248
+
249
+ def self.validate_column_properties! data_type, **column_options
250
+ validate_type_exists! data_type
251
+
252
+ required_attributes = DATA_TYPES[data_type][:required] || []
253
+ optional_attributes = DATA_TYPES[data_type][:optional] || []
254
+ possible_attributes = required_attributes + optional_attributes
255
+
256
+ # assert all required attributes are present
257
+ required_attributes.each do |attribute|
258
+ unless column_options.key?(attribute) && !column_options[attribute].nil?
259
+ raise MissingRequiredAttributeError.new data_type, attribute
260
+ end
261
+ end
262
+
263
+ # assert no unexpected attributes are present
264
+ column_options.each do |key, value|
265
+ unless value.nil? || possible_attributes.include?(key)
266
+ raise UnexpectedPropertyError.new data_type, key, value
267
+ end
268
+ end
269
+ true
270
+ end
271
+ end
272
+ end
273
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DynamicMigrations
4
+ module Postgres
5
+ class Server
6
+ class Database
7
+ module ConfiguredSchemas
8
+ class ConfiguredSchemaAlreadyExistsError < StandardError
9
+ end
10
+
11
+ class ConfiguredSchemaDoesNotExistError < StandardError
12
+ end
13
+
14
+ # adds a new configured schema for this database
15
+ def add_configured_schema schema_name
16
+ raise ExpectedSymbolError, schema_name unless schema_name.is_a? Symbol
17
+ if has_configured_schema? schema_name
18
+ raise(ConfiguredSchemaAlreadyExistsError, "Configured schema #{schema_name} already exists")
19
+ end
20
+ included_target = self
21
+ if included_target.is_a? Database
22
+ @configured_schemas[schema_name] = Schema.new :configuration, included_target, schema_name
23
+ else
24
+ raise ModuleIncludedIntoUnexpectedTargetError, included_target
25
+ end
26
+ end
27
+
28
+ # returns the configured schema object for the provided schema name, and raises an
29
+ # error if the schema does not exist
30
+ def configured_schema schema_name
31
+ raise ExpectedSymbolError, schema_name unless schema_name.is_a? Symbol
32
+ raise ConfiguredSchemaDoesNotExistError unless has_configured_schema? schema_name
33
+ @configured_schemas[schema_name]
34
+ end
35
+
36
+ # returns true if this table has a configured schema with the provided name, otherwise false
37
+ def has_configured_schema? schema_name
38
+ raise ExpectedSymbolError, schema_name unless schema_name.is_a? Symbol
39
+ @configured_schemas.key? schema_name
40
+ end
41
+
42
+ # returns an array of this tables configured schemas
43
+ def configured_schemas
44
+ @configured_schemas.values
45
+ end
46
+
47
+ # returns a hash of this tables configured schemas, keyed by schema name
48
+ def configured_schemas_hash
49
+ @configured_schemas
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DynamicMigrations
4
+ module Postgres
5
+ class Server
6
+ class Database
7
+ module Connection
8
+ class NotConnectedError < StandardError
9
+ end
10
+
11
+ class MissingDatabaseNameError < StandardError
12
+ end
13
+
14
+ class AlreadyConnectedError < StandardError
15
+ end
16
+
17
+ def connect
18
+ raise MissingDatabaseNameError unless database_name
19
+ raise AlreadyConnectedError if @connection
20
+ @connection = Postgres::Connections.create_connection server.host, server.port, server.username, server.password, database_name
21
+ end
22
+
23
+ def connection
24
+ @connection || raise(NotConnectedError)
25
+ end
26
+
27
+ def disconnect
28
+ if (conn = @connection)
29
+ Postgres::Connections.disconnect conn
30
+ @connection = nil
31
+ else
32
+ raise NotConnectedError
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end