Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.2.0

- Quote column names to prevent SQL syntax errors

## 1.1.1

- Fix issue with built package of v1.1.0
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "data-bakery",
"version": "1.1.1",
"version": "1.2.0",
"description": "A tool to prepare test SQL data for integration tests",
"main": "./src/recipe-helper.js",
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion src/utils/sql-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ export function sqlForRecipeBundle (recipeBundle: RecipeBundle, config: SQLGener
return `(${newValueList.join(', ')})`
})

sqlQueries.push(`INSERT INTO \`${tableName}\` (${columnMapKey}) VALUES ${valueLists.join(', ')};`)
const rawColumnNames = columnMapKey.split(',')
const quotedColumnNames = '`' + rawColumnNames.join('`, `') + '`'

sqlQueries.push(`INSERT INTO \`${tableName}\` (${quotedColumnNames}) VALUES ${valueLists.join(', ')};`)
})
})
})
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "data-bakery-snapshot-tests",
"version": "1.0.0",
"dependencies": {
"data-bakery": "file:../../data-bakery-1.1.1.tgz"
"data-bakery": "file:../../data-bakery-1.2.0.tgz"
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
INSERT INTO `user` (id,email) VALUES (1, 'hello@world.com');
INSERT INTO `user` (`id`, `email`) VALUES (1, 'hello@world.com');
Original file line number Diff line number Diff line change
@@ -1 +1 @@
INSERT INTO `user` (id,email) VALUES (2, 'another-hello@world.com');
INSERT INTO `user` (`id`, `email`) VALUES (2, 'another-hello@world.com');
24 changes: 12 additions & 12 deletions tests/snapshot/tests/databakeryignore/actual-output/00-schema.sql
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
CREATE DATABASE IF NOT EXISTS data_bakery_test;
USE data_bakery_test;
CREATE DATABASE IF NOT EXISTS `data_bakery_test`;
USE `data_bakery_test`;

DROP TABLE IF EXISTS users;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
firstName VARCHAR(255),
email VARCHAR(255)
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`firstName` VARCHAR(255),
`email` VARCHAR(255)
);

DROP TABLE IF EXISTS orders;
CREATE TABLE orders (
id INT AUTO_INCREMENT PRIMARY KEY,
userId INT,
amount INT
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`userId` INT,
`amount` INT
);
24 changes: 12 additions & 12 deletions tests/snapshot/tests/databakeryignore/expected-output/00-schema.sql
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
CREATE DATABASE IF NOT EXISTS data_bakery_test;
USE data_bakery_test;
CREATE DATABASE IF NOT EXISTS `data_bakery_test`;
USE `data_bakery_test`;

DROP TABLE IF EXISTS users;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
firstName VARCHAR(255),
email VARCHAR(255)
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`firstName` VARCHAR(255),
`email` VARCHAR(255)
);

DROP TABLE IF EXISTS orders;
CREATE TABLE orders (
id INT AUTO_INCREMENT PRIMARY KEY,
userId INT,
amount INT
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`userId` INT,
`amount` INT
);
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
INSERT INTO `user` (id,email,firstName) VALUES (1, 'hi@there.com', 'Eric');
INSERT INTO `user` (id,email) VALUES (2, 'hello@world.com');
INSERT INTO `orders` (id,userId,amount) VALUES (100, 2, 5000), (101, 2, 15000);
INSERT INTO `user` (`id`, `email`, `firstName`) VALUES (1, 'hi@there.com', 'Eric');
INSERT INTO `user` (`id`, `email`) VALUES (2, 'hello@world.com');
INSERT INTO `orders` (`id`, `userId`, `amount`) VALUES (100, 2, 5000), (101, 2, 15000);
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
INSERT INTO `user` (id,email) VALUES (1, 'hello@world.com');
INSERT INTO `user_extra` (id,json) VALUES (1, '{"generatedId":"1-static-prefix"}');
INSERT INTO `user` (`id`, `email`) VALUES (1, 'hello@world.com');
INSERT INTO `user_extra` (`id`, `json`) VALUES (1, '{"generatedId":"1-static-prefix"}');
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
INSERT INTO `user` (id,email,firstName) VALUES (1, 'hi@there.com', 'Eric');
INSERT INTO `user` (id,email) VALUES (2, 'hello@world.com');
INSERT INTO `orders` (id,userId,amount) VALUES (100, 2, 5000), (101, 2, 15000);
INSERT INTO `user` (`id`, `email`, `firstName`) VALUES (1, 'hi@there.com', 'Eric');
INSERT INTO `user` (`id`, `email`) VALUES (2, 'hello@world.com');
INSERT INTO `orders` (`id`, `userId`, `amount`) VALUES (100, 2, 5000), (101, 2, 15000);
Original file line number Diff line number Diff line change
@@ -1 +1 @@
INSERT INTO `user` (extra1,extra2,id,email) VALUES ('column-value-here', 9999, 450, 'hello@world.com');
INSERT INTO `user` (`extra1`, `extra2`, `id`, `email`) VALUES ('column-value-here', 9999, 450, 'hello@world.com');
Original file line number Diff line number Diff line change
@@ -1 +1 @@
INSERT INTO `user` (extra1,extra2,id,email) VALUES ('column-value-here', 9999, 451, 'another-hello@world.com');
INSERT INTO `user` (`extra1`, `extra2`, `id`, `email`) VALUES ('column-value-here', 9999, 451, 'another-hello@world.com');
12 changes: 6 additions & 6 deletions tests/unit/utils/sql-generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('sql-generator', () => {
],
})

expect(sql).toBe("INSERT INTO `user` (id,name) VALUES (1, 'Peter'), (2, 'Paul');\n")
expect(sql).toBe("INSERT INTO `user` (`id`, `name`) VALUES (1, 'Peter'), (2, 'Paul');\n")
})
it('outputs batched sql up to batchSize', () => {
const sql = sqlForRecipeBundle({
Expand All @@ -22,7 +22,7 @@ describe('sql-generator', () => {
],
}, { batchSize: 2 })

expect(sql).toBe("INSERT INTO `user` (id,name) VALUES (1, 'Peter'), (2, 'Paul');\nINSERT INTO `user` (id,name) VALUES (3, 'Testing');\n")
expect(sql).toBe("INSERT INTO `user` (`id`, `name`) VALUES (1, 'Peter'), (2, 'Paul');\nINSERT INTO `user` (`id`, `name`) VALUES (3, 'Testing');\n")
})
it('avoids batching different column groups', () => {
const sql = sqlForRecipeBundle({
Expand All @@ -32,7 +32,7 @@ describe('sql-generator', () => {
],
})

expect(sql).toBe("INSERT INTO `user` (id,name) VALUES (1, 'Peter');\nINSERT INTO `user` (id,name,extraColumn) VALUES (2, 'Paul', 'something');\n")
expect(sql).toBe("INSERT INTO `user` (`id`, `name`) VALUES (1, 'Peter');\nINSERT INTO `user` (`id`, `name`, `extraColumn`) VALUES (2, 'Paul', 'something');\n")
})
it('outputs escaped sql', () => {
const sql = sqlForRecipeBundle({
Expand All @@ -41,7 +41,7 @@ describe('sql-generator', () => {
],
})

expect(sql).toBe("INSERT INTO `user` (id,name,extra) VALUES (1, 'Bobby''s tables''''\\', 'It''s all \"groovy\"');\n")
expect(sql).toBe("INSERT INTO `user` (`id`, `name`, `extra`) VALUES (1, 'Bobby''s tables''''\\', 'It''s all \"groovy\"');\n")
})
it('handles NULL data values', () => {
const sql = sqlForRecipeBundle({
Expand All @@ -50,7 +50,7 @@ describe('sql-generator', () => {
],
})

expect(sql).toBe('INSERT INTO `user` (id,name) VALUES (1, NULL);\n')
expect(sql).toBe('INSERT INTO `user` (`id`, `name`) VALUES (1, NULL);\n')
})
it('handles RawSQL values', () => {
const sql = sqlForRecipeBundle({
Expand All @@ -59,7 +59,7 @@ describe('sql-generator', () => {
],
})

expect(sql).toBe('INSERT INTO `user` (id,name,created) VALUES (1, NULL, CURDATE());\n')
expect(sql).toBe('INSERT INTO `user` (`id`, `name`, `created`) VALUES (1, NULL, CURDATE());\n')
})
})

Expand Down
Loading