Skip to content

Commit 64c9e94

Browse files
rename database to schema_name in schema constructor and add the create_schema kwarg.
1 parent 1cd1558 commit 64c9e94

File tree

5 files changed

+62
-56
lines changed

5 files changed

+62
-56
lines changed

datajoint/__init__.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""
2-
DataJoint for Python is a high-level programming interface for MySQL databases
3-
to support data processing chains in science labs. DataJoint is built on the
4-
foundation of the relational data model and prescribes a consistent method for
5-
organizing, populating, and querying data.
2+
DataJoint for Python is a framework for building data piplines using MySQL databases
3+
to represent pipeline structure and bulk storage systems for large objects.
4+
DataJoint is built on the foundation of the relational data model and prescribes a
5+
consistent method for organizing, populating, and querying data.
6+
7+
The DataJoint data model is described in https://arxiv.org/abs/1807.11104
68
79
DataJoint is free software under the LGPL License. In addition, we request
810
that any use of DataJoint leading to a publication be acknowledged in the publication.
@@ -18,7 +20,7 @@
1820
from .version import __version__
1921

2022
__author__ = "Dimitri Yatsenko, Edgar Y. Walker, and Fabian Sinz at Baylor College of Medicine"
21-
__date__ = "July 26, 2017"
23+
__date__ = "August 24, 2018"
2224
__all__ = ['__author__', '__version__',
2325
'config', 'conn', 'kill', 'BaseRelation',
2426
'Connection', 'Heading', 'FreeRelation', 'Not', 'schema',
@@ -71,17 +73,19 @@ class key:
7173
from .errors import DataJointError, DuplicateError
7274

7375

74-
def create_virtual_module(modulename, dbname):
76+
def create_virtual_module(module_name, schema_name, create_schema=False, create_tables=False):
7577
"""
76-
Creates a python module with the given name from a database name in mysql with datajoint tables.
77-
Automatically creates the classes of the appropriate tier in the module.
78+
Creates a python module with the given name from the name of a schema on the server and
79+
automatically adds classes to it corresponding to the tables in the schema.
7880
79-
:param modulename: desired name of the module
80-
:param dbname: name of the database in mysql
81-
:return: the python module
81+
:param module_name: displayed module name
82+
:param schema_name: name of the database in mysql
83+
:param create_schema: if True, create the schema on the database server
84+
:param create_tables: if True, module.schema can be used as the decorator for declaring new
85+
:return: the python module containing classes from the schema object and the table classes
8286
"""
83-
mod = ModuleType(modulename)
84-
s = schema(dbname, create_tables=False)
85-
s.spawn_missing_classes(context=mod.__dict__)
86-
mod.__dict__['schema'] = s
87-
return mod
87+
module = ModuleType(module_name)
88+
_schema = schema(schema_name, create_schema=create_schema, create_tables=create_tables)
89+
_schema.spawn_missing_classes(context=module.__dict__)
90+
module.__dict__['schema'] = _schema
91+
return module

datajoint/base_relation.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
class BaseRelation(RelationalOperand):
2323
"""
24-
BaseRelation is an abstract class that represents a base relation, i.e. a table in the database.
24+
BaseRelation is an abstract class that represents a base relation, i.e. a table in the schema.
2525
To make it a concrete class, override the abstract properties specifying the connection,
2626
table name, database, context, and definition.
2727
A Relation implements insert and delete methods in addition to inherited relational operators.
@@ -56,7 +56,7 @@ def context(self):
5656

5757
def declare(self):
5858
"""
59-
Use self.definition to declare the table in the database
59+
Use self.definition to declare the table in the schema.
6060
"""
6161
try:
6262
sql, uses_external = declare(self.full_table_name, self.definition, self._context)
@@ -108,7 +108,7 @@ def children(self, primary=None):
108108
@property
109109
def is_declared(self):
110110
"""
111-
:return: True is the table is declared in the database
111+
:return: True is the table is declared in the schema.
112112
"""
113113
return self.connection.query(
114114
'SHOW TABLES in `{database}` LIKE "{table_name}"'.format(
@@ -117,7 +117,7 @@ def is_declared(self):
117117
@property
118118
def full_table_name(self):
119119
"""
120-
:return: full table name in the database
120+
:return: full table name in the schema
121121
"""
122122
return r"`{0:s}`.`{1:s}`".format(self.database, self.table_name)
123123

@@ -541,8 +541,8 @@ def _update(self, attrname, value=None):
541541

542542
def lookup_class_name(name, context, depth=3):
543543
"""
544-
given a table name in the form `database`.`table_name`, find its class in the context.
545-
:param name: `database`.`table_name`
544+
given a table name in the form `schema_name`.`table_name`, find its class in the context.
545+
:param name: `schema_name`.`table_name`
546546
:param context: dictionary representing the namespace
547547
:param depth: search depth into imported modules, helps avoid infinite recursion.
548548
:return: class name found in the context or None if not found
@@ -599,14 +599,14 @@ def __repr__(self):
599599
@property
600600
def table_name(self):
601601
"""
602-
:return: the table name in the database
602+
:return: the table name in the schema
603603
"""
604604
return self._table_name
605605

606606

607607
class Log(BaseRelation):
608608
"""
609-
The log table for each database.
609+
The log table for each schema.
610610
Instances are callable. Calls log the time and identifying information along with the event.
611611
"""
612612

datajoint/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
This module hosts the Connection class that manages the connection to the mysql database,
2+
This module hosts the Connection class that manages the connection to the database,
33
and the `conn` function that provides access to a persistent connection in datajoint.
44
"""
55
import warnings

datajoint/fetch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def to_dicts(recarray):
1919

2020
class Fetch:
2121
"""
22-
A fetch object that handles retrieving elements from the database table.
23-
:param relation: relation the fetch object fetches data from
22+
A fetch object that handles retrieving elements from the table expression.
23+
:param relation: the table expression to fetch from
2424
"""
2525

2626
def __init__(self, relation):

datajoint/schema.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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\nschema=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

Comments
 (0)