Skip to content
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

for sequences, account for nulls and zero #112

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions src/SequencesProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export const setSequenceValue = async (

const seqName: string = getSequenceName(tableName, columnName);
const sql = `SELECT SETVAL(\'"${conversion._schema}"."${seqName}"\',
(SELECT MAX("${columnName}") FROM "${conversion._schema}"."${tableName}"));`;
GREATEST(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jhere1
In general - good catch!
The question is, shouldn't we set sequence value to zero if table is empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the nextval would have been 2. Setting sequence value to 0 would involve changing the sequence definition min value. Take a look at my latest commit. Think this is cleaner. it does what we want (have nextval be 1) by saying the value of 1 is not used yet.

COALESCE((SELECT MAX("${columnName}") FROM "${conversion._schema}"."${tableName}"), 0)
, 1), false
);`;

const params: DBAccessQueryParams = {
conversion: conversion,
Expand Down Expand Up @@ -140,7 +143,10 @@ export const createIdentity = async (conversion: Conversion, tableName: string):
params.client = createSequenceResult.client;
params.shouldReturnClient = false;
params.sql = `SELECT SETVAL(\'"${conversion._schema}"."${seqName}"\',
(SELECT MAX("${columnName}") FROM ${fullTableName}));`;
GREATEST(
COALESCE((SELECT MAX("${columnName}") FROM ${fullTableName}), 0)
, 1), false
);`;

const sqlSetSequenceValueResult: DBAccessQueryResult = await DBAccess.query(params);

Expand Down