-
Notifications
You must be signed in to change notification settings - Fork 8
Closes #262: Copy migrations table to branch #263
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,52 @@ | ||
from django.db import connection, migrations | ||
|
||
from netbox.plugins import get_plugin_config | ||
from netbox_branching.choices import BranchStatusChoices | ||
|
||
|
||
def copy_migrations(apps, schema_editor): | ||
""" | ||
Create a copy of the migrations table in each active branch. | ||
""" | ||
Branch = apps.get_model('netbox_branching', 'Branch') | ||
|
||
table = 'django_migrations' | ||
schema_prefix = get_plugin_config('netbox_branching', 'schema_prefix') | ||
|
||
with connection.cursor() as cursor: | ||
main_table = f'public.{table}' | ||
|
||
for branch in Branch.objects.filter(status=BranchStatusChoices.READY): | ||
print(f'\n Copying migrations for branch {branch.name} ({branch.schema_id})...', end='') | ||
schema_name = f'{schema_prefix}{branch.schema_id}' | ||
schema_table = f'{schema_name}.{table}' | ||
|
||
# Copy the migrations table to the branch schema | ||
cursor.execute(f"CREATE TABLE {schema_table} ( LIKE {main_table} INCLUDING INDEXES )") | ||
|
||
# Copy table data | ||
cursor.execute(f"INSERT INTO {schema_table} SELECT * FROM {main_table}") | ||
|
||
# Designate id as an identity column | ||
cursor.execute(f"ALTER TABLE {schema_table} ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY") | ||
|
||
# Set the next value for the ID sequence | ||
cursor.execute("SELECT MAX(id) from django_migrations") | ||
starting_id = cursor.fetchone()[0] + 1 | ||
cursor.execute(f"ALTER SEQUENCE {schema_name}.django_migrations_id_seq RESTART WITH {starting_id}") | ||
|
||
print('\n ', end='') # Padding for final "OK" | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('netbox_branching', '0003_rename_indexes'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
code=copy_migrations, | ||
reverse_code=migrations.RunPython.noop | ||
), | ||
] |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth noting this needs to change in v0.6.0, since the other references to
MAIN_SCHEMA
are already changed in #254.