Skip to content

Commit c2bfd15

Browse files
authored
fix: add support for sqlite in the apitoken data migration (#2138)
Signed-off-by: tdruez <tdruez@aboutcode.org>
1 parent 5dc3f57 commit c2bfd15

1 file changed

Lines changed: 20 additions & 22 deletions

File tree

scanpipe/migrations/0079_apitoken_data.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,31 @@ def migrate_api_tokens(apps, schema_editor):
99
APIToken = apps.get_model("scanpipe", "APIToken")
1010
PREFIX_LENGTH = 8
1111

12-
with schema_editor.connection.cursor() as cursor:
13-
cursor.execute(
14-
"SELECT EXISTS (SELECT 1 FROM information_schema.tables "
15-
"WHERE table_name = 'authtoken_token')"
16-
)
17-
table_exists = cursor.fetchone()[0]
12+
table_name = "authtoken_token"
1813

19-
if not table_exists:
14+
with schema_editor.connection.cursor() as cursor:
15+
existing_tables = schema_editor.connection.introspection.table_names(cursor)
16+
if table_name not in existing_tables:
2017
return
2118

22-
cursor.execute("SELECT user_id, key, created FROM authtoken_token")
19+
cursor.execute(f"SELECT user_id, key, created FROM {table_name}")
2320
rows = cursor.fetchall()
24-
if not rows:
25-
return
2621

27-
tokens_to_create = [
28-
APIToken(
29-
user_id=user_id,
30-
prefix=key[:PREFIX_LENGTH],
31-
key_hash=make_password(key),
32-
created=created,
33-
)
34-
for user_id, key, created in rows
35-
]
36-
migrated_tokens = APIToken.objects.bulk_create(tokens_to_create, ignore_conflicts=True)
37-
if migrated_tokens:
38-
print(f" -> {len(migrated_tokens)} tokens migrated.")
22+
if not rows:
23+
return
24+
25+
tokens_to_create = [
26+
APIToken(
27+
user_id=user_id,
28+
prefix=key[:PREFIX_LENGTH],
29+
key_hash=make_password(key),
30+
created=created,
31+
)
32+
for user_id, key, created in rows
33+
]
34+
migrated_tokens = APIToken.objects.bulk_create(tokens_to_create, ignore_conflicts=True)
35+
if migrated_tokens:
36+
print(f" -> {len(migrated_tokens)} tokens migrated.")
3937

4038

4139
def reverse_migrate_api_tokens(apps, schema_editor):

0 commit comments

Comments
 (0)