From 3956f55f2f26653b54d623e096935b9db97bd3d6 Mon Sep 17 00:00:00 2001 From: GalvinGao Date: Mon, 28 Aug 2023 12:30:42 -0400 Subject: [PATCH 1/3] chore: improve schema --- internal/research/schema/battle.schema.json | 226 ++++++++++++------ internal/research/schema/incident.schema.json | 85 ++++--- internal/research/schema/portal.schema.json | 61 +++-- internal/research/schema/rest.schema.json | 19 +- 4 files changed, 258 insertions(+), 133 deletions(-) diff --git a/internal/research/schema/battle.schema.json b/internal/research/schema/battle.schema.json index 36a3639..b6429d1 100644 --- a/internal/research/schema/battle.schema.json +++ b/internal/research/schema/battle.schema.json @@ -1,7 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema", "$id": "https://rogue.penguin-stats.io/_/schema/research/battle", - "title": "作战节点", "type": "object", "additionalProperties": false, "properties": { @@ -11,55 +10,68 @@ "anyOf": [ { "const": "rogue_3_band_1", - "title": "指挥分队" + "title": "指挥分队", + "alias": "zhihuifendui zhfd" }, { "const": "rogue_3_band_2", - "title": "集群分队" + "title": "集群分队", + "alias": "jiqunfendui jqfd" }, { "const": "rogue_3_band_3", - "title": "后勤分队" + "title": "后勤分队", + "alias": "houqinfendui hqfd" }, { "const": "rogue_3_band_4", - "title": "矛头分队" + "title": "矛头分队", + "alias": "maotoufendui mtfd" }, { "const": "rogue_3_band_5", - "title": "突击战术分队(近锋)" + "title": "突击战术分队(近锋)", + "alias": "tujizhanshuzhufenduijinfeng tjzszfdjf" }, { "const": "rogue_3_band_6", - "title": "堡垒战术分队(重辅)" + "title": "堡垒战术分队(重辅)", + "alias": "baobupuleizhanshuzhufenduizhongchongfu bplzszfdzcf" }, { "const": "rogue_3_band_7", - "title": "远程战术分队(狙医)" + "title": "远程战术分队(狙医)", + "alias": "yuanchengzhanshuzhufenduijuyi yczszfdjy" }, { "const": "rogue_3_band_8", - "title": "破坏战术分队(术特)" + "title": "破坏战术分队(术特)", + "alias": "pohuaizhanshuzhufenduishuzhute phzszfdszt" }, { "const": "rogue_3_band_9", - "title": "特训分队" + "title": "特训分队", + "alias": "texunfendui txfd" }, { "const": "rogue_3_band_10", - "title": "高规格分队" + "title": "高规格分队", + "alias": "gaoguigefendui gggfd" }, { "const": "rogue_3_band_11", - "title": "永恒狩猎分队" + "title": "永恒狩猎分队", + "alias": "yonghengshouliefendui yhslfd" }, { "const": "rogue_3_band_12", - "title": "生活至上分队" + "title": "生活至上分队", + "alias": "shenghuozhishangfendui shzsfd" }, { "const": "rogue_3_band_13", - "title": "科学主义分队" + "title": "科学主义分队", + "alias": "kexuezhuyifendui kxzyfd" } ] }, @@ -85,35 +97,43 @@ "anyOf": [ { "const": "BATTLE_NORMAL", - "title": "作战" + "title": "作战", + "alias": "zuozhan zz" }, { "const": "BATTLE_ELITE", - "title": "紧急作战" + "title": "紧急作战", + "alias": "jinjizuozhan jjzz" }, { "const": "BATTLE_BOSS", - "title": "险路恶敌" + "title": "险路恶敌", + "alias": "xianluewudi xlewd" }, { "const": "INCIDENT", - "title": "不期而遇" + "title": "不期而遇", + "alias": "bufouqijieryu bfqjey" }, { "const": "INCIDENT_RES3A", - "title": "不期而遇-黑色足迹" + "title": "不期而遇-黑色足迹", + "alias": "bufouqijieryuheisezuji bfqjeyhszj" }, { "const": "INCIDENT_MIMIC_ENEMY", - "title": "不期而遇-鸭狗熊" + "title": "不期而遇-鸭狗熊", + "alias": "bufouqijieryuyagouxiong bfqjeyygx" }, { "const": "SACRIFICE", - "title": "失与得" + "title": "失与得", + "alias": "shiyudedei syd" }, { "const": "BATTLE_SHOP", - "title": "诡异行商" + "title": "诡异行商", + "alias": "guiyihangxingshang gyhxs" } ] }, @@ -163,75 +183,93 @@ "anyOf": [ { "const": "rogue_3_recruit_ticket_pioneer", - "title": "先锋" + "title": "先锋", + "alias": "xianfeng xf" }, { "const": "rogue_3_recruit_ticket_warrior", - "title": "近卫" + "title": "近卫", + "alias": "jinwei jw" }, { "const": "rogue_3_recruit_ticket_tank", - "title": "重装" + "title": "重装", + "alias": "zhongchongzhuang zcz" }, { "const": "rogue_3_recruit_ticket_sniper", - "title": "狙击" + "title": "狙击", + "alias": "juji jj" }, { "const": "rogue_3_recruit_ticket_caster", - "title": "术师" + "title": "术师", + "alias": "shuzhushi szs" }, { "const": "rogue_3_recruit_ticket_support", - "title": "辅助" + "title": "辅助", + "alias": "fuzhuchu fzc" }, { "const": "rogue_3_recruit_ticket_medic", - "title": "医疗" + "title": "医疗", + "alias": "yiliao yl" }, { "const": "rogue_3_recruit_ticket_special", - "title": "特种" + "title": "特种", + "alias": "tezhongchong tzc" }, { "const": "rogue_3_recruit_ticket_double_1", - "title": "突击协议(近锋)" + "title": "突击协议(近锋)", + "alias": "tujixieyijinfeng tjxyjf" }, { "const": "rogue_3_recruit_ticket_double_2", - "title": "堡垒协议(重辅)" + "title": "堡垒协议(重辅)", + "alias": "baobupuleixieyizhongchongfu bplxyzcf" }, { "const": "rogue_3_recruit_ticket_double_3", - "title": "远程协议(狙医)" + "title": "远程协议(狙医)", + "alias": "yuanchengxieyijuyi ycxyjy" }, { "const": "rogue_3_recruit_ticket_double_4", - "title": "破坏协议(术特)" + "title": "破坏协议(术特)", + "alias": "pohuaixieyishuzhute phxyszt" }, { "const": "rogue_3_recruit_ticket_quad_melee", - "title": "前线统合" + "title": "前线统合", + "alias": "qianxiantonghege qxthg" }, { "const": "rogue_3_recruit_ticket_quad_ranged", - "title": "后方协调" + "title": "后方协调", + "alias": "houfangxietiaodiaozhou hfxtdz" }, { "const": "rogue_3_recruit_ticket_quad_melee_discount", - "title": "前线统合资深" + "title": "前线统合资深", + "alias": "qianxiantonghegezishen qxthgzs" }, { "const": "rogue_3_recruit_ticket_quad_ranged_discount", - "title": "后方协调资深" + "title": "后方协调资深", + "alias": "houfangxietiaodiaozhouzishen hfxtdzzs" }, { "const": "rogue_3_recruit_ticket_all", - "title": "高级人事调度函" + "title": "高级人事调度函", + "alias": "gaojirenshitiaodiaozhoududuohan gjrstdzdh" }, { "const": "rogue_3_recruit_ticket_all_discount", - "title": "高级人事资深" + "title": "高级人事资深", + "alias": "gaojirenshizishen gjrszs" } ] } @@ -248,147 +286,183 @@ "anyOf": [ { "const": "rogue_3_totem_R_L1", - "title": "黜人" + "title": "黜人", + "alias": "churen cr" }, { "const": "rogue_3_totem_R_L2", - "title": "猎手" + "title": "猎手", + "alias": "lieshou ls" }, { "const": "rogue_3_totem_R_L3", - "title": "战士" + "title": "战士", + "alias": "zhanshi zs" }, { "const": "rogue_3_totem_R_L4", - "title": "萨满" + "title": "萨满", + "alias": "saman sm" }, { "const": "rogue_3_totem_R_L5", - "title": "雪祀" + "title": "雪祀", + "alias": "xuesi xs" }, { "const": "rogue_3_totem_R_L6", - "title": "英雄" + "title": "英雄", + "alias": "yingxiong yx" }, { "const": "rogue_3_totem_R_E1", - "title": "歌唱" + "title": "歌唱", + "alias": "gechang gc" }, { "const": "rogue_3_totem_R_E2", - "title": "沉默" + "title": "沉默", + "alias": "chenmo cm" }, { "const": "rogue_3_totem_R_E3", - "title": "朗诵" + "title": "朗诵", + "alias": "langsong ls" }, { "const": "rogue_3_totem_R_E4", - "title": "辩论" + "title": "辩论", + "alias": "bianlun bl" }, { "const": "rogue_3_totem_R_E5", - "title": "慰藉" + "title": "慰藉", + "alias": "weijieji wj" }, { "const": "rogue_3_totem_R_E6", - "title": "告解" + "title": "告解", + "alias": "gaojiexie gjx" }, { "const": "rogue_3_totem_G_L1", - "title": "树冠" + "title": "树冠", + "alias": "shuguan sg" }, { "const": "rogue_3_totem_G_L2", - "title": "水面" + "title": "水面", + "alias": "shuimian sm" }, { "const": "rogue_3_totem_G_L3", - "title": "眼睛" + "title": "眼睛", + "alias": "yanjing yj" }, { "const": "rogue_3_totem_G_L4", - "title": "拱门" + "title": "拱门", + "alias": "gongmen gm" }, { "const": "rogue_3_totem_G_L5", - "title": "光芒" + "title": "光芒", + "alias": "guangmang gm" }, { "const": "rogue_3_totem_G_L6", - "title": "大地" + "title": "大地", + "alias": "dadaitaidide dtd" }, { "const": "rogue_3_totem_G_E1", - "title": "喜悦" + "title": "喜悦", + "alias": "xiyue xy" }, { "const": "rogue_3_totem_G_E2", - "title": "惊讶" + "title": "惊讶", + "alias": "jingya jy" }, { "const": "rogue_3_totem_G_E3", - "title": "愤怒" + "title": "愤怒", + "alias": "fennu fn" }, { "const": "rogue_3_totem_G_E4", - "title": "疑惑" + "title": "疑惑", + "alias": "yinihuo ynh" }, { "const": "rogue_3_totem_G_E5", - "title": "憧憬" + "title": "憧憬", + "alias": "chongjing cj" }, { "const": "rogue_3_totem_G_E6", - "title": "爱恋" + "title": "爱恋", + "alias": "ailian al" }, { "const": "rogue_3_totem_B_L1", - "title": "源石" + "title": "源石", + "alias": "yuanshidan ysd" }, { "const": "rogue_3_totem_B_L2", - "title": "乔木" + "title": "乔木", + "alias": "qiaomu qm" }, { "const": "rogue_3_totem_B_L3", - "title": "砂石" + "title": "砂石", + "alias": "shashidan ssd" }, { "const": "rogue_3_totem_B_L4", - "title": "灌木" + "title": "灌木", + "alias": "guanmu gm" }, { "const": "rogue_3_totem_B_L5", - "title": "兽类" + "title": "兽类", + "alias": "shoulei sl" }, { "const": "rogue_3_totem_B_L6", - "title": "人类" + "title": "人类", + "alias": "renlei rl" }, { "const": "rogue_3_totem_B_E1", - "title": "巡视" + "title": "巡视", + "alias": "xunshi xs" }, { "const": "rogue_3_totem_B_E2", - "title": "筑巢" + "title": "筑巢", + "alias": "zhuchao zc" }, { "const": "rogue_3_totem_B_E3", - "title": "捕猎" + "title": "捕猎", + "alias": "bulie bl" }, { "const": "rogue_3_totem_B_E4", - "title": "掠夺" + "title": "掠夺", + "alias": "lveduo ld" }, { "const": "rogue_3_totem_B_E5", - "title": "繁衍" + "title": "繁衍", + "alias": "fanpoyan fpy" }, { "const": "rogue_3_totem_B_E6", - "title": "迁徙" + "title": "迁徙", + "alias": "qianxi qx" } ] } diff --git a/internal/research/schema/incident.schema.json b/internal/research/schema/incident.schema.json index e191f5b..58cebe9 100644 --- a/internal/research/schema/incident.schema.json +++ b/internal/research/schema/incident.schema.json @@ -1,7 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema", "$id": "https://rogue.penguin-stats.io/_/schema/research/incident", - "title": "不期而遇", "type": "object", "additionalProperties": false, "properties": { @@ -27,115 +26,143 @@ "anyOf": [ { "const": "ro3_res1", - "title": "吉兆" + "title": "吉兆", + "alias": "jizhao jz" }, { "const": "ro3_res2", - "title": "雨!" + "title": "雨!", + "alias": "yu y" }, { "const": "ro3_res3", - "title": "随到随取" + "title": "随到随取", + "alias": "suidaosuiqu sdsq" }, { "const": "ro3_res4", - "title": "定期维护" + "title": "定期维护", + "alias": "dingqijiweihu dqjwh" }, { "const": "ro3_res5", - "title": "昏黑之室" + "title": "昏黑之室", + "alias": "hunheizhishi hhzs" }, { "const": "ro3_res2a", - "title": "疗愈仪式" + "title": "疗愈仪式", + "alias": "liaoyuyishi lyys" }, { "const": "ro3_res3a", - "title": "黑色足迹" + "title": "黑色足迹", + "alias": "heisezuji hszj" }, { "const": "ro3_res5a", - "title": "不冻河" + "title": "不冻河", + "alias": "bufoudonghe bfdh" }, { "const": "ro3_rec1", - "title": "野外生存专家" + "title": "野外生存专家", + "alias": "yewaishengcunzhuanjiajie ywsczj" }, { "const": "ro3_rec2", - "title": "在地公共信号放大站756号" + "title": "在地公共信号放大站756号", + "alias": "zaididegonggongxinshenhaofangdadaitaizhan756hao zdggxshfdtz756h" }, { "const": "ro3_relic1", - "title": "特里蒙旅行社特派团" + "title": "特里蒙旅行社特派团", + "alias": "telimenglvhangxingshetepaituan tlmlhxstpt" }, { "const": "ro3_relic2", - "title": "在地公共移动站N6号" + "title": "在地公共移动站N6号", + "alias": "zaididegonggongyidongzhanN6hao zdggydzN6h" }, { "const": "ro3_spring", - "title": "度假胜地" + "title": "度假胜地", + "alias": "duduojiashengdide djsd" }, { "const": "ro3_height", - "title": "负伤的主树" + "title": "负伤的主树", + "alias": "fushangdedizhushu fsdzs" }, { "const": "ro3_pick1", - "title": "不见群山" + "title": "不见群山", + "alias": "bufoujianxianqunshan bfjxqs" }, { "const": "ro3_pick2", - "title": "远见所向" + "title": "远见所向", + "alias": "yuanjianxiansuoxiang yjxsx" }, { "const": "ro3_normal1", - "title": "萨米之语" + "title": "萨米之语", + "alias": "samizhiyu smzy" }, { "const": "ro3_normal2", - "title": "乌萨斯" + "title": "乌萨斯", + "alias": "wusasi wss" }, { "const": "ro3_normal3", - "title": "无用之物" + "title": "无用之物", + "alias": "wuyongzhiwu wyzw" }, { "const": "ro3_bat1", - "title": "沼泽里的抽泣声" + "title": "沼泽里的抽泣声", + "alias": "zhaozeshilidedichouqisheng zzsldcqs" }, { "const": "ro3_bat2", - "title": "有利可图" + "title": "有利可图", + "alias": "youliketu ylkt" }, { "const": "ro3_bat3", - "title": "随行人员" + "title": "随行人员", + "alias": "suihangxingrenyuanyun shxry" }, { "const": "ro3_bat4", - "title": "邪恶计划鸭" + "title": "邪恶计划鸭", + "alias": "xieyayeyuxuewujihuaya xyewjhy" }, { "const": "ro3_bat5", - "title": "百里连营" + "title": "百里连营", + "alias": "baililianying blly" }, { "const": "ro3_bat6", - "title": "请君入戏" + "title": "请君入戏", + "alias": "qingjunruxihu qjrxh" }, { "const": "ro3_bat7", - "title": "北风女巫" + "title": "北风女巫", + "alias": "beifengnvruwu bfnrw" }, { "const": "ro3_bat8", - "title": "时刻警惕" + "title": "时刻警惕", + "alias": "shikejingti skjt" }, { "const": "ro3_bat9", - "title": "猜疑链" + "title": "猜疑链", + "alias": "caiyinilian cynl" } ] } diff --git a/internal/research/schema/portal.schema.json b/internal/research/schema/portal.schema.json index 9f2a475..f1c06ac 100644 --- a/internal/research/schema/portal.schema.json +++ b/internal/research/schema/portal.schema.json @@ -1,7 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema", "$id": "https://rogue.penguin-stats.io/_/schema/research/portal", - "title": "Portal", "type": "object", "additionalProperties": false, "properties": { @@ -24,51 +23,63 @@ "anyOf": [ { "const": "battle_3-2", - "title": "战斗:3-2" + "title": "战斗:3-2", + "alias": "zhandou32 zd32" }, { "const": "battle_2-3", - "title": "战斗:2-3" + "title": "战斗:2-3", + "alias": "zhandou23 zd23" }, { "const": "battle_3-3", - "title": "战斗:3-3" + "title": "战斗:3-3", + "alias": "zhandou33 zd33" }, { "const": "event_2-2-2", - "title": "事件:2-2-2" + "title": "事件:2-2-2", + "alias": "shijian222 sj222" }, { "const": "event_3-3", - "title": "事件:3-3" + "title": "事件:3-3", + "alias": "shijian33 sj33" }, { "const": "event_2-2", - "title": "事件:2-2" + "title": "事件:2-2", + "alias": "shijian22 sj22" }, { "const": "event_2-3-2", - "title": "事件:2-3-2" + "title": "事件:2-3-2", + "alias": "shijian232 sj232" }, { "const": "mixed_2-2-1", - "title": "混合:2-2-1" + "title": "混合:2-2-1", + "alias": "hunhege221 hhg221" }, { "const": "mixed_2-3-1", - "title": "混合:2-3-1" + "title": "混合:2-3-1", + "alias": "hunhege231 hhg231" }, { "const": "mixed_2-3-2", - "title": "混合:2-3-2" + "title": "混合:2-3-2", + "alias": "hunhege232 hhg232" }, { "const": "mixed_2-2-2", - "title": "混合:2-2-2" + "title": "混合:2-2-2", + "alias": "hunhege222 hhg222" }, { "const": "mixed_3-1", - "title": "混合:3-1" + "title": "混合:3-1", + "alias": "hunhege31 hhg31" } ] }, @@ -78,35 +89,43 @@ "anyOf": [ { "const": "variation_1", - "title": "己方生命攻击提升,出现国度" + "title": "己方生命攻击提升,出现国度", + "alias": "jifangshengminggongjitidishengchuxianguoduduo jfsmgjtdscxgd" }, { "const": "variation_2", - "title": "攻击下降,距离2伤害提升" + "title": "攻击下降,距离2伤害提升", + "alias": "gongjixiajiangxiangjuli2shanghaitidisheng gjxjxjl2shtds" }, { "const": "variation_3", - "title": "同时部署人数、再部署减少" + "title": "同时部署人数、再部署减少", + "alias": "tongshibushurenshushuozaibushujianshao tsbsrszbsjs" }, { "const": "variation_4", - "title": "技力消耗降低,专注失调" + "title": "技力消耗降低,专注失调", + "alias": "jilixiaohaojiangxiangdizhuanzhushitiaodiaozhou jlxhjxdzzstdz" }, { "const": "variation_5", - "title": "敌方移速下降,重量防御法抗提升" + "title": "敌方移速下降,重量防御法抗提升", + "alias": "difangyisuxiajiangxiangzhongchongliangfangyufakangtidisheng dfysxjxzclfyfktds" }, { "const": "variation_6", - "title": "生命上限提升,会掉更多血" + "title": "生命上限提升,会掉更多血", + "alias": "shengmingshangxiantidishenghuikuaidiaogengduoxiexue smsxtdshkdgdx" }, { "const": "variation_shop", - "title": "立即获得一笔资金,暗藏低价商品" + "title": "立即获得一笔资金,暗藏低价商品", + "alias": "lijihuodedeiyibizijinancangzangdijiajieshangpin ljhdybzjaczdjsp" }, { "const": "variation_shelter", - "title": "每前进一步都能获得希望" + "title": "每前进一步都能获得希望", + "alias": "meiqianjinyibududounengnaihuodedeixiwang mqjybdnhdxw" } ] } diff --git a/internal/research/schema/rest.schema.json b/internal/research/schema/rest.schema.json index 9fcec9f..0b57268 100644 --- a/internal/research/schema/rest.schema.json +++ b/internal/research/schema/rest.schema.json @@ -1,7 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema", "$id": "https://rogue.penguin-stats.io/_/schema/research/rest", - "title": "安全屋", "type": "object", "additionalProperties": false, "properties": { @@ -25,27 +24,33 @@ "anyOf": [ { "const": "ro3_rest_1", - "title": "稍事休息" + "title": "稍事休息", + "alias": "shaoshixiuxuxi ssxx" }, { "const": "ro3_rest_2", - "title": "强化训练" + "title": "强化训练", + "alias": "qiangjianghuaxunlian qjhxl" }, { "const": "ro3_rest_3", - "title": "鼓舞士气" + "title": "鼓舞士气", + "alias": "guwushiqi gwsq" }, { "const": "ro3_rest_4", - "title": "重整编制" + "title": "重整编制", + "alias": "zhongchongzhengbianzhi zczbz" }, { "const": "ro3_rest_5", - "title": "维护设备" + "title": "维护设备", + "alias": "weihushebei whsb" }, { "const": "ro3_rest_6", - "title": "求得启示" + "title": "求得启示", + "alias": "qiudedeiqishi qdqs" } ] }, From fd7e5fea74b4e8ce94b4dd4f90aef0cde094ae36 Mon Sep 17 00:00:00 2001 From: GalvinGao Date: Mon, 28 Aug 2023 12:35:07 -0400 Subject: [PATCH 2/3] fix: opposite required --- internal/ent/schema/event.go | 2 -- internal/ent/schema/research.go | 2 +- internal/ent/schema/user.go | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/internal/ent/schema/event.go b/internal/ent/schema/event.go index 5067c09..63f91b1 100644 --- a/internal/ent/schema/event.go +++ b/internal/ent/schema/event.go @@ -47,12 +47,10 @@ func (Event) Edges() []ent.Edge { edge.From("user", User.Type). Ref("events"). Field("user_id"). - Required(). Unique(), edge.From("research", Research.Type). Ref("events"). Field("research_id"). - Required(). Unique(), } } diff --git a/internal/ent/schema/research.go b/internal/ent/schema/research.go index 7bf6fc2..0559217 100644 --- a/internal/ent/schema/research.go +++ b/internal/ent/schema/research.go @@ -36,7 +36,7 @@ func (Research) Fields() []ent.Field { func (Research) Edges() []ent.Edge { return []ent.Edge{ edge.To("events", Event.Type). - // Required(). + Required(). Annotations(entsql.OnDelete(entsql.NoAction)), } } diff --git a/internal/ent/schema/user.go b/internal/ent/schema/user.go index 5915573..3a2121a 100644 --- a/internal/ent/schema/user.go +++ b/internal/ent/schema/user.go @@ -34,7 +34,7 @@ func (User) Fields() []ent.Field { func (User) Edges() []ent.Edge { return []ent.Edge{ edge.To("events", Event.Type). - // Required(). + Required(). Annotations(entsql.OnDelete(entsql.NoAction)), } } From 0e383be2e6ca9c1c43a1d727be20ace3ddb7a3d4 Mon Sep 17 00:00:00 2001 From: GalvinGao Date: Mon, 28 Aug 2023 12:42:20 -0400 Subject: [PATCH 3/3] fix: wrong edge --- internal/ent/event.go | 48 +++++----- internal/ent/event/event.go | 32 ++++--- internal/ent/event/where.go | 140 ++++++++++++++++++++++++++++++ internal/ent/event_create.go | 56 ++++++------ internal/ent/event_query.go | 28 +++--- internal/ent/event_update.go | 108 +++++++++++------------ internal/ent/gql_collection.go | 18 ++++ internal/ent/gql_edge.go | 4 +- internal/ent/migrate/schema.go | 8 +- internal/ent/mutation.go | 134 ++++++++++++++++++++++------ internal/ent/research/research.go | 2 +- internal/ent/research_create.go | 3 - internal/ent/research_query.go | 13 ++- internal/ent/schema/event.go | 2 + internal/ent/schema/research.go | 2 +- internal/ent/schema/user.go | 2 +- internal/ent/user/user.go | 2 +- internal/ent/user_create.go | 3 - internal/ent/user_query.go | 13 ++- 19 files changed, 420 insertions(+), 198 deletions(-) diff --git a/internal/ent/event.go b/internal/ent/event.go index 10ee170..2fbd35a 100644 --- a/internal/ent/event.go +++ b/internal/ent/event.go @@ -22,16 +22,18 @@ type Event struct { ID string `json:"id,omitempty"` // CreatedAt holds the value of the "created_at" field. CreatedAt time.Time `json:"created_at,omitempty"` + // UserID holds the value of the "user_id" field. + UserID string `json:"user_id,omitempty"` + // ResearchID holds the value of the "research_id" field. + ResearchID string `json:"research_id,omitempty"` // UserAgent holds the value of the "user_agent" field. UserAgent string `json:"user_agent,omitempty"` // Content holds the value of the "content" field. Content map[string]interface{} `json:"content,omitempty"` // Edges holds the relations/edges for other nodes in the graph. // The values are being populated by the EventQuery when eager-loading is set. - Edges EventEdges `json:"edges"` - research_events *string - user_events *string - selectValues sql.SelectValues + Edges EventEdges `json:"edges"` + selectValues sql.SelectValues } // EventEdges holds the relations/edges for other nodes in the graph. @@ -80,14 +82,10 @@ func (*Event) scanValues(columns []string) ([]any, error) { switch columns[i] { case event.FieldContent: values[i] = new([]byte) - case event.FieldID, event.FieldUserAgent: + case event.FieldID, event.FieldUserID, event.FieldResearchID, event.FieldUserAgent: values[i] = new(sql.NullString) case event.FieldCreatedAt: values[i] = new(sql.NullTime) - case event.ForeignKeys[0]: // research_events - values[i] = new(sql.NullString) - case event.ForeignKeys[1]: // user_events - values[i] = new(sql.NullString) default: values[i] = new(sql.UnknownType) } @@ -115,6 +113,18 @@ func (e *Event) assignValues(columns []string, values []any) error { } else if value.Valid { e.CreatedAt = value.Time } + case event.FieldUserID: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field user_id", values[i]) + } else if value.Valid { + e.UserID = value.String + } + case event.FieldResearchID: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field research_id", values[i]) + } else if value.Valid { + e.ResearchID = value.String + } case event.FieldUserAgent: if value, ok := values[i].(*sql.NullString); !ok { return fmt.Errorf("unexpected type %T for field user_agent", values[i]) @@ -129,20 +139,6 @@ func (e *Event) assignValues(columns []string, values []any) error { return fmt.Errorf("unmarshal field content: %w", err) } } - case event.ForeignKeys[0]: - if value, ok := values[i].(*sql.NullString); !ok { - return fmt.Errorf("unexpected type %T for field research_events", values[i]) - } else if value.Valid { - e.research_events = new(string) - *e.research_events = value.String - } - case event.ForeignKeys[1]: - if value, ok := values[i].(*sql.NullString); !ok { - return fmt.Errorf("unexpected type %T for field user_events", values[i]) - } else if value.Valid { - e.user_events = new(string) - *e.user_events = value.String - } default: e.selectValues.Set(columns[i], values[i]) } @@ -192,6 +188,12 @@ func (e *Event) String() string { builder.WriteString("created_at=") builder.WriteString(e.CreatedAt.Format(time.ANSIC)) builder.WriteString(", ") + builder.WriteString("user_id=") + builder.WriteString(e.UserID) + builder.WriteString(", ") + builder.WriteString("research_id=") + builder.WriteString(e.ResearchID) + builder.WriteString(", ") builder.WriteString("user_agent=") builder.WriteString(e.UserAgent) builder.WriteString(", ") diff --git a/internal/ent/event/event.go b/internal/ent/event/event.go index 2d6b583..f8a3881 100644 --- a/internal/ent/event/event.go +++ b/internal/ent/event/event.go @@ -16,6 +16,10 @@ const ( FieldID = "event_id" // FieldCreatedAt holds the string denoting the created_at field in the database. FieldCreatedAt = "created_at" + // FieldUserID holds the string denoting the user_id field in the database. + FieldUserID = "user_id" + // FieldResearchID holds the string denoting the research_id field in the database. + FieldResearchID = "research_id" // FieldUserAgent holds the string denoting the user_agent field in the database. FieldUserAgent = "user_agent" // FieldContent holds the string denoting the content field in the database. @@ -36,31 +40,26 @@ const ( // It exists in this package in order to avoid circular dependency with the "user" package. UserInverseTable = "users" // UserColumn is the table column denoting the user relation/edge. - UserColumn = "user_events" + UserColumn = "user_id" // ResearchTable is the table that holds the research relation/edge. ResearchTable = "events" // ResearchInverseTable is the table name for the Research entity. // It exists in this package in order to avoid circular dependency with the "research" package. ResearchInverseTable = "researches" // ResearchColumn is the table column denoting the research relation/edge. - ResearchColumn = "research_events" + ResearchColumn = "research_id" ) // Columns holds all SQL columns for event fields. var Columns = []string{ FieldID, FieldCreatedAt, + FieldUserID, + FieldResearchID, FieldUserAgent, FieldContent, } -// ForeignKeys holds the SQL foreign-keys that are owned by the "events" -// table and are not defined as standalone fields in the schema. -var ForeignKeys = []string{ - "research_events", - "user_events", -} - // ValidColumn reports if the column name is valid (part of the table columns). func ValidColumn(column string) bool { for i := range Columns { @@ -68,11 +67,6 @@ func ValidColumn(column string) bool { return true } } - for i := range ForeignKeys { - if column == ForeignKeys[i] { - return true - } - } return false } @@ -96,6 +90,16 @@ func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() } +// ByUserID orders the results by the user_id field. +func ByUserID(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldUserID, opts...).ToFunc() +} + +// ByResearchID orders the results by the research_id field. +func ByResearchID(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldResearchID, opts...).ToFunc() +} + // ByUserAgent orders the results by the user_agent field. func ByUserAgent(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldUserAgent, opts...).ToFunc() diff --git a/internal/ent/event/where.go b/internal/ent/event/where.go index 25af99e..9c432f7 100644 --- a/internal/ent/event/where.go +++ b/internal/ent/event/where.go @@ -70,6 +70,16 @@ func CreatedAt(v time.Time) predicate.Event { return predicate.Event(sql.FieldEQ(FieldCreatedAt, v)) } +// UserID applies equality check predicate on the "user_id" field. It's identical to UserIDEQ. +func UserID(v string) predicate.Event { + return predicate.Event(sql.FieldEQ(FieldUserID, v)) +} + +// ResearchID applies equality check predicate on the "research_id" field. It's identical to ResearchIDEQ. +func ResearchID(v string) predicate.Event { + return predicate.Event(sql.FieldEQ(FieldResearchID, v)) +} + // UserAgent applies equality check predicate on the "user_agent" field. It's identical to UserAgentEQ. func UserAgent(v string) predicate.Event { return predicate.Event(sql.FieldEQ(FieldUserAgent, v)) @@ -115,6 +125,136 @@ func CreatedAtLTE(v time.Time) predicate.Event { return predicate.Event(sql.FieldLTE(FieldCreatedAt, v)) } +// UserIDEQ applies the EQ predicate on the "user_id" field. +func UserIDEQ(v string) predicate.Event { + return predicate.Event(sql.FieldEQ(FieldUserID, v)) +} + +// UserIDNEQ applies the NEQ predicate on the "user_id" field. +func UserIDNEQ(v string) predicate.Event { + return predicate.Event(sql.FieldNEQ(FieldUserID, v)) +} + +// UserIDIn applies the In predicate on the "user_id" field. +func UserIDIn(vs ...string) predicate.Event { + return predicate.Event(sql.FieldIn(FieldUserID, vs...)) +} + +// UserIDNotIn applies the NotIn predicate on the "user_id" field. +func UserIDNotIn(vs ...string) predicate.Event { + return predicate.Event(sql.FieldNotIn(FieldUserID, vs...)) +} + +// UserIDGT applies the GT predicate on the "user_id" field. +func UserIDGT(v string) predicate.Event { + return predicate.Event(sql.FieldGT(FieldUserID, v)) +} + +// UserIDGTE applies the GTE predicate on the "user_id" field. +func UserIDGTE(v string) predicate.Event { + return predicate.Event(sql.FieldGTE(FieldUserID, v)) +} + +// UserIDLT applies the LT predicate on the "user_id" field. +func UserIDLT(v string) predicate.Event { + return predicate.Event(sql.FieldLT(FieldUserID, v)) +} + +// UserIDLTE applies the LTE predicate on the "user_id" field. +func UserIDLTE(v string) predicate.Event { + return predicate.Event(sql.FieldLTE(FieldUserID, v)) +} + +// UserIDContains applies the Contains predicate on the "user_id" field. +func UserIDContains(v string) predicate.Event { + return predicate.Event(sql.FieldContains(FieldUserID, v)) +} + +// UserIDHasPrefix applies the HasPrefix predicate on the "user_id" field. +func UserIDHasPrefix(v string) predicate.Event { + return predicate.Event(sql.FieldHasPrefix(FieldUserID, v)) +} + +// UserIDHasSuffix applies the HasSuffix predicate on the "user_id" field. +func UserIDHasSuffix(v string) predicate.Event { + return predicate.Event(sql.FieldHasSuffix(FieldUserID, v)) +} + +// UserIDEqualFold applies the EqualFold predicate on the "user_id" field. +func UserIDEqualFold(v string) predicate.Event { + return predicate.Event(sql.FieldEqualFold(FieldUserID, v)) +} + +// UserIDContainsFold applies the ContainsFold predicate on the "user_id" field. +func UserIDContainsFold(v string) predicate.Event { + return predicate.Event(sql.FieldContainsFold(FieldUserID, v)) +} + +// ResearchIDEQ applies the EQ predicate on the "research_id" field. +func ResearchIDEQ(v string) predicate.Event { + return predicate.Event(sql.FieldEQ(FieldResearchID, v)) +} + +// ResearchIDNEQ applies the NEQ predicate on the "research_id" field. +func ResearchIDNEQ(v string) predicate.Event { + return predicate.Event(sql.FieldNEQ(FieldResearchID, v)) +} + +// ResearchIDIn applies the In predicate on the "research_id" field. +func ResearchIDIn(vs ...string) predicate.Event { + return predicate.Event(sql.FieldIn(FieldResearchID, vs...)) +} + +// ResearchIDNotIn applies the NotIn predicate on the "research_id" field. +func ResearchIDNotIn(vs ...string) predicate.Event { + return predicate.Event(sql.FieldNotIn(FieldResearchID, vs...)) +} + +// ResearchIDGT applies the GT predicate on the "research_id" field. +func ResearchIDGT(v string) predicate.Event { + return predicate.Event(sql.FieldGT(FieldResearchID, v)) +} + +// ResearchIDGTE applies the GTE predicate on the "research_id" field. +func ResearchIDGTE(v string) predicate.Event { + return predicate.Event(sql.FieldGTE(FieldResearchID, v)) +} + +// ResearchIDLT applies the LT predicate on the "research_id" field. +func ResearchIDLT(v string) predicate.Event { + return predicate.Event(sql.FieldLT(FieldResearchID, v)) +} + +// ResearchIDLTE applies the LTE predicate on the "research_id" field. +func ResearchIDLTE(v string) predicate.Event { + return predicate.Event(sql.FieldLTE(FieldResearchID, v)) +} + +// ResearchIDContains applies the Contains predicate on the "research_id" field. +func ResearchIDContains(v string) predicate.Event { + return predicate.Event(sql.FieldContains(FieldResearchID, v)) +} + +// ResearchIDHasPrefix applies the HasPrefix predicate on the "research_id" field. +func ResearchIDHasPrefix(v string) predicate.Event { + return predicate.Event(sql.FieldHasPrefix(FieldResearchID, v)) +} + +// ResearchIDHasSuffix applies the HasSuffix predicate on the "research_id" field. +func ResearchIDHasSuffix(v string) predicate.Event { + return predicate.Event(sql.FieldHasSuffix(FieldResearchID, v)) +} + +// ResearchIDEqualFold applies the EqualFold predicate on the "research_id" field. +func ResearchIDEqualFold(v string) predicate.Event { + return predicate.Event(sql.FieldEqualFold(FieldResearchID, v)) +} + +// ResearchIDContainsFold applies the ContainsFold predicate on the "research_id" field. +func ResearchIDContainsFold(v string) predicate.Event { + return predicate.Event(sql.FieldContainsFold(FieldResearchID, v)) +} + // UserAgentEQ applies the EQ predicate on the "user_agent" field. func UserAgentEQ(v string) predicate.Event { return predicate.Event(sql.FieldEQ(FieldUserAgent, v)) diff --git a/internal/ent/event_create.go b/internal/ent/event_create.go index 5a53176..93076cd 100644 --- a/internal/ent/event_create.go +++ b/internal/ent/event_create.go @@ -36,6 +36,18 @@ func (ec *EventCreate) SetNillableCreatedAt(t *time.Time) *EventCreate { return ec } +// SetUserID sets the "user_id" field. +func (ec *EventCreate) SetUserID(s string) *EventCreate { + ec.mutation.SetUserID(s) + return ec +} + +// SetResearchID sets the "research_id" field. +func (ec *EventCreate) SetResearchID(s string) *EventCreate { + ec.mutation.SetResearchID(s) + return ec +} + // SetUserAgent sets the "user_agent" field. func (ec *EventCreate) SetUserAgent(s string) *EventCreate { ec.mutation.SetUserAgent(s) @@ -62,39 +74,11 @@ func (ec *EventCreate) SetNillableID(s *string) *EventCreate { return ec } -// SetUserID sets the "user" edge to the User entity by ID. -func (ec *EventCreate) SetUserID(id string) *EventCreate { - ec.mutation.SetUserID(id) - return ec -} - -// SetNillableUserID sets the "user" edge to the User entity by ID if the given value is not nil. -func (ec *EventCreate) SetNillableUserID(id *string) *EventCreate { - if id != nil { - ec = ec.SetUserID(*id) - } - return ec -} - // SetUser sets the "user" edge to the User entity. func (ec *EventCreate) SetUser(u *User) *EventCreate { return ec.SetUserID(u.ID) } -// SetResearchID sets the "research" edge to the Research entity by ID. -func (ec *EventCreate) SetResearchID(id string) *EventCreate { - ec.mutation.SetResearchID(id) - return ec -} - -// SetNillableResearchID sets the "research" edge to the Research entity by ID if the given value is not nil. -func (ec *EventCreate) SetNillableResearchID(id *string) *EventCreate { - if id != nil { - ec = ec.SetResearchID(*id) - } - return ec -} - // SetResearch sets the "research" edge to the Research entity. func (ec *EventCreate) SetResearch(r *Research) *EventCreate { return ec.SetResearchID(r.ID) @@ -150,12 +134,24 @@ func (ec *EventCreate) check() error { if _, ok := ec.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Event.created_at"`)} } + if _, ok := ec.mutation.UserID(); !ok { + return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "Event.user_id"`)} + } + if _, ok := ec.mutation.ResearchID(); !ok { + return &ValidationError{Name: "research_id", err: errors.New(`ent: missing required field "Event.research_id"`)} + } if _, ok := ec.mutation.UserAgent(); !ok { return &ValidationError{Name: "user_agent", err: errors.New(`ent: missing required field "Event.user_agent"`)} } if _, ok := ec.mutation.Content(); !ok { return &ValidationError{Name: "content", err: errors.New(`ent: missing required field "Event.content"`)} } + if _, ok := ec.mutation.UserID(); !ok { + return &ValidationError{Name: "user", err: errors.New(`ent: missing required edge "Event.user"`)} + } + if _, ok := ec.mutation.ResearchID(); !ok { + return &ValidationError{Name: "research", err: errors.New(`ent: missing required edge "Event.research"`)} + } return nil } @@ -217,7 +213,7 @@ func (ec *EventCreate) createSpec() (*Event, *sqlgraph.CreateSpec) { for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } - _node.user_events = &nodes[0] + _node.UserID = nodes[0] _spec.Edges = append(_spec.Edges, edge) } if nodes := ec.mutation.ResearchIDs(); len(nodes) > 0 { @@ -234,7 +230,7 @@ func (ec *EventCreate) createSpec() (*Event, *sqlgraph.CreateSpec) { for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } - _node.research_events = &nodes[0] + _node.ResearchID = nodes[0] _spec.Edges = append(_spec.Edges, edge) } return _node, _spec diff --git a/internal/ent/event_query.go b/internal/ent/event_query.go index cac091d..95b70f3 100644 --- a/internal/ent/event_query.go +++ b/internal/ent/event_query.go @@ -25,7 +25,6 @@ type EventQuery struct { predicates []predicate.Event withUser *UserQuery withResearch *ResearchQuery - withFKs bool modifiers []func(*sql.Selector) loadTotal []func(context.Context, []*Event) error // intermediate query (i.e. traversal path). @@ -407,19 +406,12 @@ func (eq *EventQuery) prepareQuery(ctx context.Context) error { func (eq *EventQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Event, error) { var ( nodes = []*Event{} - withFKs = eq.withFKs _spec = eq.querySpec() loadedTypes = [2]bool{ eq.withUser != nil, eq.withResearch != nil, } ) - if eq.withUser != nil || eq.withResearch != nil { - withFKs = true - } - if withFKs { - _spec.Node.Columns = append(_spec.Node.Columns, event.ForeignKeys...) - } _spec.ScanValues = func(columns []string) ([]any, error) { return (*Event).scanValues(nil, columns) } @@ -465,10 +457,7 @@ func (eq *EventQuery) loadUser(ctx context.Context, query *UserQuery, nodes []*E ids := make([]string, 0, len(nodes)) nodeids := make(map[string][]*Event) for i := range nodes { - if nodes[i].user_events == nil { - continue - } - fk := *nodes[i].user_events + fk := nodes[i].UserID if _, ok := nodeids[fk]; !ok { ids = append(ids, fk) } @@ -485,7 +474,7 @@ func (eq *EventQuery) loadUser(ctx context.Context, query *UserQuery, nodes []*E for _, n := range neighbors { nodes, ok := nodeids[n.ID] if !ok { - return fmt.Errorf(`unexpected foreign-key "user_events" returned %v`, n.ID) + return fmt.Errorf(`unexpected foreign-key "user_id" returned %v`, n.ID) } for i := range nodes { assign(nodes[i], n) @@ -497,10 +486,7 @@ func (eq *EventQuery) loadResearch(ctx context.Context, query *ResearchQuery, no ids := make([]string, 0, len(nodes)) nodeids := make(map[string][]*Event) for i := range nodes { - if nodes[i].research_events == nil { - continue - } - fk := *nodes[i].research_events + fk := nodes[i].ResearchID if _, ok := nodeids[fk]; !ok { ids = append(ids, fk) } @@ -517,7 +503,7 @@ func (eq *EventQuery) loadResearch(ctx context.Context, query *ResearchQuery, no for _, n := range neighbors { nodes, ok := nodeids[n.ID] if !ok { - return fmt.Errorf(`unexpected foreign-key "research_events" returned %v`, n.ID) + return fmt.Errorf(`unexpected foreign-key "research_id" returned %v`, n.ID) } for i := range nodes { assign(nodes[i], n) @@ -554,6 +540,12 @@ func (eq *EventQuery) querySpec() *sqlgraph.QuerySpec { _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) } } + if eq.withUser != nil { + _spec.Node.AddColumnOnce(event.FieldUserID) + } + if eq.withResearch != nil { + _spec.Node.AddColumnOnce(event.FieldResearchID) + } } if ps := eq.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { diff --git a/internal/ent/event_update.go b/internal/ent/event_update.go index 7071150..fbc46e2 100644 --- a/internal/ent/event_update.go +++ b/internal/ent/event_update.go @@ -29,29 +29,27 @@ func (eu *EventUpdate) Where(ps ...predicate.Event) *EventUpdate { return eu } -// SetUserAgent sets the "user_agent" field. -func (eu *EventUpdate) SetUserAgent(s string) *EventUpdate { - eu.mutation.SetUserAgent(s) +// SetUserID sets the "user_id" field. +func (eu *EventUpdate) SetUserID(s string) *EventUpdate { + eu.mutation.SetUserID(s) return eu } -// SetContent sets the "content" field. -func (eu *EventUpdate) SetContent(m map[string]interface{}) *EventUpdate { - eu.mutation.SetContent(m) +// SetResearchID sets the "research_id" field. +func (eu *EventUpdate) SetResearchID(s string) *EventUpdate { + eu.mutation.SetResearchID(s) return eu } -// SetUserID sets the "user" edge to the User entity by ID. -func (eu *EventUpdate) SetUserID(id string) *EventUpdate { - eu.mutation.SetUserID(id) +// SetUserAgent sets the "user_agent" field. +func (eu *EventUpdate) SetUserAgent(s string) *EventUpdate { + eu.mutation.SetUserAgent(s) return eu } -// SetNillableUserID sets the "user" edge to the User entity by ID if the given value is not nil. -func (eu *EventUpdate) SetNillableUserID(id *string) *EventUpdate { - if id != nil { - eu = eu.SetUserID(*id) - } +// SetContent sets the "content" field. +func (eu *EventUpdate) SetContent(m map[string]interface{}) *EventUpdate { + eu.mutation.SetContent(m) return eu } @@ -60,20 +58,6 @@ func (eu *EventUpdate) SetUser(u *User) *EventUpdate { return eu.SetUserID(u.ID) } -// SetResearchID sets the "research" edge to the Research entity by ID. -func (eu *EventUpdate) SetResearchID(id string) *EventUpdate { - eu.mutation.SetResearchID(id) - return eu -} - -// SetNillableResearchID sets the "research" edge to the Research entity by ID if the given value is not nil. -func (eu *EventUpdate) SetNillableResearchID(id *string) *EventUpdate { - if id != nil { - eu = eu.SetResearchID(*id) - } - return eu -} - // SetResearch sets the "research" edge to the Research entity. func (eu *EventUpdate) SetResearch(r *Research) *EventUpdate { return eu.SetResearchID(r.ID) @@ -123,7 +107,21 @@ func (eu *EventUpdate) ExecX(ctx context.Context) { } } +// check runs all checks and user-defined validators on the builder. +func (eu *EventUpdate) check() error { + if _, ok := eu.mutation.UserID(); eu.mutation.UserCleared() && !ok { + return errors.New(`ent: clearing a required unique edge "Event.user"`) + } + if _, ok := eu.mutation.ResearchID(); eu.mutation.ResearchCleared() && !ok { + return errors.New(`ent: clearing a required unique edge "Event.research"`) + } + return nil +} + func (eu *EventUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := eu.check(); err != nil { + return n, err + } _spec := sqlgraph.NewUpdateSpec(event.Table, event.Columns, sqlgraph.NewFieldSpec(event.FieldID, field.TypeString)) if ps := eu.mutation.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { @@ -216,29 +214,27 @@ type EventUpdateOne struct { mutation *EventMutation } -// SetUserAgent sets the "user_agent" field. -func (euo *EventUpdateOne) SetUserAgent(s string) *EventUpdateOne { - euo.mutation.SetUserAgent(s) +// SetUserID sets the "user_id" field. +func (euo *EventUpdateOne) SetUserID(s string) *EventUpdateOne { + euo.mutation.SetUserID(s) return euo } -// SetContent sets the "content" field. -func (euo *EventUpdateOne) SetContent(m map[string]interface{}) *EventUpdateOne { - euo.mutation.SetContent(m) +// SetResearchID sets the "research_id" field. +func (euo *EventUpdateOne) SetResearchID(s string) *EventUpdateOne { + euo.mutation.SetResearchID(s) return euo } -// SetUserID sets the "user" edge to the User entity by ID. -func (euo *EventUpdateOne) SetUserID(id string) *EventUpdateOne { - euo.mutation.SetUserID(id) +// SetUserAgent sets the "user_agent" field. +func (euo *EventUpdateOne) SetUserAgent(s string) *EventUpdateOne { + euo.mutation.SetUserAgent(s) return euo } -// SetNillableUserID sets the "user" edge to the User entity by ID if the given value is not nil. -func (euo *EventUpdateOne) SetNillableUserID(id *string) *EventUpdateOne { - if id != nil { - euo = euo.SetUserID(*id) - } +// SetContent sets the "content" field. +func (euo *EventUpdateOne) SetContent(m map[string]interface{}) *EventUpdateOne { + euo.mutation.SetContent(m) return euo } @@ -247,20 +243,6 @@ func (euo *EventUpdateOne) SetUser(u *User) *EventUpdateOne { return euo.SetUserID(u.ID) } -// SetResearchID sets the "research" edge to the Research entity by ID. -func (euo *EventUpdateOne) SetResearchID(id string) *EventUpdateOne { - euo.mutation.SetResearchID(id) - return euo -} - -// SetNillableResearchID sets the "research" edge to the Research entity by ID if the given value is not nil. -func (euo *EventUpdateOne) SetNillableResearchID(id *string) *EventUpdateOne { - if id != nil { - euo = euo.SetResearchID(*id) - } - return euo -} - // SetResearch sets the "research" edge to the Research entity. func (euo *EventUpdateOne) SetResearch(r *Research) *EventUpdateOne { return euo.SetResearchID(r.ID) @@ -323,7 +305,21 @@ func (euo *EventUpdateOne) ExecX(ctx context.Context) { } } +// check runs all checks and user-defined validators on the builder. +func (euo *EventUpdateOne) check() error { + if _, ok := euo.mutation.UserID(); euo.mutation.UserCleared() && !ok { + return errors.New(`ent: clearing a required unique edge "Event.user"`) + } + if _, ok := euo.mutation.ResearchID(); euo.mutation.ResearchCleared() && !ok { + return errors.New(`ent: clearing a required unique edge "Event.research"`) + } + return nil +} + func (euo *EventUpdateOne) sqlSave(ctx context.Context) (_node *Event, err error) { + if err := euo.check(); err != nil { + return _node, err + } _spec := sqlgraph.NewUpdateSpec(event.Table, event.Columns, sqlgraph.NewFieldSpec(event.FieldID, field.TypeString)) id, ok := euo.mutation.ID() if !ok { diff --git a/internal/ent/gql_collection.go b/internal/ent/gql_collection.go index 469a675..6da0dfd 100644 --- a/internal/ent/gql_collection.go +++ b/internal/ent/gql_collection.go @@ -44,6 +44,10 @@ func (e *EventQuery) collectField(ctx context.Context, opCtx *graphql.OperationC return err } e.withUser = query + if _, ok := fieldSeen[event.FieldUserID]; !ok { + selectedFields = append(selectedFields, event.FieldUserID) + fieldSeen[event.FieldUserID] = struct{}{} + } case "research": var ( alias = field.Alias @@ -54,11 +58,25 @@ func (e *EventQuery) collectField(ctx context.Context, opCtx *graphql.OperationC return err } e.withResearch = query + if _, ok := fieldSeen[event.FieldResearchID]; !ok { + selectedFields = append(selectedFields, event.FieldResearchID) + fieldSeen[event.FieldResearchID] = struct{}{} + } case "createdAt": if _, ok := fieldSeen[event.FieldCreatedAt]; !ok { selectedFields = append(selectedFields, event.FieldCreatedAt) fieldSeen[event.FieldCreatedAt] = struct{}{} } + case "userID": + if _, ok := fieldSeen[event.FieldUserID]; !ok { + selectedFields = append(selectedFields, event.FieldUserID) + fieldSeen[event.FieldUserID] = struct{}{} + } + case "researchID": + if _, ok := fieldSeen[event.FieldResearchID]; !ok { + selectedFields = append(selectedFields, event.FieldResearchID) + fieldSeen[event.FieldResearchID] = struct{}{} + } case "userAgent": if _, ok := fieldSeen[event.FieldUserAgent]; !ok { selectedFields = append(selectedFields, event.FieldUserAgent) diff --git a/internal/ent/gql_edge.go b/internal/ent/gql_edge.go index 553a2be..508c28f 100644 --- a/internal/ent/gql_edge.go +++ b/internal/ent/gql_edge.go @@ -13,7 +13,7 @@ func (e *Event) User(ctx context.Context) (*User, error) { if IsNotLoaded(err) { result, err = e.QueryUser().Only(ctx) } - return result, MaskNotFound(err) + return result, err } func (e *Event) Research(ctx context.Context) (*Research, error) { @@ -21,7 +21,7 @@ func (e *Event) Research(ctx context.Context) (*Research, error) { if IsNotLoaded(err) { result, err = e.QueryResearch().Only(ctx) } - return result, MaskNotFound(err) + return result, err } func (r *Research) Events(ctx context.Context) (result []*Event, err error) { diff --git a/internal/ent/migrate/schema.go b/internal/ent/migrate/schema.go index 6562ac9..836d54b 100644 --- a/internal/ent/migrate/schema.go +++ b/internal/ent/migrate/schema.go @@ -14,8 +14,8 @@ var ( {Name: "created_at", Type: field.TypeTime}, {Name: "user_agent", Type: field.TypeString}, {Name: "content", Type: field.TypeJSON}, - {Name: "research_events", Type: field.TypeString, Nullable: true}, - {Name: "user_events", Type: field.TypeString, Nullable: true}, + {Name: "research_id", Type: field.TypeString}, + {Name: "user_id", Type: field.TypeString}, } // EventsTable holds the schema information for the "events" table. EventsTable = &schema.Table{ @@ -48,12 +48,12 @@ var ( Columns: []*schema.Column{EventsColumns[2]}, }, { - Name: "event_user_events", + Name: "event_user_id", Unique: false, Columns: []*schema.Column{EventsColumns[5]}, }, { - Name: "event_research_events", + Name: "event_research_id", Unique: false, Columns: []*schema.Column{EventsColumns[4]}, }, diff --git a/internal/ent/mutation.go b/internal/ent/mutation.go index 9af2d0d..4ec6c0d 100644 --- a/internal/ent/mutation.go +++ b/internal/ent/mutation.go @@ -190,6 +190,78 @@ func (m *EventMutation) ResetCreatedAt() { m.created_at = nil } +// SetUserID sets the "user_id" field. +func (m *EventMutation) SetUserID(s string) { + m.user = &s +} + +// UserID returns the value of the "user_id" field in the mutation. +func (m *EventMutation) UserID() (r string, exists bool) { + v := m.user + if v == nil { + return + } + return *v, true +} + +// OldUserID returns the old "user_id" field's value of the Event entity. +// If the Event object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *EventMutation) OldUserID(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldUserID is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldUserID requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldUserID: %w", err) + } + return oldValue.UserID, nil +} + +// ResetUserID resets all changes to the "user_id" field. +func (m *EventMutation) ResetUserID() { + m.user = nil +} + +// SetResearchID sets the "research_id" field. +func (m *EventMutation) SetResearchID(s string) { + m.research = &s +} + +// ResearchID returns the value of the "research_id" field in the mutation. +func (m *EventMutation) ResearchID() (r string, exists bool) { + v := m.research + if v == nil { + return + } + return *v, true +} + +// OldResearchID returns the old "research_id" field's value of the Event entity. +// If the Event object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *EventMutation) OldResearchID(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldResearchID is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldResearchID requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldResearchID: %w", err) + } + return oldValue.ResearchID, nil +} + +// ResetResearchID resets all changes to the "research_id" field. +func (m *EventMutation) ResetResearchID() { + m.research = nil +} + // SetUserAgent sets the "user_agent" field. func (m *EventMutation) SetUserAgent(s string) { m.user_agent = &s @@ -262,11 +334,6 @@ func (m *EventMutation) ResetContent() { m.content = nil } -// SetUserID sets the "user" edge to the User entity by id. -func (m *EventMutation) SetUserID(id string) { - m.user = &id -} - // ClearUser clears the "user" edge to the User entity. func (m *EventMutation) ClearUser() { m.cleareduser = true @@ -277,14 +344,6 @@ func (m *EventMutation) UserCleared() bool { return m.cleareduser } -// UserID returns the "user" edge ID in the mutation. -func (m *EventMutation) UserID() (id string, exists bool) { - if m.user != nil { - return *m.user, true - } - return -} - // UserIDs returns the "user" edge IDs in the mutation. // Note that IDs always returns len(IDs) <= 1 for unique edges, and you should use // UserID instead. It exists only for internal usage by the builders. @@ -301,11 +360,6 @@ func (m *EventMutation) ResetUser() { m.cleareduser = false } -// SetResearchID sets the "research" edge to the Research entity by id. -func (m *EventMutation) SetResearchID(id string) { - m.research = &id -} - // ClearResearch clears the "research" edge to the Research entity. func (m *EventMutation) ClearResearch() { m.clearedresearch = true @@ -316,14 +370,6 @@ func (m *EventMutation) ResearchCleared() bool { return m.clearedresearch } -// ResearchID returns the "research" edge ID in the mutation. -func (m *EventMutation) ResearchID() (id string, exists bool) { - if m.research != nil { - return *m.research, true - } - return -} - // ResearchIDs returns the "research" edge IDs in the mutation. // Note that IDs always returns len(IDs) <= 1 for unique edges, and you should use // ResearchID instead. It exists only for internal usage by the builders. @@ -374,10 +420,16 @@ func (m *EventMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *EventMutation) Fields() []string { - fields := make([]string, 0, 3) + fields := make([]string, 0, 5) if m.created_at != nil { fields = append(fields, event.FieldCreatedAt) } + if m.user != nil { + fields = append(fields, event.FieldUserID) + } + if m.research != nil { + fields = append(fields, event.FieldResearchID) + } if m.user_agent != nil { fields = append(fields, event.FieldUserAgent) } @@ -394,6 +446,10 @@ func (m *EventMutation) Field(name string) (ent.Value, bool) { switch name { case event.FieldCreatedAt: return m.CreatedAt() + case event.FieldUserID: + return m.UserID() + case event.FieldResearchID: + return m.ResearchID() case event.FieldUserAgent: return m.UserAgent() case event.FieldContent: @@ -409,6 +465,10 @@ func (m *EventMutation) OldField(ctx context.Context, name string) (ent.Value, e switch name { case event.FieldCreatedAt: return m.OldCreatedAt(ctx) + case event.FieldUserID: + return m.OldUserID(ctx) + case event.FieldResearchID: + return m.OldResearchID(ctx) case event.FieldUserAgent: return m.OldUserAgent(ctx) case event.FieldContent: @@ -429,6 +489,20 @@ func (m *EventMutation) SetField(name string, value ent.Value) error { } m.SetCreatedAt(v) return nil + case event.FieldUserID: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetUserID(v) + return nil + case event.FieldResearchID: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetResearchID(v) + return nil case event.FieldUserAgent: v, ok := value.(string) if !ok { @@ -495,6 +569,12 @@ func (m *EventMutation) ResetField(name string) error { case event.FieldCreatedAt: m.ResetCreatedAt() return nil + case event.FieldUserID: + m.ResetUserID() + return nil + case event.FieldResearchID: + m.ResetResearchID() + return nil case event.FieldUserAgent: m.ResetUserAgent() return nil diff --git a/internal/ent/research/research.go b/internal/ent/research/research.go index ff345ed..0b757ab 100644 --- a/internal/ent/research/research.go +++ b/internal/ent/research/research.go @@ -28,7 +28,7 @@ const ( // It exists in this package in order to avoid circular dependency with the "event" package. EventsInverseTable = "events" // EventsColumn is the table column denoting the events relation/edge. - EventsColumn = "research_events" + EventsColumn = "research_id" ) // Columns holds all SQL columns for research fields. diff --git a/internal/ent/research_create.go b/internal/ent/research_create.go index b854009..ab55073 100644 --- a/internal/ent/research_create.go +++ b/internal/ent/research_create.go @@ -115,9 +115,6 @@ func (rc *ResearchCreate) check() error { if _, ok := rc.mutation.Schema(); !ok { return &ValidationError{Name: "schema", err: errors.New(`ent: missing required field "Research.schema"`)} } - if len(rc.mutation.EventsIDs()) == 0 { - return &ValidationError{Name: "events", err: errors.New(`ent: missing required edge "Research.events"`)} - } return nil } diff --git a/internal/ent/research_query.go b/internal/ent/research_query.go index 49b1584..6c349ea 100644 --- a/internal/ent/research_query.go +++ b/internal/ent/research_query.go @@ -430,7 +430,9 @@ func (rq *ResearchQuery) loadEvents(ctx context.Context, query *EventQuery, node init(nodes[i]) } } - query.withFKs = true + if len(query.ctx.Fields) > 0 { + query.ctx.AppendFieldOnce(event.FieldResearchID) + } query.Where(predicate.Event(func(s *sql.Selector) { s.Where(sql.InValues(s.C(research.EventsColumn), fks...)) })) @@ -439,13 +441,10 @@ func (rq *ResearchQuery) loadEvents(ctx context.Context, query *EventQuery, node return err } for _, n := range neighbors { - fk := n.research_events - if fk == nil { - return fmt.Errorf(`foreign-key "research_events" is nil for node %v`, n.ID) - } - node, ok := nodeids[*fk] + fk := n.ResearchID + node, ok := nodeids[fk] if !ok { - return fmt.Errorf(`unexpected referenced foreign-key "research_events" returned %v for node %v`, *fk, n.ID) + return fmt.Errorf(`unexpected referenced foreign-key "research_id" returned %v for node %v`, fk, n.ID) } assign(node, n) } diff --git a/internal/ent/schema/event.go b/internal/ent/schema/event.go index 63f91b1..5067c09 100644 --- a/internal/ent/schema/event.go +++ b/internal/ent/schema/event.go @@ -47,10 +47,12 @@ func (Event) Edges() []ent.Edge { edge.From("user", User.Type). Ref("events"). Field("user_id"). + Required(). Unique(), edge.From("research", Research.Type). Ref("events"). Field("research_id"). + Required(). Unique(), } } diff --git a/internal/ent/schema/research.go b/internal/ent/schema/research.go index 0559217..7bf6fc2 100644 --- a/internal/ent/schema/research.go +++ b/internal/ent/schema/research.go @@ -36,7 +36,7 @@ func (Research) Fields() []ent.Field { func (Research) Edges() []ent.Edge { return []ent.Edge{ edge.To("events", Event.Type). - Required(). + // Required(). Annotations(entsql.OnDelete(entsql.NoAction)), } } diff --git a/internal/ent/schema/user.go b/internal/ent/schema/user.go index 3a2121a..5915573 100644 --- a/internal/ent/schema/user.go +++ b/internal/ent/schema/user.go @@ -34,7 +34,7 @@ func (User) Fields() []ent.Field { func (User) Edges() []ent.Edge { return []ent.Edge{ edge.To("events", Event.Type). - Required(). + // Required(). Annotations(entsql.OnDelete(entsql.NoAction)), } } diff --git a/internal/ent/user/user.go b/internal/ent/user/user.go index 7ed9510..c638929 100644 --- a/internal/ent/user/user.go +++ b/internal/ent/user/user.go @@ -32,7 +32,7 @@ const ( // It exists in this package in order to avoid circular dependency with the "event" package. EventsInverseTable = "events" // EventsColumn is the table column denoting the events relation/edge. - EventsColumn = "user_events" + EventsColumn = "user_id" ) // Columns holds all SQL columns for user fields. diff --git a/internal/ent/user_create.go b/internal/ent/user_create.go index e042fc9..337b749 100644 --- a/internal/ent/user_create.go +++ b/internal/ent/user_create.go @@ -135,9 +135,6 @@ func (uc *UserCreate) check() error { return &ValidationError{Name: "credential", err: fmt.Errorf(`ent: validator failed for field "User.credential": %w`, err)} } } - if len(uc.mutation.EventsIDs()) == 0 { - return &ValidationError{Name: "events", err: errors.New(`ent: missing required edge "User.events"`)} - } return nil } diff --git a/internal/ent/user_query.go b/internal/ent/user_query.go index 74404cd..fd00153 100644 --- a/internal/ent/user_query.go +++ b/internal/ent/user_query.go @@ -430,7 +430,9 @@ func (uq *UserQuery) loadEvents(ctx context.Context, query *EventQuery, nodes [] init(nodes[i]) } } - query.withFKs = true + if len(query.ctx.Fields) > 0 { + query.ctx.AppendFieldOnce(event.FieldUserID) + } query.Where(predicate.Event(func(s *sql.Selector) { s.Where(sql.InValues(s.C(user.EventsColumn), fks...)) })) @@ -439,13 +441,10 @@ func (uq *UserQuery) loadEvents(ctx context.Context, query *EventQuery, nodes [] return err } for _, n := range neighbors { - fk := n.user_events - if fk == nil { - return fmt.Errorf(`foreign-key "user_events" is nil for node %v`, n.ID) - } - node, ok := nodeids[*fk] + fk := n.UserID + node, ok := nodeids[fk] if !ok { - return fmt.Errorf(`unexpected referenced foreign-key "user_events" returned %v for node %v`, *fk, n.ID) + return fmt.Errorf(`unexpected referenced foreign-key "user_id" returned %v for node %v`, fk, n.ID) } assign(node, n) }