50
50
variables than code running outside. It will be missing a lot of data, and
51
51
will contain some user-specific data.
56
IVLE exclusively uses the `Storm`_ API for database access. Do not write any
57
SQL code yourself, or make use of low-level database libraries. The only
58
exception is in preparing the database schema, which is stored as an SQL file.
60
.. _Storm: https://storm.canonical.com/
62
... update the database schema?
63
-------------------------------
65
Modify :file:`userdb/users.sql`. Any changes also need to be made in to a
66
migrations file, in :file:`userdb/migrations/`.
68
TODO: More detail on migrations.
70
.. _ref-dev-faq-read-data:
72
... read data from the database?
73
--------------------------------
78
# Typically, you import all database classes you want here
79
from ivle.database import User
81
You need a `store` object to perform any interactions with the database. If
82
you are inside the web app, get a hold of the `req` (request) object, and use
83
``req.store``. In other code, create a new store as follows (where `config` is
84
a :ref:`config <ref-dev-faq-config>` object)::
86
store = ivle.database.get_store(config)
88
You can read objects out of the database through the store. For example, to
91
user = store.find(User, User.login==username).one()
93
(Note that ``store.find(User)`` just returns a sequence of all users.)
95
You can then treat `user` as a normal object, and read from its attributes.
96
All of the classes are defined in ``ivle/database.py``.
99
The code must be executed outside of the jail. Jail code runs under user
100
privileges and cannot access the database.
103
For help with the database API, see the `Storm`_ documentation.
105
... write data to the database?
106
--------------------------------
108
Get an object out of the database, as :ref:`above <ref-dev-faq-read-data>`,
109
and simply write to the object's attributes. This updates the *in-memory* copy
112
To write the changes back to the database, simply use::
116
using the same store object as used to retrieve the object in the first place.
118
... insert a new object into the database?
119
------------------------------------------
121
Create the new object using its constructor, as with any Python object. e.g.::
124
user = ivle.database.User()
126
You can then set the attributes of the object as desired. As with writing,
127
this only creates an *in-memory* object.
129
To add the object to the database, get a :ref:`store <ref-dev-faq-read-data>`,