aboutsummaryrefslogtreecommitdiffstats
path: root/tests/com/iciql/test/UpgradesTest.java
blob: 7de691f6f5ca3c95fa4bd376a8ad3760bdb6b45a (plain)
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
/*
 * Copyright 2011 James Moger.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.iciql.test;

import static org.junit.Assert.assertEquals;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;

import com.iciql.Db;
import com.iciql.DbUpgrader;
import com.iciql.Iciql.IQVersion;
import com.iciql.test.models.Product;
import com.iciql.test.models.SupportedTypes;
import com.iciql.test.models.SupportedTypes.SupportedTypes2;

/**
 * Tests the database and table upgrade functions.
 * 
 */
public class UpgradesTest {

	@Test
	public void testDatabaseUpgrade() {
		Db db = IciqlSuite.openNewDb();

		List<Product> products = Product.getList();

		// set the v1 upgrader and insert a record.
		// this will trigger the upgrade.
		V1DbUpgrader v1 = new V1DbUpgrader();
		db.setDbUpgrader(v1);
		db.insert(products.get(0));

		// confirm that upgrade occurred
		assertEquals(0, v1.oldVersion.get());
		assertEquals(1, v1.newVersion.get());

		// open a second connection to the database
		// and then apply the v2 upgrade.
		// For H2 its important to keep the first connection
		// alive so that the database is not destroyed.
		Db db2 = IciqlSuite.openCurrentDb();

		// set the v2 upgrader and insert a record.
		// this will trigger the upgrade.
		V2DbUpgrader v2 = new V2DbUpgrader();
		db2.setDbUpgrader(v2);
		db2.insert(products.get(1));

		// confirm that upgrade occurred
		assertEquals(1, v2.oldVersion.get());
		assertEquals(2, v2.newVersion.get());

		db.executeUpdate("DROP TABLE iq_versions");
		db.close();
		db2.close();
	}
	
	@Test
	public void testDatabaseInheritedUpgrade() {
		Db db = IciqlSuite.openNewDb();

		List<Product> products = Product.getList();

		// set the v1 upgrader and insert a record.
		// this will trigger the upgrade.
		V1DbUpgrader v1 = new V1DbUpgrader();
		db.setDbUpgrader(v1);
		db.insert(products.get(0));

		// confirm that upgrade occurred
		assertEquals(0, v1.oldVersion.get());
		assertEquals(1, v1.newVersion.get());

		// open a second connection to the database
		// and then apply the v2 upgrade.
		// For H2 its important to keep the first connection
		// alive so that the database is not destroyed.
		Db db2 = IciqlSuite.openCurrentDb();

		// set the v2 upgrader and insert a record.
		// this will trigger the upgrade.
		V2DbUpgraderImpl v2 = new V2DbUpgraderImpl();
		db2.setDbUpgrader(v2);
		db2.insert(products.get(1));

		// confirm that upgrade occurred
		assertEquals(1, v2.oldVersion.get());
		assertEquals(2, v2.newVersion.get());

		db.executeUpdate("DROP TABLE iq_versions");
		db.close();
		db2.close();
	}

	@Test
	public void testTableUpgrade() {
		Db db = IciqlSuite.openNewDb();

		// insert first, this will create version record automatically
		List<SupportedTypes> original = SupportedTypes.createList();
		db.insertAll(original);

		// reset the dbUpgrader (clears the update check cache)
		V2DbUpgrader dbUpgrader = new V2DbUpgrader();
		db.setDbUpgrader(dbUpgrader);

		SupportedTypes2 s2 = new SupportedTypes2();

		List<SupportedTypes2> types = db.from(s2).select();
		assertEquals(10, types.size());
		assertEquals(1, dbUpgrader.oldVersion.get());
		assertEquals(2, dbUpgrader.newVersion.get());
		db.executeUpdate("DROP TABLE iq_versions");
		db.close();
	}

	/**
	 * A sample database upgrader class.
	 */
	class BaseDbUpgrader implements DbUpgrader {
		final AtomicInteger oldVersion = new AtomicInteger(0);
		final AtomicInteger newVersion = new AtomicInteger(0);

		public boolean upgradeTable(Db db, String schema, String table, int fromVersion, int toVersion) {
			// just claims success on upgrade request
			oldVersion.set(fromVersion);
			newVersion.set(toVersion);
			return true;
		}

		public boolean upgradeDatabase(Db db, int fromVersion, int toVersion) {
			// just claims success on upgrade request
			oldVersion.set(fromVersion);
			newVersion.set(toVersion);
			return true;
		}
	}

	/**
	 * A sample V1 database upgrader class.
	 */
	@IQVersion(1)
	class V1DbUpgrader extends BaseDbUpgrader {
	}

	/**
	 * A sample V2 database upgrader class.
	 */
	@IQVersion(2)
	class V2DbUpgrader extends BaseDbUpgrader {
	}

	
	/**
	 * A sample V2 database upgrader class which inherits its
	 * version from the parent class.
	 */
	@IQVersion()
	class V2DbUpgraderImpl extends V2DbUpgrader {
	}

}