File: migrate.bs

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (179 lines) | stat: -rw-r--r-- 3,746 bytes parent folder | download
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
use sql;
use test;

// Initial state:
DATABASE Migrate1 {
	TABLE test(
		id INTEGER PRIMARY KEY,
		b INTEGER(6), // Ask for an 8-byte integer
		c TEXT(32) DEFAULT "c", // Note: MySQL/MariaDB has a limit on the lenght of things that are indexed.
		d TEXT DEFAULT "d"
		);

	INDEX ON test(b);
}

// First migration:
DATABASE Migrate2 {
	TABLE test(
		id INTEGER,
		b INTEGER(6),
		c TEXT(32) ALLOW NULL,
		d TEXT UNIQUE DEFAULT "c",
		added INTEGER ALLOW NULL, // TODO: Check what happens if we don't allow null nor specify a default!
		PRIMARY KEY(id, b)
		);

	INDEX ON test(b, c);
}

// Second migration:
DATABASE Migrate3 {
	TABLE test(
		id INTEGER PRIMARY KEY,
		d TEXT,
		b INTEGER(6),
		added INTEGER ALLOW NULL
		);
}

// Versions that don't bother with modifying attributes.
DATABASE Migrate2Simple {
	TABLE test(
		id INTEGER PRIMARY KEY,
		b INTEGER(6),
		c TEXT DEFAULT "c",
		d TEXT DEFAULT "d",
		added INTEGER ALLOW NULL
		);

	INDEX ON test(b, c);
}
DATABASE Migrate3Simple {
	TABLE test(
		id INTEGER PRIMARY KEY,
		d TEXT DEFAULT "d",
		b INTEGER(6),
		added INTEGER ALLOW NULL
		);
}

Bool verify(Str expected, Str actual) {
	if (expected != actual.trimWhitespace) {
		print("Expected:\n${expected}\nGot:\n${actual}");
		return false;
	} else {
		return true;
	}
}

Bool doMigrationsI(DBConnection db) {
	MigrationPolicy policy = MigrationPolicy:allowReorder + MigrationPolicy:allowRemoval;

	// Clear out the db beforehand to make it easier to run on persistent databases.
	var prepared = db.prepare("DROP TABLE IF EXISTS test");
	prepared.execute();
	prepared.finalize();

	{
		Migrate1 t(db, policy);
		WITH t: INSERT INTO test(b) VALUES (1);
		WITH t: INSERT INTO test(b, c, d) VALUES (2, "two", "three");
	}

	if (!db.features.has(DBFeatures:limitedUpdate)) {

		{
			Migrate2 t(db, policy);
			// TODO: We should try to insert something as well.
			StrBuf out;
			for (row in WITH t: SELECT * FROM test) {
				out << row << "\n";
			}
			var expected = str{
				{ id: 1, b: 1, c: c, d: d, added: null }
				{ id: 2, b: 2, c: two, d: three, added: null }
			};
			if (!verify(expected, out.toS))
				return false;
		}

		{
			Migrate3 t(db, policy);
			StrBuf out;
			for (row in WITH t: SELECT * FROM test) {
				out << row << "\n";
			}
			var expected = str{
				{ id: 1, d: d, b: 1, added: null }
				{ id: 2, d: three, b: 2, added: null }
			};
			if (!verify(expected, out.toS))
				return false;
		}

	} else {

		{
			Migrate2Simple t(db, policy);
			StrBuf out;
			for (row in WITH t: SELECT * FROM test) {
				out << row << "\n";
			}
			var expected = str{
				{ id: 1, b: 1, c: c, d: d, added: null }
				{ id: 2, b: 2, c: two, d: three, added: null }
			};
			if (!verify(expected, out.toS))
				return false;
		}

		{
			Migrate3Simple t(db, policy);
			StrBuf out;
			for (row in WITH t: SELECT * FROM test) {
				out << row << "\n";
			}
			var expected = str{
				{ id: 1, d: d, b: 1, added: null }
				{ id: 2, d: three, b: 2, added: null }
			};
			if (!verify(expected, out.toS))
				return false;
		}

	}

	true;
}

Bool doMigrations(DBConnection db) {
	try {
		doMigrationsI(db);
	} catch (Exception e) {
		print("Error: ${e}");
		db.close();
		throw e;
	}
}

// Test on MariaDB - requires a local database, so it is not a "real" test.
void migrateMariaDB() {
	if (doMigrations(MariaDB(Host:local(), null, null, "test")))
		print("OK!");
	else
		print("Failed");
}

// Test on PostgreSQL - requires a local database, so it is not a "real" test.
void migratePostgreSQL() {
	if (doMigrations(PostgreSQL(Host:local(), null, null, "test")))
		print("OK!");
	else
		print("Failed");
}

test AutoMigrateComplex {
	// This is the only DB we can use without additional setup.
	check doMigrations(SQLite());
}