Skip to content

Commit 7631392

Browse files
Dropping test database automatically
1 parent 7d6c0f1 commit 7631392

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
# chinook database is used in non-destructive tests
33
CHINOOK_DATABASE_URL="sqlitecloud://user:password@xxx.sqlite.cloud:8860/chinook.db"
44

5-
# testing database is used in destructive tests
5+
# testing databases are also created automatically as testing-xxx.db and used in destructive tests
66
TESTING_DATABASE_URL="sqlitecloud://user:password@xxx.sqlite.cloud:8860/testing.db"

test/connection.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ import {
1313
getChinookConnection,
1414
WARN_SPEED_MS,
1515
EXPECT_SPEED_MS,
16-
EXTRA_LONG_TIMEOUT
16+
clearTestingDatabasesAsync
1717
} from './shared'
1818

1919
describe('connection', () => {
2020
let chinook: SQLiteCloudConnection
2121

22+
beforeAll(async () => {
23+
await clearTestingDatabasesAsync()
24+
})
25+
2226
beforeEach(() => {
2327
chinook = getChinookConnection()
2428
})

test/shared.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,26 @@ export async function getTestingDatabaseAsync(): Promise<Database> {
145145
return database
146146
}
147147

148+
/** Drop databases that are no longer in use */
149+
export async function clearTestingDatabasesAsync() {
150+
const chinook = getChinookDatabase()
151+
152+
let numDeleted = 0
153+
let databases = await chinook.sql`LIST DATABASES;`
154+
const testingPattern = /^testing-\d{16}\.db$/
155+
for (let i = 0; i < databases.length; i++) {
156+
const databaseName = databases[i]['name']
157+
if (testingPattern.test(databaseName)) {
158+
const result = await chinook.sql`REMOVE DATABASE ${databaseName};`
159+
console.assert(result)
160+
numDeleted++
161+
}
162+
}
163+
if (numDeleted > 0) {
164+
console.log(`Deleted ${numDeleted} testing databases`)
165+
}
166+
}
167+
148168
//
149169
// more utilities
150170
//

0 commit comments

Comments
 (0)