35d72fccbe
Feature/remove mocks * remove mocks * cleanup db migrations
44 lines
1.0 KiB
PL/PgSQL
44 lines
1.0 KiB
PL/PgSQL
-- Migration 001: Extensions and Core Functions
|
|
-- Creates PostgreSQL extensions and utility functions used throughout the schema
|
|
|
|
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
|
|
|
|
-- UUID v7 generator function (time-ordered UUIDs)
|
|
CREATE OR REPLACE FUNCTION uuid_generate_v7()
|
|
RETURNS uuid
|
|
AS $$
|
|
SELECT encode(
|
|
set_bit(
|
|
set_bit(
|
|
overlay(uuid_send(gen_random_uuid())
|
|
placing substring(int8send(floor(extract(epoch from clock_timestamp()) * 1000)::bigint) from 3)
|
|
from 1 for 6
|
|
),
|
|
52, 1
|
|
),
|
|
53, 1
|
|
),
|
|
'hex')::uuid;
|
|
$$
|
|
LANGUAGE SQL
|
|
VOLATILE;
|
|
|
|
-- Version checking function for temporal data patterns
|
|
CREATE FUNCTION isInVersion(
|
|
_version int,
|
|
_addedVersion int,
|
|
_removedVersion int
|
|
)
|
|
RETURNS boolean AS $$
|
|
BEGIN
|
|
RETURN _version IS NULL OR (
|
|
_version >= _addedVersion AND
|
|
(_removedVersion IS NULL OR _version < _removedVersion)
|
|
);
|
|
END;
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
-- Domain for unsigned small integers
|
|
CREATE DOMAIN unsignedsmallint AS SMALLINT
|
|
CHECK (VALUE >= 0);
|