Last active
October 31, 2020 12:48
-
-
Save pepijnolivier/1b3f1e879c8d1a2673e00fe270e3eff2 to your computer and use it in GitHub Desktop.
Mysql Workbench script to ensure all id columns and foreign keys are unsigned integers. All ID columns should also be auto-incremental
This file contains 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
# -*- coding: utf-8 -*- | |
# MySQL Workbench Python script | |
# | |
# <description> | |
# Written in MySQL Workbench 6.3.6 | |
import grt | |
# get a reference to the schema in the model. This will get the 1st schema in it. | |
schema = grt.root.wb.doc.physicalModels[0].catalog.schemata[0] | |
# iterate through all tables | |
for table in schema.tables: | |
print '' | |
print table.name | |
hasId = False | |
idCol = False | |
fks = [] | |
for column in table.columns: | |
name = column.name | |
if(name == 'id'): | |
hasId = True | |
idCol = column | |
elif(name.endswith('_id')): | |
fks.append(column) | |
if(hasId == True): | |
#set type INT | |
idCol.setParseType("INT", None) | |
#set unsigned auto increment | |
idCol.flags.remove('UNSIGNED') | |
idCol.flags.append('UNSIGNED') | |
idCol.flags.remove("AUTO_INCREMENT") | |
idCol.flags.append("AUTO_INCREMENT") | |
for fkCol in fks: | |
# set type INT | |
fkCol.setParseType("INT", None) | |
# set unsigned | |
fkCol.flags.append('UNSIGNED') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment