# Skip PostgreSQL 10, 11 as they do not support generated columns.
! only postgres10|postgres11

apply 1.hcl
cmpshow users 1.sql

! apply 2.fail1.hcl 'changing column "a" to generated column is not supported (drop and add is required)'
! apply 2.fail2.hcl 'changing the generation expression for a column "b" is not supported'

# Skip PostgreSQL 12 as it does not support 'DROP EXPRESSION'.
! only postgres12
apply 3.hcl
cmpshow users 3.sql

-- 1.hcl --
schema "$db" {}

table "users" {
    schema = schema.$db
    column "a" {
        type = int
    }
    column "b" {
        type = int
        as = "1"
    }
    column "c" {
        type = int
        as {
            expr = "2"
            type = STORED
        }
    }
}

-- 1.sql --
                  Table "script_column_generated.users"
 Column |  Type   | Collation | Nullable |            Default
--------+---------+-----------+----------+--------------------------------
 a      | integer |           | not null |
 b      | integer |           | not null | generated always as (1) stored
 c      | integer |           | not null | generated always as (2) stored

-- 2.fail1.hcl --
schema "$db" {}

table "users" {
    schema = schema.$db
    column "a" {
        type = int
        as = "0"
    }
    column "b" {
        type = int
        as = "1"
    }
    column "c" {
        type = int
        as {
            expr = "2"
            type = STORED
        }
    }
}

-- 2.fail2.hcl --
schema "$db" {}

table "users" {
    schema = schema.$db
    column "a" {
        type = int
    }
    column "b" {
        type = int
        as = "2"
    }
    column "c" {
        type = int
        as {
            expr = "3"
            type = STORED
        }
    }
}


-- 3.hcl --
schema "$db" {}

table "users" {
    schema = schema.$db
    column "a" {
        type = int
    }
    column "b" {
        type = int
    }
    column "c" {
        type = int
    }
}

-- 3.sql --
       Table "script_column_generated.users"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 a      | integer |           | not null |
 b      | integer |           | not null |
 c      | integer |           | not null |

