Skip to content
Snippets Groups Projects
downgrade_test.lua 30.2 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
local server = require('luatest.server')
local t = require('luatest')

local g = t.group()

g.before_each(function(cg)
    cg.server = server:new{alias = 'downgrade', box_cfg = {}}
    cg.server:start()
end)

g.after_each(function(cg)
    cg.server:drop()
end)

g.test_downgrade_versions = function(cg)
    cg.server:exec(function()
        local versions = box.schema.downgrade_versions()
        local helper = require('test.box-luatest.downgrade_helper')

        -- ascending order
        local function version_cmp(a, b)
            return helper.parse_version(a) < helper.parse_version(b)
        end

        -- Check versions are listed in descending order and indirectly test
        -- items format too.
        local sorted = table.copy(versions)
        table.sort(sorted, version_cmp)
        t.assert_equals(versions, sorted)
    end)
end

g.test_downgrade_sanity_checks = function(cg)
    cg.server:exec(function()
        -- Return schema version from _schema space in string format
        -- like '2.10.4'.
        local function schema_version()
            local t = box.space._schema:get{'version'}
            return string.format('%d.%d.%d', t[2], t[3], t[4])
        end

        local version = schema_version()
        t.assert_error_msg_contains('version should be in format A.B.C',
                                    box.schema.downgrade, 'xxx')
        t.assert_equals(schema_version(), version)
        t.assert_error_msg_contains('version should be in format A.B.C',
                                    box.schema.downgrade, '2')
        t.assert_equals(schema_version(), version)
        t.assert_error_msg_contains('version should be in format A.B.C',
                                    box.schema.downgrade, '2.2')
        t.assert_equals(schema_version(), version)
        t.assert_error_msg_contains('version should be in format A.B.C',
                                    box.schema.downgrade, '2.2.2.2')
        t.assert_equals(schema_version(), version)

        t.assert_error_msg_contains(
            'Downgrade is only possible to version listed in' ..
            ' box.schema.downgrade_versions().',
            box.schema.downgrade, '2.9.255')
        t.assert_equals(schema_version(), version)

        -- Check that after downgrade schema version is in accordance
        -- with list of existent versions.
        box.schema.downgrade("2.10.4")
        t.assert_equals(schema_version(), "2.10.4")
        box.schema.downgrade("2.10.3")
        t.assert_equals(schema_version(), "2.10.1")
        box.schema.downgrade("2.8.2")
        t.assert_equals(schema_version(), "2.7.1")

        t.assert_error_msg_contains(
            'Cannot downgrade as current schema version 2.7.1 is older' ..
            ' then schema version 2.9.1 for Tarantool 2.10.0',
            box.schema.downgrade, '2.10.0')
        t.assert_equals(schema_version(), "2.7.1")
    end)
end

g.test_downgrade_and_upgrade = function(cg)
    cg.server:exec(function()
        box.schema.downgrade("2.8.2")
        -- Snapshot is required here (and is documented as required in downgrade
        -- process). Otherwise on start we will load SQL_BUILTIN functions
        -- added by downgrade from 2.9.1 while current schema version is
        -- recent one. And this is prohibited.
        box.snapshot()
    end)
    cg.server:stop()
    cg.server:start()
end

-----------------------------
-- Check downgrade from 2.9.1
-----------------------------

