From 5b5807e672aaeb94266f1e763a83c9baf9ce76f4 Mon Sep 17 00:00:00 2001 From: Dmitry Novikov Date: Mon, 24 Sep 2018 17:29:16 +0300 Subject: [PATCH] migrate to postgresql --- abonapp/migrations/0005_current_tariff.py | 19 ++++ abonapp/models.py | 2 +- .../migrations/0003_auto_20180814_1921.py | 102 ++++++++++++------ statistics/models.py | 1 + 4 files changed, 90 insertions(+), 34 deletions(-) create mode 100644 abonapp/migrations/0005_current_tariff.py diff --git a/abonapp/migrations/0005_current_tariff.py b/abonapp/migrations/0005_current_tariff.py new file mode 100644 index 0000000..75bdf40 --- /dev/null +++ b/abonapp/migrations/0005_current_tariff.py @@ -0,0 +1,19 @@ +# Generated by Django 2.1 on 2018-09-22 14:24 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [ + ('abonapp', '0004_auto_20180918_1734'), + ] + + operations = [ + migrations.AlterField( + model_name='abon', + name='current_tariff', + field=models.OneToOneField(blank=True, default=None, null=True, + on_delete=django.db.models.deletion.SET_NULL, to='abonapp.AbonTariff'), + ), + ] diff --git a/abonapp/models.py b/abonapp/models.py index 71e5c7d..b3b5892 100644 --- a/abonapp/models.py +++ b/abonapp/models.py @@ -87,7 +87,7 @@ class AbonManager(MyUserManager): class Abon(BaseAccount): - current_tariff = models.OneToOneField(AbonTariff, null=True, blank=True, on_delete=models.SET_NULL) + current_tariff = models.OneToOneField(AbonTariff, null=True, blank=True, on_delete=models.SET_NULL, default=None) group = models.ForeignKey(Group, on_delete=models.SET_NULL, blank=True, null=True, verbose_name=_('User group')) ballance = models.FloatField(default=0.0) ip_addresses = models.ManyToManyField(IpLeaseModel, verbose_name=_('Ip addresses')) diff --git a/statistics/migrations/0003_auto_20180814_1921.py b/statistics/migrations/0003_auto_20180814_1921.py index e932341..9e6c5be 100644 --- a/statistics/migrations/0003_auto_20180814_1921.py +++ b/statistics/migrations/0003_auto_20180814_1921.py @@ -1,43 +1,79 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.11 on 2018-08-14 19:21 -from __future__ import unicode_literals - -from django.db import migrations, models +# Generated by Django 2.1 on 2018-09-22 14:30 +from django.core.exceptions import ImproperlyConfigured +from django.db import migrations, connection, models from statistics.fields import UnixDateTimeField +# def psql_migr(apps, _): +# pass + + class Migration(migrations.Migration): + dependencies = [ - ('abonapp', '0002_auto_20180808_1448'), + ('abonapp', '0005_current_tariff'), ('statistics', '0002_auto_20180808_1236'), ] operations = [ - migrations.RunSQL( - ( - "DROP TABLE `flowcache`;", - "CREATE TABLE `flowcache` ( " - " `last_time` INT(10) UNSIGNED NOT NULL, " - " `abon_id` INT(11) DEFAULT NULL UNIQUE, " - " `octets` INT(10) UNSIGNED NOT NULL, " - " `packets` INT(10) UNSIGNED NOT NULL, " - " KEY `flowcache_abon_id_91e1085d` (`abon_id`) " - ") ENGINE = MEMORY DEFAULT CHARSET = utf8;" - ), - state_operations=[ - migrations.DeleteModel(name='statcache'), - migrations.CreateModel( - name='statcache', - fields=[ - ('last_time', UnixDateTimeField()), - ('abon', models.OneToOneField('abonapp.Abon', on_delete=models.CASCADE, primary_key=True)), - ('octets', models.PositiveIntegerField(default=0)), - ('packets', models.PositiveIntegerField(default=0)) - ], - options={ - 'db_table': 'flowcache', - }, - ) - ] - ) + migrations.AlterModelOptions( + name='statcache', + options={'ordering': ('-last_time',)}, + ), ] + + +db_e = connection.settings_dict.get('ENGINE') +if db_e is None: + raise ImproperlyConfigured('Database ENGINE is not set') +# if 'postgresql' in db_e: +# # Postgres + Migration.operations.insert(0, migrations.RunPython(psql_migr)) +if 'mysql' in db_e: + Migration.operations.insert(0, migrations.RunSQL( + ( + "DROP TABLE `flowcache`;", + "CREATE TABLE `flowcache` ( " + " `last_time` INT(10) UNSIGNED NOT NULL, " + " `abon_id` INT(11) DEFAULT NULL UNIQUE, " + " `octets` INT(10) UNSIGNED NOT NULL, " + " `packets` INT(10) UNSIGNED NOT NULL, " + " KEY `flowcache_abon_id_91e1085d` (`abon_id`) " + ") ENGINE = MEMORY DEFAULT CHARSET = utf8;" + ), + state_operations=[ + migrations.DeleteModel(name='statcache'), + migrations.CreateModel( + name='statcache', + fields=[ + ('last_time', UnixDateTimeField()), + ('abon', models.OneToOneField('abonapp.Abon', on_delete=models.CASCADE, primary_key=True)), + ('octets', models.PositiveIntegerField(default=0)), + ('packets', models.PositiveIntegerField(default=0)) + ], + options={ + 'db_table': 'flowcache', + }, + ) + ] + )) +else: + Migration.operations.extend( + ( + migrations.DeleteModel(name='statcache'), + migrations.CreateModel( + name='statcache', + fields=[ + ('last_time', UnixDateTimeField()), + ('abon', models.OneToOneField('abonapp.Abon', on_delete=models.CASCADE, primary_key=True)), + ('octets', models.PositiveIntegerField(default=0)), + ('packets', models.PositiveIntegerField(default=0)) + ], + options={ + 'db_table': 'flowcache', + 'ordering': ('-last_time',), + #'db_tablespace': 'ram' + }, + ) + ) + ) diff --git a/statistics/models.py b/statistics/models.py index 2aea821..6812a0c 100644 --- a/statistics/models.py +++ b/statistics/models.py @@ -129,3 +129,4 @@ class StatCache(models.Model): class Meta: db_table = 'flowcache' ordering = ('-last_time',) + # db_tablespace = 'ram'