diff options
Diffstat (limited to 'db/migrate/20211012015903_next_traversal_ids_sibling_function.rb')
-rw-r--r-- | db/migrate/20211012015903_next_traversal_ids_sibling_function.rb | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/db/migrate/20211012015903_next_traversal_ids_sibling_function.rb b/db/migrate/20211012015903_next_traversal_ids_sibling_function.rb new file mode 100644 index 00000000000..f32b8fc5a65 --- /dev/null +++ b/db/migrate/20211012015903_next_traversal_ids_sibling_function.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class NextTraversalIdsSiblingFunction < Gitlab::Database::Migration[1.0] + include Gitlab::Database::SchemaHelpers + + FUNCTION_NAME = 'next_traversal_ids_sibling' + + def up + # Given array [1,2,3,4,5], concatenate the first part of the array [1,2,3,4] + # with the last element in the array (5) after being incremented ([6]). + # + # [1,2,3,4,5] => [1,2,3,4,6] + execute(<<~SQL) + CREATE OR REPLACE FUNCTION #{FUNCTION_NAME}(traversal_ids INT[]) RETURNS INT[] + AS $$ + BEGIN + return traversal_ids[1:array_length(traversal_ids, 1)-1] || + ARRAY[traversal_ids[array_length(traversal_ids, 1)]+1]; + END; + $$ + LANGUAGE plpgsql + IMMUTABLE + RETURNS NULL ON NULL INPUT; + SQL + end + + def down + execute("DROP FUNCTION #{FUNCTION_NAME}(traversal_ids INT[])") + end +end |