drop database if exists dump_autoinc_db;
drop database if exists restore_autoinc_db;

create database dump_autoinc_db with dbcompatibility = 'B';
create database restore_autoinc_db with dbcompatibility = 'B';

\c dump_autoinc_db

-- test rename schema
create schema gs_dump_test_tmp;
set search_path to gs_dump_test_tmp;
create table test(test int auto_increment primary key, id int );
create table test2(test int auto_increment primary key, id int );
create table test3(test int auto_increment primary key, id int );
vacuum full test;
alter schema gs_dump_test_tmp rename to gs_dump_test;
set search_path to gs_dump_test;
vacuum full test2;
reset search_path;

-- create auto_increment table
CREATE TABLE test_dump_autoinc_pk(col1 int auto_increment PRIMARY KEY, col2 int) AUTO_INCREMENT = 1;
INSERT INTO test_dump_autoinc_pk VALUES(0,0);
INSERT INTO test_dump_autoinc_pk VALUES(0,0);
CREATE TABLE test_dump_autoinc_null(col1 int auto_increment NULL UNIQUE, col2 int default 1) AUTO_INCREMENT = 10;
INSERT INTO test_dump_autoinc_null VALUES(0,0);
INSERT INTO test_dump_autoinc_null VALUES(0,0);
CREATE TEMPORARY TABLE test_dump_autoinc_ltmp(col1 int auto_increment UNIQUE, col2 int) AUTO_INCREMENT = 1000;
INSERT INTO test_dump_autoinc_ltmp VALUES(0,0);
INSERT INTO test_dump_autoinc_ltmp VALUES(0,0);
CREATE GLOBAL TEMPORARY TABLE test_dump_autoinc_gtmp(col1 int, col2 int  auto_increment PRIMARY KEY) AUTO_INCREMENT = 10000;
INSERT INTO test_dump_autoinc_gtmp VALUES(0,0);
INSERT INTO test_dump_autoinc_gtmp VALUES(0,0);
CREATE UNLOGGED TABLE test_dump_autoinc_unlog(col1 int auto_increment NULL UNIQUE, col2 int) AUTO_INCREMENT = 100000;
INSERT INTO test_dump_autoinc_unlog VALUES(0,0);
INSERT INTO test_dump_autoinc_unlog VALUES(0,0);
CREATE TABLE test_dump_autoinc_range_list
(
    col_1 int  auto_increment primary key,
    col_2 int  NOT NULL ,
    col_3 VARCHAR2 ( 30 ) NOT NULL ,
    col_4 int  generated always as(2*col_2) stored  ,
    check (col_4 >= col_2)
)
with(FILLFACTOR=80)
PARTITION BY RANGE (col_1) SUBPARTITION BY list (col_2)
(
  PARTITION p_range_1 VALUES LESS THAN( -10 )
  (
    SUBPARTITION p_list_1_1 VALUES ( '-1','-2','-3','-4','-5'),
    SUBPARTITION p_list_1_2 VALUES ( default )
  ),
    PARTITION p_range_2 VALUES LESS THAN( 10 )
  (
    SUBPARTITION p_list_2_1 VALUES ( '6','7','8','9','10'),
    SUBPARTITION p_list_2_2 VALUES ( '16','17','18','19','20'),
    SUBPARTITION p_list_2_3 VALUES ( '26','27','28','29','30'),
    SUBPARTITION p_list_2_4 VALUES ( default )
  ),
    PARTITION p_range_3 VALUES LESS THAN( 20 )
  (
    SUBPARTITION p_list_3_1 VALUES ( '1','2','3','4','5'),
    SUBPARTITION p_list_3_2 VALUES ( default )
  ),

   PARTITION p_range_4 VALUES LESS THAN( 30 )
   (
    SUBPARTITION p_list_4_1 VALUES ( default )
  ),
   PARTITION p_range_5 VALUES LESS THAN( 40 ),
   PARTITION p_range_6 VALUES LESS THAN( 90 )
  (
    SUBPARTITION p_list_6_1 VALUES ( '6','7','8','9','10'),
    SUBPARTITION p_list_6_2 VALUES ( '16','17','18','19','20'),
    SUBPARTITION p_list_6_3 VALUES ( '26','27','28','29','30'),
    SUBPARTITION p_list_6_4 VALUES ( default )
  ),
   PARTITION p_range_7 VALUES LESS THAN( 100)
   (
   SUBPARTITION p_list_7_1 VALUES ( '6','7','8','9','10')
   )
) ENABLE ROW MOVEMENT;
INSERT INTO test_dump_autoinc_range_list VALUES(0,1,'1');
INSERT INTO test_dump_autoinc_range_list VALUES(0,6,'6');

CREATE TABLE "TEST_DUMP_autoinc_word"("Col1" int auto_increment UNIQUE, col2 int default 1) AUTO_INCREMENT = 10;

create table "t_dump001"("c1 colmun" int not null primary key auto_increment comment '主键'); 
create table "t_dump002"("c1 \ colmun" int not null primary key auto_increment);
create table "t_dump003"("c1 ? colmun" int not null primary key auto_increment);
create table "t_dump004"("c1 \? colmun" int not null primary key auto_increment);
create table "t_dump005"("c1 \ \ colmun" int not null primary key auto_increment);
create table "t_dump006"("c1 \\\ colmun" int not null primary key auto_increment);
create table "t_dump007"("c1 &*^@~!%#$+-=/.><[{}]() colmun" int not null primary key auto_increment);
\d+ t_dump001
\d+ t_dump002
\d+ t_dump003
\d+ t_dump004
\d+ t_dump005
\d+ t_dump006
\d+ t_dump007

\! @abs_bindir@/gs_dump dump_autoinc_db -p @portstring@ -f @abs_bindir@/dump_autoinc_test.tar -F t >/dev/null 2>&1; echo $?
\! @abs_bindir@/gs_restore -d restore_autoinc_db -p @portstring@ @abs_bindir@/dump_autoinc_test.tar >/dev/null 2>&1; echo $?

\c restore_autoinc_db
-- TEST_DUMP_autoinc_word
\d "TEST_DUMP_autoinc_word"
SELECT pg_get_tabledef('"TEST_DUMP_autoinc_word"'::regclass);
-- local temp table was not dumped
\d+ test_dump_autoinc_ltmp
-- check restored tables
\d+ test_dump_autoinc_pk
INSERT INTO test_dump_autoinc_pk VALUES(0,0);
select col1,col2 from test_dump_autoinc_pk order by 1,2;

\d+ test_dump_autoinc_null
INSERT INTO test_dump_autoinc_null VALUES(0,0);
select col1,col2 from test_dump_autoinc_null order by 1,2;

\d+ test_dump_autoinc_gtmp
INSERT INTO test_dump_autoinc_gtmp VALUES(0,0);
select col1,col2 from test_dump_autoinc_gtmp order by 1,2;

\d+ test_dump_autoinc_unlog
INSERT INTO test_dump_autoinc_unlog VALUES(0,0);
select col1,col2 from test_dump_autoinc_unlog order by 1,2;

\d+ test_dump_autoinc_range_list
INSERT INTO test_dump_autoinc_range_list VALUES(0,16,'16');
select col_1,col_2,col_3,col_4 from test_dump_autoinc_range_list order by 1,2,3,4;

\d+ t_dump001
\d+ t_dump002
\d+ t_dump003
\d+ t_dump004
\d+ t_dump005
\d+ t_dump006
\d+ t_dump007

\c regression
drop database if exists restore_autoinc_db;
drop database if exists dump_autoinc_db;