-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2926 from IamEzio/remove-duplication
Removed code duplication while importing datafiles
- Loading branch information
Showing
7 changed files
with
119 additions
and
163 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from mathesar.database.base import create_mathesar_engine | ||
from mathesar.models.base import Table | ||
from mathesar.imports.csv import create_db_table_from_csv_data_file | ||
from mathesar.imports.json import create_db_table_from_json_data_file | ||
from db.tables.operations.select import get_oid_from_table | ||
from mathesar.errors import InvalidTableError | ||
|
||
ALLOWED_DELIMITERS = ",\t:|;" | ||
SAMPLE_SIZE = 20000 | ||
CHECK_ROWS = 10 | ||
|
||
|
||
def create_table_from_data_file(data_file, name, schema, comment=None): | ||
engine = create_mathesar_engine(schema.database.name) | ||
if data_file.type == 'csv' or data_file.type == 'tsv': | ||
db_table = create_db_table_from_csv_data_file( | ||
data_file, name, schema, comment=comment | ||
) | ||
elif data_file.type == 'json': | ||
db_table = create_db_table_from_json_data_file( | ||
data_file, name, schema, comment=comment | ||
) | ||
else: | ||
raise InvalidTableError | ||
db_table_oid = get_oid_from_table(db_table.name, db_table.schema, engine) | ||
# Using current_objects to create the table instead of objects. objects | ||
# triggers re-reflection, which will cause a race condition to create the table | ||
table = Table.current_objects.get( | ||
oid=db_table_oid, | ||
schema=schema, | ||
) | ||
table.import_verified = False | ||
table.save() | ||
data_file.table_imported_to = table | ||
data_file.save() | ||
return table |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from db.identifiers import truncate_if_necessary | ||
from db.constants import COLUMN_NAME_TEMPLATE | ||
|
||
|
||
def process_column_names(column_names): | ||
column_names = ( | ||
column_name.strip() | ||
for column_name | ||
in column_names | ||
) | ||
column_names = ( | ||
truncate_if_necessary(column_name) | ||
for column_name | ||
in column_names | ||
) | ||
column_names = ( | ||
f"{COLUMN_NAME_TEMPLATE}{i}" if name == '' else name | ||
for i, name | ||
in enumerate(column_names) | ||
) | ||
return list(column_names) |
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.