Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ dateparser = "~=1.1"
# Use Twelve-factor methodology to configure environment variables
django-environ = "<1.0.0"
django_compressor = "~=4.4"
# Serveradmin extras:
# Used for API access - adminapi
paramiko = ">=2.7,<4"
pexpect = "<5.0.0"
rich = "*"
typing-extensions = "*"

[dev-packages]
Expand Down
141 changes: 54 additions & 87 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 48 additions & 64 deletions serveradmin/serverdb/migrations/0012_migrate_change_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import math

from django.db import migrations, transaction, connection
from rich.progress import Progress

BATCH_SIZE = 100000

Expand All @@ -13,83 +12,68 @@ def migrate_change_add(apps, schema_editor):
total = change_add.objects.count()
batches = math.ceil(total / BATCH_SIZE)

with Progress() as progress:
migration = progress.add_task("\t[green]Migrating ChangeAdd data...", total=batches)

with connection.cursor() as cursor:
while not progress.finished:
with transaction.atomic():
cursor.execute("""
WITH moved AS (
DELETE FROM serverdb_changeadd
WHERE id IN (SELECT id FROM serverdb_changeadd ORDER BY id DESC LIMIT %s FOR UPDATE)
RETURNING
server_id as object_id,
'create' as change_type,
attributes_json::jsonb as change_json,
commit_id
)
INSERT INTO serverdb_change (object_id, change_type, change_json, commit_id)
SELECT * FROM moved;
""", [BATCH_SIZE])

progress.update(migration, advance=1)
with connection.cursor() as cursor:
for _ in range(batches):
with transaction.atomic():
cursor.execute("""
WITH moved AS (
DELETE FROM serverdb_changeadd
WHERE id IN (SELECT id FROM serverdb_changeadd ORDER BY id DESC LIMIT %s FOR UPDATE)
RETURNING
server_id as object_id,
'create' as change_type,
attributes_json::jsonb as change_json,
commit_id
)
INSERT INTO serverdb_change (object_id, change_type, change_json, commit_id)
SELECT * FROM moved;
""", [BATCH_SIZE])


def migrate_change_delete(apps, schema_editor):
change_delete = apps.get_model('serverdb', 'ChangeDelete')
total = change_delete.objects.count()
batches = math.ceil(total / BATCH_SIZE)

with Progress() as progress:
migration = progress.add_task("\t[green]Migrating ChangeDelete data...", total=batches)

with connection.cursor() as cursor:
while not progress.finished:
with transaction.atomic():
cursor.execute("""
WITH moved AS (
DELETE FROM serverdb_changedelete
WHERE id IN (SELECT id FROM serverdb_changedelete ORDER BY id DESC LIMIT %s FOR UPDATE)
RETURNING
server_id as object_id,
'delete' as change_type,
attributes_json::jsonb as change_json,
commit_id
)
INSERT INTO serverdb_change (object_id, change_type, change_json, commit_id)
SELECT * FROM moved;
""", [BATCH_SIZE])

progress.update(migration, advance=1)
with connection.cursor() as cursor:
for _ in range(batches):
with transaction.atomic():
cursor.execute("""
WITH moved AS (
DELETE FROM serverdb_changedelete
WHERE id IN (SELECT id FROM serverdb_changedelete ORDER BY id DESC LIMIT %s FOR UPDATE)
RETURNING
server_id as object_id,
'delete' as change_type,
attributes_json::jsonb as change_json,
commit_id
)
INSERT INTO serverdb_change (object_id, change_type, change_json, commit_id)
SELECT * FROM moved;
""", [BATCH_SIZE])


def migrate_change_update(apps, schema_editor):
change_update = apps.get_model('serverdb', 'ChangeUpdate')
total = change_update.objects.count()
batches = math.ceil(total / BATCH_SIZE)

with Progress() as progress:
migration = progress.add_task("\t[green]Migrating ChangeUpdate data...", total=batches)

with connection.cursor() as cursor:
while not progress.finished:
with transaction.atomic():
cursor.execute("""
WITH moved AS (
DELETE FROM serverdb_changeupdate
WHERE id IN (SELECT id FROM serverdb_changeupdate ORDER BY id DESC LIMIT %s FOR UPDATE)
RETURNING
server_id as object_id,
'change' as change_type,
updates_json::jsonb as change_json,
commit_id
)
INSERT INTO serverdb_change (object_id, change_type, change_json, commit_id)
SELECT * FROM moved;
""", [BATCH_SIZE])

progress.update(migration, advance=1)
with connection.cursor() as cursor:
for _ in range(batches):
with transaction.atomic():
cursor.execute("""
WITH moved AS (
DELETE FROM serverdb_changeupdate
WHERE id IN (SELECT id FROM serverdb_changeupdate ORDER BY id DESC LIMIT %s FOR UPDATE)
RETURNING
server_id as object_id,
'change' as change_type,
updates_json::jsonb as change_json,
commit_id
)
INSERT INTO serverdb_change (object_id, change_type, change_json, commit_id)
SELECT * FROM moved;
""", [BATCH_SIZE])


class Migration(migrations.Migration):
Expand Down
Loading