create table if not exists ennoblements
(
    id           bigint generated by default as identity
        primary key,
    server_key   varchar(100)                                       not null
        references servers,
    village_id   bigint                                             not null,
    new_owner_id bigint,
    new_tribe_id bigint,
    old_owner_id bigint,
    old_tribe_id bigint,
    points       bigint                   default 0,
    created_at   timestamp with time zone default CURRENT_TIMESTAMP not null
);

create unique index if not exists ennoblements_hash_key
    on ennoblements (hash_record_extended(
                             ROW (server_key, village_id, new_owner_id, new_tribe_id, old_owner_id, old_tribe_id, points, created_at),
                             0::bigint));

create index if not exists ennoblements_server_key_created_at_idx
    on ennoblements (server_key, created_at);

create index if not exists ennoblements_server_key_village_id_idx
    on ennoblements (server_key, village_id);

create index if not exists ennoblements_server_key_new_owner_id_idx
    on ennoblements (server_key, new_owner_id);

create index if not exists ennoblements_server_key_old_owner_id_idx
    on ennoblements (server_key, old_owner_id);

create index if not exists ennoblements_server_key_new_tribe_id_idx
    on ennoblements (server_key, new_tribe_id);

create index if not exists ennoblements_server_key_old_tribe_id_idx
    on ennoblements (server_key, old_tribe_id);
