neon 8.4.5 → 8.6.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 (79) hide show
  1. package/README.md +18 -9
  2. package/dist/_chunks/{env-4WGRW2nw.js → env-BNm69Qh8.js} +2 -5
  3. package/dist/commands/config.js +11 -28
  4. package/dist/commands/dev.js +93 -27
  5. package/dist/commands/env.js +2 -3
  6. package/dist/commands/init.js +1 -2
  7. package/dist/commands/link.js +1 -1
  8. package/dist/commands/manifest.js +1 -3
  9. package/dist/commands/subscribe.js +16 -16
  10. package/dist/credential_io.js +1 -2
  11. package/dist/dev/env.js +1 -2
  12. package/dist/dev/runtime.js +8 -0
  13. package/dist/env_file.js +9 -1
  14. package/dist/index.js +1 -1
  15. package/dist/init/auth.js +1 -4
  16. package/dist/init/choices.js +21 -7
  17. package/dist/init/copy.js +21 -7
  18. package/dist/init/run.js +25 -16
  19. package/dist/plugins/run.js +2 -1
  20. package/dist/psql/command/cmd_io.js +1 -2
  21. package/dist/psql/command/cmd_pipeline.js +1 -2
  22. package/dist/psql/core/common.js +1 -2
  23. package/dist/psql/core/startup.js +1 -2
  24. package/dist/psql/index.js +1 -2
  25. package/dist/psql/types/index.js +1 -2
  26. package/dist/skills/catalog.js +5 -0
  27. package/dist/utils/hooks.js +0 -2
  28. package/dist/utils/staging.js +1 -14
  29. package/{LICENSE → dist/vendor/postgres-meta/LICENSE} +0 -1
  30. package/dist/vendor/postgres-meta/constants.js +14 -0
  31. package/dist/vendor/postgres-meta/lib/PostgresMeta.js +49 -0
  32. package/dist/vendor/postgres-meta/lib/PostgresMetaColumnPrivileges.js +85 -0
  33. package/dist/vendor/postgres-meta/lib/PostgresMetaColumns.js +235 -0
  34. package/dist/vendor/postgres-meta/lib/PostgresMetaConfig.js +16 -0
  35. package/dist/vendor/postgres-meta/lib/PostgresMetaExtensions.js +79 -0
  36. package/dist/vendor/postgres-meta/lib/PostgresMetaForeignTables.js +81 -0
  37. package/dist/vendor/postgres-meta/lib/PostgresMetaFunctions.js +173 -0
  38. package/dist/vendor/postgres-meta/lib/PostgresMetaIndexes.js +42 -0
  39. package/dist/vendor/postgres-meta/lib/PostgresMetaMaterializedViews.js +84 -0
  40. package/dist/vendor/postgres-meta/lib/PostgresMetaPolicies.js +120 -0
  41. package/dist/vendor/postgres-meta/lib/PostgresMetaPublications.js +193 -0
  42. package/dist/vendor/postgres-meta/lib/PostgresMetaRelationships.js +87 -0
  43. package/dist/vendor/postgres-meta/lib/PostgresMetaRoles.js +203 -0
  44. package/dist/vendor/postgres-meta/lib/PostgresMetaSchemas.js +105 -0
  45. package/dist/vendor/postgres-meta/lib/PostgresMetaTablePrivileges.js +91 -0
  46. package/dist/vendor/postgres-meta/lib/PostgresMetaTables.js +183 -0
  47. package/dist/vendor/postgres-meta/lib/PostgresMetaTriggers.js +156 -0
  48. package/dist/vendor/postgres-meta/lib/PostgresMetaTypes.js +22 -0
  49. package/dist/vendor/postgres-meta/lib/PostgresMetaVersion.js +20 -0
  50. package/dist/vendor/postgres-meta/lib/PostgresMetaViews.js +83 -0
  51. package/dist/vendor/postgres-meta/lib/constants.js +8 -0
  52. package/dist/vendor/postgres-meta/lib/db.js +147 -0
  53. package/dist/vendor/postgres-meta/lib/generators.js +106 -0
  54. package/dist/vendor/postgres-meta/lib/helpers.js +26 -0
  55. package/dist/vendor/postgres-meta/lib/index.js +1 -0
  56. package/dist/vendor/postgres-meta/lib/sql/column_privileges.sql.js +154 -0
  57. package/dist/vendor/postgres-meta/lib/sql/columns.sql.js +123 -0
  58. package/dist/vendor/postgres-meta/lib/sql/common.js +1 -0
  59. package/dist/vendor/postgres-meta/lib/sql/config.sql.js +32 -0
  60. package/dist/vendor/postgres-meta/lib/sql/extensions.sql.js +20 -0
  61. package/dist/vendor/postgres-meta/lib/sql/foreign_tables.sql.js +20 -0
  62. package/dist/vendor/postgres-meta/lib/sql/functions.sql.js +141 -0
  63. package/dist/vendor/postgres-meta/lib/sql/indexes.sql.js +51 -0
  64. package/dist/vendor/postgres-meta/lib/sql/materialized_views.sql.js +21 -0
  65. package/dist/vendor/postgres-meta/lib/sql/policies.sql.js +53 -0
  66. package/dist/vendor/postgres-meta/lib/sql/publications.sql.js +46 -0
  67. package/dist/vendor/postgres-meta/lib/sql/roles.sql.js +40 -0
  68. package/dist/vendor/postgres-meta/lib/sql/schemas.sql.js +26 -0
  69. package/dist/vendor/postgres-meta/lib/sql/table.sql.js +109 -0
  70. package/dist/vendor/postgres-meta/lib/sql/table_privileges.sql.js +85 -0
  71. package/dist/vendor/postgres-meta/lib/sql/table_relationships.sql.js +52 -0
  72. package/dist/vendor/postgres-meta/lib/sql/triggers.sql.js +64 -0
  73. package/dist/vendor/postgres-meta/lib/sql/types.sql.js +65 -0
  74. package/dist/vendor/postgres-meta/lib/sql/version.sql.js +15 -0
  75. package/dist/vendor/postgres-meta/lib/sql/views.sql.js +22 -0
  76. package/dist/vendor/postgres-meta/lib/sql/views_key_dependencies.sql.js +198 -0
  77. package/dist/vendor/postgres-meta/lib/types.js +1 -0
  78. package/dist/vendor/postgres-meta/templates/typescript.js +608 -0
  79. package/package.json +12 -5
