From 03464281ee6e8ddfdadae09c048af1c86e1b1d52 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 3 Jul 2024 15:47:07 +0200 Subject: [PATCH] Fix codegen to work with dev-mode --- .../static/codegen/src/db/Migrations.res | 53 ++++++++++++++++++- .../static/codegen/src/db/TablesStatic.res | 6 +-- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/codegenerator/cli/templates/static/codegen/src/db/Migrations.res b/codegenerator/cli/templates/static/codegen/src/db/Migrations.res index 5578d035b..5f11a0816 100644 --- a/codegenerator/cli/templates/static/codegen/src/db/Migrations.res +++ b/codegenerator/cli/templates/static/codegen/src/db/Migrations.res @@ -203,6 +203,15 @@ module EntityHistory = { `) let _ = await sql->unsafe(` +CREATE OR REPLACE FUNCTION entity_type_to_int(entity_type ENTITY_TYPE) +RETURNS integer AS $$ +BEGIN + RETURN (ARRAY_POSITION(ENUM_RANGE(NULL::ENTITY_TYPE), entity_type)); +END; +$$ LANGUAGE plpgsql IMMUTABLE; +`) + + let _ = await sql->unsafe(` CREATE OR REPLACE FUNCTION get_entity_history_filter( start_timestamp integer, start_chain_id integer, @@ -218,7 +227,7 @@ module EntityHistory = { RETURN QUERY SELECT DISTINCT ON (coalesce(old.entity_id, new.entity_id)) - coalesce(old.entity_id, new.entity_id) as entity_id, + coalesce(old.entity_id, new.entity_id) as relevant_entity_id, new.chain_id as chain_id, coalesce(old.params, 'null') as old_val, coalesce(new.params, 'null') as new_val, @@ -226,7 +235,7 @@ module EntityHistory = { old.block_number as previous_block_number, new.log_index as log_index, old.log_index as previous_log_index, - new.entity_type as entity_type + entity_type_to_int(new.entity_type) as entity_type FROM entity_history old INNER JOIN entity_history next ON @@ -328,6 +337,46 @@ module EntityHistory = { END; $$ LANGUAGE plpgsql STABLE; `) + +// let _ = await sql->unsafe(` +// CREATE OR REPLACE FUNCTION get_entity_history_filter_hasura( +// end_block integer, +// end_log_index integer, +// end_chain_id integer, +// end_timestamp integer, +// start_block integer, +// start_chain_id integer, +// start_log_index integer, +// start_timestamp integer +// ) +// RETURNS SETOF entity_history_filter AS $$ +// BEGIN +// RETURN QUERY +// SELECT +// entity_id, +// chain_id, +// old_val, +// new_val, +// block_number, +// previous_block_number, +// log_index, +// previous_log_index, +// entity_type_to_int(entity_type) AS entity_type -- Convert enum to integer +// FROM +// get_entity_history_filter( +// end_block, +// end_log_index, +// end_chain_id, +// end_timestamp, +// start_block, +// start_chain_id, +// start_log_index, +// start_timestamp +// ); +// END; +// $$ LANGUAGE plpgsql STABLE; +// `) + } } diff --git a/codegenerator/cli/templates/static/codegen/src/db/TablesStatic.res b/codegenerator/cli/templates/static/codegen/src/db/TablesStatic.res index 1e0e30e66..93c65c614 100644 --- a/codegenerator/cli/templates/static/codegen/src/db/TablesStatic.res +++ b/codegenerator/cli/templates/static/codegen/src/db/TablesStatic.res @@ -218,16 +218,16 @@ module EntityHistoryFilter = { "entity_history_filter", ~fields=[ // NULL for an `entity_id` means that the entity was deleted. - mkField("entity_id", Text, ~isPrimaryKey), + mkField("relevant_entity_id", Text, ~isPrimaryKey), // there is some obscure hasura error if named 'entity_id' that I don't know - so renamed field. mkField("chain_id", Integer, ~isPrimaryKey), mkField("old_val", Json, ~isNullable), mkField("new_val", Json, ~isNullable), mkField("block_number", Integer, ~isPrimaryKey), - mkField("block_timestamp", Integer, ~isPrimaryKey), + // mkField("block_timestamp", Integer, ~isPrimaryKey), // not available - TODO: remove before merge once dev-mode is working. mkField("previous_block_number", Integer, ~isNullable), mkField("log_index", Integer, ~isPrimaryKey), mkField("previous_log_index", Integer, ~isNullable, ~isPrimaryKey), - mkField("entity_type", Enum(EntityType.enum.name), ~isPrimaryKey), + mkField("entity_type", Integer, ~isPrimaryKey), ], ) }