# 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;
