test_run = require('test_run').new()
engine = test_run:get_cfg('engine')
_ = box.space._session_settings:update('sql_default_engine', {{'=', 2, engine}})

work_dir = 'sql/upgrade/1.10/'
test_run:cmd('create server upgrade with script="sql/upgrade/upgrade.lua", workdir="' .. work_dir .. '"')
test_run:cmd('start server upgrade')

test_run:switch('upgrade')
box.schema.upgrade()
box.execute([[SET SESSION "sql_seq_scan" = true;]])

-- test system tables
box.space._space.index['name']:get('_trigger')

box.space._index:get({box.space._space.index['name']:get('_trigger').id, 0})

box.space._schema:format()

-- test data migration
box.space._space.index['name']:get('T1')
box.space._index:get({box.space._space.index['name']:get('T1').id, 0})

-- test system tables functionality
box.execute("CREATE TABLE T(X INTEGER PRIMARY KEY);")
box.execute("CREATE TABLE T_OUT(X INTEGER PRIMARY KEY);")
box.execute("CREATE TRIGGER T1T AFTER INSERT ON T FOR EACH ROW BEGIN INSERT INTO T_OUT VALUES(1); END;")
box.execute("CREATE TRIGGER T2T AFTER INSERT ON T FOR EACH ROW BEGIN INSERT INTO T_OUT VALUES(2); END;")
box.space._space.index['name']:get('T')
box.space._space.index['name']:get('T_OUT')
t1t = box.space._trigger:get('T1T')
t2t = box.space._trigger:get('T2T')
t1t.name
t1t.opts
t2t.name
t2t.opts
assert(t1t.space_id == t2t.space_id)
assert(t1t.space_id == box.space.T.id)

box.execute("INSERT INTO T VALUES(1);")
box.space.T:select()
box.space.T_OUT:select()
box.execute("SELECT * FROM T")
box.execute("SELECT * FROM T")


box.execute("DROP TABLE T;")
box.execute("DROP TABLE T_OUT;")


test_run:switch('default')
test_run:cmd('stop server upgrade')
test_run:cmd('cleanup server upgrade')

-- Test Tarantool 2.1.0 to 2.1.1 migration.
work_dir = 'sql/upgrade/2.1.0/'
test_run:cmd('create server upgrade210 with script="sql/upgrade/upgrade.lua", workdir="' .. work_dir .. '"')
test_run:cmd('start server upgrade210')

test_run:switch('upgrade210')
box.schema.upgrade()

s = box.space.T5
s ~= nil
i = box.space._index:select(s.id)
i ~= nil
i[1].opts.sql == nil
box.space._space:get(s.id).flags.checks == nil
name, func_id = next(box.space[s.id].constraint)
name ~= nil
name
s:drop()
box.space._func:delete(func_id).body

test_run:switch('default')
test_run:cmd('stop server upgrade210')
test_run:cmd('cleanup server upgrade210')
