@pgpm/metaschema-schema 0.16.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.
Files changed (106) hide show
  1. package/LICENSE +22 -0
  2. package/Makefile +6 -0
  3. package/README.md +324 -0
  4. package/deploy/schemas/metaschema_private/schema.sql +12 -0
  5. package/deploy/schemas/metaschema_public/schema.sql +12 -0
  6. package/deploy/schemas/metaschema_public/tables/check_constraint/table.sql +33 -0
  7. package/deploy/schemas/metaschema_public/tables/database/indexes/databases_database_unique_name_idx.sql +20 -0
  8. package/deploy/schemas/metaschema_public/tables/database/table.sql +30 -0
  9. package/deploy/schemas/metaschema_public/tables/database_extension/table.sql +23 -0
  10. package/deploy/schemas/metaschema_public/tables/extension/table.sql +28 -0
  11. package/deploy/schemas/metaschema_public/tables/field/indexes/databases_field_uniq_names_idx.sql +13 -0
  12. package/deploy/schemas/metaschema_public/tables/field/table.sql +85 -0
  13. package/deploy/schemas/metaschema_public/tables/foreign_key_constraint/table.sql +40 -0
  14. package/deploy/schemas/metaschema_public/tables/full_text_search/table.sql +31 -0
  15. package/deploy/schemas/metaschema_public/tables/index/table.sql +39 -0
  16. package/deploy/schemas/metaschema_public/tables/limit_function/table.sql +34 -0
  17. package/deploy/schemas/metaschema_public/tables/policy/table.sql +41 -0
  18. package/deploy/schemas/metaschema_public/tables/primary_key_constraint/table.sql +30 -0
  19. package/deploy/schemas/metaschema_public/tables/procedure/table.sql +32 -0
  20. package/deploy/schemas/metaschema_public/tables/rls_function/table.sql +36 -0
  21. package/deploy/schemas/metaschema_public/tables/schema/table.sql +33 -0
  22. package/deploy/schemas/metaschema_public/tables/schema_grant/table.sql +27 -0
  23. package/deploy/schemas/metaschema_public/tables/table/indexes/databases_table_unique_name_idx.sql +20 -0
  24. package/deploy/schemas/metaschema_public/tables/table/table.sql +55 -0
  25. package/deploy/schemas/metaschema_public/tables/table_grant/table.sql +27 -0
  26. package/deploy/schemas/metaschema_public/tables/trigger/table.sql +32 -0
  27. package/deploy/schemas/metaschema_public/tables/trigger_function/table.sql +23 -0
  28. package/deploy/schemas/metaschema_public/tables/unique_constraint/table.sql +37 -0
  29. package/metaschema-schema.control +7 -0
  30. package/package.json +41 -0
  31. package/pgpm.plan +29 -0
  32. package/revert/schemas/metaschema_private/schema.sql +7 -0
  33. package/revert/schemas/metaschema_public/schema.sql +6 -0
  34. package/revert/schemas/metaschema_public/tables/check_constraint/table.sql +7 -0
  35. package/revert/schemas/metaschema_public/tables/database/indexes/databases_database_unique_name_idx.sql +6 -0
  36. package/revert/schemas/metaschema_public/tables/database/table.sql +6 -0
  37. package/revert/schemas/metaschema_public/tables/database_extension/table.sql +7 -0
  38. package/revert/schemas/metaschema_public/tables/extension/table.sql +7 -0
  39. package/revert/schemas/metaschema_public/tables/field/indexes/databases_field_uniq_names_idx.sql +7 -0
  40. package/revert/schemas/metaschema_public/tables/field/table.sql +9 -0
  41. package/revert/schemas/metaschema_public/tables/foreign_key_constraint/table.sql +7 -0
  42. package/revert/schemas/metaschema_public/tables/full_text_search/table.sql +7 -0
  43. package/revert/schemas/metaschema_public/tables/index/table.sql +7 -0
  44. package/revert/schemas/metaschema_public/tables/limit_function/table.sql +7 -0
  45. package/revert/schemas/metaschema_public/tables/policy/table.sql +7 -0
  46. package/revert/schemas/metaschema_public/tables/primary_key_constraint/table.sql +7 -0
  47. package/revert/schemas/metaschema_public/tables/procedure/table.sql +7 -0
  48. package/revert/schemas/metaschema_public/tables/rls_function/table.sql +7 -0
  49. package/revert/schemas/metaschema_public/tables/schema/table.sql +7 -0
  50. package/revert/schemas/metaschema_public/tables/schema_grant/table.sql +7 -0
  51. package/revert/schemas/metaschema_public/tables/table/indexes/databases_table_unique_name_idx.sql +6 -0
  52. package/revert/schemas/metaschema_public/tables/table/table.sql +6 -0
  53. package/revert/schemas/metaschema_public/tables/table_grant/table.sql +7 -0
  54. package/revert/schemas/metaschema_public/tables/trigger/table.sql +7 -0
  55. package/revert/schemas/metaschema_public/tables/trigger_function/table.sql +7 -0
  56. package/revert/schemas/metaschema_public/tables/unique_constraint/table.sql +7 -0
  57. package/revert/schemas/services_private/schema.sql +7 -0
  58. package/revert/schemas/services_public/schema.sql +7 -0
  59. package/revert/schemas/services_public/tables/api_extensions/table.sql +7 -0
  60. package/revert/schemas/services_public/tables/api_modules/table.sql +7 -0
  61. package/revert/schemas/services_public/tables/api_schemata/table.sql +7 -0
  62. package/revert/schemas/services_public/tables/apis/table.sql +7 -0
  63. package/revert/schemas/services_public/tables/apps/table.sql +7 -0
  64. package/revert/schemas/services_public/tables/domains/table.sql +7 -0
  65. package/revert/schemas/services_public/tables/site_metadata/table.sql +7 -0
  66. package/revert/schemas/services_public/tables/site_modules/table.sql +7 -0
  67. package/revert/schemas/services_public/tables/site_themes/table.sql +7 -0
  68. package/revert/schemas/services_public/tables/sites/table.sql +7 -0
  69. package/sql/metaschema-schema--0.15.5.sql +582 -0
  70. package/verify/schemas/metaschema_private/schema.sql +7 -0
  71. package/verify/schemas/metaschema_public/schema.sql +6 -0
  72. package/verify/schemas/metaschema_public/tables/check_constraint/table.sql +7 -0
  73. package/verify/schemas/metaschema_public/tables/database/indexes/databases_database_unique_name_idx.sql +6 -0
  74. package/verify/schemas/metaschema_public/tables/database/table.sql +6 -0
  75. package/verify/schemas/metaschema_public/tables/database_extension/table.sql +7 -0
  76. package/verify/schemas/metaschema_public/tables/extension/table.sql +7 -0
  77. package/verify/schemas/metaschema_public/tables/field/indexes/databases_field_uniq_names_idx.sql +7 -0
  78. package/verify/schemas/metaschema_public/tables/field/table.sql +6 -0
  79. package/verify/schemas/metaschema_public/tables/foreign_key_constraint/table.sql +7 -0
  80. package/verify/schemas/metaschema_public/tables/full_text_search/table.sql +7 -0
  81. package/verify/schemas/metaschema_public/tables/index/table.sql +7 -0
  82. package/verify/schemas/metaschema_public/tables/limit_function/table.sql +7 -0
  83. package/verify/schemas/metaschema_public/tables/policy/table.sql +7 -0
  84. package/verify/schemas/metaschema_public/tables/primary_key_constraint/table.sql +7 -0
  85. package/verify/schemas/metaschema_public/tables/procedure/table.sql +7 -0
  86. package/verify/schemas/metaschema_public/tables/rls_function/table.sql +7 -0
  87. package/verify/schemas/metaschema_public/tables/schema/table.sql +7 -0
  88. package/verify/schemas/metaschema_public/tables/schema_grant/table.sql +7 -0
  89. package/verify/schemas/metaschema_public/tables/table/indexes/databases_table_unique_name_idx.sql +6 -0
  90. package/verify/schemas/metaschema_public/tables/table/table.sql +6 -0
  91. package/verify/schemas/metaschema_public/tables/table_grant/table.sql +7 -0
  92. package/verify/schemas/metaschema_public/tables/trigger/table.sql +7 -0
  93. package/verify/schemas/metaschema_public/tables/trigger_function/table.sql +7 -0
  94. package/verify/schemas/metaschema_public/tables/unique_constraint/table.sql +7 -0
  95. package/verify/schemas/services_private/schema.sql +7 -0
  96. package/verify/schemas/services_public/schema.sql +7 -0
  97. package/verify/schemas/services_public/tables/api_extensions/table.sql +7 -0
  98. package/verify/schemas/services_public/tables/api_modules/table.sql +7 -0
  99. package/verify/schemas/services_public/tables/api_schemata/table.sql +7 -0
  100. package/verify/schemas/services_public/tables/apis/table.sql +7 -0
  101. package/verify/schemas/services_public/tables/apps/table.sql +7 -0
  102. package/verify/schemas/services_public/tables/domains/table.sql +7 -0
  103. package/verify/schemas/services_public/tables/site_metadata/table.sql +7 -0
  104. package/verify/schemas/services_public/tables/site_modules/table.sql +7 -0
  105. package/verify/schemas/services_public/tables/site_themes/table.sql +7 -0
  106. package/verify/schemas/services_public/tables/sites/table.sql +7 -0
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
4
+ Copyright (c) 2025 Constructive
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/Makefile ADDED
@@ -0,0 +1,6 @@
1
+ EXTENSION = metaschema-schema
2
+ DATA = sql/metaschema-schema--0.15.5.sql
3
+
4
+ PG_CONFIG = pg_config
5
+ PGXS := $(shell $(PG_CONFIG) --pgxs)
6
+ include $(PGXS)
package/README.md ADDED
@@ -0,0 +1,324 @@
1
+ # @pgpm/metaschema-schema
2
+
3
+ <p align="center" width="100%">
4
+ <img height="250" src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" />
5
+ </p>
6
+
7
+ <p align="center" width="100%">
8
+ <a href="https://github.com/constructive-io/pgpm-modules/actions/workflows/ci.yml">
9
+ <img height="20" src="https://github.com/constructive-io/pgpm-modules/actions/workflows/ci.yml/badge.svg" />
10
+ </a>
11
+ <a href="https://github.com/constructive-io/pgpm-modules/blob/main/LICENSE"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/></a>
12
+ <a href="https://www.npmjs.com/package/@pgpm/metaschema-schema"><img height="20" src="https://img.shields.io/github/package-json/v/constructive-io/pgpm-modules?filename=packages%2Fmetaschema-schema%2Fpackage.json"/></a>
13
+ </p>
14
+
15
+ Database metadata utilities and introspection functions.
16
+
17
+ ## Overview
18
+
19
+ `@pgpm/metaschema-schema` provides a comprehensive metadata management system for PostgreSQL databases. This package creates tables and schemas for storing and querying database structure information including databases, schemas, tables, fields, constraints, indexes, and more. It enables runtime schema introspection, metadata-driven code generation, and database structure management.
20
+
21
+ ## Features
22
+
23
+ - **Database Metadata Storage**: Store information about databases, schemas, tables, and fields
24
+ - **Constraint Tracking**: Track primary keys, foreign keys, unique constraints, and check constraints
25
+ - **Index Management**: Store and query index definitions
26
+ - **Trigger and Procedure Metadata**: Track database functions and triggers
27
+ - **RLS and Policy Information**: Store row-level security policies
28
+ - **Extension Tracking**: Manage database extensions and their relationships
29
+ - **API and Site Metadata**: Store API configurations and site information
30
+ - **GraphQL Integration**: Smart tags and annotations for GraphQL schema generation
31
+
32
+ ## Installation
33
+
34
+ If you have `pgpm` installed:
35
+
36
+ ```bash
37
+ pgpm install @pgpm/metaschema-schema
38
+ pgpm deploy
39
+ ```
40
+
41
+ This is a quick way to get started. The sections below provide more detailed installation options.
42
+
43
+ ### Prerequisites
44
+
45
+ ```bash
46
+ # Install pgpm CLI
47
+ npm install -g pgpm
48
+
49
+ # Start local Postgres (via Docker) and export env vars
50
+ pgpm docker start
51
+ eval "$(pgpm env)"
52
+ ```
53
+
54
+ > **Tip:** Already running Postgres? Skip the Docker step and just export your `PG*` environment variables.
55
+
56
+ ### **Add to an Existing Package**
57
+
58
+ ```bash
59
+ # 1. Install the package
60
+ pgpm install @pgpm/metaschema-schema
61
+
62
+ # 2. Deploy locally
63
+ pgpm deploy
64
+ ```
65
+
66
+ ### **Add to a New Project**
67
+
68
+ ```bash
69
+ # 1. Create a workspace
70
+ pgpm init workspace
71
+
72
+ # 2. Create your first module
73
+ cd my-workspace
74
+ pgpm init
75
+
76
+ # 3. Install a package
77
+ cd packages/my-module
78
+ pgpm install @pgpm/metaschema-schema
79
+
80
+ # 4. Deploy everything
81
+ pgpm deploy --createdb --database mydb1
82
+ ```
83
+
84
+ ## Core Schemas
85
+
86
+ ### metaschema_public Schema
87
+
88
+ Stores database structure metadata:
89
+
90
+ - **database**: Database definitions with schema names and hashes
91
+ - **schema**: Schema definitions within databases
92
+ - **table**: Table definitions with RLS, timestamps, and naming conventions
93
+ - **field**: Column definitions with types, constraints, and validation rules
94
+ - **primary_key_constraint**: Primary key definitions
95
+ - **foreign_key_constraint**: Foreign key relationships
96
+ - **unique_constraint**: Unique constraints
97
+ - **check_constraint**: Check constraint definitions
98
+ - **index**: Index definitions
99
+ - **trigger**: Trigger definitions
100
+ - **procedure**: Stored procedure definitions
101
+ - **policy**: Row-level security policies
102
+ - **extension**: PostgreSQL extensions
103
+ - **database_extension**: Extension installations per database
104
+
105
+ ### metaschema_private Schema
106
+
107
+ Private schema for internal metadata operations.
108
+
109
+ ### services_public Schema
110
+
111
+ Application-level metadata:
112
+
113
+ - **apis**: API configurations
114
+ - **api_extensions**: API extension relationships
115
+ - **api_modules**: API module definitions
116
+ - **api_schemata**: API schema configurations
117
+ - **sites**: Site definitions
118
+ - **apps**: Application definitions
119
+ - **domains**: Domain configurations
120
+ - **site_metadata**: Site metadata
121
+ - **site_modules**: Site module configurations
122
+ - **site_themes**: Site theme definitions
123
+
124
+ ## Usage
125
+
126
+ ### Storing Database Metadata
127
+
128
+ ```sql
129
+ -- Create a database entry
130
+ INSERT INTO metaschema_public.database (name, label, schema_name, private_schema_name)
131
+ VALUES ('my_app', 'My Application', 'my_app_public', 'my_app_private')
132
+ RETURNING id;
133
+
134
+ -- Create a schema entry
135
+ INSERT INTO metaschema_public.schema (database_id, name)
136
+ VALUES ('database-uuid', 'public')
137
+ RETURNING id;
138
+
139
+ -- Create a table entry
140
+ INSERT INTO metaschema_public.table (
141
+ database_id,
142
+ schema_id,
143
+ name,
144
+ label,
145
+ use_rls,
146
+ timestamps,
147
+ peoplestamps
148
+ ) VALUES (
149
+ 'database-uuid',
150
+ 'schema-uuid',
151
+ 'users',
152
+ 'Users',
153
+ true,
154
+ true,
155
+ true
156
+ );
157
+
158
+ -- Create field entries
159
+ INSERT INTO metaschema_public.field (
160
+ database_id,
161
+ table_id,
162
+ name,
163
+ label,
164
+ type,
165
+ is_required,
166
+ field_order
167
+ ) VALUES
168
+ ('database-uuid', 'table-uuid', 'id', 'ID', 'uuid', true, 1),
169
+ ('database-uuid', 'table-uuid', 'email', 'Email', 'email', true, 2),
170
+ ('database-uuid', 'table-uuid', 'name', 'Name', 'text', false, 3);
171
+ ```
172
+
173
+ ### Querying Metadata
174
+
175
+ ```sql
176
+ -- Get all tables in a database
177
+ SELECT t.name, t.label, s.name as schema_name
178
+ FROM metaschema_public.table t
179
+ JOIN metaschema_public.schema s ON t.schema_id = s.id
180
+ WHERE t.database_id = 'database-uuid';
181
+
182
+ -- Get all fields for a table
183
+ SELECT f.name, f.label, f.type, f.is_required, f.default_value
184
+ FROM metaschema_public.field f
185
+ WHERE f.table_id = 'table-uuid'
186
+ ORDER BY f.field_order;
187
+
188
+ -- Get foreign key relationships
189
+ SELECT
190
+ fk.name as constraint_name,
191
+ t1.name as from_table,
192
+ t2.name as to_table
193
+ FROM metaschema_public.foreign_key_constraint fk
194
+ JOIN metaschema_public.table t1 ON fk.table_id = t1.id
195
+ JOIN metaschema_public.table t2 ON fk.foreign_table_id = t2.id
196
+ WHERE fk.database_id = 'database-uuid';
197
+ ```
198
+
199
+ ### Smart Tags for GraphQL
200
+
201
+ The package supports smart tags for GraphQL schema generation:
202
+
203
+ ```sql
204
+ -- Add smart tags to a table
205
+ UPDATE metaschema_public.table
206
+ SET smart_tags = '{
207
+ "@omit": "create,update,delete",
208
+ "@name": "CustomTableName"
209
+ }'::jsonb
210
+ WHERE id = 'table-uuid';
211
+
212
+ -- Add smart tags to a field
213
+ UPDATE metaschema_public.field
214
+ SET smart_tags = '{
215
+ "@omit": true,
216
+ "@deprecated": "Use new_field instead"
217
+ }'::jsonb
218
+ WHERE id = 'field-uuid';
219
+ ```
220
+
221
+ ## Table Structures
222
+
223
+ ### database Table
224
+
225
+ Stores database definitions:
226
+ - `id`: UUID primary key
227
+ - `owner_id`: Owner UUID
228
+ - `schema_hash`: Unique schema hash
229
+ - `schema_name`: Public schema name
230
+ - `private_schema_name`: Private schema name
231
+ - `name`: Database name
232
+ - `label`: Display label
233
+ - `hash`: Database hash
234
+
235
+ ### table Table
236
+
237
+ Stores table definitions:
238
+ - `id`: UUID primary key
239
+ - `database_id`: Foreign key to database
240
+ - `schema_id`: Foreign key to schema
241
+ - `name`: Table name
242
+ - `label`: Display label
243
+ - `description`: Table description
244
+ - `smart_tags`: JSONB smart tags for GraphQL
245
+ - `use_rls`: Enable row-level security
246
+ - `timestamps`: Enable created_at/updated_at
247
+ - `peoplestamps`: Enable created_by/updated_by
248
+ - `plural_name`: Plural form for API
249
+ - `singular_name`: Singular form for API
250
+ - `inherits_id`: Table inheritance
251
+
252
+ ### field Table
253
+
254
+ Stores column definitions:
255
+ - `id`: UUID primary key
256
+ - `database_id`: Foreign key to database
257
+ - `table_id`: Foreign key to table
258
+ - `name`: Column name
259
+ - `label`: Display label
260
+ - `description`: Column description
261
+ - `smart_tags`: JSONB smart tags
262
+ - `is_required`: NOT NULL constraint
263
+ - `default_value`: Default value
264
+ - `is_hidden`: Hide from API
265
+ - `type`: PostgreSQL type
266
+ - `field_order`: Display order
267
+ - `regexp`: Validation regex
268
+ - `chk`: Check constraint JSON
269
+ - `min`/`max`: Numeric constraints
270
+
271
+ ## Use Cases
272
+
273
+ ### Schema-Driven Code Generation
274
+
275
+ Use metadata to generate:
276
+ - GraphQL schemas
277
+ - TypeScript types
278
+ - API documentation
279
+ - Database migration scripts
280
+ - Admin interfaces
281
+
282
+ ### Runtime Schema Introspection
283
+
284
+ Query metadata at runtime to:
285
+ - Build dynamic forms
286
+ - Generate validation rules
287
+ - Create custom queries
288
+ - Implement multi-tenancy
289
+
290
+ ### Database Documentation
291
+
292
+ Generate documentation from metadata:
293
+ - Entity-relationship diagrams
294
+ - Data dictionaries
295
+ - API specifications
296
+
297
+ ## Dependencies
298
+
299
+ - `@pgpm/database-jobs`: Background job processing
300
+ - `@pgpm/inflection`: String inflection utilities
301
+ - `@pgpm/types`: Core PostgreSQL types
302
+ - `@pgpm/verify`: Verification utilities
303
+
304
+ ## Testing
305
+
306
+ ```bash
307
+ pnpm test
308
+ ```
309
+
310
+ ## Related Tooling
311
+
312
+ * [pgpm](https://github.com/constructive-io/constructive/tree/main/packages/pgpm): **🖥️ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
313
+ * [pgsql-test](https://github.com/constructive-io/constructive/tree/main/packages/pgsql-test): **📊 Isolated testing environments** with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
314
+ * [supabase-test](https://github.com/constructive-io/constructive/tree/main/packages/supabase-test): **🧪 Supabase-native test harness** preconfigured for the local Supabase stack—per-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
315
+ * [graphile-test](https://github.com/constructive-io/constructive/tree/main/packages/graphile-test): **🔐 Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
316
+ * [pgsql-parser](https://github.com/constructive-io/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
317
+ * [libpg-query-node](https://github.com/constructive-io/libpg-query-node): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
318
+ * [pg-proto-parser](https://github.com/constructive-io/pg-proto-parser): **📦 Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
319
+
320
+ ## Disclaimer
321
+
322
+ AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
323
+
324
+ No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
@@ -0,0 +1,12 @@
1
+ -- Deploy schemas/metaschema_private/schema to pg
2
+
3
+ BEGIN;
4
+
5
+ CREATE SCHEMA metaschema_private;
6
+
7
+ GRANT USAGE ON SCHEMA metaschema_private TO authenticated;
8
+ ALTER DEFAULT PRIVILEGES IN SCHEMA metaschema_private GRANT ALL ON TABLES TO authenticated;
9
+ ALTER DEFAULT PRIVILEGES IN SCHEMA metaschema_private GRANT ALL ON SEQUENCES TO authenticated;
10
+ ALTER DEFAULT PRIVILEGES IN SCHEMA metaschema_private GRANT ALL ON FUNCTIONS TO authenticated;
11
+
12
+ COMMIT;
@@ -0,0 +1,12 @@
1
+ -- Deploy schemas/metaschema_public/schema to pg
2
+
3
+ BEGIN;
4
+
5
+ CREATE SCHEMA metaschema_public;
6
+
7
+ GRANT USAGE ON SCHEMA metaschema_public TO authenticated;
8
+ ALTER DEFAULT PRIVILEGES IN SCHEMA metaschema_public GRANT ALL ON TABLES TO authenticated;
9
+ ALTER DEFAULT PRIVILEGES IN SCHEMA metaschema_public GRANT ALL ON SEQUENCES TO authenticated;
10
+ ALTER DEFAULT PRIVILEGES IN SCHEMA metaschema_public GRANT ALL ON FUNCTIONS TO authenticated;
11
+
12
+ COMMIT;
@@ -0,0 +1,33 @@
1
+ -- Deploy schemas/metaschema_public/tables/check_constraint/table to pg
2
+
3
+ -- requires: schemas/metaschema_public/schema
4
+ -- requires: schemas/metaschema_public/tables/database/table
5
+ -- requires: schemas/metaschema_public/tables/table/table
6
+
7
+ BEGIN;
8
+
9
+ CREATE TABLE metaschema_public.check_constraint (
10
+ id uuid PRIMARY KEY DEFAULT uuid_generate_v4 (),
11
+ database_id uuid NOT NULL DEFAULT uuid_nil(),
12
+
13
+ table_id uuid NOT NULL,
14
+ name text,
15
+ type text,
16
+ field_ids uuid[] NOT NULL,
17
+ expr jsonb,
18
+ --
19
+
20
+ CONSTRAINT db_fkey FOREIGN KEY (database_id) REFERENCES metaschema_public.database (id) ON DELETE CASCADE,
21
+ CONSTRAINT table_fkey FOREIGN KEY (table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
22
+
23
+ UNIQUE (table_id, name),
24
+ CHECK (field_ids <> '{}')
25
+ );
26
+
27
+ COMMENT ON CONSTRAINT table_fkey ON metaschema_public.check_constraint IS E'@omit manyToMany';
28
+ COMMENT ON CONSTRAINT db_fkey ON metaschema_public.check_constraint IS E'@omit manyToMany';
29
+
30
+ CREATE INDEX check_constraint_table_id_idx ON metaschema_public.check_constraint ( table_id );
31
+ CREATE INDEX check_constraint_database_id_idx ON metaschema_public.check_constraint ( database_id );
32
+
33
+ COMMIT;
@@ -0,0 +1,20 @@
1
+ -- Deploy schemas/metaschema_public/tables/database/indexes/databases_database_unique_name_idx to pg
2
+ -- requires: schemas/metaschema_private/schema
3
+ -- requires: schemas/metaschema_public/schema
4
+ -- requires: schemas/metaschema_public/tables/database/table
5
+
6
+ BEGIN;
7
+
8
+ CREATE FUNCTION metaschema_private.database_name_hash (name text)
9
+ RETURNS bytea
10
+ AS $BODY$
11
+ SELECT
12
+ DECODE(MD5(LOWER(inflection.plural (name))), 'hex');
13
+ $BODY$
14
+ LANGUAGE sql
15
+ IMMUTABLE;
16
+
17
+ CREATE UNIQUE INDEX databases_database_unique_name_idx ON metaschema_public.database (owner_id, metaschema_private.database_name_hash (name));
18
+
19
+ COMMIT;
20
+
@@ -0,0 +1,30 @@
1
+ -- Deploy schemas/metaschema_public/tables/database/table to pg
2
+
3
+ -- requires: schemas/metaschema_public/schema
4
+
5
+ BEGIN;
6
+
7
+ CREATE TABLE metaschema_public.database (
8
+ id uuid PRIMARY KEY DEFAULT uuid_generate_v4 (),
9
+ owner_id uuid,
10
+ schema_hash text,
11
+ schema_name text,
12
+ private_schema_name text,
13
+
14
+ name text,
15
+ label text,
16
+
17
+ hash uuid,
18
+ unique(schema_hash),
19
+ unique(schema_name),
20
+ unique(private_schema_name)
21
+ );
22
+
23
+ ALTER TABLE metaschema_public.database
24
+ ADD CONSTRAINT db_namechk CHECK (char_length(name) > 2);
25
+
26
+ COMMENT ON COLUMN metaschema_public.database.schema_hash IS '@omit';
27
+ -- COMMENT ON COLUMN metaschema_public.database.schema_name IS '@omit';
28
+ -- COMMENT ON COLUMN metaschema_public.database.private_schema_name IS '@omit';
29
+
30
+ COMMIT;
@@ -0,0 +1,23 @@
1
+ -- Deploy schemas/metaschema_public/tables/database_extension/table to pg
2
+
3
+ -- requires: schemas/metaschema_public/schema
4
+ -- requires: schemas/metaschema_public/tables/extension/table
5
+ -- requires: schemas/metaschema_public/tables/database/table
6
+
7
+ BEGIN;
8
+
9
+ CREATE TABLE metaschema_public.database_extension (
10
+ name text NOT NULL PRIMARY KEY,
11
+ database_id uuid NOT NULL,
12
+
13
+ --
14
+
15
+ CONSTRAINT ext_fkey FOREIGN KEY (name) REFERENCES metaschema_public.extension (name) ON DELETE CASCADE,
16
+ CONSTRAINT db_fkey FOREIGN KEY (database_id) REFERENCES metaschema_public.database (id) ON DELETE CASCADE,
17
+ UNIQUE (database_id, name)
18
+ );
19
+
20
+ COMMENT ON CONSTRAINT db_fkey ON metaschema_public.database_extension IS E'@omit manyToMany';
21
+ CREATE INDEX database_extension_database_id_idx ON metaschema_public.database_extension ( database_id );
22
+
23
+ COMMIT;
@@ -0,0 +1,28 @@
1
+ -- Deploy schemas/metaschema_public/tables/extension/table to pg
2
+
3
+ -- requires: schemas/metaschema_public/schema
4
+
5
+ BEGIN;
6
+
7
+ -- TODO add package name
8
+
9
+ CREATE TABLE metaschema_public.extension (
10
+ name text NOT NULL PRIMARY KEY,
11
+ public_schemas text[],
12
+ private_schemas text[]
13
+ );
14
+
15
+ INSERT INTO metaschema_public.extension (name, public_schemas, private_schemas) VALUES
16
+ (
17
+ 'collections',
18
+ ARRAY['metaschema_public'],
19
+ ARRAY['metaschema_private']
20
+ ),
21
+ (
22
+ 'meta',
23
+ ARRAY['services_public'],
24
+ ARRAY['services_private']
25
+ )
26
+ ;
27
+
28
+ COMMIT;
@@ -0,0 +1,13 @@
1
+ -- Deploy schemas/metaschema_public/tables/field/indexes/databases_field_uniq_names_idx to pg
2
+
3
+ -- requires: schemas/metaschema_public/schema
4
+ -- requires: schemas/metaschema_public/tables/field/table
5
+
6
+ BEGIN;
7
+
8
+ CREATE UNIQUE INDEX databases_field_uniq_names_idx ON metaschema_public.field (
9
+ -- strip out any _id, etc., so that if you do create one and make foreign key relation, there is no conflict
10
+ table_id, DECODE(MD5(LOWER(regexp_replace(name, '^(.+?)(_row_id|_id|_uuid|_fk|_pk)$', '\1', 'i'))), 'hex')
11
+ );
12
+
13
+ COMMIT;
@@ -0,0 +1,85 @@
1
+ -- Deploy schemas/metaschema_public/tables/field/table to pg
2
+
3
+
4
+ -- requires: schemas/metaschema_public/schema
5
+ -- requires: schemas/metaschema_public/tables/table/table
6
+
7
+ BEGIN;
8
+
9
+ -- TODO should we just query this table and make a view?
10
+ -- https://www.postgresql.org/docs/9.2/catalog-pg-attribute.html
11
+
12
+ -- IF YOU WANT TO REMOVE THIS TABLE, answer the qustion, how would you add RLS to this:
13
+ -- SELECT
14
+ -- attrelid::text AS tbl
15
+ -- , attname::text AS col
16
+ -- , p.attnum::int as id,
17
+ -- t.typname as typename
18
+
19
+ -- FROM pg_catalog.pg_attribute p
20
+ -- INNER JOIN pg_catalog.pg_type t ON (t.oid = p.atttypid)
21
+ -- WHERE attrelid = 'dude_schema.products'::regclass
22
+ -- AND p.attnum > 0
23
+ -- AND NOT attisdropped;
24
+
25
+ CREATE TYPE metaschema_public.field_category AS ENUM ('core', 'module', 'app');
26
+
27
+ CREATE TABLE metaschema_public.field (
28
+ id uuid PRIMARY KEY DEFAULT uuid_generate_v4 (),
29
+ database_id uuid NOT NULL DEFAULT uuid_nil(),
30
+
31
+ table_id uuid NOT NULL,
32
+
33
+ name text NOT NULL,
34
+ label text,
35
+
36
+ description text,
37
+ smart_tags jsonb,
38
+
39
+ is_required boolean NOT NULL DEFAULT FALSE,
40
+ default_value text NULL DEFAULT NULL,
41
+ -- AST column for SQL expression validation (AST is the source of truth)
42
+ default_value_ast jsonb NULL DEFAULT NULL,
43
+
44
+ -- hidden from API using @omit keyword, a Graphile feature ONLY
45
+ is_hidden boolean NOT NULL DEFAULT FALSE,
46
+
47
+ type citext NOT NULL,
48
+
49
+ -- typmods DO THIS SOON!
50
+
51
+ field_order int not null default 0,
52
+
53
+ regexp text default null,
54
+ chk jsonb default null,
55
+ chk_expr jsonb default null,
56
+ min float default null,
57
+ max float default null,
58
+
59
+ tags citext[] NOT NULL DEFAULT '{}',
60
+
61
+ -- Field categorization for system/module/app fields (mirrors table categorization)
62
+ -- category: 'core' for system fields (id, entity_id, actor_id), 'module' for module-generated fields, 'app' for user-defined fields
63
+ -- module: the module name that created this field (e.g., 'users', 'permissions', 'memberships')
64
+ -- scope: membership_type int (1=app, 2=org, 3=group, NULL=not scoped)
65
+ category metaschema_public.field_category NOT NULL DEFAULT 'app',
66
+ module text NULL,
67
+ scope int NULL,
68
+
69
+ --
70
+ CONSTRAINT db_fkey FOREIGN KEY (database_id) REFERENCES metaschema_public.database (id) ON DELETE CASCADE,
71
+ CONSTRAINT table_fkey FOREIGN KEY (table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
72
+
73
+ UNIQUE (table_id, name)
74
+ );
75
+
76
+ COMMENT ON CONSTRAINT table_fkey ON metaschema_public.field IS E'@omit manyToMany';
77
+ COMMENT ON CONSTRAINT db_fkey ON metaschema_public.field IS E'@omit manyToMany';
78
+
79
+ CREATE INDEX field_table_id_idx ON metaschema_public.field ( table_id );
80
+ CREATE INDEX field_database_id_idx ON metaschema_public.field ( database_id );
81
+
82
+ -- Smart comment for Graphile SQL expression validator plugin
83
+ COMMENT ON COLUMN metaschema_public.field.default_value IS E'@sqlExpression';
84
+
85
+ COMMIT;
@@ -0,0 +1,40 @@
1
+ -- Deploy schemas/metaschema_public/tables/foreign_key_constraint/table to pg
2
+
3
+ -- requires: schemas/metaschema_public/tables/field/table
4
+ -- requires: schemas/metaschema_public/tables/table/table
5
+ -- requires: schemas/metaschema_public/schema
6
+
7
+ BEGIN;
8
+
9
+ CREATE TABLE metaschema_public.foreign_key_constraint (
10
+ id uuid PRIMARY KEY DEFAULT uuid_generate_v4 (),
11
+ database_id uuid NOT NULL DEFAULT uuid_nil(),
12
+
13
+ table_id uuid NOT NULL,
14
+ name text,
15
+ description text,
16
+ smart_tags jsonb,
17
+ type text,
18
+ field_ids uuid[] NOT NULL,
19
+ ref_table_id uuid NOT NULL REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
20
+ ref_field_ids uuid[] NOT NULL,
21
+ delete_action char(1) DEFAULT 'c', -- postgres default is 'a'
22
+ update_action char(1) DEFAULT 'a',
23
+
24
+ --
25
+
26
+ CONSTRAINT db_fkey FOREIGN KEY (database_id) REFERENCES metaschema_public.database (id) ON DELETE CASCADE,
27
+ CONSTRAINT table_fkey FOREIGN KEY (table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
28
+
29
+ UNIQUE(table_id, name),
30
+ CHECK (field_ids <> '{}'),
31
+ CHECK (ref_field_ids <> '{}')
32
+ );
33
+
34
+ COMMENT ON CONSTRAINT table_fkey ON metaschema_public.foreign_key_constraint IS E'@omit manyToMany';
35
+ COMMENT ON CONSTRAINT db_fkey ON metaschema_public.foreign_key_constraint IS E'@omit manyToMany';
36
+
37
+ CREATE INDEX foreign_key_constraint_table_id_idx ON metaschema_public.foreign_key_constraint ( table_id );
38
+ CREATE INDEX foreign_key_constraint_database_id_idx ON metaschema_public.foreign_key_constraint ( database_id );
39
+
40
+ COMMIT;