Skip to content

Commit

Permalink
make app.default_version_struct column non-null
Browse files Browse the repository at this point in the history
  • Loading branch information
imor committed Jan 3, 2024
1 parent 46d0c7e commit 1ef85d5
Showing 1 changed file with 11 additions and 35 deletions.
46 changes: 11 additions & 35 deletions supabase/migrations/20231205051816_add_default_version.sql
Original file line number Diff line number Diff line change
@@ -1,39 +1,7 @@
-- The original semver domain defined in 20220117141507_semver.sql doesn't allow null
-- app.semver_struct values, but we need a nullable app.semver column for the new
-- default_version_struct column in the app.packages table (see alter table app.packages below).
-- So we modify the `is_valid` function such that it returns true if the input version itself is
-- null. All the existing tables where app.semver domain is used already have an additional
-- non null constraint, so their behaviour doesn't change.
create or replace function app.is_valid(version app.semver_struct)
returns boolean
immutable
language sql
as $$
select (
version is null or (
version.major is not null
and version.minor is not null
and version.patch is not null
)
)
$$;

-- same definition as the original function defined in 20220117141507_semver.sql with the only
-- difference being that this is marked strict. This is done so that the function returns null
-- on null input instead of `..`
create or replace function app.semver_to_text(version app.semver)
returns text
immutable
strict
language sql
as $$
select
format('%s.%s.%s', version.major, version.minor, version.patch)
$$;

-- default version columns are nullable for backward compatibility with older clients
-- default_version column has a default value '0.0.0' only temporarily because the column is not null.
-- It will be removed below.
alter table app.packages
add column default_version_struct app.semver,
add column default_version_struct app.semver not null default app.text_to_semver('0.0.0'),
add column default_version text generated always as (app.semver_to_text(default_version_struct)) stored;

-- for now we set the default version to current latest version
Expand All @@ -43,6 +11,10 @@ set default_version_struct = app.text_to_semver(pp.latest_version)
from public.packages pp
where packages.id = pp.id;

-- now that every row has a valid default_version, remove the default value of '0.0.0'
alter table app.packages
alter column default_version_struct drop default;

-- add new default_version column to the view
create or replace view public.packages as
select
Expand Down Expand Up @@ -95,6 +67,10 @@ begin
raise exception 'user not logged in';
end if;

if default_version is null then
raise exception 'default_version is required. If you are on `dbdev` CLI version 0.1.5 or older upgrade to the latest version.';
end if;

foreach require in array requires
loop
if not exists (
Expand Down

0 comments on commit 1ef85d5

Please sign in to comment.