Skip to content

Commit 769c44f

Browse files
authored
File name validation (#19)
1 parent b00d4e2 commit 769c44f

File tree

6 files changed

+26
-3
lines changed

6 files changed

+26
-3
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.1",
3+
"version": "0.3.2",
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ export class Podil {
1818
const appliedScripts: Script[] = await migration.fetchAppliedMigrations()
1919
const migrationsAbsolutePath = path.resolve(migrationsDir)
2020
const files = fs.readdirSync(migrationsAbsolutePath)
21+
for (const file of files) {
22+
if (!file.toLowerCase().endsWith('.sql')) {
23+
throw new Error(
24+
`File '${file}' cannot be executed. Make sure the directory with migrations contains only '.sql' files.`
25+
)
26+
}
27+
}
2128
const fsScripts = files.sort((a, b) => a.localeCompare(b))
2229
if (fsScripts.length < appliedScripts.length) {
2330
throw new Error(`Migrations mismatch: the database has ${appliedScripts.length}` +

test/PodilTest.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,18 @@ describe('Podil', async () => {
199199
const nameFromDb = result.rows[0].test
200200
assert.strictEqual(nameFromDb, 'value from the second script')
201201
})
202+
203+
it('should fail to apply migration given wrong script extension', async () => {
204+
try {
205+
// given, when
206+
await podil.migrate(connectionString, { migrationsDir: './test/PodilTest/wrong_extensions' })
207+
assert.fail('migration is expected to fail because the second script has wrong extension')
208+
} catch (e) {
209+
// then
210+
assert.strictEqual(
211+
(e as Error).toString(),
212+
'Error: File \'002__wrong_extension.xls\' cannot be executed. Make sure the directory with migrations contains only \'.sql\' files.'
213+
)
214+
}
215+
})
202216
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE test_table (name VARCHAR(255) PRIMARY KEY);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
wrong extension

0 commit comments

Comments
 (0)