-- Manual check:
--
--  Tarantool 2.11.0-entrypoint-851-gc5dc51262
--  type 'help' for interactive help
--  tarantool> box.cfg{}
--  tarantool> box.schema.downgrade('2.8.2')
--  tarantool> box.snapshot()
--  tarantool> os.exit()
--
--  Tarantool 2.8.4-0-g47e6bd362
--  tarantool> box.cfg{}
--  tarantool> \set language sql
--  tarantool> CREATE TABLE t (s INTEGER PRIMARY KEY, CHECK(LEAST(s, 10) = 10));
--   tarantool> insert into t values (1);
--  ---
--  - null
--  - 'Check constraint failed ''ck_unnamed_T_1'': LEAST(s, 10) = 10'
--  ...
--  tarantool> tarantool> insert into t values (11);
--  ---
--  - row_count: 1
--  ...
--  tarantool> os.exit()
--
-- Without restoring builtin sql function creating table fails:
--
--  tarantool> CREATE TABLE t (s INTEGER PRIMARY KEY, CHECK(LEAST(s, 10) = 10));
--  ---
--  - null
--  - 'Failed to create check constraint ''ck_unnamed_T_1'': Function ''LEAST''
--    does not exist'
--  ...
g.test_downgrade_insert_back_sql_builtin_functions = function(cg)
    cg.server:exec(function()
        local fun = require('fun')
        local helper = require('test.box-luatest.downgrade_helper')

        box.space._func:create_index('__language',
                                     {parts = {{5, 'string'}}, unique = false})
        local rows = box.space._func.index.__language:select('SQL_BUILTIN')
        t.assert_equals(#rows, 0)

        local app_version = helper.app_version('2.9.1')
        t.assert_equals(box.schema.downgrade_issues(app_version), {})
        box.schema.downgrade(app_version)
        rows = box.space._func.index.__language:select('SQL_BUILTIN')
        t.assert_equals(#rows, 0)

        local prev_version = helper.prev_version('2.9.1')
        t.assert_equals(box.schema.downgrade_issues(prev_version), {})
        rows = box.space._func.index.__language:select('SQL_BUILTIN')
        t.assert_equals(#rows, 0)

        local names_expected = {
            "TRIM", "TYPEOF", "PRINTF", "UNICODE", "CHAR", "HEX", "VERSION",
            "QUOTE", "REPLACE", "SUBSTR", "GROUP_CONCAT", "JULIANDAY", "DATE",
            "TIME", "DATETIME", "STRFTIME", "CURRENT_TIME",
            "CURRENT_TIMESTAMP", "CURRENT_DATE", "LENGTH", "POSITION", "ROUND",
            "UPPER", "LOWER", "IFNULL", "RANDOM", "CEIL", "CEILING",
            "CHARACTER_LENGTH", "CHAR_LENGTH", "FLOOR", "MOD", "OCTET_LENGTH",
            "ROW_COUNT", "COUNT", "LIKE", "ABS", "EXP", "LN", "POWER", "SQRT",
            "SUM", "TOTAL", "AVG", "RANDOMBLOB", "NULLIF", "ZEROBLOB", "MIN",
            "MAX", "COALESCE", "EVERY", "EXISTS", "EXTRACT", "SOME", "GREATER",
            "LESSER", "SOUNDEX", "LIKELIHOOD", "LIKELY", "UNLIKELY",
            "_sql_stat_get", "_sql_stat_push", "_sql_stat_init", "GREATEST",
            "LEAST",
        }

        box.schema.downgrade(prev_version)
        rows = box.space._func.index.__language:select('SQL_BUILTIN')
        local names_actual = fun.totable(fun.map(function(t) return t.name end,
                                                 rows))
        t.assert_equals(names_actual, names_expected)

        -- idempotence check
        box.schema.downgrade(prev_version)
        rows = box.space._func.index.__language:select('SQL_BUILTIN')
        local names_actual = fun.totable(fun.map(function(t) return t.name end,
                                                 rows))
        t.assert_equals(names_actual, names_expected)
    end)
end

------------------------------
-- Check downgrade from 2.10.0
------------------------------

-- Manual check:
--
--  Tarantool Enterprise 2.11.0-entrypoint-113-g803baaf
--  type 'help' for interactive help
--  tarantool> box.cfg{}
--  tarantool> box.schema.space.create('test', {
--      engine = 'vinyl',
--      defer_deletes = true,
--  })
--  tarantool> box.space._space:get{box.space.test.id}.flags
--  ---
--  - {'defer_deletes': true}
--  ...
--  tarantool> box.schema.downgrade('2.9.1')
--  tarantool> box.space._space:get{box.space.test.id}.flags
--  ---
--  - {}
--  ...
g.test_downgrade_deferred_deletes = function(cg)
    cg.server:exec(function()
        local helper = require('test.box-luatest.downgrade_helper')

        local test = box.schema.space.create('test', {engine = 'vinyl'})
        local flags = box.space._space:get{test.id}.flags
        flags.defer_deletes = true
        box.space._space:update(test.id, {{'=', 'flags', flags}})

        local app_version = helper.app_version('2.10.0')
        t.assert_equals(box.schema.downgrade_issues(app_version), {})
        box.schema.downgrade('2.10.0')
        t.assert_equals(box.space._space:get{test.id}.flags.defer_deletes, true)

        local prev_version = helper.prev_version('2.10.0')
        t.assert_equals(box.schema.downgrade_issues(prev_version), {})
        t.assert_equals(box.space._space:get{test.id}.flags.defer_deletes, true)

        box.schema.downgrade(prev_version)
        t.assert_equals(box.space._space:get{test.id}.flags.defer_deletes, nil)

        -- idempotence check
        box.schema.downgrade(prev_version)
        t.assert_equals(box.space._space:get{test.id}.flags.defer_deletes, nil)
    end)
end

-- Manual check:
--
--  Tarantool Enterprise 2.11.0-entrypoint-113-g803baaf
--  type 'help' for interactive help
--  tarantool> box.cfg{}
--  tarantool> box.schema.space.create('city')
--  tarantool> box.space.city:format{{name='payload', type='string'}}
--  tarantool> box.space.city:create_index('payload')
--  tarantool> box.space.city:replace{'moscow'}
--  tarantool> box.schema.func.create('capitalize', {
--      language = 'LUA',
--      is_deterministic = true,
--      body = [[function(row) error('boom') end]],
--  })
--  tarantool> box.space.city:upgrade{func = 'capitalize'}
--  tarantool> box.schema.downgrade('2.9.1')
--  ---
--  - error: 'builtin/box/upgrade.lua:1861: Background update is active in space
--      ''city''. It is supported starting from version 2.10.0.'
--  ...
g.test_downgrade_space_upgrade = function(cg)
    t.tarantool.skip_if_not_enterprise()
    cg.server:exec(function()
        local helper = require('test.box-luatest.downgrade_helper')

        local name = box.schema.space.create('name')
        name:format{{name='payload', type='string'}}
        name:create_index('payload')

        local city = box.schema.space.create('city')
        city:format{{name='payload', type='string'}}
        city:create_index('payload')

        name:replace{'ivan'}
        city:replace{'moscow'}

        box.schema.func.create('capitalize', {
            language = 'LUA',
            is_deterministic = true,
            body = [[function(row) error('boom') end]],
        })

        local flags = {
            upgrade = {
                func = box.func.capitalize.id,
                owner = box.info.uuid,
            },
        }
        box.space._space:update(name.id, {{'=', 'flags', flags}})
        box.space._space:update(city.id, {{'=', 'flags', flags}})

        helper.check_issues('2.10.0', {
            "Background update is active in space 'name'. " ..
            "It is supported starting from version 2.10.0.",
            "Background update is active in space 'city'. " ..
            "It is supported starting from version 2.10.0.",
        })
    end)
end

-- Manual check:
--
--  Tarantool Enterprise 2.11.0-entrypoint-113-g803baaf
--  type 'help' for interactive help
--  tarantool> box.cfg{}
--  tarantool> box.schema.space.create('word')
--  tarantool> box.space.word:format{
--      {name = 'payload', type = 'string', compression = 'zstd'}
--  }
--  tarantool> box.schema.downgrade('2.9.1')
--  ---
--  - error: 'builtin/box/upgrade.lua:1861: Tuple compression is found in space
--      ''word'', field ''payload''. It is supported starting from version
--      2.10.0.'
--  ...
g.test_downgrade_tuple_compression = function(cg)
    t.tarantool.skip_if_not_enterprise()
    cg.server:exec(function()
        local helper = require('test.box-luatest.downgrade_helper')

        local word = box.schema.space.create('word')
        local format = {
            {name = 'payload', type = 'string', compression = 'zstd'},
        }
        box.space._space:update(word.id, {{'=', 'format', format}})

        local sentence = box.schema.space.create('sentence')
        format = {
            {name = 'payload', type = 'string', compression = 'lz4'},
        }
        box.space._space:update(sentence.id, {{'=', 'format', format}})

        helper.check_issues('2.10.0', {
            "Tuple compression is found in space 'word', field 'payload'. " ..
            "It is supported starting from version 2.10.0.",
            "Tuple compression is found in space 'sentence', field" ..
            " 'payload'. It is supported starting from version 2.10.0.",
        })
    end)
end

-- Manual check:
--
--  Tarantool Enterprise 2.11.0-entrypoint-113-g803baaf
--  type 'help' for interactive help
--  tarantool> box.cfg{}
--  tarantool> box.schema.space.create('person')
--  tarantool> box.space.person:format{{name = 'id', type = 'unsigned'},
--                          {name = 'name', type = 'string'}}
--  tarantool> box.schema.space.create('participant')
--  tarantool> box.space.participant:format{
--      {name = 'id', type = 'unsigned'},
--      {
--          name = 'person_id',
--          foreign_key = {space = 'person', field = 'id'},
--      },
--  }
--  tarantool> box.schema.downgrade('2.9.1')
--  ---
--  - error: 'builtin/box/upgrade.lua:1861: Foreign key constraint is found in
--      space ''participant'', field ''person_id''. It is supported starting
--      from version 2.10.0.'
--  ...
g.test_downgrade_field_foreign_key_contraint = function(cg)
    cg.server:exec(function()
        local helper = require('test.box-luatest.downgrade_helper')

        local person = box.schema.space.create('person')
        person:format{{name = 'id', type = 'unsigned'},
                      {name = 'name', type = 'string'}}

        local participant = box.schema.space.create('participant')
        local format = {
            {name = 'id', type = 'unsigned'},
            {
                name = 'person_id',
                foreign_key = {person = {space = person.id, field = 'id'}},
            },
        }
        box.space._space:update(participant.id, {{'=', 'format', format}})

        local developer = box.schema.space.create('developer')
        box.space._space:update(developer.id, {{'=', 'format', format}})

        helper.check_issues('2.10.0', {
            "Foreign key constraint is found in space 'participant'," ..
            " field 'person_id'. It is supported starting from version 2.10.0.",
            "Foreign key constraint is found in space 'developer'," ..
            " field 'person_id'. It is supported starting from version 2.10.0.",
        })
    end)
end

-- Manual check:
--
--  Tarantool Enterprise 2.11.0-entrypoint-113-g803baaf
--  type 'help' for interactive help
--  tarantool> box.cfg{}
--  tarantool> box.schema.func.create('azimuth_normalized', {
--      language = 'LUA',
--      is_deterministic = true,
--      body = [[function(value)
--          return normalize_somehow(value)
--      end]],
--  })
--  tarantool> box.schema.space.create('movement2d')
--  tarantool> box.space.movement2d:format{
--      {name = 'id', type = 'unsigned'},
--      {name = 'distance', type = 'number'},
--      {
--          name = 'azimuth',
--          type = 'number',
--          constraint = 'azimuth_normalized',
--      },
--  }
--  ---
--  - error: 'builtin/box/upgrade.lua:1861: Field constraint is found in space
--      ''movement2d'', field ''azimuth''. It is supported starting from version
--      2.10.0.'
--  ...
g.test_downgrade_field_contraint = function(cg)
    cg.server:exec(function()
        local helper = require('test.box-luatest.downgrade_helper')

        box.schema.func.create('azimuth_normalized', {
            language = 'LUA',
            is_deterministic = true,
            body = [[function(value)
                return normalize_somehow(value)
            end]],
        })

        local movement2d = box.schema.space.create('movement2d')
        local format = {
            {name = 'id', type = 'unsigned'},
            {name = 'distance', type = 'number'},
            {
                name = 'azimuth',
                type = 'number',
                -- azimuth is in [0, 2 * PI)
                constraint = {normalized = box.func.azimuth_normalized.id}
            },
        }
        box.space._space:update(movement2d.id, {{'=', 'format', format}})

        local movement3d = box.schema.space.create('movement3d')
        format = {
            {name = 'id', type = 'unsigned'},
            {name = 'distance', type = 'number'},
            {name = 'polar', type = 'number'},
            {
                name = 'azimuth',
                type = 'number',
                -- azimuth is in [0, 2 * PI)
                constraint = {normalized = box.func.azimuth_normalized.id}
            },
        }
        box.space._space:update(movement3d.id, {{'=', 'format', format}})

        helper.check_issues('2.10.0', {
            "Field constraint is found in space 'movement2d'," ..
            " field 'azimuth'. It is supported starting from version 2.10.0.",
            "Field constraint is found in space 'movement3d'," ..
            " field 'azimuth'. It is supported starting from version 2.10.0.",
        })
    end)
end

-- Manual check:
--
--  Tarantool Enterprise 2.11.0-entrypoint-113-g803baaf
--  type 'help' for interactive help
--  tarantool> box.cfg{}
--  tarantool> box.schema.func.create('in_box', {
--      language = 'LUA',
--      is_deterministic = true,
--      body = [[function(row)
--          return row.x > 0 and row.x < 1 and row.y > 0 and row.y < 1
--      end]],
--  })
--  tarantool> box.schema.space.create('pos_in_box', {constraint = 'in_box'})
--  tarantool> box.space.pos_in_box:format{{name = 'id', type = 'unsigned'},
--                              {name = 'x', type = 'number'},
--                              {name = 'y', type = 'number'}}
--  tarantool> box.schema.downgrade('2.9.1')
--  ---
--  - error: 'builtin/box/upgrade.lua:1861: Tuple constraint is found in space
--      ''pos_in_box''. It is supported starting from version 2.10.0.'
--  ...
g.test_downgrade_tuple_contraint = function(cg)
    cg.server:exec(function()
        local helper = require('test.box-luatest.downgrade_helper')

        box.schema.func.create('in_box', {
            language = 'LUA',
            is_deterministic = true,
            body = [[function(row)
                return row.x > 0 and row.x < 1 and row.y > 0 and row.y < 1
            end]],
        })
        local pos_in_box = box.schema.space.create('pos_in_box')
        pos_in_box:format{{name = 'id', type = 'unsigned'},
                          {name = 'x', type = 'number'},
                          {name = 'y', type = 'number'}}
        local flags = {constraint = {in_box = box.func.in_box.id}}
        box.space._space:update(pos_in_box.id, {{'=', 'flags', flags}})

        box.schema.func.create('in_circle', {
            language = 'LUA',
            is_deterministic = true,
            body = [[function(row)
                return row.x * row.x + row.y * row.y < 1
            end]],
        })
        local pos_in_circle = box.schema.space.create('pos_in_circle')
        pos_in_box:format{{name = 'id', type = 'unsigned'},
                          {name = 'x', type = 'number'},
                          {name = 'y', type = 'number'}}
        local flags = {constraint = {in_circle = box.func.in_circle.id}}
        box.space._space:update(pos_in_circle.id, {{'=', 'flags', flags}})

        helper.check_issues('2.10.0', {
            "Tuple constraint is found in space 'pos_in_box'. " ..
            "It is supported starting from version 2.10.0.",
            "Tuple constraint is found in space 'pos_in_circle'. " ..
            "It is supported starting from version 2.10.0.",
        })
    end)
end

-- Check that FOREIGN KEY constraints could be downgraded.
g.test_downgrade_sql_tuple_foreign_keys = function(cg)
    cg.server:exec(function()
        local helper = require('test.box-luatest.downgrade_helper')

        box.execute([[CREATE TABLE t(i INT PRIMARY KEY, a INT, UNIQUE(a, i));]])
        box.execute([[ALTER TABLE t ADD CONSTRAINT f ]]..
                    [[FOREIGN KEY (i, a) REFERENCES t(a, i);]])

        local function foreign_key_before()
            local space = box.space._space.index.name:get{'T'}
            t.assert(space.flags.foreign_key ~= nil)
            t.assert(space.flags.foreign_key.F ~= nil)
            t.assert_equals(space.flags.foreign_key.F.space, space.id)
            t.assert_equals(space.flags.foreign_key.F.field, {[0] = 1, [1] = 0})
        end

        local function foreign_key_after()
            local space = box.space._space.index.name:get{'T'}
            t.assert(space.flags.foreign_key == nil)
            local result = box.space._fk_constraint:get{'F', space.id}
            local expected = {'F', space.id, space.id, false, 'full',
                              'no_action', 'no_action', {0, 1}, {1, 0}}
            t.assert_equals(result, expected)
        end

        foreign_key_before()

        local app_version = helper.app_version('2.10.0')
        t.assert_equals(box.schema.downgrade_issues(app_version), {})
        box.schema.downgrade(app_version)
        foreign_key_before()

        local prev_version = helper.prev_version('2.10.0')
        t.assert_equals(box.schema.downgrade_issues(prev_version), {})
        foreign_key_before()

        box.schema.downgrade(prev_version)
        foreign_key_after()

        -- idempotence check
        box.schema.downgrade(prev_version)
        foreign_key_after()
    end)
end

------------------------------
-- Check downgrade from 2.10.5
------------------------------

-- No manual check required
g.test_downgrade_vspace_sequence = function(cg)
    cg.server:exec(function()
        local helper = require('test.box-luatest.downgrade_helper')

        local function get_defs()
            return {
                box.space._space:get{box.schema.VSPACE_SEQUENCE_ID},
                box.space._index:select{box.schema.VSPACE_SEQUENCE_ID},
                grant = box.space._priv:get{
                    -- 2 is PUBLIC
                    2, 'space', box.schema.VSPACE_SEQUENCE_ID},
            }
        end

        local defs = get_defs()
        local app_version = helper.app_version('2.10.5')
        t.assert_equals(box.schema.downgrade_issues(app_version), {})
        t.assert_equals(get_defs(), defs)

        local prev_version = helper.prev_version('2.10.5')
        t.assert_equals(box.schema.downgrade_issues(prev_version), {})
        t.assert_equals(get_defs(), defs)

        local nodefs = { nil, {}, nil}
        box.schema.downgrade(prev_version)
        t.assert_equals(get_defs(), nodefs)

        -- idempotence check
        box.schema.downgrade(prev_version)
        t.assert_equals(get_defs(), nodefs)
    end)
end

------------------------------
-- Check downgrade from 2.11.0
------------------------------

-- Manual check:
--
--  Tarantool Enterprise 2.11.0-entrypoint-113-g803baaf
--  type 'help' for interactive help
--  tarantool> box.cfg{}
--  tarantool> box.schema.space.create('word')
--  tarantool> box.space.word:format{{name = 'payload',
--                                    type = 'string',
--                                    compression = 'zlib'}}
--  tarantool> box.schema.downgrade('2.10.0')
--  ---
--  - error: 'builtin/box/upgrade.lua:1861: Tuple compression with ''zlib'' algo
--      is found in space ''word'', field ''payload''. It is supported starting
--      from version 2.11.0.'
--  ...
g.test_downgrade_zlib_compression = function(cg)
    t.tarantool.skip_if_not_enterprise()
    cg.server:exec(function()
        local helper = require('test.box-luatest.downgrade_helper')

        local format = {
            {name = 'payload', type = 'string', compression = 'zlib'},
        }

        local word = box.schema.space.create('word')
        box.space._space:update(word.id, {{'=', 'format', format}})
        local sentence = box.schema.space.create('sentence')
        box.space._space:update(sentence.id, {{'=', 'format', format}})

        helper.check_issues('2.11.0', {
            "Tuple compression with 'zlib' algo is found in space 'word'," ..
            " field 'payload'. It is supported starting from version 2.11.0.",
            "Tuple compression with 'zlib' algo is found in space" ..
            " 'sentence', field 'payload'. It is supported starting from" ..
            " version 2.11.0.",
        })
    end)
end

-- Manual check:
--
--  Tarantool Enterprise 2.11.0-entrypoint-113-g803baaf
--  type 'help' for interactive help
--  tarantool> box.cfg{}
--  tarantool> box.schema.func.create('is_true', {body = 'field',
--                                                language = 'SQL_EXPR'})
--  tarantool> box.schema.downgrade('2.10.0')
--  ---
--  - error: 'builtin/box/upgrade.lua:1861: Function ''is_true'' has language
--      type SQL_EXPR. It is supported starting from version 2.11.0.'
--  ...
g.test_downgrade_sql_expr_function = function(cg)
    cg.server:exec(function()
        local helper = require('test.box-luatest.downgrade_helper')

        local datetime = os.date("%Y-%m-%d %H:%M:%S")
        local create_func = function(name, body)
            local empty = setmetatable({}, { __serialize = 'map' })
            box.space._func:auto_increment{
                1, name, 1, 'SQL_EXPR', body, 'function', {},
                'any', 'none', 'none', true, true, true, {'LUA'},
                empty, '', datetime, datetime
            }
        end

        create_func('is_a', "field == 'a'")
        create_func('is_b', "field == 'b'")

        helper.check_issues('2.11.0', {
            "Function 'is_a' has language type SQL_EXPR. " ..
            "It is supported starting from version 2.11.0.",
            "Function 'is_b' has language type SQL_EXPR. " ..
            "It is supported starting from version 2.11.0.",
        })
    end)
end

-- Manual check
--
--  Tarantool Enterprise 2.11.0-entrypoint-113-g803baaf
--  type 'help' for interactive help
--  tarantool> box.cfg{auth_type = 'pap-sha256'}
--  tarantool> box.schema.user.create('alice', {password = 'secret'})
--  tarantool> box.schema.downgrade('2.10.0')
--  ---
--  - error: 'builtin/box/upgrade.lua:1861: Auth type ''pap-sha256'' is found
--      for user ''alice''. It is supported starting from version 2.11.0.'
--  ...
g.test_downgrade_non_chap_sha1_auth = function(cg)
    cg.server:exec(function()
        local helper = require('test.box-luatest.downgrade_helper')

        -- ALICE password with some salt
        local auth = {
            ['pap-sha256'] = {
                'WOi6OgOXNUwxr0YU1sR9/rzJYnU=',
                'ZfRPND3Q0ls0hmKuzcPKBJngvDrJx6cooa8HEjj4MH0=',
            },
        }
        box.space._user:auto_increment{1, 'alice', 'user', auth, {}, os.time()}
        -- BOB password with some salt
        auth = {
            ['pap-sha256'] = {
                'FIXvGZk8bQmNztFmsCeTlpSxobk=',
                '87wjwffInpRGMBNATigbXcmKMC9sdwgplKZRjqBcdxQ=',
            },
        }
        box.space._user:auto_increment{1, 'bob', 'user', auth, {}, os.time()}

        helper.check_issues('2.11.0', {
            "Auth type 'pap-sha256' is found for user 'alice'. " ..
            "It is supported starting from version 2.11.0.",
            "Auth type 'pap-sha256' is found for user 'bob'. " ..
            "It is supported starting from version 2.11.0.",
        })
    end)
end

-- No manual check required
g.test_downgrade_user_auth_history_and_last_modified = function(cg)
    cg.server:exec(function()
        local helper = require('test.box-luatest.downgrade_helper')

        local spaces_ids = {box.space._user.id, box.space._vuser.id}
        local function check_has_new_auth_fields()
            for _, row in box.space._user:pairs() do
                t.assert_equals(#row, 7)
                t.assert_equals(row.auth_history, {})
                t.assert_equals(row.last_modified, 0)
            end
            for _, id in pairs(spaces_ids) do
                local row = box.space._space:get(id)
                t.assert_equals(row[7][6],
                                {name = 'auth_history', type = 'array'})
                t.assert_equals(row[7][7],
                                {name = 'last_modified', type = 'unsigned'})
            end
        end

        local function check_NO_new_auth_fields()
            for _, row in box.space._user:pairs() do
                t.assert_equals(#row, 5)
            end
            for _, id in pairs(spaces_ids) do
                local row = box.space._space:get(id)
                t.assert_equals(row[7][6], nil)
                t.assert_equals(row[7][7], nil)
            end
        end

        local app_version = helper.app_version('2.11.0')
        t.assert_equals(box.schema.downgrade_issues(app_version), {})
        box.schema.downgrade(app_version)
        check_has_new_auth_fields()

        local prev_version = helper.prev_version('2.11.0')
        t.assert_equals(box.schema.downgrade_issues(prev_version), {})
        check_has_new_auth_fields()

        box.schema.downgrade(prev_version)
        check_NO_new_auth_fields()

        -- idempotence check
        box.schema.downgrade(prev_version)
        check_NO_new_auth_fields()
    end)
end

-- Check that SQL CHECK constraints could be downgraded.
g.test_downgrade_sql_tuple_check_contraints = function(cg)
    cg.server:exec(function()
        local helper = require('test.box-luatest.downgrade_helper')

        box.execute([[CREATE TABLE t(i INT PRIMARY KEY, a INT);]])
        box.execute([[ALTER TABLE t ADD CONSTRAINT c CHECK (a > 10);]])

        local function constraint_before()
            local space = box.space._space.index.name:get{'T'}
            t.assert(space.flags.constraint ~= nil)
            local func_id = space.flags.constraint.C
            t.assert(func_id ~= nil)
            local func = box.space._func:get{func_id}
            t.assert(func ~= nil)
            t.assert_equals(func.body, 'a > 10')
        end

        local function constraint_after(func_id)
            local space = box.space._space.index.name:get{'T'}
            t.assert(box.space._func:get{func_id} == nil)
            t.assert(space.flags.constraint == nil)
            local result = box.space._ck_constraint:get{space.id, 'C'}
            local expected = {space.id, 'C', false, 'SQL', 'a > 10', true}
            t.assert_equals(result, expected)
        end

        constraint_before()
        local func_id = box.space._space.index.name:get{'T'}.flags.constraint.C

        local app_version = helper.app_version('2.11.0')
        t.assert_equals(box.schema.downgrade_issues(app_version), {})
        box.schema.downgrade(app_version)
        constraint_before()

        local prev_version = helper.prev_version('2.11.0')
        t.assert_equals(box.schema.downgrade_issues(prev_version), {})
        constraint_before()

        box.schema.downgrade(prev_version)
        constraint_after(func_id)

        -- idempotence check
        box.schema.downgrade(prev_version)
        constraint_after(func_id)
    end)
end