# Test dumping in the presence of cycles.
# This used to crash before with stack overflow due to an infinite loop before:
# https://github.com/cockroachdb/cockroach/pull/20255

sql
CREATE DATABASE d;
CREATE TABLE d.t (
	PRIMARY KEY (id),
	FOREIGN KEY (next_id) REFERENCES d.t(id),
	id INT,
	next_id INT
);
INSERT INTO d.t VALUES (
	1,
	NULL
);
----
INSERT 1

dump d t
----
----
CREATE TABLE t (
	id INT8 NOT NULL,
	next_id INT8 NULL,
	CONSTRAINT "primary" PRIMARY KEY (id ASC),
	INDEX t_auto_index_fk_next_id_ref_t (next_id ASC),
	FAMILY "primary" (id, next_id)
);

INSERT INTO t (id, next_id) VALUES
	(1, NULL);

ALTER TABLE t ADD CONSTRAINT fk_next_id_ref_t FOREIGN KEY (next_id) REFERENCES t (id);

-- Validate foreign key constraints. These can fail if there was unvalidated data during the dump.
ALTER TABLE t VALIDATE CONSTRAINT fk_next_id_ref_t;
----
----

# Now make a reference forces the dump to add the FKs after the data has been inserted.

sql
UPDATE d.t SET next_id = 1
----
UPDATE 1

dump d t
----
----
CREATE TABLE t (
	id INT8 NOT NULL,
	next_id INT8 NULL,
	CONSTRAINT "primary" PRIMARY KEY (id ASC),
	INDEX t_auto_index_fk_next_id_ref_t (next_id ASC),
	FAMILY "primary" (id, next_id)
);

INSERT INTO t (id, next_id) VALUES
	(1, 1);

ALTER TABLE t ADD CONSTRAINT fk_next_id_ref_t FOREIGN KEY (next_id) REFERENCES t (id);

-- Validate foreign key constraints. These can fail if there was unvalidated data during the dump.
ALTER TABLE t VALIDATE CONSTRAINT fk_next_id_ref_t;
----
----

# Make some weirdo identifiers and the second FK.

sql
ALTER TABLE d.t RENAME COLUMN next_id TO "'";
ALTER TABLE d.t RENAME TO d."table";
----
RENAME TABLE

dump d table
----
----
CREATE TABLE "table" (
	id INT8 NOT NULL,
	"'" INT8 NULL,
	CONSTRAINT "primary" PRIMARY KEY (id ASC),
	INDEX t_auto_index_fk_next_id_ref_t ("'" ASC),
	FAMILY "primary" (id, "'")
);

INSERT INTO "table" (id, "'") VALUES
	(1, 1);

ALTER TABLE "table" ADD CONSTRAINT fk_next_id_ref_t FOREIGN KEY ("'") REFERENCES "table" (id);

-- Validate foreign key constraints. These can fail if there was unvalidated data during the dump.
ALTER TABLE "table" VALIDATE CONSTRAINT fk_next_id_ref_t;
----
----

# Dumping only the schema doesn't need to use the ALTER TABLE FK stuff.

dump d --dump-mode=schema
----
----
CREATE TABLE "table" (
	id INT8 NOT NULL,
	"'" INT8 NULL,
	CONSTRAINT "primary" PRIMARY KEY (id ASC),
	INDEX t_auto_index_fk_next_id_ref_t ("'" ASC),
	FAMILY "primary" (id, "'")
);

ALTER TABLE "table" ADD CONSTRAINT fk_next_id_ref_t FOREIGN KEY ("'") REFERENCES "table" (id);

-- Validate foreign key constraints. These can fail if there was unvalidated data during the dump.
ALTER TABLE "table" VALIDATE CONSTRAINT fk_next_id_ref_t;
----
----
# Dumping only the data shouldn't have the ALTER stuff either.

dump d --dump-mode=data
noroundtrip
----
----

INSERT INTO "table" (id, "'") VALUES
	(1, 1);
----
----
