1
- -- Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
2
- -- For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
3
-
1
+ -- Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
2
+ -- For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
4
3
package.path = package.path .. " ;../?.lua;../../share/?.lua;../../share/?/?.lua"
5
4
local lu = require (" luaunit" )
6
5
local sqlite = require (" lsqlite3complete" )
@@ -41,35 +40,35 @@ function test_migration_up_success()
41
40
scripts [5 ] = " ALTER TABLE test_1 ADD new_column5 TEXT;"
42
41
43
42
--- Migrate from version 0 to 1
44
- spawn_db (" test_1.sql" , 0 , test_db_schema )
45
- lu .assertEquals (migration .up (" test_1.sql" , scripts , 1 ), migration .retcode .OK )
46
- lu .assertEquals (migration .get_db_version (" test_1.sql" ), 1 )
43
+ spawn_db (" databases/ test_1.sql" , 0 , test_db_schema )
44
+ lu .assertEquals (migration .up (" databases/ test_1.sql" , scripts , 1 ), migration .retcode .OK )
45
+ lu .assertEquals (migration .get_db_version (" databases/ test_1.sql" ), 1 )
47
46
48
47
-- Migrate from version 1 to 2
49
- lu .assertEquals (migration .up (" test_1.sql" , scripts , 2 ), migration .retcode .OK )
50
- lu .assertEquals (migration .get_db_version (" test_1.sql" ), 2 )
48
+ lu .assertEquals (migration .up (" databases/ test_1.sql" , scripts , 2 ), migration .retcode .OK )
49
+ lu .assertEquals (migration .get_db_version (" databases/ test_1.sql" ), 2 )
51
50
52
51
--- Migrate from version 0 to 5
53
- spawn_db (" test_1.sql" , 0 , test_db_schema )
54
- lu .assertEquals (migration .up (" test_1.sql" , scripts , 5 ), migration .retcode .OK )
55
- lu .assertEquals (migration .get_db_version (" test_1.sql" ), 5 )
52
+ spawn_db (" databases/ test_1.sql" , 0 , test_db_schema )
53
+ lu .assertEquals (migration .up (" databases/ test_1.sql" , scripts , 5 ), migration .retcode .OK )
54
+ lu .assertEquals (migration .get_db_version (" databases/ test_1.sql" ), 5 )
56
55
end
57
56
58
57
function test_migration_up_db_errors ()
59
58
scripts = {}
60
59
61
60
--- Trigger DB error by trying to operate on empty database
62
- spawn_db (" test_1.sql" , 1 , " " )
61
+ spawn_db (" databases/ test_1.sql" , 1 , " " )
63
62
scripts [1 ] = " ALTER TABLE test_1 ADD new_column TEXT;"
64
- lu .assertError (migration .up , " test_1.sql" , scripts , 2 )
63
+ lu .assertError (migration .up , " databases/ test_1.sql" , scripts , 2 )
65
64
66
65
--- Target version set to the same value as the current DB version
67
- spawn_db (" test_1.sql" , 1 , test_db_schema )
68
- lu .assertEquals (migration .up (" test_1.sql" , scripts , 1 ), migration .retcode .ALREADY_UP_TO_DATE )
66
+ spawn_db (" databases/ test_1.sql" , 1 , test_db_schema )
67
+ lu .assertEquals (migration .up (" databases/ test_1.sql" , scripts , 1 ), migration .retcode .ALREADY_UP_TO_DATE )
69
68
70
69
--- Target version set to the lower number than the current DB version
71
- spawn_db (" test_1.sql" , 2 , test_db_schema )
72
- lu .assertEquals (migration .up (" test_1.sql" , scripts , 1 ), migration .retcode .WRONG_TARGET_VERSION )
70
+ spawn_db (" databases/ test_1.sql" , 2 , test_db_schema )
71
+ lu .assertEquals (migration .up (" databases/ test_1.sql" , scripts , 1 ), migration .retcode .WRONG_TARGET_VERSION )
73
72
end
74
73
75
74
function test_migration_down_success ()
@@ -81,35 +80,35 @@ function test_migration_down_success()
81
80
scripts [5 ] = " ALTER TABLE test_1 ADD new_column5 TEXT;"
82
81
83
82
--- Migrate from version 2 to 1
84
- spawn_db (" test_1.sql" , 2 , test_db_schema )
85
- lu .assertEquals (migration .down (" test_1.sql" , scripts , 1 ), migration .retcode .OK )
86
- lu .assertEquals (migration .get_db_version (" test_1.sql" ), 1 )
83
+ spawn_db (" databases/ test_1.sql" , 2 , test_db_schema )
84
+ lu .assertEquals (migration .down (" databases/ test_1.sql" , scripts , 1 ), migration .retcode .OK )
85
+ lu .assertEquals (migration .get_db_version (" databases/ test_1.sql" ), 1 )
87
86
88
87
-- Migrate from version 1 to 0
89
- lu .assertEquals (migration .down (" test_1.sql" , scripts , 0 ), migration .retcode .OK )
90
- lu .assertEquals (migration .get_db_version (" test_1.sql" ), 0 )
88
+ lu .assertEquals (migration .down (" databases/ test_1.sql" , scripts , 0 ), migration .retcode .OK )
89
+ lu .assertEquals (migration .get_db_version (" databases/ test_1.sql" ), 0 )
91
90
92
91
--- Migrate from version 5 to 0
93
- spawn_db (" test_1.sql" , 5 , test_db_schema )
94
- lu .assertEquals (migration .down (" test_1.sql" , scripts , 0 ), migration .retcode .OK )
95
- lu .assertEquals (migration .get_db_version (" test_1.sql" ), 0 )
92
+ spawn_db (" databases/ test_1.sql" , 5 , test_db_schema )
93
+ lu .assertEquals (migration .down (" databases/ test_1.sql" , scripts , 0 ), migration .retcode .OK )
94
+ lu .assertEquals (migration .get_db_version (" databases/ test_1.sql" ), 0 )
96
95
end
97
96
98
97
function test_migration_down_errors ()
99
98
scripts = {}
100
99
101
100
--- Trigger DB error by trying to operate on empty database
102
- spawn_db (" test_1.sql" , 2 , " " )
101
+ spawn_db (" databases/ test_1.sql" , 2 , " " )
103
102
scripts [1 ] = " ALTER TABLE test_1 ADD new_column TEXT;"
104
- lu .assertError (migration .down , " test_1.sql" , scripts , 1 )
103
+ lu .assertError (migration .down , " databases/ test_1.sql" , scripts , 1 )
105
104
106
105
--- Target version set to the same value as the current DB version
107
- spawn_db (" test_1.sql" , 1 , test_db_schema )
108
- lu .assertEquals (migration .down (" test_1.sql" , scripts , 1 ), migration .retcode .ALREADY_UP_TO_DATE )
106
+ spawn_db (" databases/ test_1.sql" , 1 , test_db_schema )
107
+ lu .assertEquals (migration .down (" databases/ test_1.sql" , scripts , 1 ), migration .retcode .ALREADY_UP_TO_DATE )
109
108
110
109
--- Target version set to the higher number than the current DB version
111
- spawn_db (" test_1.sql" , 2 , test_db_schema )
112
- lu .assertEquals (migration .down (" test_1.sql" , scripts , 3 ), migration .retcode .WRONG_TARGET_VERSION )
110
+ spawn_db (" databases/ test_1.sql" , 2 , test_db_schema )
111
+ lu .assertEquals (migration .down (" databases/ test_1.sql" , scripts , 3 ), migration .retcode .WRONG_TARGET_VERSION )
113
112
end
114
113
115
114
function test_automatic_migration ()
@@ -124,6 +123,13 @@ function test_automatic_migration()
124
123
spawn_script (" down.sql" , " migrations" , " test_2" , 1 , " delete from test_1 where _id=1;" )
125
124
spawn_script (" down.sql" , " migrations" , " test_2" , 2 , " delete from test_1 where _id=2;" )
126
125
126
+ migration .migrate (" databases" , " migrations" , {
127
+ test_1 = 0 ,
128
+ test_2 = 0
129
+ })
130
+ lu .assertEquals (migration .get_db_version (" databases/test_1.db" ), 0 )
131
+ lu .assertEquals (migration .get_db_version (" databases/test_2.db" ), 0 )
132
+
127
133
migration .migrate (" databases" , " migrations" , {
128
134
test_1 = 2 ,
129
135
test_2 = 2
0 commit comments