Skip to content

Commit d93f096

Browse files
#112 negative argument as IDENTITY (#149)
* support IDENTITY with negative arguments; detect end of table more generically * Add tests * rebase patch by potrusil-osi Co-authored-by: Tomas Potrusil <[email protected]>
1 parent 11f82db commit d93f096

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

contributors

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ bsacks99
1616
keyjote
1717
mark-jay
1818
mikes-gh
19+
postrusil-osi
1920
sebpcspkr
2021
stuey1978
2122

regression/issue_112.sql

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
CREATE TABLE [dbo].[AFElementAttributeCategory](
2+
[rid] [bigint] IDENTITY(-1,-1) NOT NULL,
3+
[id] [uniqueidentifier] NOT NULL,
4+
[rowversion] [timestamp] NOT NULL,
5+
[fkelementversionid] [bigint] NOT NULL,
6+
[fkparentattributeid] [uniqueidentifier] NULL,
7+
[fkcategoryid] [uniqueidentifier] NOT NULL,
8+
[changedby] [int] NOT NULL,
9+
CONSTRAINT [PK_AFElementAttributeCategory] PRIMARY KEY CLUSTERED
10+
(
11+
[rid] ASC
12+
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [ASSETS]
13+
) ON [ASSETS]
14+
GO
15+
16+
CREATE TABLE [dbo].[AFCaseAdjustment](
17+
[rid] [bigint] IDENTITY(-1,-1) NOT NULL,
18+
[id] [uniqueidentifier] NOT NULL,
19+
[rowversion] [timestamp] NOT NULL,
20+
[fkcaseid] [bigint] NOT NULL,
21+
[attributeid] [uniqueidentifier] NOT NULL,
22+
[adjustedvalue] [varbinary](max) NULL,
23+
[comment] [nvarchar](1000) NULL,
24+
[previousvalue] [varbinary](max) NULL,
25+
[creator] [nvarchar](50) NULL,
26+
[creationdate] [datetime2](7) NULL,
27+
[changedby] [int] NOT NULL,
28+
CONSTRAINT [PK_AFCaseAdjustment] PRIMARY KEY NONCLUSTERED
29+
(
30+
[rid] ASC
31+
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [ANALYSIS]
32+
) ON [ANALYSIS] TEXTIMAGE_ON [ANALYSIS]
33+
GO
34+
35+
CREATE TABLE [dbo].[sd](
36+
[rid] [int] IDENTITY(1000,1) NOT NULL,
37+
[rowversion] [timestamp] NOT NULL,
38+
[sd] [nvarchar](max) NOT NULL,
39+
[ownerRights] [int] NOT NULL,
40+
[lupd] [datetime2](7) NULL,
41+
CONSTRAINT [pk_sd] PRIMARY KEY CLUSTERED
42+
(
43+
[rid] ASC
44+
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [ASSETS]
45+
) ON [ASSETS] TEXTIMAGE_ON [ASSETS]
46+
GO

sqlserver2pgsql.pl

+4-6
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,7 @@ sub add_column_to_table
13481348

13491349
# We have an identity field. We remember the default value and
13501350
# initialize the sequence correctly in the after script
1351-
$isidentity =~ /IDENTITY\s*\((\d+),\s*(\d+)\)/
1351+
$isidentity =~ /IDENTITY\s*\((-?\d+),\s*(-?\d+)\)/
13521352
or die "Cannot understand <$isidentity>";
13531353
my $startseq = $1;
13541354
my $stepseq = $2;
@@ -1365,8 +1365,6 @@ sub add_column_to_table
13651365

13661366
$objects->{SCHEMAS}->{$schemaname}->{SEQUENCES}->{$seqname}->{START}
13671367
= $startseq;
1368-
$objects->{SCHEMAS}->{$schemaname}->{SEQUENCES}->{$seqname}->{MIN}
1369-
= $startseq;
13701368
$objects->{SCHEMAS}->{$schemaname}->{SEQUENCES}->{$seqname}->{STEP}
13711369
= $stepseq;
13721370
$objects->{SCHEMAS}->{$schemaname}->{SEQUENCES}->{$seqname}
@@ -1449,7 +1447,7 @@ sub parse_dump
14491447
# column name, typical microsoft stuff :( )
14501448
# To make matters even worse, they seem to systematically add a space after it :)
14511449
if ($line =~
1452-
/^\s+\[(.*)\]\s*(?:\[(.*)\]\.)?\[(.*)\]\s*(\(.+?\))?(?: COLLATE (\S+))?( IDENTITY\s*\(\d+,\s*\d+\))?(?: ROWGUIDCOL ?)? (?:NOT FOR REPLICATION )?(?:SPARSE +)?(NOT NULL|NULL)(?:\s+CONSTRAINT \[.*\])?(?:\s+DEFAULT \((.*)\))?(?:,|$)?/
1450+
/^\s+\[(.*)\]\s*(?:\[(.*)\]\.)?\[(.*)\]\s*(\(.+?\))?(?: COLLATE (\S+))?( IDENTITY\s*\(-?\d+,\s*-?\d+\))?(?: ROWGUIDCOL ?)? (?:NOT FOR REPLICATION )?(?:SPARSE +)?(NOT NULL|NULL)(?:\s+CONSTRAINT \[.*\])?(?:\s+DEFAULT \((.*)\))?(?:,|$)?/
14531451
)
14541452
{
14551453
# Deported into a function because we can also meet alter table add columns on their own
@@ -1983,7 +1981,7 @@ sub parse_dump
19831981
# Added table columns… this seems to appear in SQL Server when some columns have ANSI padding, and some not.
19841982
# PG follows ANSI, that is not an option. The end of the regexp is pasted from the create table
19851983
elsif ($line =~
1986-
/^ALTER TABLE \[(.*)\]\.\[(.*)\] ADD \[(.*)\] (?:\[(.*)\]\.)?\[(.*)\](\(.+?\))?( IDENTITY\(\d+,\s*\d+\))? (NOT NULL|NULL)(?: CONSTRAINT \[.*\] )?(?: DEFAULT \(.*\))?$/
1984+
/^ALTER TABLE \[(.*)\]\.\[(.*)\] ADD \[(.*)\] (?:\[(.*)\]\.)?\[(.*)\](\(.+?\))?( IDENTITY\(-?\d+,\s*-?\d+\))? (NOT NULL|NULL)(?: CONSTRAINT \[.*\] )?(?: DEFAULT \(.*\))?$/
19871985
)
19881986
{
19891987
my $schemaname=relabel_schemas($1);
@@ -2883,7 +2881,7 @@ sub generate_schema
28832881
# This may not be an identity. Skip it then
28842882
next unless defined ($seqref->{OWNERCOL});
28852883
print AFTER "select setval('" . format_identifier($schema) . '.'
2886-
. format_identifier($sequence) . "',(select max("
2884+
. format_identifier($sequence) . "',(select " . ($seqref->{STEP} > 0 ? "max" : "min") . "("
28872885
. format_identifier($seqref->{OWNERCOL}) .") from "
28882886
. format_identifier($seqref->{OWNERSCHEMA}) . '.'
28892887
. format_identifier($seqref->{OWNERTABLE}) . ")::bigint);\n";

0 commit comments

Comments
 (0)