Skip to content

Commit 360f112

Browse files
authored
Merge pull request #26 from stac-utils/fix/rde/migrate
Fix issue with logic around type of object returned by smart_open
2 parents fde6646 + 1f99542 commit 360f112

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

pypgstac/pypgstac/pypgstac.py

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
from io import BufferedIOBase
32
import os
43
import time
54
from typing import Any, AsyncGenerator, Dict, Iterable, Optional, TypeVar
@@ -109,25 +108,24 @@ async def run_migration(dsn: Optional[str] = None) -> str:
109108
f"from {oldversion} to {version} ({migration_file})"
110109
)
111110

112-
open_migration_file = open(migration_file)
113-
if isinstance(open_migration_file, BufferedIOBase):
114-
with open_migration_file as f:
115-
migration_sql = f.read()
116-
logging.debug(migration_sql)
117-
async with conn.transaction():
118-
conn.add_log_listener(pglogger)
119-
await conn.execute(migration_sql)
120-
await conn.execute(
121-
"""
122-
INSERT INTO pgstac.migrations (version)
123-
VALUES ($1);
124-
""",
125-
version,
126-
)
127-
128-
await conn.close()
129-
else:
130-
raise IOError(f"Unable to open {migration_file}")
111+
open_migration_file: Any = open(migration_file)
112+
113+
with open_migration_file as f:
114+
migration_sql = f.read()
115+
logging.debug(migration_sql)
116+
async with conn.transaction():
117+
conn.add_log_listener(pglogger)
118+
await conn.execute(migration_sql)
119+
await conn.execute(
120+
"""
121+
INSERT INTO pgstac.migrations (version)
122+
VALUES ($1);
123+
""",
124+
version,
125+
)
126+
127+
await conn.close()
128+
131129
return version
132130

133131

@@ -293,13 +291,11 @@ async def load_ndjson(
293291
file: str, table: tables, method: loadopt = loadopt.insert, dsn: str = None
294292
) -> None:
295293
print(f"loading {file} into {table} using {method}")
296-
open_file = open(file, "rb")
297-
if isinstance(open_file, BufferedIOBase):
298-
with open_file as f:
299-
async with DB(dsn) as conn:
300-
await load_iterator(f, table, conn, method)
301-
else:
302-
raise IOError(f"Cannot read {file}")
294+
open_file: Any = open(file, "rb")
295+
296+
with open_file as f:
297+
async with DB(dsn) as conn:
298+
await load_iterator(f, table, conn, method)
303299

304300

305301
@app.command()

0 commit comments

Comments
 (0)