Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@eyeseast
Copy link
Contributor

@eyeseast eyeseast commented Mar 11, 2023

This also passes in the extension path when specified in GIS methods. Wherever we know an extension path, we use db.init_spatialite(find_spatialite() or load_extension).


๐Ÿ“š Documentation preview ๐Ÿ“š: https://sqlite-utils--531.org.readthedocs.build/en/531/

@codecov
Copy link

codecov bot commented Mar 11, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.03 ๐ŸŽ‰

Comparison is base (c0251cc) 96.25% compared to head (afdf618) 96.29%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #531      +/-   ##
==========================================
+ Coverage   96.25%   96.29%   +0.03%     
==========================================
  Files           6        6              
  Lines        2671     2671              
==========================================
+ Hits         2571     2572       +1     
+ Misses        100       99       -1     
Impacted Files Coverage ฮ”
sqlite_utils/utils.py 95.13% <รธ> (รธ)
sqlite_utils/cli.py 95.26% <100.00%> (+0.09%) โฌ†๏ธ

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

โ˜” View full report in Codecov by Sentry.
๐Ÿ“ข Do you have feedback about the report comment? Let us know in this issue.

# load spatialite from expected locations and initialize metadata
if init_spatialite:
db.init_spatialite()
db.init_spatialite(find_spatialite() or load_extension)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this pattern.

The init_spatialite() function already calls find_spatialite() if necessary:

if path is None:
path = find_spatialite()
self.conn.enable_load_extension(True)
self.conn.load_extension(path)

The load_extension option is a bit different: It's actually a list of extensions passed using --load-extension path on the command-line, which is used by this code:

def _load_extensions(db, load_extension):
if load_extension:
db.conn.enable_load_extension(True)
for ext in load_extension:
if ext == "spatialite" and not os.path.exists(ext):
ext = find_spatialite()
if ":" in ext:
path, _, entrypoint = ext.partition(":")
db.conn.execute("SELECT load_extension(?, ?)", [path, entrypoint])
else:
db.conn.load_extension(ext)

@simonw
Copy link
Owner

simonw commented Mar 12, 2023

Aah, I think I see why you wrote it like that.

The problem is that init_spatialite() does other stuff too:

if path is None:
path = find_spatialite()
self.conn.enable_load_extension(True)
self.conn.load_extension(path)
# Initialize SpatiaLite if not yet initialized
if "spatial_ref_sys" in self.table_names():
return False
cursor = self.execute("select InitSpatialMetadata(1)")
result = cursor.fetchone()
return result and bool(result[0])

So it needs to be able to load the SpatiaLite extension from the correct place, and THEN run select InitSpatialMetadata() to configure the database, if needed.

So the problem you're trying to solve here is to let people optionally pass in the path to SpatiaLite if it's not one of the ones that are searched by default.

@eyeseast
Copy link
Contributor Author

Exactly, that's what I was running into. On my M2 MacBook, SpatiaLite ends up in what is -- for the moment -- a non-standard location, so even when I passed in the location with --load-extension, I still hit an error on create-spatial-index.

What I learned doing this originally is that SQLite needs to load the extension for each connection, even if all the SpatiaLite stuff is already in the database. So that's why init_spatialite() gets called again.

Here's the code where I hit the error: https://github.com/eyeseast/boston-parcels/blob/main/Makefile#L30 It works using this branch.

I'm not attached to this solution if you can think of something better. And I'm not sure, TBH, my test would actually catch what I'm after here.

@eyeseast
Copy link
Contributor Author

eyeseast commented Apr 9, 2023

I'm going to close this in favor of #536. Will try a cleaner approach to custom paths once that one is merge.

@eyeseast eyeseast closed this Apr 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants