2021-12-04 - Sorry the Gramps wiki is currently being upgraded to a newer software version. Please check back after the 2021-12-06.
GEPS 010: Relational Backend
Error creating thumbnail: Unable to save thumbnail to destination
This proposal has been withdrawn.
This original GEP was a design to use a relational database backend for Gramps. However, after experiments, it was shown to be too slow. However, it was shown that we could use other backends, but in an non-relational manner. Also, there are many NOSQL solutions that would also work. Thus, a new proposal, GEPS 032: Database Backend API was created.
This proposal is also related to GEPS 013: Gramps Webapp which can create a SQL interface.
There is also a SQL import and export Addon.
This page is for the discussion of a proposed implementation of a relational (SQL) backend for GRAMPS.
SQL stands for "Structured Query Language" and is pronounced "sequel" (it is a joke: as it came after QUEL, it is its sequel).
- 1 Reasons for adding a relational backend
- 2 Research
- 3 Questions and concerns
- 4 What now?
- 5 See Also
Reasons for adding a relational backend
Currently, GRAMPS uses a BSDDB database as its internal file format. While this is considerably better than, say, an XML format, the choice of the BSD-DB has a considerable number of drawbacks. This proposal explores the use of SQL as an alternative backend. This should allow easy, single db file implementations (eg, SQLite) to more complex and sophisticated client/server (eg, MySQL).
First, there are a number of facts related to this proposal:
- BSDDB is being removed from the standard distribution of Python (as of Python 2.6)
- SQLITE is being added to the standard Python distribution
- BSDDB is not a relational database, but a hierarchical datastore
- BSDDB databases do not have schema or data-definitions. BSDDB requires all of the database structure logic to reside in code
- BSDDB is a programmer's API
- SQL is a declarative, independent abstraction layer
- SQL can optimize queries (in low-level C) whereas BSDDB is done in Python
- An SQL version of a GRAMPS DB should be faster
- An SQLite version of a GRAMPS DB should be smaller than a BSDDB file.
- SQL Engines can perform query optimizations
- More code would reside in the db, rather than in Python
- Enterprise SQL versions of GRAMPS would allow people to create and manage much larger trees
- An SQLite version of GRAMPS might allow people to create larger trees
- Because we move all of the DB logic into SQL, we can focus on making GRAMPS stable with large databases
- SQL code is simpler than the equivalent BSDDB code, because SQL is declarative/abstract and BSDDB is a low-level API
- A fullscale MySQL backend would be a trivial step from SQLite
True as long as the SQL written to access the database conforms to a particular defined standard. SQLite appears to support (most of) SQL-92. So for portability reasons we must code to the SQL-92 standard if SQLlite is chosen as the lowest common denominator. --Gburto01 23:06, 4 April 2009 (UTC)
- Easy to allow multiple users in a SQLite database (uses file-locking)
- There is a lot of code that we have written dealing with BSDDB. It would have to all be rewritten in SQL (on the other hand, a lot of code can be deleted, which will make GRAMPS easier to maintain and adapt)
- We will have to develop SQL experts
I have been writing SQL for 20 years :) --Gburto01 23:06, 4 April 2009 (UTC)
Perhaps one of the most important reasons for moving away from a low-level datastore is to allow more sophisticated interfaces. Currently BSDDB offers transactions and recover, or multiuser access, but not both together. If we want to have a GRAMPS webapp or a multiuser GTK interface (for examples), then we would have to either disable transactions and recover (dangerous!) or switch backends. (Another option would be to write a server interface, but the complexity of that is probably on par with just switching backends. See GEPS 013: Gramps Webapp for more discussion.)
Discussions of BSDDB in Python
BSDDB has had a hard time in Python. Python Developers have been wrestling with trying to keep it stable. Guido finally decided to separate BSDDB from the standard Python Distribution. See discussions:
PEP 3108 marks BSDDB to be removed: http://www.python.org/dev/peps/pep-3108/ Development is not death however, it will only be out of sync of the python cycle. The home of pybsdb offering the bsddb3 package is hereL http://www.jcea.es/programacion/pybsddb.htm
Questions and concerns
Native DB access for other languages
If we use a well-known SQL backend, we should consider the ability for other languages to be able to natively access the database. For example, a PHP program should be able to use the same database. Does using a Python-based ORM tie the data to Python? Or can the database still be used natively from other systems?
Using a Python based ORM wont tie the data just to python. any language should be able to access the db just fine. However, they wouldn't have access to pythons orm layer. Since I haven't used a true orm before I'm not certain exactly how it will effect our table relationships but I don't believe they wont make some sense in a relational way. Not that I'm saying we should use it but a quick google search started to bring up things like this php python package. so there may be some hope for even using the orm layer but how complex would we really want to make it! And of course there is always the option of just using an orm and building similar objects in the new language. --AaronS 03:30, 26 March 2009 (UTC)
Power vs Dependencies
Do we want to have an additional layer over the Database Abstraction Layer (eg, an ORM)?
- Makes GRAMPS code more abstract
- Makes it harder for other languages to use the native GRAMPS db (but they can use the native db)
- Adds a dependency
Given that GRAMPS's developers have, in the past, written their own db transactions, and their own HTML abstractions, does it make sense to add such a dependency?
Is the ORM available for all platforms?
I'm not certain if we need the orm layer or not. One option would be to just use sqlAlchemy's SQL Expression Language layer for abstraction and forgo the orm. sqlAlchemy's home and intro might help explain some things. Another is that what ever we do we will be using a DB-API so perhaps we should test how far that takes us first before adding yet another dependency. Is the ORM available for all platforms? if your asking about programming languages no sqlAlchemy is a python module. if your asking about databases the support databases list is decent. --AaronS 19:16, 26 March 2009 (UTC)
Actually, I was wondering about OS platforms, but that info is useful, too. --Dsblank 00:40, 27 March 2009 (UTC)
I only researched python orms so they should work where ever python does. I confirmed sqlalchemy probably does since it supports MS-SQL and MSAccess --AaronS 03:33, 27 March 2009 (UTC)
Create Object model
Going over src/gen/lib/, create an object model of how GRAMPS uses and manipulates genealogy data.
For this GEP to succeed it is extremely important that the experienced developers on the devel list agree with the object model. This goes without saying. For this to succeed, the developers should agree with all of the major decisions.
Select an SQL framework
- finish research and pick a database.
- finish research and pick a database abstraction layer.
- use the framework to set up a model of the database
- generate the tables
- create a dump of bsddb database in the sql database
- validate that all things present in bsddb are present in the sql database
- check validation rules. Eg, handle should be unique, rules must be added to ensure adding to the family table an object with handle like a person object is impossible on the database layer. These kind of rules can be done technically (a primary object table with key on handle) or with rules.
- best would be a framework that based on the model can generate an admin module to browse the database, see eg the admin module in django.
New db backend for GRAMPS
- write an implementation of src/gen/db/base.py to interface the DB abstraction layer with the rest of gramps. Gramps 3.x only has one implementation: src/gen/db/dbdir.py, but in branch22 a gedcom and a gramps xml implementation can be found (these have been deprecated).
- once written, this can be added as an experimental backend to GRAMPS
- Family Tree manager needs to list the family tree type (bsddb, sqlite), on creation of new family tree user must choose the backend.
- User can import .gramps/gedcom files just as this is done with bsddb backend once family tree is set up.
- it will be very important to use properties in src/gen/lib to make this work. Obtaining a person via get_person_from_handle, should only hit the person table. Only when the calling method needs attributes, should the attribute table be hit. This requires attributes that are not yet defined up to the moment they are accessed. It also means that the gen/lib objects for sql need to be aware of the database as it needs to know where to obtain these values... . This looks like a huge work to me, but definitely doable. Just rewriting gen/lib for an sql datamodel might be easier though, but that means rewriting the core of GRAMPS....
I don't understand the use of slots in the above. How is that idea related to db access? --Dsblank 11:14, 26 March 2009 (UTC)
Doug, I meant properties. The idea is the following. In gen/person an person contains all secondary objects and all references. In a database, this depends on the implementation. So bsddb has a flat table with the person object in one table. So one get. For sql, the secondary objects will be in other tables. Hitting all relevant tables on get of a person would be stupid. We only need to access the tables when the data is needed. In base.py, a person is obtained with get_person_from_handle. This fills up a Person() object. dbdir.py uses the unserialize method to do this. My suggestion of properties would be that with an sql backend, the handle is get, but all the other Person attributes are properties that remain None. If data is needed, the getter checks if value is None, if so, the sql database is hit, otherwise the data is just returned. Just an idea, the concept needs to be worked out. The alternative is changing /gen/lib dramatically, but I don't see the benefit of that. Note that making all objects in /gen/lib new style classes with slots would be a good move nonetheless. bmcage 13:22, 8 April 2009 (UTC)
Thanks for the clarification... that makes sense now :) The integration of the SQL DB with the existing GRAMPS gen/lib and gen/db does seem to be the biggest job to be done in this project. I've had a look at gen/lib and gen/db and there are lots of assumptions all over the place about how the data is stored. Trying to hide this through the use of properties may work, but in the long run, GRAMPS may need an API overhaul. But I like the idea of seeing how far we can get by using properties. --Dsblank 12:56, 9 April 2009 (UTC)
Once an sql backend is stable, base.py can be extended to offer extra functionality, or better optimize for SQL. Eg, in SQL one would have probably an attribute table. To know which persons have a specific attribute, SQL would select that from the attributes table, and then look up the people. In bsddb it means however to loop over all persons, and obtain the attribute sub table of a person and looking if attribute is present there.
Above clearly indicates that how one goes about in the two backends is very different. The bsddb way will work in sql though (as the get_person method works, and speed should be comparable to bsddb if above deferred obtaining of values via slots is implemented). Nevertheless, a clear mechanism to optimize for sql is needed. Continuing above example, see _HasAttributeBase.py
For sql, one would use the prepare method, obtain all people in a list, then return True or False if person is in this list. As db is passed, db can have a support_sql attribute, and code can be written depending on this setting. This does not look very ideal though.