@@ -40,39 +40,40 @@ class Schema:
4040 It also specifies the namespace `context` in which other UserRelation classes are defined.
4141 """
4242
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 ):
4444 """
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.
4746
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.
5152 """
5253 if connection is None :
5354 connection = conn ()
5455 self ._log = None
55- self .database = database
56+ self .database = schema_name
5657 self .connection = connection
5758 self .context = context
5859 self .create_tables = create_tables
5960 self ._jobs = None
6061 self ._external = None
6162 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 ))
6567 else :
6668 # 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 ))
6970 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 ))
7273 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 ))
7677 else :
7778 self .log ('created' )
7879 self .log ('connect' )
@@ -85,12 +86,12 @@ def log(self):
8586 return self ._log
8687
8788 def __repr__ (self ):
88- return 'Schema database: `{database }`\n ' .format (database = self .database )
89+ return 'Schema `{name }`\n ' .format (name = self .database )
8990
9091 @property
9192 def size_on_disk (self ):
9293 """
93- :return: size of the database in bytes
94+ :return: size of the entire schema in bytes
9495 """
9596 return int (self .connection .query (
9697 """
@@ -106,7 +107,8 @@ def _make_module_code(self):
106107 """
107108
108109 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 )))
110112 db = self .database
111113
112114 def make_class_definition (table ):
@@ -136,13 +138,13 @@ def repl(s):
136138 return '\n \n \n ' .join ((
137139 '"""This module was auto-generated by datajoint from an existing schema"""' ,
138140 "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 )
140142 for k , v in module_lookup .items ()),
141143 body ))
142144
143145 def spawn_missing_classes (self , context = None ):
144146 """
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
146148 in the context.
147149 :param context: alternative context to place the missing classes into, e.g. locals()
148150 """
@@ -186,25 +188,25 @@ def spawn_missing_classes(self, context=None):
186188
187189 def drop (self , force = False ):
188190 """
189- Drop the associated database if it exists
191+ Drop the associated schema if it exists
190192 """
191193 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 ))
193195 elif (not config ['safemode' ] or
194196 force or
195197 user_choice ("Proceed to delete entire schema `%s`?" % self .database , default = 'no' ) == 'yes' ):
196198 logger .info ("Dropping `{database}`." .format (database = self .database ))
197199 try :
198200 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 ))
200202 except pymysql .OperationalError :
201- raise DataJointError ("An attempt to drop database named `{database}` "
203+ raise DataJointError ("An attempt to drop schema `{database}` "
202204 "has failed. Check permissions." .format (database = self .database ))
203205
204206 @property
205207 def exists (self ):
206208 """
207- :return: true if the associated database exists on the server
209+ :return: true if the associated schema exists on the server
208210 """
209211 cur = self .connection .query ("SHOW DATABASES LIKE '{database}'" .format (database = self .database ))
210212 return cur .rowcount > 0
@@ -238,8 +240,8 @@ def process_relation_class(self, relation_class, context, assert_declared=False)
238240
239241 def __call__ (self , cls ):
240242 """
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.
243245 """
244246 context = self .context if self .context is not None else inspect .currentframe ().f_back .f_locals
245247 if issubclass (cls , Part ):
0 commit comments