forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c90e2b
commit fcb3e9f
Showing
4 changed files
with
139 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
ALTER TABLE hdb_catalog.hdb_version | ||
ADD COLUMN pg_version text NULL | ||
ADD COLUMN pg_upgraded_on timestamptz NULL; | ||
|
||
CREATE VIEW hdb_catalog.hdb_columns_ordinary_default_values AS | ||
SELECT table_schema::text, table_name::text, column_name::text, column_default::text | ||
FROM information_schema.columns | ||
WHERE column_default IS NOT NULL; | ||
|
||
-- Same as hdb_columns_ordinary_default_values on versions of Postgres <10, but | ||
-- includes identity columns on Postgres >=10. | ||
CREATE VIEW hdb_catalog.hdb_columns_default_values AS | ||
SELECT * FROM hdb_catalog.hdb_columns_ordinary_default_values; | ||
|
||
CREATE OR REPLACE FUNCTION | ||
hdb_catalog.inject_table_defaults(view_schema text, view_name text, tab_schema text, tab_name text) RETURNS void | ||
LANGUAGE plpgsql AS $$ | ||
DECLARE | ||
r RECORD; | ||
BEGIN | ||
FOR r IN SELECT column_name, column_default | ||
FROM hdb_catalog.hdb_columns_default_values | ||
WHERE table_schema = tab_schema AND table_name = tab_name | ||
EXECUTE format('ALTER VIEW %I.%I ALTER COLUMN %I SET DEFAULT %s;', view_schema, view_name, r.column_name, r.column_default); | ||
END LOOP; | ||
END $$; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-- Postgres 10 identity columns (i.e. those created with GENERATED BY DEFAULT AS IDENTITY) don’t have | ||
-- ordinary default values (as reported by information_schema.columns.column_default), but we still | ||
-- need them for the purposes of hdb_catalog.inject_table_defaults. | ||
-- | ||
-- Internally, generated columns are backed by ordinary sequences. This view exposes them using the | ||
-- same interface as information_schema. | ||
CREATE VIEW hdb_catalog.hdb_columns_identity_default_values AS | ||
SELECT table_schema.nspname::text AS table_schema | ||
, table_class.relname::text AS table_name | ||
, identity_column.attname::text AS column_name | ||
, format('nextval(%L::regclass)', sequence.seqrelid::regclass)::text AS column_default | ||
FROM pg_sequence sequence | ||
JOIN pg_depend depend | ||
ON depend.classid = 'pg_class'::regclass | ||
AND depend.refclassid = 'pg_class'::regclass | ||
AND depend.objid = sequence.seqrelid | ||
AND depend.deptype = 'i' | ||
JOIN pg_attribute identity_column | ||
ON identity_column.attrelid = depend.refobjid | ||
AND identity_column.attnum = depend.refobjsubid | ||
JOIN pg_class table_class ON table_class.oid = identity_column.attrelid | ||
JOIN pg_namespace table_schema ON table_schema.oid = table_class.relnamespace; | ||
|
||
CREATE OR REPLACE VIEW hdb_catalog.hdb_columns_default_values | ||
AS SELECT * FROM hdb_catalog.hdb_columns_ordinary_default_values | ||
UNION SELECT * FROM hdb_catalog.hdb_columns_identity_default_values; | ||
|
||
-- TODO: Refresh any views that depend on tables with identity columns. |