# Modify column from nullable to non-nullable with default value.
atlas migrate lint --dir file://migrations1 --dev-url URL --latest=1
stdout 'Analyzing changes from version 1 to 2 \(1 migration in total\):'
stdout ''
stdout '  -- analyzing version 2'
stdout '    -- no diagnostics found'
stdout '  -- ok \(.+\)'
stdout ''
stdout '  -------------------------'
stdout '  -- .+'
stdout '  -- 1 version ok'
stdout '  -- 3 schema changes'

# Modify column from nullable to non-nullable without default value.
atlas migrate lint --dir file://migrations2 --dev-url URL --latest=1
stdout 'Analyzing changes from version 1 to 2 \(1 migration in total\):'
stdout ''
stdout '  -- analyzing version 2'
stdout '    -- data dependent changes detected:'
stdout '      -- L4: Modifying nullable column "a" to non-nullable without default value might fail in'
stdout '         case it contains NULL values https://atlasgo.io/lint/analyzers#LT101'
stdout '  -- ok \(.+\)'
stdout ''
stdout '  -------------------------'
stdout '  -- .+'
stdout '  -- 1 version with warnings'
stdout '  -- 3 schema changes'
stdout '  -- 1 diagnostic'

# 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
stdout 'Analyzing changes from version 1 to 2 \(1 migration in total\):'
stdout ''
stdout '  -- analyzing version 2'
stdout '    -- no diagnostics found'
stdout '  -- ok \(.+\)'
stdout ''
stdout '  -------------------------'
stdout '  -- .+'
stdout '  -- 1 version ok'
stdout '  -- 4 schema changes'

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

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