! atlas migrate apply
stderr 'Error: checksum file not found'
stdout 'You have a checksum error in your migration directory.'
stdout 'atlas migrate hash'

atlas migrate hash

# Apply all of them
atlas migrate apply --url URL --revisions-schema $db
stdout 'Migrating to version 3 \(3 migrations in total\):'
stdout '-- migrating version 1'
stdout '-> CREATE TABLE "users" \("id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, "age" bigint NOT NULL, "name" character varying NOT NULL, PRIMARY KEY \("id"\)\);'
stdout '-- migrating version 2'
stdout '-> CREATE UNIQUE INDEX "users_age_key" ON "users" \("age"\);'
stdout '-- migrating version 3'
stdout '-> CREATE TABLE "pets" \("id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, "name" character varying NOT NULL, PRIMARY KEY \("id"\)\);'
stdout '-- 3 migrations'
stdout '-- 3 sql statements'
cmpshow users users.sql
cmpshow pets pets.sql

atlas migrate apply --url URL --revisions-schema $db 1
stdout 'No migration files to execute'

clearSchema

# Apply one by one
atlas migrate apply --url URL --revisions-schema $db 1
stdout 'Migrating to version 1 \(1 migrations in total\):'
cmpshow users users_1.sql

atlas migrate apply --url URL --revisions-schema $db 1
stdout 'Migrating to version 2 from 1 \(1 migrations in total\):'
cmpshow users users.sql

atlas migrate apply --url URL --revisions-schema $db 1
stdout 'Migrating to version 3 from 2 \(1 migrations in total\):'
cmpshow users users.sql
cmpshow pets pets.sql

atlas migrate apply --url URL --revisions-schema $db 1
stdout 'No migration files to execute'

clearSchema

# Move the broken migration into the migrations directory and check the different transaction modes.
cp broken.sql migrations/4_fourth.sql
atlas migrate hash

! atlas migrate apply --url URL --revisions-schema $db --tx-mode invalid
stderr 'unknown tx-mode "invalid"'

# Test --tx-mode all

! atlas migrate apply --url URL --revisions-schema $db --tx-mode all
stderr 'executing statement "THIS IS A FAILING STATEMENT;" from version "4"'
atlas schema inspect --url URL --exclude $db.atlas_schema_revisions
cmp stdout empty.hcl

# Apply one migration, after rolling everything back, the first revision must still exist.
atlas migrate apply --url URL --revisions-schema $db 1
atlas schema inspect --url URL --exclude $db.atlas_schema_revisions --exclude $db.users
cmp stdout empty.hcl
cmpshow users users_1.sql

! atlas migrate apply --url URL --revisions-schema $db --tx-mode all
stderr 'executing statement "THIS IS A FAILING STATEMENT;" from version "4"'
atlas schema inspect --url URL --exclude $db.atlas_schema_revisions --exclude $db.users
cmp stdout empty.hcl

# If the broken migration is gone, we can apply everything without any problems.
rm migrations/4_fourth.sql
atlas migrate hash

atlas migrate apply --url URL --revisions-schema $db
cmpshow users users.sql
cmpshow pets pets.sql
atlas schema inspect --url URL --exclude $db.atlas_schema_revisions --exclude $db.users --exclude $db.pets
cmp stdout empty.hcl

clearSchema

# Test --tx-mode file

cp broken.sql migrations/4_fourth.sql
atlas migrate hash

! atlas migrate apply --url URL --revisions-schema $db --tx-mode file
stderr 'executing statement "THIS IS A FAILING STATEMENT;" from version "4"'
atlas schema inspect --url URL --exclude $db.atlas_schema_revisions
cmpshow users users.sql
cmpshow pets pets.sql

# Table "broken" does not exist since we rolled back that migration.
atlas schema inspect --url URL --exclude $db.atlas_schema_revisions --exclude $db.users --exclude $db.pets
cmp stdout empty.hcl

# If the broken migration is gone, we can apply everything without any problems.
rm migrations/4_fourth.sql
atlas migrate hash

atlas migrate apply --url URL --revisions-schema $db
cmpshow users users.sql
cmpshow pets pets.sql
atlas schema inspect --url URL --exclude $db.atlas_schema_revisions --exclude $db.users --exclude $db.pets
cmp stdout empty.hcl

clearSchema

# Test --tx-mode none

cp broken.sql migrations/4_fourth.sql
atlas migrate hash

! atlas migrate apply --url URL --revisions-schema $db --tx-mode none
stderr 'executing statement "THIS IS A FAILING STATEMENT;" from version "4"'
atlas schema inspect --url URL --exclude $db.atlas_schema_revisions
cmpshow users users.sql
cmpshow pets pets.sql

# Table "broken" does exist since we do not have transactions.
atlas schema inspect --url URL --exclude $db.atlas_schema_revisions --exclude $db.users --exclude $db.pets
cmp stdout broken.hcl

-- migrations/1_first.sql --
CREATE TABLE "users" ("id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, "age" bigint NOT NULL, "name" character varying NOT NULL, PRIMARY KEY ("id"));

-- migrations/2_second.sql --
CREATE UNIQUE INDEX "users_age_key" ON "users" ("age");

-- migrations/3_third.sql --
CREATE TABLE "pets" ("id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, "name" character varying NOT NULL, PRIMARY KEY ("id"));

-- broken.sql --
CREATE TABLE "broken" ("id" bigint);
THIS IS A FAILING STATEMENT;

-- empty.hcl --
schema "script_cli_migrate_apply" {
}
-- broken.hcl --
table "broken" {
  schema = schema.script_cli_migrate_apply
  column "id" {
    null = true
    type = bigint
  }
}
schema "script_cli_migrate_apply" {
}
-- users.sql --
                        Table "script_cli_migrate_apply.users"
 Column |       Type        | Collation | Nullable |             Default
--------+-------------------+-----------+----------+----------------------------------
 id     | bigint            |           | not null | generated by default as identity
 age    | bigint            |           | not null |
 name   | character varying |           | not null |
Indexes:
    "users_pkey" PRIMARY KEY, btree (id)
    "users_age_key" UNIQUE, btree (age)

-- users_1.sql --
                        Table "script_cli_migrate_apply.users"
 Column |       Type        | Collation | Nullable |             Default
--------+-------------------+-----------+----------+----------------------------------
 id     | bigint            |           | not null | generated by default as identity
 age    | bigint            |           | not null |
 name   | character varying |           | not null |
Indexes:
    "users_pkey" PRIMARY KEY, btree (id)

-- pets.sql --
                        Table "script_cli_migrate_apply.pets"
 Column |       Type        | Collation | Nullable |             Default
--------+-------------------+-----------+----------+----------------------------------
 id     | bigint            |           | not null | generated by default as identity
 name   | character varying |           | not null |
Indexes:
    "pets_pkey" PRIMARY KEY, btree (id)
