-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Ability to append an upload to a previously uploaded file/sqlite DB as a new table - Good cache busting and detection of file changes on uploads - Separate the upload UI from the 'add connection' UI, as they are materially different - Fix a small bug with bar chart generation, when values are null - Ability to refresh a connection's schema and data (if it's an upload) from the connections list view
- Loading branch information
1 parent
3546dbf
commit 841549d
Showing
25 changed files
with
587 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,43 @@ | ||
import os | ||
from io import BytesIO | ||
|
||
from explorer.utils import secure_filename | ||
from explorer.ee.db_connections.type_infer import get_parser | ||
from explorer.ee.db_connections.utils import pandas_to_sqlite | ||
from explorer.ee.db_connections.utils import pandas_to_sqlite, uploaded_db_local_path | ||
|
||
|
||
def parse_to_sqlite(file) -> (BytesIO, str): | ||
f_name = file.name | ||
f_bytes = file.read() | ||
def get_names(file, append_conn=None, user_id=None): | ||
s_filename = secure_filename(file.name) | ||
table_name, _ = os.path.splitext(s_filename) | ||
|
||
# f_name represents the filename of both the sqlite DB on S3, and on the local filesystem. | ||
# If we are appending to an existing data source, then we re-use the same name. | ||
# New connections get a new database name. | ||
if append_conn: | ||
f_name = os.path.basename(append_conn.name) | ||
else: | ||
f_name = f"{table_name}_{user_id}.db" | ||
|
||
return table_name, f_name | ||
|
||
|
||
def parse_to_sqlite(file, append_conn=None, user_id=None) -> (BytesIO, str): | ||
|
||
table_name, f_name = get_names(file, append_conn, user_id) | ||
|
||
# When appending, make sure the database exists locally so that we can write to it | ||
if append_conn: | ||
append_conn.download_sqlite_if_needed() | ||
|
||
df_parser = get_parser(file) | ||
if df_parser: | ||
df = df_parser(f_bytes) | ||
try: | ||
f_bytes = pandas_to_sqlite(df, local_path=f"{f_name}_tmp_local.db") | ||
df = df_parser(file.read()) | ||
local_path = uploaded_db_local_path(f_name) | ||
f_bytes = pandas_to_sqlite(df, table_name, local_path) | ||
except Exception as e: # noqa | ||
raise ValueError(f"Error while parsing {f_name}: {e}") from e | ||
# replace the previous extension with .db, as it is now a sqlite file | ||
name, _ = os.path.splitext(f_name) | ||
f_name = f"{name}.db" | ||
else: | ||
return BytesIO(f_bytes), f_name # if it's a SQLite file already, simply cough it up as a BytesIO object | ||
# If it's a SQLite file already, simply cough it up as a BytesIO object | ||
return BytesIO(file.read()), f_name | ||
return f_bytes, f_name | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.