@@ -40,39 +40,40 @@ class Schema:
40
40
It also specifies the namespace `context` in which other UserRelation classes are defined.
41
41
"""
42
42
43
- def __init__ (self , database , context = None , connection = None , create_tables = True ):
43
+ def __init__ (self , schema_name , context = None , connection = None , create_schema = True , create_tables = True ):
44
44
"""
45
- Associates the specified database with this schema object. If the target database does not exist
46
- already, will attempt on creating the database.
45
+ Associate database schema `schema_name`. If the schema does not exist, attempt to create it on the server.
47
46
48
- :param database: name of the database to associate the decorated class with
49
- :param context: dictionary for looking up foreign keys references, leave None to use local context
50
- :param connection: Connection object. Defaults to datajoint.conn()
47
+ :param schema_name: the database schema to associate.
48
+ :param context: dictionary for looking up foreign key references, leave None to use local context.
49
+ :param connection: Connection object. Defaults to datajoint.conn().
50
+ :param create_schema: When False, do not create the schema and raise an error if missing.
51
+ :param create_tables: When False, do not create tables and raise errors when accessing missing tables.
51
52
"""
52
53
if connection is None :
53
54
connection = conn ()
54
55
self ._log = None
55
- self .database = database
56
+ self .database = schema_name
56
57
self .connection = connection
57
58
self .context = context
58
59
self .create_tables = create_tables
59
60
self ._jobs = None
60
61
self ._external = None
61
62
if not self .exists :
62
- if not self .create_tables :
63
- raise DataJointError ("Database named `{database}` was not defined. "
64
- "Set the create_tables flag to create it." .format (database = database ))
63
+ if not create_schema :
64
+ raise DataJointError (
65
+ "Database named `{name}` was not defined. "
66
+ "Set argument create_schema=True to create it." .format (name = schema_name ))
65
67
else :
66
68
# create database
67
- logger .info ("Database `{database}` could not be found. "
68
- "Attempting to create the database." .format (database = database ))
69
+ logger .info ("Creating schema `{name}`." .format (name = schema_name ))
69
70
try :
70
- connection .query ("CREATE DATABASE `{database }`" .format (database = database ))
71
- logger .info ('Created database `{database }`.' .format (database = database ))
71
+ connection .query ("CREATE DATABASE `{name }`" .format (name = schema_name ))
72
+ logger .info ('Creating schema `{name }`.' .format (name = schema_name ))
72
73
except pymysql .OperationalError :
73
- raise DataJointError ("Database named `{database}` was not defined, and"
74
- " an attempt to create has failed. Check "
75
- " permissions." .format (database = database ))
74
+ raise DataJointError (
75
+ "Schema `{name}` does not exist and could not be created. "
76
+ "Check permissions." .format (name = schema_name ))
76
77
else :
77
78
self .log ('created' )
78
79
self .log ('connect' )
@@ -85,12 +86,12 @@ def log(self):
85
86
return self ._log
86
87
87
88
def __repr__ (self ):
88
- return 'Schema database: `{database }`\n ' .format (database = self .database )
89
+ return 'Schema `{name }`\n ' .format (name = self .database )
89
90
90
91
@property
91
92
def size_on_disk (self ):
92
93
"""
93
- :return: size of the database in bytes
94
+ :return: size of the entire schema in bytes
94
95
"""
95
96
return int (self .connection .query (
96
97
"""
@@ -106,7 +107,8 @@ def _make_module_code(self):
106
107
"""
107
108
108
109
module_count = itertools .count ()
109
- module_lookup = collections .defaultdict (lambda : 'vmodule' + str (next (module_count )))
110
+ # add virtual modules for referenced modules with names vmod0, vmod1, ...
111
+ module_lookup = collections .defaultdict (lambda : 'vmod' + str (next (module_count )))
110
112
db = self .database
111
113
112
114
def make_class_definition (table ):
@@ -136,13 +138,13 @@ def repl(s):
136
138
return '\n \n \n ' .join ((
137
139
'"""This module was auto-generated by datajoint from an existing schema"""' ,
138
140
"import datajoint as dj\n \n schema=dj.schema('{db}')" .format (db = db ),
139
- '\n ' .join ("{module} = dj.create_virtual_module('{module}', '{database }')" .format (module = v , database = k )
141
+ '\n ' .join ("{module} = dj.create_virtual_module('{module}', '{schema_name }')" .format (module = v , schema_name = k )
140
142
for k , v in module_lookup .items ()),
141
143
body ))
142
144
143
145
def spawn_missing_classes (self , context = None ):
144
146
"""
145
- Creates the appropriate python user relation classes from tables in the database and places them
147
+ Creates the appropriate python user relation classes from tables in the schema and places them
146
148
in the context.
147
149
:param context: alternative context to place the missing classes into, e.g. locals()
148
150
"""
@@ -186,25 +188,25 @@ def spawn_missing_classes(self, context=None):
186
188
187
189
def drop (self , force = False ):
188
190
"""
189
- Drop the associated database if it exists
191
+ Drop the associated schema if it exists
190
192
"""
191
193
if not self .exists :
192
- logger .info ("Database named `{database}` does not exist. Doing nothing." .format (database = self .database ))
194
+ logger .info ("Schema named `{database}` does not exist. Doing nothing." .format (database = self .database ))
193
195
elif (not config ['safemode' ] or
194
196
force or
195
197
user_choice ("Proceed to delete entire schema `%s`?" % self .database , default = 'no' ) == 'yes' ):
196
198
logger .info ("Dropping `{database}`." .format (database = self .database ))
197
199
try :
198
200
self .connection .query ("DROP DATABASE `{database}`" .format (database = self .database ))
199
- logger .info ("Database `{database}` was dropped successfully." .format (database = self .database ))
201
+ logger .info ("Schema `{database}` was dropped successfully." .format (database = self .database ))
200
202
except pymysql .OperationalError :
201
- raise DataJointError ("An attempt to drop database named `{database}` "
203
+ raise DataJointError ("An attempt to drop schema `{database}` "
202
204
"has failed. Check permissions." .format (database = self .database ))
203
205
204
206
@property
205
207
def exists (self ):
206
208
"""
207
- :return: true if the associated database exists on the server
209
+ :return: true if the associated schema exists on the server
208
210
"""
209
211
cur = self .connection .query ("SHOW DATABASES LIKE '{database}'" .format (database = self .database ))
210
212
return cur .rowcount > 0
@@ -238,8 +240,8 @@ def process_relation_class(self, relation_class, context, assert_declared=False)
238
240
239
241
def __call__ (self , cls ):
240
242
"""
241
- Binds the passed in class object to a database . This is intended to be used as a decorator.
242
- :param cls: class to be decorated
243
+ Binds the supplied class to a schema . This is intended to be used as a decorator.
244
+ :param cls: class to decorate.
243
245
"""
244
246
context = self .context if self .context is not None else inspect .currentframe ().f_back .f_locals
245
247
if issubclass (cls , Part ):
0 commit comments