Allow for subclasses in defaults allowed types#1052
Open
Bobronium wants to merge 6 commits intopiccolo-orm:masterfrom
Open
Allow for subclasses in defaults allowed types#1052Bobronium wants to merge 6 commits intopiccolo-orm:masterfrom
Bobronium wants to merge 6 commits intopiccolo-orm:masterfrom
Conversation
Member
|
@Bobronium Thanks for this. Do you know the status of |
Author
|
Actually, I'm just defining custom function for now ✨ Example migrationfrom 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) |
|
Will it be merged? Maybe we can add a UUID7 class with this change so no need for custom class creation |
Author
|
Here's a workaround for the time being: 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.UUID:
return uuid7()
class UUID(columns.UUID):
_validated = True # HACK: to allow custom defaults to be accepted by piccolo # noqa: FIX004, https://github.com/piccolo-orm/piccolo/pull/1052
class User(Table, tablename="users"):
id = UUID(primary_key=True, default=UUID7()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
These changes allow for extension of existing Default types, for instance, introducing UUID7 would now be possible just like this: