Skip to content

Commit

Permalink
improvement: mark ash_raise_error as STABLE
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Jan 13, 2025
1 parent 8e59757 commit 1d0437a
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 4 deletions.
25 changes: 23 additions & 2 deletions lib/migration_generator/ash_functions.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule AshPostgres.MigrationGenerator.AshFunctions do
@latest_version 4
@latest_version 5

def latest_version, do: @latest_version

Expand Down Expand Up @@ -143,7 +143,26 @@ defmodule AshPostgres.MigrationGenerator.AshFunctions do
end

def install(3) do
uuid_generate_v7()
"""
execute("ALTER FUNCTION ash_raise_error(jsonb) STABLE;")
execute("ALTER FUNCTION ash_raise_error(jsonb, ANYCOMPATIBLE) STABLE")
#{uuid_generate_v7()}
"""
end

def install(4) do
"""
execute("ALTER FUNCTION ash_raise_error(jsonb) STABLE;")
execute("ALTER FUNCTION ash_raise_error(jsonb, ANYCOMPATIBLE) STABLE")
#{uuid_generate_v7()}
"""
end

def drop(4) do
"""
execute("ALTER FUNCTION ash_raise_error(jsonb) VOLATILE;")
execute("ALTER FUNCTION ash_raise_error(jsonb, ANYCOMPATIBLE) VOLATILE")
"""
end

def drop(3) do
Expand Down Expand Up @@ -184,6 +203,7 @@ defmodule AshPostgres.MigrationGenerator.AshFunctions do
RETURN NULL;
END;
$$ LANGUAGE plpgsql
STABLE
SET search_path = '';
\"\"\")
Expand All @@ -197,6 +217,7 @@ defmodule AshPostgres.MigrationGenerator.AshFunctions do
RETURN NULL;
END;
$$ LANGUAGE plpgsql
STABLE
SET search_path = '';
\"\"\")
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"ash_functions_version": 4,
"ash_functions_version": 5,
"installed": [
"ash-functions",
"uuid-ossp",
Expand Down
2 changes: 1 addition & 1 deletion priv/resource_snapshots/test_repo/extensions.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"ash_functions_version": 4,
"ash_functions_version": 5,
"installed": [
"ash-functions",
"uuid-ossp",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
defmodule AshPostgres.TestNoSandboxRepo.Migrations.MigrateResourcesExtensions1 do
@moduledoc """
Installs any extensions that are mentioned in the repo's `installed_extensions/0` callback
This file was autogenerated with `mix ash_postgres.generate_migrations`
"""

use Ecto.Migration

def up do
execute("ALTER FUNCTION ash_raise_error(jsonb) STABLE;")
execute("ALTER FUNCTION ash_raise_error(jsonb, ANYCOMPATIBLE) STABLE")

execute("""
CREATE OR REPLACE FUNCTION uuid_generate_v7()
RETURNS UUID
AS $$
DECLARE
timestamp TIMESTAMPTZ;
microseconds INT;
BEGIN
timestamp = clock_timestamp();
microseconds = (cast(extract(microseconds FROM timestamp)::INT - (floor(extract(milliseconds FROM timestamp))::INT * 1000) AS DOUBLE PRECISION) * 4.096)::INT;
RETURN encode(
set_byte(
set_byte(
overlay(uuid_send(gen_random_uuid()) placing substring(int8send(floor(extract(epoch FROM timestamp) * 1000)::BIGINT) FROM 3) FROM 1 FOR 6
),
6, (b'0111' || (microseconds >> 8)::bit(4))::bit(8)::int
),
7, microseconds::bit(8)::int
),
'hex')::UUID;
END
$$
LANGUAGE PLPGSQL
SET search_path = ''
VOLATILE;
""")

execute("""
CREATE OR REPLACE FUNCTION timestamp_from_uuid_v7(_uuid uuid)
RETURNS TIMESTAMP WITHOUT TIME ZONE
AS $$
SELECT to_timestamp(('x0000' || substr(_uuid::TEXT, 1, 8) || substr(_uuid::TEXT, 10, 4))::BIT(64)::BIGINT::NUMERIC / 1000);
$$
LANGUAGE SQL
SET search_path = ''
IMMUTABLE PARALLEL SAFE STRICT;
""")
end

def down do
# Uncomment this if you actually want to uninstall the extensions
# when this migration is rolled back:
execute("ALTER FUNCTION ash_raise_error(jsonb) VOLATILE;")
execute("ALTER FUNCTION ash_raise_error(jsonb, ANYCOMPATIBLE) VOLATILE")
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
defmodule AshPostgres.TestRepo.Migrations.MigrateResourcesExtensions1 do
@moduledoc """
Installs any extensions that are mentioned in the repo's `installed_extensions/0` callback
This file was autogenerated with `mix ash_postgres.generate_migrations`
"""

use Ecto.Migration

def up do
execute("ALTER FUNCTION ash_raise_error(jsonb) STABLE;")
execute("ALTER FUNCTION ash_raise_error(jsonb, ANYCOMPATIBLE) STABLE")

execute("""
CREATE OR REPLACE FUNCTION uuid_generate_v7()
RETURNS UUID
AS $$
DECLARE
timestamp TIMESTAMPTZ;
microseconds INT;
BEGIN
timestamp = clock_timestamp();
microseconds = (cast(extract(microseconds FROM timestamp)::INT - (floor(extract(milliseconds FROM timestamp))::INT * 1000) AS DOUBLE PRECISION) * 4.096)::INT;
RETURN encode(
set_byte(
set_byte(
overlay(uuid_send(gen_random_uuid()) placing substring(int8send(floor(extract(epoch FROM timestamp) * 1000)::BIGINT) FROM 3) FROM 1 FOR 6
),
6, (b'0111' || (microseconds >> 8)::bit(4))::bit(8)::int
),
7, microseconds::bit(8)::int
),
'hex')::UUID;
END
$$
LANGUAGE PLPGSQL
SET search_path = ''
VOLATILE;
""")

execute("""
CREATE OR REPLACE FUNCTION timestamp_from_uuid_v7(_uuid uuid)
RETURNS TIMESTAMP WITHOUT TIME ZONE
AS $$
SELECT to_timestamp(('x0000' || substr(_uuid::TEXT, 1, 8) || substr(_uuid::TEXT, 10, 4))::BIT(64)::BIGINT::NUMERIC / 1000);
$$
LANGUAGE SQL
SET search_path = ''
IMMUTABLE PARALLEL SAFE STRICT;
""")
end

def down do
# Uncomment this if you actually want to uninstall the extensions
# when this migration is rolled back:
execute("ALTER FUNCTION ash_raise_error(jsonb) VOLATILE;")
execute("ALTER FUNCTION ash_raise_error(jsonb, ANYCOMPATIBLE) VOLATILE")
end
end

0 comments on commit 1d0437a

Please sign in to comment.