fix: insert default instance configurations using migrations scripts
ci/woodpecker/push/woodpecker Pipeline was successful Details

wip-payments
Aravinth Manivannan 2022-06-19 13:36:28 +05:30
parent 3f7f9fc1ed
commit 429934c795
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
5 changed files with 35 additions and 29 deletions

View File

@ -8,7 +8,6 @@ pipeline:
- pip install virtualenv
- make env
- make lint
- make migrate
- make test
- make coverage

View File

@ -18,8 +18,3 @@ from django.apps import AppConfig
class DashConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "dash"
def ready(self):
from .models import InstanceConfiguration
InstanceConfiguration.create_default_configs()

View File

@ -0,0 +1,35 @@
# Generated by Django 4.0.3 on 2022-06-19 08:00
from django.db import migrations
from django.db.utils import IntegrityError
def create_default_configs(apps, schema_editor):
InstanceConfiguration = apps.get_model('dash', 'InstanceConfiguration')
configs = [
InstanceConfiguration(name="s1-2", rent=10, ram=2, cpu=1, storage=10),
InstanceConfiguration(name="s1-4", rent=20, ram=4, cpu=1, storage=20),
InstanceConfiguration(name="s1-8", rent=40, ram=8, cpu=2, storage=40),
]
for config in configs:
print(f"[*] Saving configuration {config.name}")
try:
config.save()
except IntegrityError as db_error:
print(f"[!] Configuration {config.name} exists")
continue
except Exception as e:
print(f"[ERROR] {e}")
raise e
class Migration(migrations.Migration):
dependencies = [
('dash', '0005_alter_instance_owned_by'),
]
operations = [
migrations.RunPython(create_default_configs)
]

View File

@ -16,10 +16,8 @@
from django.db import models
from django.contrib.auth.models import User
from django.utils.http import urlencode
from django.db.utils import IntegrityError
from django.urls import reverse
class InstanceConfiguration(models.Model):
ID = models.AutoField(primary_key=True)
name = models.CharField(
@ -43,23 +41,6 @@ class InstanceConfiguration(models.Model):
created_at = models.DateTimeField(auto_now_add=True, blank=True)
@classmethod
def create_default_configs(cls: "InstanceConfiguration"):
configs = [
cls(name="s1-2", rent=10, ram=2, cpu=1, storage=10),
cls(name="s1-4", rent=20, ram=4, cpu=1, storage=20),
cls(name="s1-8", rent=40, ram=8, cpu=2, storage=40),
]
for config in configs:
print(f"[*] Saving configuration {config.name}")
try:
config.save()
except IntegrityError as db_error:
print(f"[!] Configuration {config.name} exists")
continue
except Exception as e:
print(f"[ERROR] {e}")
raise e
def __str__(self):
return f"{self.name}"

View File

@ -21,7 +21,6 @@ from django.db.utils import IntegrityError
from .models import InstanceConfiguration, Instance
def register_util(t: TestCase, username: str):
t.password = "password121231"
t.username = username
@ -133,9 +132,6 @@ class InstancesConfig(TestCase):
ref: https://gitea.hostea.org/Hostea/july-mvp/issues/10#issuecomment-639
"""
InstanceConfiguration.create_default_configs()
self.assertEqual(
InstanceConfiguration.objects.filter(
name="s1-2", rent=10, ram=2, cpu=1, storage=10