@@ -0,0 +1,64 @@
1
+ //#region src/vendor/postgres-meta/lib/sql/triggers.sql.ts
2
+ const TRIGGERS_SQL = (props) => `
3
+ SELECT
4
+ pg_t.oid AS id,
5
+ pg_t.tgrelid AS table_id,
6
+ CASE
7
+ WHEN pg_t.tgenabled = 'D' THEN 'DISABLED'
8
+ WHEN pg_t.tgenabled = 'O' THEN 'ORIGIN'
9
+ WHEN pg_t.tgenabled = 'R' THEN 'REPLICA'
10
+ WHEN pg_t.tgenabled = 'A' THEN 'ALWAYS'
11
+ END AS enabled_mode,
12
+ (
13
+ STRING_TO_ARRAY(
14
+ ENCODE(pg_t.tgargs, 'escape'), '\\000'
15
+ )
16
+ )[:pg_t.tgnargs] AS function_args,
17
+ is_t.trigger_name AS name,
18
+ is_t.event_object_table AS table,
19
+ is_t.event_object_schema AS schema,
20
+ is_t.action_condition AS condition,
21
+ is_t.action_orientation AS orientation,
22
+ is_t.action_timing AS activation,
23
+ ARRAY_AGG(is_t.event_manipulation)::text[] AS events,
24
+ pg_p.proname AS function_name,
25
+ pg_n.nspname AS function_schema
26
+ FROM
27
+ pg_trigger AS pg_t
28
+ JOIN
29
+ pg_class AS pg_c
30
+ ON pg_t.tgrelid = pg_c.oid
31
+ JOIN pg_namespace AS table_ns
32
+ ON pg_c.relnamespace = table_ns.oid
33
+ JOIN information_schema.triggers AS is_t
34
+ ON is_t.trigger_name = pg_t.tgname
35
+ AND pg_c.relname = is_t.event_object_table
36
+ AND pg_c.relnamespace = (quote_ident(is_t.event_object_schema))::regnamespace
37
+ JOIN pg_proc AS pg_p
38
+ ON pg_t.tgfoid = pg_p.oid
39
+ JOIN pg_namespace AS pg_n
40
+ ON pg_p.pronamespace = pg_n.oid
41
+ WHERE
42
+ ${props.schemaFilter ? `table_ns.nspname ${props.schemaFilter}` : "true"}
43
+ ${props.tableNameFilter ? `AND pg_c.relname ${props.tableNameFilter}` : ""}
44
+ ${props.nameFilter ? `AND is_t.trigger_name ${props.nameFilter}` : ""}
45
+ ${props.idsFilter ? `AND pg_t.oid ${props.idsFilter}` : ""}
46
+ GROUP BY
47
+ pg_t.oid,
48
+ pg_t.tgrelid,
49
+ pg_t.tgenabled,
50
+ pg_t.tgargs,
51
+ pg_t.tgnargs,
52
+ is_t.trigger_name,
53
+ is_t.event_object_table,
54
+ is_t.event_object_schema,
55
+ is_t.action_condition,
56
+ is_t.action_orientation,
57
+ is_t.action_timing,
58
+ pg_p.proname,
59
+ pg_n.nspname
60
+ ${props.limit ? `limit ${props.limit}` : ""}
61
+ ${props.offset ? `offset ${props.offset}` : ""}
62
+ `;
63
+ //#endregion
64
+ export { TRIGGERS_SQL };
@@ -0,0 +1,65 @@
1
+ //#region src/vendor/postgres-meta/lib/sql/types.sql.ts
2
+ const TYPES_SQL = (props) => `
3
+ select
4
+ t.oid::int8 as id,
5
+ t.typname as name,
6
+ n.nspname as schema,
7
+ format_type (t.oid, null) as format,
8
+ coalesce(t_enums.enums, '[]') as enums,
9
+ coalesce(t_attributes.attributes, '[]') as attributes,
10
+ obj_description (t.oid, 'pg_type') as comment,
11
+ nullif(t.typrelid::int8, 0) as type_relation_id
12
+ from
13
+ pg_type t
14
+ left join pg_namespace n on n.oid = t.typnamespace
15
+ left join (
16
+ select
17
+ enumtypid,
18
+ jsonb_agg(enumlabel order by enumsortorder) as enums
19
+ from
20
+ pg_enum
21
+ group by
22
+ enumtypid
23
+ ) as t_enums on t_enums.enumtypid = t.oid
24
+ left join (
25
+ select
26
+ oid,
27
+ jsonb_agg(
28
+ jsonb_build_object('name', a.attname, 'type_id', a.atttypid::int8)
29
+ order by a.attnum asc
30
+ ) as attributes
31
+ from
32
+ pg_class c
33
+ join pg_attribute a on a.attrelid = c.oid
34
+ where
35
+ c.relkind = 'c' and not a.attisdropped
36
+ group by
37
+ c.oid
38
+ ) as t_attributes on t_attributes.oid = t.typrelid
39
+ where
40
+ (
41
+ t.typrelid = 0
42
+ or (
43
+ select
44
+ c.relkind ${props.includeTableTypes ? `in ('c', 'r', 'v', 'm', 'p')` : `= 'c'`}
45
+ from
46
+ pg_class c
47
+ where
48
+ c.oid = t.typrelid
49
+ )
50
+ )
51
+ ${!props.includeArrayTypes ? `and not exists (
52
+ select
53
+ from
54
+ pg_type el
55
+ where
56
+ el.oid = t.typelem
57
+ and el.typarray = t.oid
58
+ )` : ""}
59
+ ${props.schemaFilter ? `and n.nspname ${props.schemaFilter}` : ""}
60
+ ${props.idsFilter ? `and t.oid ${props.idsFilter}` : ""}
61
+ ${props.limit ? `limit ${props.limit}` : ""}
62
+ ${props.offset ? `offset ${props.offset}` : ""}
63
+ `;
64
+ //#endregion
65
+ export { TYPES_SQL };
@@ -0,0 +1,15 @@
1
+ //#region src/vendor/postgres-meta/lib/sql/version.sql.ts
2
+ const VERSION_SQL = () => `
3
+ SELECT
4
+ version(),
5
+ current_setting('server_version_num') :: int8 AS version_number,
6
+ (
7
+ SELECT
8
+ COUNT(*) AS active_connections
9
+ FROM
10
+ pg_stat_activity
11
+ ) AS active_connections,
12
+ current_setting('max_connections') :: int8 AS max_connections
13
+ `;
14
+ //#endregion
15
+ export { VERSION_SQL };
@@ -0,0 +1,22 @@
1
+ //#region src/vendor/postgres-meta/lib/sql/views.sql.ts
2
+ const VIEWS_SQL = (props) => `
3
+ SELECT
4
+ c.oid :: int8 AS id,
5
+ n.nspname AS schema,
6
+ c.relname AS name,
7
+ -- See definition of information_schema.views
8
+ (pg_relation_is_updatable(c.oid, false) & 20) = 20 AS is_updatable,
9
+ obj_description(c.oid) AS comment
10
+ FROM
11
+ pg_class c
12
+ JOIN pg_namespace n ON n.oid = c.relnamespace
13
+ WHERE
14
+ ${props.schemaFilter ? `n.nspname ${props.schemaFilter} AND` : ""}
15
+ ${props.idsFilter ? `c.oid ${props.idsFilter} AND` : ""}
16
+ ${props.viewIdentifierFilter ? `(n.nspname || '.' || c.relname) ${props.viewIdentifierFilter} AND` : ""}
17
+ c.relkind = 'v'
18
+ ${props.limit ? `limit ${props.limit}` : ""}
19
+ ${props.offset ? `offset ${props.offset}` : ""}
20
+ `;
21
+ //#endregion
22
+ export { VIEWS_SQL };
@@ -0,0 +1,198 @@
1
+ //#region src/vendor/postgres-meta/lib/sql/views_key_dependencies.sql.ts
2
+ const VIEWS_KEY_DEPENDENCIES_SQL = (props) => `
3
+ -- Adapted from
4
+ -- https://github.com/PostgREST/postgrest/blob/f9f0f79fa914ac00c11fbf7f4c558e14821e67e2/src/PostgREST/SchemaCache.hs#L820
5
+ with recursive
6
+ pks_fks as (
7
+ -- pk + fk referencing col
8
+ select
9
+ contype::text as contype,
10
+ conname,
11
+ array_length(conkey, 1) as ncol,
12
+ conrelid as resorigtbl,
13
+ col as resorigcol,
14
+ ord
15
+ from pg_constraint
16
+ left join lateral unnest(conkey) with ordinality as _(col, ord) on true
17
+ where contype IN ('p', 'f')
18
+ union
19
+ -- fk referenced col
20
+ select
21
+ concat(contype, '_ref') as contype,
22
+ conname,
23
+ array_length(confkey, 1) as ncol,
24
+ confrelid,
25
+ col,
26
+ ord
27
+ from pg_constraint
28
+ left join lateral unnest(confkey) with ordinality as _(col, ord) on true
29
+ where contype='f'
30
+ ${props.schemaFilter ? `and connamespace::regnamespace::text ${props.schemaFilter}` : ""}
31
+ ),
32
+ views as (
33
+ select
34
+ c.oid as view_id,
35
+ n.nspname as view_schema,
36
+ c.relname as view_name,
37
+ r.ev_action as view_definition
38
+ from pg_class c
39
+ join pg_namespace n on n.oid = c.relnamespace
40
+ join pg_rewrite r on r.ev_class = c.oid
41
+ where c.relkind in ('v', 'm')
42
+ ${props.schemaFilter ? `and n.nspname ${props.schemaFilter}` : ""}
43
+ ),
44
+ transform_json as (
45
+ select
46
+ view_id, view_schema, view_name,
47
+ -- the following formatting is without indentation on purpose
48
+ -- to allow simple diffs, with less whitespace noise
49
+ replace(
50
+ replace(
51
+ replace(
52
+ replace(
53
+ replace(
54
+ replace(
55
+ replace(
56
+ regexp_replace(
57
+ replace(
58
+ replace(
59
+ replace(
60
+ replace(
61
+ replace(
62
+ replace(
63
+ replace(
64
+ replace(
65
+ replace(
66
+ replace(
67
+ replace(
68
+ view_definition::text,
69
+ -- This conversion to json is heavily optimized for performance.
70
+ -- The general idea is to use as few regexp_replace() calls as possible.
71
+ -- Simple replace() is a lot faster, so we jump through some hoops
72
+ -- to be able to use regexp_replace() only once.
73
+ -- This has been tested against a huge schema with 250+ different views.
74
+ -- The unit tests do NOT reflect all possible inputs. Be careful when changing this!
75
+ -- -----------------------------------------------
76
+ -- pattern | replacement | flags
77
+ -- -----------------------------------------------
78
+ -- <> in pg_node_tree is the same as null in JSON, but due to very poor performance of json_typeof
79
+ -- we need to make this an empty array here to prevent json_array_elements from throwing an error
80
+ -- when the targetList is null.
81
+ -- We'll need to put it first, to make the node protection below work for node lists that start with
82
+ -- null: (<> ..., too. This is the case for coldefexprs, when the first column does not have a default value.
83
+ '<>' , '()'
84
+ -- , is not part of the pg_node_tree format, but used in the regex.
85
+ -- This removes all , that might be part of column names.
86
+ ), ',' , ''
87
+ -- The same applies for { and }, although those are used a lot in pg_node_tree.
88
+ -- We remove the escaped ones, which might be part of column names again.
89
+ ), E'\\\\{' , ''
90
+ ), E'\\\\}' , ''
91
+ -- The fields we need are formatted as json manually to protect them from the regex.
92
+ ), ' :targetList ' , ',"targetList":'
93
+ ), ' :resno ' , ',"resno":'
94
+ ), ' :resorigtbl ' , ',"resorigtbl":'
95
+ ), ' :resorigcol ' , ',"resorigcol":'
96
+ -- Make the regex also match the node type, e.g. \`{QUERY ...\`, to remove it in one pass.
97
+ ), '{' , '{ :'
98
+ -- Protect node lists, which start with \`({\` or \`((\` from the greedy regex.
99
+ -- The extra \`{\` is removed again later.
100
+ ), '((' , '{(('
101
+ ), '({' , '{({'
102
+ -- This regex removes all unused fields to avoid the need to format all of them correctly.
103
+ -- This leads to a smaller json result as well.
104
+ -- Removal stops at \`,\` for used fields (see above) and \`}\` for the end of the current node.
105
+ -- Nesting can't be parsed correctly with a regex, so we stop at \`{\` as well and
106
+ -- add an empty key for the followig node.
107
+ ), ' :[^}{,]+' , ',"":' , 'g'
108
+ -- For performance, the regex also added those empty keys when hitting a \`,\` or \`}\`.
109
+ -- Those are removed next.
110
+ ), ',"":}' , '}'
111
+ ), ',"":,' , ','
112
+ -- This reverses the "node list protection" from above.
113
+ ), '{(' , '('
114
+ -- Every key above has been added with a \`,\` so far. The first key in an object doesn't need it.
115
+ ), '{,' , '{'
116
+ -- pg_node_tree has \`()\` around lists, but JSON uses \`[]\`
117
+ ), '(' , '['
118
+ ), ')' , ']'
119
+ -- pg_node_tree has \` \` between list items, but JSON uses \`,\`
120
+ ), ' ' , ','
121
+ )::json as view_definition
122
+ from views
123
+ ),
124
+ target_entries as(
125
+ select
126
+ view_id, view_schema, view_name,
127
+ json_array_elements(view_definition->0->'targetList') as entry
128
+ from transform_json
129
+ ),
130
+ results as(
131
+ select
132
+ view_id, view_schema, view_name,
133
+ (entry->>'resno')::int as view_column,
134
+ (entry->>'resorigtbl')::oid as resorigtbl,
135
+ (entry->>'resorigcol')::int as resorigcol
136
+ from target_entries
137
+ ),
138
+ -- CYCLE detection according to PG docs: https://www.postgresql.org/docs/current/queries-with.html#QUERIES-WITH-CYCLE
139
+ -- Can be replaced with CYCLE clause once PG v13 is EOL.
140
+ recursion(view_id, view_schema, view_name, view_column, resorigtbl, resorigcol, is_cycle, path) as(
141
+ select
142
+ r.*,
143
+ false,
144
+ ARRAY[resorigtbl]
145
+ from results r
146
+ where ${props.schemaFilter ? `view_schema ${props.schemaFilter}` : "true"}
147
+ union all
148
+ select
149
+ view.view_id,
150
+ view.view_schema,
151
+ view.view_name,
152
+ view.view_column,
153
+ tab.resorigtbl,
154
+ tab.resorigcol,
155
+ tab.resorigtbl = ANY(path),
156
+ path || tab.resorigtbl
157
+ from recursion view
158
+ join results tab on view.resorigtbl=tab.view_id and view.resorigcol=tab.view_column
159
+ where not is_cycle
160
+ ),
161
+ repeated_references as(
162
+ select
163
+ view_id,
164
+ view_schema,
165
+ view_name,
166
+ resorigtbl,
167
+ resorigcol,
168
+ array_agg(attname) as view_columns
169
+ from recursion
170
+ join pg_attribute vcol on vcol.attrelid = view_id and vcol.attnum = view_column
171
+ group by
172
+ view_id,
173
+ view_schema,
174
+ view_name,
175
+ resorigtbl,
176
+ resorigcol
177
+ )
178
+ select
179
+ sch.nspname as table_schema,
180
+ tbl.relname as table_name,
181
+ rep.view_schema,
182
+ rep.view_name,
183
+ pks_fks.conname as constraint_name,
184
+ pks_fks.contype as constraint_type,
185
+ jsonb_agg(
186
+ jsonb_build_object('table_column', col.attname, 'view_columns', view_columns) order by pks_fks.ord
187
+ ) as column_dependencies
188
+ from repeated_references rep
189
+ join pks_fks using (resorigtbl, resorigcol)
190
+ join pg_class tbl on tbl.oid = rep.resorigtbl
191
+ join pg_attribute col on col.attrelid = tbl.oid and col.attnum = rep.resorigcol
192
+ join pg_namespace sch on sch.oid = tbl.relnamespace
193
+ group by sch.nspname, tbl.relname, rep.view_schema, rep.view_name, pks_fks.conname, pks_fks.contype, pks_fks.ncol
194
+ -- make sure we only return key for which all columns are referenced in the view - no partial PKs or FKs
195
+ having ncol = array_length(array_agg(row(col.attname, view_columns) order by pks_fks.ord), 1)
196
+ `;
197
+ //#endregion
198
+ export { VIEWS_KEY_DEPENDENCIES_SQL };
@@ -0,0 +1 @@
1
+ export {};