In Python scripting for MySQL Workbench 5.2.34, grt.root.wb does not have physicalModels
The object 'wb' (and, obviously, all superior objects) loads and holds information, but the tree to get at the database's physical model is simply not loading. Internet searching got me bipkis, thus far.
(I was intending to write a routine to test for the existence of certain columns and add the appropriate trigger programatically. I've worked out a kludge using a regular expression and a database export, but this rankles a bit.)
Missing some form of simple activation? (connection perhaps)
The following fails with the error "AttributeError: 'NoneType' object has no attribute 'physicalModels'":
# -*- coding: utf-8 -*-
# MySQL Workbench Python script
# <description>
# Written in MySQL Workbench 5.2.34
import grt
#import mforms #??
stOut=""
stTrigger="""
delimiter $$
create trigger `docdb_mk2`.tsi_{t} before insert on `docdb_mk2`.`{t}`
for each r开发者_如何学Cow begin
set new.inserted=now();
end$$"""
# iterate through all tables from schema"""
schema = grt.root.wb.doc.physicalModels[0].catalog.schemata[0]
for table in schema.tables:
#print table.name
#if table.
#check to make sure both inserted and Updated are in the table
#Since I have no clue how to do that in here, I'll skip it for now.
stOut=stOut+stTrigger(t=table.name)
Even this fails:
# -*- coding: utf-8 -*-
# MySQL Workbench Python script
# <description>
# Written in MySQL Workbench 5.2.34
import grt
#import mforms
# iterate through all schemas
for schema in grt.root.wb.doc.physicalModels[0].catalog.schemata:
print schema.name
Please note that I'm not asking for advice on my SQL at the moment, just on this irritating barrier in scripting.
Edit: Apparently, some of the objects load by loading up those elements in the UI. If there is some way of getting them to load elseways, I'd appreciate knowing, but it does seem that if you load the scripting module from the main Workbench screen, you get a partially initialized wb object. If you load the thing from a different subsystem, such as the data modeling module, (Scripting > Run Workbench Script File...) other parts are loaded and begin to work. Hmm.
While this is not a complete solution this is my work in progress on trying to figure this out as well. From what I can tell the wb.docs.physicalModels object is only available when in 'design/model' mode, what I need to do it run a script in SQL Editor mode. The code below does execute queries and returns results, but I'm struggling with documentation not matching what I'm seeing in the objects right now. Hopefully this is of some help
import grt
for editor in grt.root.wb.sqlEditors:
# add some text to the 'Output' tab
editor.addToOutput("Test", 0)
results = editor.executeScript("show tables")
#print results.rowCount()
# the above throws an exception because what is returned from executeScript() differs from the docs
print results
for buffer in editor.queryBuffers:
print buffer.replaceContents("test")
You must open a model (*.mwb file) from your workbench or if not exists, create one (choose "create eer model from existing database"). This should make your error disappear.
I found a solution for this:
print results.rowCount()
# the above throws an exception because what is returned from executeScript() differs from the docs
It will work if you write your statements against the first element in the db_query_Resultset
list without the ().
I.e. print results[0].rowCount
works.
精彩评论