Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support opening multiple databases with the same stem #509

Closed
simonw opened this issue Jun 15, 2019 · 4 comments
Closed

Support opening multiple databases with the same stem #509

simonw opened this issue Jun 15, 2019 · 4 comments
Assignees
Milestone

Comments

@simonw
Copy link
Owner

simonw commented Jun 15, 2019

e.g. I should be able to do this:

datasette App/data.db Other_App/data.db

This currently errors because you can't have two databases taking the /data URL path.

Instead, how about in this particular case assigning the second database /data-1?

@simonw
Copy link
Owner Author

simonw commented Jun 15, 2019

Experimental exploratory patch:

diff --git a/datasette/app.py b/datasette/app.py
index 2ef7da4..ca51866 100644
--- a/datasette/app.py
+++ b/datasette/app.py
@@ -164,8 +164,10 @@ class Datasette:
                 is_memory = True
             is_mutable = path not in self.immutables
             db = Database(self, path, is_mutable=is_mutable, is_memory=is_memory)
+            i = 1
             if db.name in self.databases:
-                raise Exception("Multiple files with same stem: {}".format(db.name))
+                db.stem = db.name + "-" + str(i)
+                i += 1
             self.databases[db.name] = db
         self.cache_headers = cache_headers
         self.cors = cors
diff --git a/datasette/database.py b/datasette/database.py
index e491577..75c8681 100644
--- a/datasette/database.py
+++ b/datasette/database.py
@@ -14,6 +14,8 @@ from .inspect import inspect_hash
 
 
 class Database:
+    stem = None
+
     def __init__(self, ds, path=None, is_mutable=False, is_memory=False):
         self.ds = ds
         self.path = path
@@ -73,6 +75,8 @@ class Database:
     def name(self):
         if self.is_memory:
             return ":memory:"
+        elif self.stem:
+            return self.stem
         else:
             return Path(self.path).stem

@simonw
Copy link
Owner Author

simonw commented Dec 18, 2020

Related problem caused by the new _schemas database - if a user attempts to open their own _schemas.db file it will fail. I'd like to open and mount that as /_schemas_ instead.

@simonw simonw added this to the Datasette 1.0 milestone Dec 18, 2020
@simonw
Copy link
Owner Author

simonw commented Dec 22, 2020

I'm fixing this in #1155.

@simonw
Copy link
Owner Author

simonw commented Dec 22, 2020

If you open multiple files with the same filename, e.g. like this:

datasette fixtures.db templates/fixtures.db plugins/fixtures.db

You'll now get this:

Datasette__fixtures__fixtures_2__fixtures_3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant