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

Fix: Automation script of merging ALTER PK to CREATE DDL in tests #1657

Closed
wants to merge 9 commits into from
67 changes: 67 additions & 0 deletions migtests/scripts/add-pk-from-alter-to-create
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python3


import os
import re
import shutil

table_file_path = os.getenv('TEST_DIR')+'/export-dir/schema/tables/table.sql'

#copy the table_file_path to table_file_path+'.old'
src = table_file_path
dest = table_file_path + '.old'

shutil.copy(src,dest)

with open(table_file_path, 'r') as file:
lines = file.readlines()

create_tables = []
alter_tables = []
create_table_map = {}
alter_table_map = {}
for i, line in enumerate(lines):
if line.startswith('CREATE TABLE'):
match = re.search(r'CREATE TABLE (.*) ', line.split('(')[0])
if match:
table_name = match.group(1)
create_table_map[table_name] = line
create_tables.append(line)
elif line.strip(' ').startswith('ADD CONSTRAINT') and line.strip(' ').find('PRIMARY KEY') != -1 and lines[i-1].startswith('ALTER TABLE'):
match = re.search(r'ALTER TABLE ONLY (.*)', lines[i-1])
if match:
table_name = match.group(1)
alter_table_map[table_name] = lines[i]
alter_tables.append(lines[i])


output = []

for k,v in create_table_map.items():
# Extract the primary key constraint from the ALTER TABLE statement
if k not in alter_table_map:
output.append(v)
continue
match = re.search(r'ADD CONSTRAINT (.*) PRIMARY KEY \((.*)\);', alter_table_map[k])
if match:
constraint = match.group(1)
columns = match.group(2)

# Add the primary key constraint to the CREATE TABLE statement
if "WITH" in v:
create_table = v.replace(') WITH', ',' + 'PRIMARY KEY (' + columns + ')) WITH')
else:
create_table = v + ' PRIMARY KEY (' + columns + '),\n'
output.append(create_table)

with open(table_file_path, 'w') as file:
# modify create_table lines to output ones
for i, line in enumerate(lines):
if line in create_tables:
file.write(output.pop(0))
elif line in alter_tables and line.find('PRIMARY KEY') != -1: # remove the ADD CONTRAINT lines with PK only
pass
elif line.startswith('ALTER TABLE') and (i < len(lines)-1 and lines[i+1].find('PRIMARY KEY') != -1): # remove the ALTER TABLE lines for PK only
pass
else:
file.write(line)
4 changes: 2 additions & 2 deletions migtests/scripts/live-migration-fallb-run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ main() {
run_ysql yugabyte "CREATE DATABASE ${TARGET_DB_NAME}"
fi

if [ -x "${TEST_DIR}/add-pk-from-alter-to-create" ]
if [ -x "${SCRIPTS}/add-pk-from-alter-to-create" ]
then
"${TEST_DIR}/add-pk-from-alter-to-create"
"${SCRIPTS}/add-pk-from-alter-to-create"
fi

step "Import schema."
Expand Down
4 changes: 2 additions & 2 deletions migtests/scripts/live-migration-fallf-run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ main() {
run_ysql yugabyte "CREATE DATABASE ${TARGET_DB_NAME}"
fi

if [ -x "${TEST_DIR}/add-pk-from-alter-to-create" ]
if [ -x "${SCRIPTS}/add-pk-from-alter-to-create" ]
then
"${TEST_DIR}/add-pk-from-alter-to-create"
"${SCRIPTS}/add-pk-from-alter-to-create"
fi

step "Import schema."
Expand Down
4 changes: 2 additions & 2 deletions migtests/scripts/live-migration-run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ main() {
run_ysql yugabyte "CREATE DATABASE ${TARGET_DB_NAME}"
fi

if [ -x "${TEST_DIR}/add-pk-from-alter-to-create" ]
if [ -x "${SCRIPTS}/add-pk-from-alter-to-create" ]
then
"${TEST_DIR}/add-pk-from-alter-to-create"
"${SCRIPTS}/add-pk-from-alter-to-create"
fi

step "Import schema."
Expand Down
4 changes: 2 additions & 2 deletions migtests/scripts/run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ main() {
run_ysql yugabyte "CREATE DATABASE ${TARGET_DB_NAME}"
fi

if [ -x "${TEST_DIR}/add-pk-from-alter-to-create" ]
if [ -x "${SCRIPTS}/add-pk-from-alter-to-create" ]
then
"${TEST_DIR}/add-pk-from-alter-to-create"
"${SCRIPTS}/add-pk-from-alter-to-create"
fi

step "Import schema."
Expand Down

This file was deleted.

46 changes: 0 additions & 46 deletions migtests/tests/pg/partitions/add-pk-from-alter-to-create

This file was deleted.

Loading