Skip to content

Instantly share code, notes, and snippets.

@Samuel-Bie
Created July 31, 2020 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Samuel-Bie/80d9932f3b6f82a12062eae7f5cac246 to your computer and use it in GitHub Desktop.
Save Samuel-Bie/80d9932f3b6f82a12062eae7f5cac246 to your computer and use it in GitHub Desktop.
Add columns to mysql workbench
# -*- coding: utf-8 -*-
# MySQL Workbench Python script
# <description>
# Written in MySQL Workbench 8.0.18
import grt
#import mforms
#define your columns and types here
#columns_types = {"created_at" : "DATETIME", "updated_at" : "DATETIME"}
columns_types = {"uuid" : "char(45)"}
# tables you want to skip
skip_tables = ["main_defs", "main_menu_items", "delayed_jobs", "delayed_job_workers", "delayed_job_logs"]
# 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:
# skip the current table if it is in skip_tables
if table.name in skip_tables:
continue
# iterate through all columns to be added
for column_name, type in columns_types.items():
# skip this column_name if there is already a column with this name
column_names = [x.name for x in table.columns]
if column_name in column_names:
continue
# create a new column object and set its name
column = grt.classes.db_mysql_Column()
column.name = column_name
# add it to the table
table.addColumn(column)
# set the datatype of the column
column.setParseType(type, None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment