-
Notifications
You must be signed in to change notification settings - Fork 690
Fix SERIAL columns to respect explicitly provided values including NULL #2204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ffc5eab
Initial plan
Copilot 5a8c5bf
test: Add test895.js to demonstrate SERIAL auto-increment issue
Copilot a160959
fix: Allow SERIAL columns to accept explicit values instead of always…
Copilot ec87d46
refactor: Address PR feedback - add NULL check, type coercion, arrow …
Copilot e8bb2aa
refactor: Rename test variable to testId and use meaningful table names
Copilot d2bb26d
Merge branch 'develop' into copilot/fix-serial-id-overwrite-issue
mathiasrw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| if (typeof exports === 'object') { | ||
| var assert = require('assert'); | ||
| var alasql = require('..'); | ||
| } | ||
|
|
||
| describe('Test 895 - SERIAL type should not overwrite explicitly provided values', function () { | ||
| const test = '895'; | ||
|
|
||
| before(() => { | ||
| alasql('create database test' + test); | ||
| alasql('use test' + test); | ||
| }); | ||
|
|
||
| after(() => { | ||
| alasql('drop database test' + test); | ||
| }); | ||
|
|
||
| it('A) SERIAL column should auto-increment when not provided', () => { | ||
| alasql(` | ||
| CREATE TABLE test_serial (id serial, name varchar(50)); | ||
mathiasrw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| INSERT INTO test_serial (name) VALUES ("first"); | ||
| INSERT INTO test_serial (name) VALUES ("second"); | ||
| INSERT INTO test_serial (name) VALUES ("third"); | ||
| `); | ||
| var res = alasql('SELECT * FROM test_serial ORDER BY id'); | ||
| assert.deepEqual(res, [ | ||
| {id: 1, name: 'first'}, | ||
| {id: 2, name: 'second'}, | ||
| {id: 3, name: 'third'}, | ||
| ]); | ||
| }); | ||
|
|
||
| it('B) SERIAL column should accept explicitly provided value', () => { | ||
| alasql(` | ||
| CREATE TABLE test_serial2 (id serial, name varchar(50)); | ||
| INSERT INTO test_serial2 (id, name) VALUES (10, "first"); | ||
| INSERT INTO test_serial2 (id, name) VALUES (20, "second"); | ||
| INSERT INTO test_serial2 (id, name) VALUES (30, "third"); | ||
| `); | ||
| var res = alasql('SELECT * FROM test_serial2 ORDER BY id'); | ||
| assert.deepEqual(res, [ | ||
| {id: 10, name: 'first'}, | ||
| {id: 20, name: 'second'}, | ||
| {id: 30, name: 'third'}, | ||
| ]); | ||
| }); | ||
|
|
||
| it('C) SERIAL column should accept explicitly provided value even if lower than counter', () => { | ||
| alasql(` | ||
| CREATE TABLE test_serial3 (id serial, name varchar(50)); | ||
| INSERT INTO test_serial3 (name) VALUES ("auto1"); | ||
| INSERT INTO test_serial3 (name) VALUES ("auto2"); | ||
| INSERT INTO test_serial3 (name) VALUES ("auto3"); | ||
| INSERT INTO test_serial3 (id, name) VALUES (100, "explicit"); | ||
| `); | ||
| var res = alasql('SELECT * FROM test_serial3 ORDER BY id'); | ||
| assert.deepEqual(res, [ | ||
| {id: 1, name: 'auto1'}, | ||
| {id: 2, name: 'auto2'}, | ||
| {id: 3, name: 'auto3'}, | ||
| {id: 100, name: 'explicit'}, | ||
| ]); | ||
| }); | ||
|
|
||
| it('D) SERIAL with mixed auto and explicit values', () => { | ||
| alasql(` | ||
| CREATE TABLE test_serial4 (id serial, name varchar(50)); | ||
| INSERT INTO test_serial4 (id, name) VALUES (5, "explicit5"); | ||
| INSERT INTO test_serial4 (name) VALUES ("auto"); | ||
| INSERT INTO test_serial4 (id, name) VALUES (10, "explicit10"); | ||
| INSERT INTO test_serial4 (name) VALUES ("auto2"); | ||
| `); | ||
| var res = alasql('SELECT * FROM test_serial4 ORDER BY id'); | ||
| assert.deepEqual(res, [ | ||
| {id: 5, name: 'explicit5'}, | ||
| {id: 6, name: 'auto'}, | ||
| {id: 10, name: 'explicit10'}, | ||
| {id: 11, name: 'auto2'}, | ||
| ]); | ||
| }); | ||
|
|
||
| it('E) Bulk insert with explicit SERIAL values', () => { | ||
| alasql(` | ||
| CREATE TABLE test_serial5 (id serial, name varchar(50)); | ||
| INSERT INTO test_serial5 (id, name) VALUES (4, "item4"), (8, "item8"), (12, "item12"); | ||
| `); | ||
| var res = alasql('SELECT * FROM test_serial5 ORDER BY id'); | ||
| assert.deepEqual(res, [ | ||
| {id: 4, name: 'item4'}, | ||
| {id: 8, name: 'item8'}, | ||
| {id: 12, name: 'item12'}, | ||
| ]); | ||
| }); | ||
|
|
||
| it('F) Re-inserting data after truncate with explicit IDs', () => { | ||
| alasql(` | ||
| CREATE TABLE test_serial6 (id serial, name varchar(50)); | ||
| INSERT INTO test_serial6 (name) VALUES ("first"), ("second"), ("third"); | ||
| `); | ||
| var res1 = alasql('SELECT * FROM test_serial6 ORDER BY id'); | ||
| assert.deepEqual(res1, [ | ||
| {id: 1, name: 'first'}, | ||
| {id: 2, name: 'second'}, | ||
| {id: 3, name: 'third'}, | ||
| ]); | ||
|
|
||
| // Simulate the flush scenario from the issue | ||
| alasql(` | ||
| DELETE FROM test_serial6; | ||
| INSERT INTO test_serial6 (id, name) VALUES (4, "item4"); | ||
| INSERT INTO test_serial6 (id, name) VALUES (7, "item7"); | ||
| INSERT INTO test_serial6 (id, name) VALUES (9, "item9"); | ||
| `); | ||
|
|
||
| var res2 = alasql('SELECT * FROM test_serial6 ORDER BY id'); | ||
| assert.deepEqual(res2, [ | ||
| {id: 4, name: 'item4'}, | ||
| {id: 7, name: 'item7'}, | ||
| {id: 9, name: 'item9'}, | ||
| ]); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.