How to create specific migrations files per sqlite database in same app

31 views
Skip to first unread message

AlighaThor

unread,
Oct 12, 2017, 11:32:02 PM10/12/17
to web2py-users
Hi everyone. I'm experimenting with an multi-tenant app. My goal is to use a sqlite file per tenant (the tenant data). I have a main sqlite file where I have defined the auth tables and a Client (tenant) table.

The structure I'm looking for is something like this, where inside a tenant folder there should be the migrations files and the sqlite db (in this example I only got working the sqlite db creation, not the migrations files):


So, I have two DB models, one for the main database and the second for the tentant DB. The tenant model loads a variable wich contains the identifier of the requested tenant, so It should create the sqlite file and generate the migrations files.


My problem is that I want specific migrations files per database, not the same .table files for every sqlite db. So, I'm trying (whitout success) to generate the migration files inside the tenants folders. The sqlite db is created without problems, but the following example does not:


In the tenant model:




So, whatever path I put in the folder argument of the DAL's constructor, I get a time out exception. I don't know why. I've tried relative paths, joining the os.path with the request.folder, nothing works. Also the error I'm getting is a time-out exception, so I don't know what is the conflict, because according to the book, the folder parameter is "where .table files will be created". If "folder" is empty or not defined, it works, but as I'm saying, I would like specific migrations files per tenant database.

It this possible? What's wrong?

Thanks in advance.


Bernhard Radermacher

unread,
Oct 15, 2017, 12:23:53 PM10/15/17
to web2py-users
Not sure if that's the issue, but first you should use os.path.join to join every part:

os.path.join(request.folder, 'databases', 'clients', 'client_{0}'.format(tenant))

otherwise you introduce a OS dependency.

I would try 

con='sqlite://' + os.path.join(request.folder, 'databases', 'clients', 'client_{0}'.format(tenant))
db=DAL(con, pool_size....)
i.e. using the complete, absolute path.

AlighaThor

unread,
Oct 15, 2017, 2:32:56 PM10/15/17
to web2py-users
Yep...that worked. In the meantime I did this per table definition:

def migration(table):
   
return '{0}/{1}.table'.format(tenant_folder, table)

#In the tables definitions
...
migrate
=migration('buildings')

That worked, but I like your clarification more. Thanks mate.

Bernhard Radermacher

unread,
Oct 16, 2017, 1:51:03 AM10/16/17
to web2py-users
Glad I could help. I found that you better use absolute paths as soon as you want to go down to sub directories. Using '.' as the begin of the path might work, but I have not tested that.
Reply all
Reply to author
Forward
0 new messages