Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initial work to separate task processor from main repository #1

Merged
merged 20 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 7 additions & 27 deletions task_processor/migrations/helpers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
import os
import typing
from contextlib import suppress
"""
Note: django doesn't support adding submodules to the migrations module directory
that don't include a Migration class. As such, I've defined this helpers submodule
and simplified the imports by defining the __all__ attribute.
"""

from django.db import migrations
from task_processor.migrations.helpers.postgres_helpers import PostgresOnlyRunSQL

Choose a reason for hiding this comment

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

I like this change 👍🏼


class PostgresOnlyRunSQL(migrations.RunSQL):
@classmethod
def from_sql_file(
cls,
file_path: typing.Union[str, os.PathLike],
reverse_sql: typing.Union[str, os.PathLike] = None,
) -> "PostgresOnlyRunSQL":
with open(file_path) as forward_sql:
with suppress(FileNotFoundError, TypeError):
with open(reverse_sql) as reverse_sql_file:
reverse_sql = reverse_sql_file.read()
return cls(forward_sql.read(), reverse_sql=reverse_sql)

def database_forwards(self, app_label, schema_editor, from_state, to_state):
if schema_editor.connection.vendor != "postgresql":
return
super().database_forwards(app_label, schema_editor, from_state, to_state)

def database_backwards(self, app_label, schema_editor, from_state, to_state):
if schema_editor.connection.vendor != "postgresql":
return
super().database_backwards(app_label, schema_editor, from_state, to_state)
__all__ = ["PostgresOnlyRunSQL"]
29 changes: 29 additions & 0 deletions task_processor/migrations/helpers/postgres_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
import typing
from contextlib import suppress

from django.db import migrations


class PostgresOnlyRunSQL(migrations.RunSQL):
@classmethod
def from_sql_file(
cls,
file_path: typing.Union[str, os.PathLike],
reverse_sql: typing.Union[str, os.PathLike] = None,
) -> "PostgresOnlyRunSQL":
with open(file_path) as forward_sql:
with suppress(FileNotFoundError, TypeError):
with open(reverse_sql) as reverse_sql_file:
reverse_sql = reverse_sql_file.read()
return cls(forward_sql.read(), reverse_sql=reverse_sql)

def database_forwards(self, app_label, schema_editor, from_state, to_state):
if schema_editor.connection.vendor != "postgresql":
return
super().database_forwards(app_label, schema_editor, from_state, to_state)

def database_backwards(self, app_label, schema_editor, from_state, to_state):
if schema_editor.connection.vendor != "postgresql":
return
super().database_backwards(app_label, schema_editor, from_state, to_state)
Loading