diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f9a614284b..951aa0a1a87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ (Add entries below in the order of server, console, cli, docs, others) + +- server: fix v2 -> v1 downgrade bug when cron triggers exist - server: add index on the `event_id` column of the `hdb_cron_event_invocation_logs` table - server: fix GraphQL type for remote relationship field (close #7284) - server: support EdDSA algorithm and key type for JWT diff --git a/server/src-lib/Hasura/RQL/DDL/Schema/LegacyCatalog.hs b/server/src-lib/Hasura/RQL/DDL/Schema/LegacyCatalog.hs index c3455d2e8a5..15f3bfb8b30 100644 --- a/server/src-lib/Hasura/RQL/DDL/Schema/LegacyCatalog.hs +++ b/server/src-lib/Hasura/RQL/DDL/Schema/LegacyCatalog.hs @@ -3,6 +3,7 @@ module Hasura.RQL.DDL.Schema.LegacyCatalog ( saveMetadataToHdbTables , fetchMetadataFromHdbTables , recreateSystemMetadata + , addCronTriggerForeignKeyConstraint ) where import Hasura.Prelude @@ -529,6 +530,16 @@ fetchMetadataFromHdbTables = liftTx do , RemoteRelationshipMetadata name definition ) +addCronTriggerForeignKeyConstraint :: MonadTx m => m () +addCronTriggerForeignKeyConstraint = + liftTx $ + Q.unitQE defaultTxErrorHandler [Q.sql| + ALTER TABLE hdb_catalog.hdb_cron_events ADD CONSTRAINT + hdb_cron_events_trigger_name_fkey FOREIGN KEY (trigger_name) + REFERENCES hdb_catalog.hdb_cron_triggers(name) + ON UPDATE CASCADE ON DELETE CASCADE; + |] () False + -- | Drops and recreates all “system-defined” metadata, aka metadata for tables and views in the -- @information_schema@ and @hdb_catalog@ schemas. These tables and views are tracked to expose them -- to the console, which allows us to reuse the same functionality we use to implement user-defined diff --git a/server/src-lib/Hasura/Server/Migrate.hs b/server/src-lib/Hasura/Server/Migrate.hs index 9ed773c3e7b..99cb19571d8 100644 --- a/server/src-lib/Hasura/Server/Migrate.hs +++ b/server/src-lib/Hasura/Server/Migrate.hs @@ -301,5 +301,12 @@ migrations maybeDefaultSourceConfig dryRun maintenanceMode = MetadataNoSources _smTables _smFunctions _metaRemoteSchemas _metaQueryCollections _metaAllowlist _metaCustomTypes _metaActions _metaCronTriggers _ -> throw400 NotSupported "Cannot downgrade since there are more than one source" - liftTx $ runHasSystemDefinedT (SystemDefined False) $ saveMetadataToHdbTables metadataV2 + liftTx $ do + runHasSystemDefinedT (SystemDefined False) $ saveMetadataToHdbTables metadataV2 + -- when the graphql-engine is migrated from v1 to v2, we drop the foreign key + -- constraint of the `hdb_catalog.hdb_cron_event` table because the cron triggers + -- in v2 are saved in the `hdb_catalog.hdb_metadata` table. So, when a downgrade + -- happens, we need to delay adding the foreign key constraint until the + -- cron triggers are added in the `hdb_catalog.hdb_cron_triggers` + addCronTriggerForeignKeyConstraint recreateSystemMetadata diff --git a/server/src-rsr/migrations/43_to_42.sql b/server/src-rsr/migrations/43_to_42.sql index 62ecf0327bf..44062824083 100644 --- a/server/src-rsr/migrations/43_to_42.sql +++ b/server/src-rsr/migrations/43_to_42.sql @@ -675,11 +675,16 @@ CREATE TABLE hdb_catalog.hdb_cron_triggers comment TEXT ); +-- The below statement is run separately and is run after the cron triggers +-- are inserted in the hdb_catalog.hdb_cron_events table. See `from43To42` +-- in Migrate.hs for more details. + + -- Add foreign key constraint from hdb_cron_events to hdb_cron_triggers -ALTER TABLE hdb_catalog.hdb_cron_events ADD CONSTRAINT - hdb_cron_events_trigger_name_fkey FOREIGN KEY (trigger_name) - REFERENCES hdb_catalog.hdb_cron_triggers(name) - ON UPDATE CASCADE ON DELETE CASCADE; +-- ALTER TABLE hdb_catalog.hdb_cron_events ADD CONSTRAINT +-- hdb_cron_events_trigger_name_fkey FOREIGN KEY (trigger_name) +-- REFERENCES hdb_catalog.hdb_cron_triggers(name) +-- ON UPDATE CASCADE ON DELETE CASCADE; CREATE VIEW hdb_catalog.hdb_cron_events_stats AS SELECT ct.name,