Add independent MIT account-rate500 policy source supplement
This commit is contained in:
+58
@@ -0,0 +1,58 @@
|
||||
"""Account configuration only; does not activate the disabled budget executor."""
|
||||
import django.core.validators
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
def lock_accounts_before_policy_ddl(apps, schema_editor):
|
||||
User = apps.get_model("accounts", "User")
|
||||
Policy = apps.get_model("shared_budget", "AccountBandwidthPolicy")
|
||||
if schema_editor.connection.vendor == "postgresql":
|
||||
# Serialize registration and policy edits for complete coverage. The
|
||||
# existing fixed-ID exemption and every owner policy byte stay intact.
|
||||
quote = schema_editor.connection.ops.quote_name
|
||||
with schema_editor.connection.cursor() as cursor:
|
||||
cursor.execute("LOCK TABLE " + quote(User._meta.db_table) + " IN SHARE MODE")
|
||||
cursor.execute("LOCK TABLE " + quote(Policy._meta.db_table) + " IN SHARE ROW EXCLUSIVE MODE")
|
||||
|
||||
|
||||
def set_ordinary_account_defaults(apps, schema_editor):
|
||||
User = apps.get_model("accounts", "User")
|
||||
Policy = apps.get_model("shared_budget", "AccountBandwidthPolicy")
|
||||
database = schema_editor.connection.alias
|
||||
ordinary = User.objects.using(database).exclude(pk=5)
|
||||
policies = Policy.objects.using(database)
|
||||
policies.exclude(user_id=5).update(upload_limit_kib_per_second=500, download_limit_kib_per_second=500)
|
||||
missing = ordinary.exclude(pk__in=policies.values("user_id")).values_list("pk", flat=True)
|
||||
policies.bulk_create([Policy(user_id=pk, upload_limit_kib_per_second=500, download_limit_kib_per_second=500)
|
||||
for pk in missing], batch_size=1000)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [("shared_budget", "0001_initial")]
|
||||
operations = [
|
||||
# Must precede every DDL table lock: registration holds User first and
|
||||
# its post_save signal writes Policy. Acquiring Policy DDL first could
|
||||
# deadlock that transaction when we subsequently lock User.
|
||||
migrations.RunPython(lock_accounts_before_policy_ddl, migrations.RunPython.noop),
|
||||
migrations.RemoveConstraint(model_name="accountbandwidthpolicy", name="sb_policy_upload_bounded"),
|
||||
migrations.RemoveConstraint(model_name="accountbandwidthpolicy", name="sb_policy_download_bounded"),
|
||||
migrations.RemoveConstraint(model_name="accountbandwidthbucket", name="bandwidth_bucket_rate_bounded"),
|
||||
migrations.RemoveConstraint(model_name="accountbandwidthbucket", name="bandwidth_bucket_credit_bounded"),
|
||||
migrations.AlterField(model_name="accountbandwidthpolicy", name="upload_limit_kib_per_second",
|
||||
field=models.PositiveSmallIntegerField("账号上传上限(KiB/s)", default=500, db_default=500,
|
||||
validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(10240)])),
|
||||
migrations.AlterField(model_name="accountbandwidthpolicy", name="download_limit_kib_per_second",
|
||||
field=models.PositiveSmallIntegerField("账号下载上限(KiB/s)", default=500, db_default=500,
|
||||
validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(10240)])),
|
||||
migrations.AddConstraint(model_name="accountbandwidthpolicy", constraint=models.CheckConstraint(
|
||||
condition=models.Q(upload_limit_kib_per_second__gte=1, upload_limit_kib_per_second__lte=10240), name="sb_policy_upload_bounded")),
|
||||
migrations.AddConstraint(model_name="accountbandwidthpolicy", constraint=models.CheckConstraint(
|
||||
condition=models.Q(download_limit_kib_per_second__gte=1, download_limit_kib_per_second__lte=10240), name="sb_policy_download_bounded")),
|
||||
migrations.AddConstraint(model_name="accountbandwidthbucket", constraint=models.CheckConstraint(
|
||||
condition=models.Q(rate_bytes_per_second__gte=1, rate_bytes_per_second__lte=10485760), name="bandwidth_bucket_rate_bounded")),
|
||||
migrations.AddConstraint(model_name="accountbandwidthbucket", constraint=models.CheckConstraint(
|
||||
condition=models.Q(available_microbytes__lte=2097152000000), name="bandwidth_bucket_credit_bounded")),
|
||||
# Irreversible data policy: an automatic downgrade must not overwrite
|
||||
# later administrator choices or silently recreate 200 KiB/s limits.
|
||||
migrations.RunPython(set_ordinary_account_defaults),
|
||||
]
|
||||
Reference in New Issue
Block a user