# Modify column from nullable to non-nullable with default value.
atlas migrate lint --dir file://migrations1 --dev-url URL --latest=1 > got.txt
cmp got.txt empty.txt

# Modify column from nullable to non-nullable without default value.
atlas migrate lint --dir file://migrations2 --dev-url URL --latest=1 > got.txt
cmp got.txt expected2.txt

# Modify column from nullable to non-nullable without default value but backfill previous rows.
atlas migrate lint --dir file://migrations3 --dev-url URL --latest=1 > got.txt
cmp got.txt empty.txt

-- empty.txt --
-- migrations1/1.sql --
CREATE TABLE `users` (`a` int NULL);

-- migrations1/2.sql --
-- disable the enforcement of foreign-keys constraints
PRAGMA foreign_keys = off;
-- create "new_users" table
CREATE TABLE `new_users` (`a` int NOT NULL DEFAULT 1);
-- copy rows from old table "users" to new temporary table "new_users"
INSERT INTO `new_users` (`a`) SELECT IFNULL(`a`, 1) FROM `users`;
-- drop "users" table after copying rows
DROP TABLE `users`;
-- rename temporary table "new_users" to "users"
ALTER TABLE `new_users` RENAME TO `users`;
-- enable back the enforcement of foreign-keys constraints
PRAGMA foreign_keys = on;

-- migrations2/1.sql --
CREATE TABLE `users` (`a` int NULL);

-- migrations2/2.sql --
-- disable the enforcement of foreign-keys constraints
PRAGMA foreign_keys = off;
-- create "new_users" table
CREATE TABLE `new_users` (`a` int NOT NULL);
-- copy rows from old table "users" to new temporary table "new_users"
INSERT INTO `new_users` (`a`) SELECT `a` FROM `users`;
-- drop "users" table after copying rows
DROP TABLE `users`;
-- rename temporary table "new_users" to "users"
ALTER TABLE `new_users` RENAME TO `users`;
-- enable back the enforcement of foreign-keys constraints
PRAGMA foreign_keys = on;

-- expected2.txt --
2.sql: data dependent changes detected:

	L4: Modifying nullable column "a" to non-nullable without default value might fail in case it contains NULL values

-- migrations3/1.sql --
CREATE TABLE `users` (`a` int NULL);

-- migrations3/2.sql --
-- backfill previous rows
UPDATE `users` SET `a` = 1 WHERE `a` IS NULL;
-- disable the enforcement of foreign-keys constraints
PRAGMA foreign_keys = off;
-- create "new_users" table
CREATE TABLE `new_users` (`a` int NOT NULL);
-- copy rows from old table "users" to new temporary table "new_users"
INSERT INTO `new_users` (`a`) SELECT `a` FROM `users`;
-- drop "users" table after copying rows
DROP TABLE `users`;
-- rename temporary table "new_users" to "users"
ALTER TABLE `new_users` RENAME TO `users`;
-- enable back the enforcement of foreign-keys constraints
PRAGMA foreign_keys = on;
