Skip to content

Commit d0c6209

Browse files
authored
Merge pull request #2219 from dolthub/zachmu/merge-bug
additional tests for merging foreign keys and constraint violations
2 parents ea22964 + 07d0f0a commit d0c6209

1 file changed

Lines changed: 136 additions & 0 deletions

File tree

testing/go/foreign_keys_test.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
package _go
1616

1717
import (
18+
"math/big"
1819
"testing"
1920

2021
"github.com/dolthub/go-mysql-server/sql"
22+
"github.com/jackc/pgx/v5/pgtype"
2123
)
2224

2325
func TestForeignKeys(t *testing.T) {
@@ -988,6 +990,140 @@ func TestForeignKeys(t *testing.T) {
988990
},
989991
},
990992
},
993+
{
994+
Name: "merging",
995+
SetUpScript: []string{
996+
`CREATE TABLE "evaluation_job_config" (
997+
"tenant_id" varchar(256) NOT NULL,
998+
"id" varchar(256) NOT NULL,
999+
"project_id" varchar(256) NOT NULL,
1000+
"job_filters" jsonb,
1001+
"created_at" timestamp DEFAULT now() NOT NULL,
1002+
"updated_at" timestamp DEFAULT now() NOT NULL,
1003+
CONSTRAINT "evaluation_job_config_tenant_id_project_id_id_pk" PRIMARY KEY("tenant_id","project_id","id")
1004+
);`,
1005+
`CREATE TABLE "evaluation_job_config_evaluator_relations" (
1006+
"tenant_id" varchar(256) NOT NULL,
1007+
"id" varchar(256) NOT NULL,
1008+
"project_id" varchar(256) NOT NULL,
1009+
"evaluation_job_config_id" text NOT NULL,
1010+
"evaluator_id" text NOT NULL,
1011+
"created_at" timestamp DEFAULT now() NOT NULL,
1012+
"updated_at" timestamp DEFAULT now() NOT NULL,
1013+
CONSTRAINT "eval_job_cfg_evaluator_rel_pk" PRIMARY KEY("tenant_id","project_id","id")
1014+
);`,
1015+
`CREATE TABLE "agent" (
1016+
"tenant_id" varchar(256) NOT NULL,
1017+
"id" varchar(256) NOT NULL,
1018+
"project_id" varchar(256) NOT NULL,
1019+
"name" varchar(256) NOT NULL,
1020+
"description" text,
1021+
"default_sub_agent_id" varchar(256),
1022+
"context_config_id" varchar(256),
1023+
"models" jsonb,
1024+
"status_updates" jsonb,
1025+
"prompt" text,
1026+
"stop_when" jsonb,
1027+
"created_at" timestamp DEFAULT now() NOT NULL,
1028+
"updated_at" timestamp DEFAULT now() NOT NULL,
1029+
CONSTRAINT "agent_tenant_id_project_id_id_pk" PRIMARY KEY("tenant_id","project_id","id")
1030+
);`,
1031+
`CREATE TABLE "projects" (
1032+
"tenant_id" varchar(256) NOT NULL,
1033+
"id" varchar(256) NOT NULL,
1034+
"name" varchar(256) NOT NULL,
1035+
"description" text,
1036+
"models" jsonb,
1037+
"stop_when" jsonb,
1038+
"created_at" timestamp DEFAULT now() NOT NULL,
1039+
"updated_at" timestamp DEFAULT now() NOT NULL,
1040+
CONSTRAINT "projects_tenant_id_id_pk" PRIMARY KEY("tenant_id","id")
1041+
);`,
1042+
`ALTER TABLE "evaluation_job_config" ADD CONSTRAINT "evaluation_job_config_project_fk" FOREIGN KEY ("tenant_id","project_id") REFERENCES "public"."projects"("tenant_id","id") ON DELETE cascade ON UPDATE no action;`,
1043+
`ALTER TABLE "evaluation_job_config_evaluator_relations" ADD CONSTRAINT "eval_job_cfg_evaluator_rel_job_cfg_fk" FOREIGN KEY ("tenant_id","project_id","evaluation_job_config_id") REFERENCES "public"."evaluation_job_config"("tenant_id","project_id","id") ON DELETE cascade ON UPDATE no action;`,
1044+
`INSERT INTO projects VALUES ('tenant1', 'project1', 'Project One', 'First project', '{"model": "gpt-4"}', '{"condition": "complete"}', now(), now());`,
1045+
`INSERT INTO evaluation_job_config VALUES ('tenant1', 'jobconfig1', 'project1', '{"filter": "all"}', now(), now());`,
1046+
`INSERT INTO evaluation_job_config_evaluator_relations VALUES ('tenant1', 'rel1', 'project1', 'jobconfig1', 'evaluator1', now(), now());`,
1047+
`INSERT INTO agent VALUES ('tenant1', 'agent1', 'project1', 'Agent One', 'First agent', null, null, '{"model": "gpt-4"}', '{}', 'You are an agent.', '{}', now(), now());`,
1048+
`SELECT DOLT_COMMIT('-Am', 'initial tables')`,
1049+
`SELECT DOLT_BRANCH('feature')`,
1050+
`CREATE TABLE "triggers" (
1051+
"tenant_id" varchar(256) NOT NULL,
1052+
"id" varchar(256) NOT NULL,
1053+
"project_id" varchar(256) NOT NULL,
1054+
"agent_id" varchar(256) NOT NULL,
1055+
"name" varchar(256) NOT NULL,
1056+
"description" text,
1057+
"enabled" boolean DEFAULT true NOT NULL,
1058+
"input_schema" jsonb,
1059+
"output_transform" jsonb,
1060+
"message_template" text NOT NULL,
1061+
"authentication" jsonb,
1062+
"signing_secret" text,
1063+
"created_at" timestamp DEFAULT now() NOT NULL,
1064+
"updated_at" timestamp DEFAULT now() NOT NULL,
1065+
CONSTRAINT "triggers_tenant_id_project_id_agent_id_id_pk" PRIMARY KEY("tenant_id","project_id","agent_id","id")
1066+
);`,
1067+
`ALTER TABLE "triggers" ADD CONSTRAINT "triggers_agent_fk" FOREIGN KEY ("tenant_id","project_id","agent_id") REFERENCES "public"."agent"("tenant_id","project_id","id") ON DELETE cascade ON UPDATE no action;`,
1068+
`select DOLT_COMMIT('-Am', 'add triggers table')`,
1069+
`select dolt_checkout('feature')`,
1070+
},
1071+
Assertions: []ScriptTestAssertion{
1072+
{
1073+
Query: "insert into agent VALUES ('tenant1', 'agent2', 'project1', 'Agent Two', 'Second agent', null, null, '{\"model\": \"gpt-4\"}', '{}', 'You are another agent.', '{}', now(), now());",
1074+
},
1075+
{
1076+
Query: "select dolt_commit('-Am', 'add second agent')",
1077+
SkipResultsCheck: true,
1078+
},
1079+
{
1080+
Query: "select strpos(dolt_merge('main')::text, 'merge successful') > 1;",
1081+
Expected: []sql.Row{{"t"}},
1082+
},
1083+
},
1084+
},
1085+
{
1086+
Name: "merge with constraint violations",
1087+
SetUpScript: []string{
1088+
"CREATE TABLE parent (a INT PRIMARY KEY, b INT UNIQUE);",
1089+
"CREATE TABLE child (c INT PRIMARY KEY, d INT);",
1090+
"alter table child add constraint fk foreign key (d) references parent(b);",
1091+
"INSERT INTO parent VALUES (1, 1), (2, 2), (3, 3);",
1092+
"INSERT INTO child VALUES (1, 1), (2, 2);",
1093+
"SELECT DOLT_COMMIT('-Am', 'initial commit')",
1094+
"SELECT DOLT_BRANCH('feature')",
1095+
"insert into child VALUES (3, 3);",
1096+
"SELECT DOLT_COMMIT('-Am', 'new child')",
1097+
"select dolt_checkout('feature')",
1098+
"delete from parent where b = 3;",
1099+
"SELECT DOLT_COMMIT('-Am', 'delete from parent')",
1100+
},
1101+
Assertions: []ScriptTestAssertion{
1102+
{
1103+
Query: "select dolt_merge('main')",
1104+
ExpectedErr: "constraint violations",
1105+
},
1106+
{
1107+
Query: "set dolt_force_transaction_commit = 1;",
1108+
},
1109+
{
1110+
Query: "select dolt_merge('main')",
1111+
SkipResultsCheck: true,
1112+
},
1113+
{
1114+
Query: "select * from dolt_constraint_violations order by 1",
1115+
Expected: []sql.Row{
1116+
{"child", pgtype.Numeric{Int: big.NewInt(1), Valid: true}},
1117+
},
1118+
},
1119+
{
1120+
Query: "select violation_type, c, d, violation_info from dolt_constraint_violations_child order by 1",
1121+
Expected: []sql.Row{
1122+
{"foreign key", 3, 3, "{\"Columns\":[\"d\"],\"ForeignKey\":\"fk\",\"Index\":\"fk\",\"OnDelete\":\"RESTRICT\",\"OnUpdate\":\"RESTRICT\",\"ReferencedColumns\":[\"b\"],\"ReferencedIndex\":\"b\",\"ReferencedTable\":\"parent\",\"Table\":\"child\"}"},
1123+
},
1124+
},
1125+
},
1126+
},
9911127
},
9921128
)
9931129
}

0 commit comments

Comments
 (0)