Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for subclasses in defaults allowed types #1052

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

Bobronium
Copy link

These changes allow for extension of existing Default types, for instance, introducing UUID7 would now be possible just like this:

from uuid import UUID

from piccolo import columns
from piccolo.columns.defaults import UUID4
from piccolo.table import Table
from uuid_utils.compat import uuid7


class UUID7(UUID4):
    @property
    def postgres(self) -> str:
        return "uuid_generate_v7()"

    def python(self) -> UUID:
        return uuid7()


class User(Table, tablename="users"):
    id = columns.UUID(primary_key=True, default=UUID7())

@dantownsend
Copy link
Member

@Bobronium Thanks for this.

Do you know the status of uuid7 in Postgres - presumably you're using this extension: https://pgxn.org/dist/pg_uuidv7/

@Bobronium
Copy link
Author

Actually, I'm just defining custom function for now ✨

Example migration
from piccolo.apps.migrations.auto import MigrationManager
from piccolo.table import Table

ID = "2024-07-13T21:20:34:913115"
VERSION = "1.13.1"
DESCRIPTION = "ADD UUIDv7"
UUID_GENERATE_V7_SQL = """\
-- Function to generate new v7 UUIDs.
-- Would be better off using https://github.com/fboulnois/pg_uuidv7, but can't on RDS.
-- Or, once the UUIDv7 spec is finalized, it will probably make it into the 'uuid-ossp' extension
-- and a custom function will no longer be necessary.
create or replace function uuid_generate_v7()
returns uuid
as $$
declare
  unix_ts_ms bytea;
  uuid_bytes bytea;
begin
  unix_ts_ms = substring(int8send(floor(extract(epoch from clock_timestamp()) * 1000)::bigint) from 3);
  uuid_bytes = uuid_send(gen_random_uuid());
  uuid_bytes = overlay(uuid_bytes placing unix_ts_ms from 1 for 6);
  uuid_bytes = set_byte(uuid_bytes, 6, (b'0111' || get_byte(uuid_bytes, 6)::bit(4))::bit(8)::int);
  return encode(uuid_bytes, 'hex')::uuid;
end
$$
language plpgsql
volatile;"""  # noqa: E501


class RawTable(Table):
    pass


async def add_uuid_v7() -> None:
    await RawTable.raw(UUID_GENERATE_V7_SQL)


async def forwards() -> None:
    manager = MigrationManager(migration_id=ID, app_name="app", description=DESCRIPTION)

    manager.add_raw(add_uuid_v7)

@noamsto
Copy link

noamsto commented Oct 8, 2024

Will it be merged?
I want to use UUID7 as well...
There are extensions for it and also it's possible to do it on RDS https://aws.amazon.com/blogs/database/implement-uuidv7-in-amazon-rds-for-postgresql-using-trusted-language-extensions/

Maybe we can add a UUID7 class with this change so no need for custom class creation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants