Skip to content

Commit 4a277fb

Browse files
authored
Bugfix: verifying checkusm reapplies a migration (#17)
1 parent 35cd92a commit 4a277fb

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "podil",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "Lightweight and secure database migration tool",
55
"main": "dist/cjs/index.js",
66
"module": "dist/esm/index.js",

src/Podil.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export class Podil {
3232
}
3333
if (verifyChecksum) {
3434
const migrationScript = fs.readFileSync(path.join(migrationsAbsolutePath, fsScriptName)).toString()
35-
await migration.executeSql(migrationScript)
3635
const checksum = this.calculateCheckSum(migrationScript)
3736
if (checksum !== appliedScript.checksum) {
3837
throw new Error(`Checksum mismatch: File ${fsScriptName} is expected to have checksum ` +

test/PodilTest.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ describe('Podil', async () => {
3636
assert.strictEqual(nameFromDb, 'test data')
3737
})
3838

39+
it('should apply migration only once', async () => {
40+
// given
41+
await podil.migrate(connectionString, { migrationsDir: './test/PodilTest/single_migration' })
42+
43+
// when running the second time nothing should happen
44+
await podil.migrate(connectionString, { migrationsDir: './test/PodilTest/single_migration' })
45+
46+
// then
47+
const result = await client.query('SELECT name FROM test_table')
48+
const nameFromDb = result.rows[0].name
49+
assert.strictEqual(nameFromDb, 'test data')
50+
})
51+
3952
it('should apply multiple migrations', async () => {
4053
// when
4154
await podil.migrate(connectionString, { migrationsDir: './test/PodilTest/two_migrations' })
@@ -56,6 +69,8 @@ describe('Podil', async () => {
5669
checksum CHAR(64) NOT NULL
5770
)`
5871
)
72+
await client.query('CREATE TABLE test_table (name VARCHAR(255) PRIMARY KEY)')
73+
await client.query('INSERT INTO test_table values (\'test data\')')
5974
await client.query(
6075
'INSERT INTO podil_migrations(name, checksum) VALUES($1, $2)',
6176
['01__init.sql', '06ea2d756243a1e96ebf74d971812d3fe678b6888108bc44b929ae0e560f0924']
@@ -65,9 +80,9 @@ describe('Podil', async () => {
6580
await podil.migrate(connectionString, { migrationsDir: './test/PodilTest/two_migrations' })
6681

6782
// then
68-
const result = await client.query('SELECT name FROM test_table')
69-
const nameFromDb = result.rows[0].name
70-
assert.strictEqual(nameFromDb, 'test data')
83+
const result = await client.query('SELECT test FROM test_table')
84+
const nameFromDb = result.rows[0].test
85+
assert.strictEqual(nameFromDb, 'value from the second script')
7186
})
7287

7388
it('should fail to apply migration when checksum check fails', async () => {

0 commit comments

Comments
 (0)