-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding "connection-name" optional parameter (#15)
* Adding "connection-name" optional parameter Updating on poetry installation steps, README.md, and requirements to support Django 1.11+ * Changes from feedback: correction of source for DEFAULT_DB_ALIAS Additional tweaks to provide convenient access to the connection_name parameter Setting min Django version to 1.11.17 since that's the first version supporting Python 3.7 * Bump version to 0.2.0 * Update poetry.lock
- Loading branch information
Showing
13 changed files
with
508 additions
and
367 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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
FROM python:3 | ||
FROM python:3.9 | ||
|
||
WORKDIR /code | ||
|
||
RUN pip install --upgrade pip | ||
|
||
# Install and setup Poetry | ||
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python - | ||
RUN echo export PATH=\$PATH:\$HOME/.poetry/bin >> /root/.bashrc | ||
RUN curl -sSL https://install.python-poetry.org | python3 - | ||
RUN echo export PATH=\$PATH:\$HOME/.local/bin >> /root/.bashrc |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,51 @@ | ||
from abc import ABC | ||
|
||
from django.core.management import BaseCommand | ||
from django.db import connections, DEFAULT_DB_ALIAS | ||
from django.db.migrations.recorder import MigrationRecorder | ||
|
||
from vmigration_helper.helpers.migration_records import MigrationRecordsHelper | ||
|
||
|
||
class MigrationCommand(BaseCommand, ABC): | ||
""" | ||
Abstract base class for migration commands in this app. | ||
This class provides a place for common concerns to all migration commands to be implemented. For instance, it | ||
registers the "connection-name" optional parameter to the command and surfaces the parameter as | ||
the "connection_name" property. | ||
""" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'--connection-name', | ||
default=DEFAULT_DB_ALIAS, | ||
help=f'The connection to use for migration commands. Defaults to django.db.DEFAULT_DB_ALIAS' | ||
) | ||
|
||
def execute(self, *args, **options): | ||
self.connection_name = options["connection_name"] | ||
super().execute(*args, **options) | ||
|
||
def create_migration_helper(self, connection=None) -> MigrationRecordsHelper: | ||
""" | ||
Initializes a connection and returns an instance of MigrationRecordsHelper initialized to the connection | ||
provided. | ||
:param connection: optional connection to use. If not provided, a connection to the current connection name | ||
is created and used. If a connection is provided, its prepare_database() MUST have been called. | ||
:returns: an instance of MigrationRecordsHelper | ||
""" | ||
if not connection: | ||
connection = connections[self.connection_name] | ||
connection.prepare_database() | ||
return MigrationRecordsHelper(MigrationRecorder(connection)) | ||
|
||
@property | ||
def connection_name(self) -> str: | ||
return self._connection_name | ||
|
||
@connection_name.setter | ||
def connection_name(self, value: str) -> None: | ||
self._connection_name = value |
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
17 changes: 6 additions & 11 deletions
17
vmigration_helper/management/commands/migration_current_id.py
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,31 +1,26 @@ | ||
from django.core.management import BaseCommand | ||
from django.db import DEFAULT_DB_ALIAS, connections, OperationalError | ||
from django.db.migrations.recorder import MigrationRecorder | ||
from django.db import OperationalError | ||
from django.db.models import Max | ||
|
||
from vmigration_helper.helpers.migration_records import MigrationRecordsHelper | ||
from vmigration_helper.helpers.command import MigrationCommand | ||
|
||
|
||
class Command(BaseCommand): | ||
class Command(MigrationCommand): | ||
""" | ||
Displays the ID of the last entry in the migration records (from the ``django_migrations`` table). | ||
""" | ||
|
||
@staticmethod | ||
def create_snapshot_name(connection) -> int: | ||
def create_snapshot_name(self) -> int: | ||
""" | ||
Returns the current max ID of the migration records table (django_migrations). If there are no records, | ||
0 is returned. | ||
""" | ||
helper = MigrationRecordsHelper(MigrationRecorder(connection)) | ||
helper = self.create_migration_helper() | ||
latest_migration_id = helper.get_migration_records_qs().aggregate(Max('id'))['id__max'] | ||
return latest_migration_id or 0 | ||
|
||
def handle(self, *args, **options): | ||
try: | ||
connection = connections[DEFAULT_DB_ALIAS] | ||
connection.prepare_database() | ||
print(self.create_snapshot_name(connection)) | ||
print(self.create_snapshot_name()) | ||
except OperationalError as e: | ||
print(f'DB ERROR: {e}') | ||
exit(1) |
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