From 594500c4c751f05c184d75255de3eb58a770640b Mon Sep 17 00:00:00 2001 From: Griatch Date: Sun, 9 Jul 2017 11:51:47 +0200 Subject: [PATCH] Correct migrations to not re-remove fields. --- .../migrations/0013_auto_20170705_1726.py | 60 +++++++++++++------ .../migrations/0015_auto_20170706_2041.py | 46 +++++++++----- evennia/comms/models.py | 11 ++-- .../0009_remove_objectdb_db_player.py | 31 +++++++--- .../0011_remove_scriptdb_db_player.py | 29 ++++++--- 5 files changed, 123 insertions(+), 54 deletions(-) diff --git a/evennia/comms/migrations/0013_auto_20170705_1726.py b/evennia/comms/migrations/0013_auto_20170705_1726.py index b48b822b7b..e5c2dacb3d 100644 --- a/evennia/comms/migrations/0013_auto_20170705_1726.py +++ b/evennia/comms/migrations/0013_auto_20170705_1726.py @@ -5,6 +5,7 @@ from __future__ import unicode_literals from django.db import migrations, models, connection from django.db import OperationalError + def _table_exists(db_cursor, tablename): "Returns bool if table exists or not" sql_check_exists = "SELECT * from %s;" % tablename @@ -24,12 +25,48 @@ class Migration(migrations.Migration): db_cursor = connection.cursor() - if not _table_exists(db_cursor, 'comms_msg_db_hide_from_players'): + operations = [ + migrations.AddField( + model_name='channeldb', + name='db_account_subscriptions', + field=models.ManyToManyField(blank=True, db_index=True, related_name='account_subscription_set', to='accounts.AccountDB', verbose_name=b'account subscriptions'), + ), + migrations.AlterField( + model_name='channeldb', + name='db_object_subscriptions', + field=models.ManyToManyField(blank=True, db_index=True, related_name='object_subscription_set', to='objects.ObjectDB', verbose_name=b'object subscriptions'), + ), + migrations.AlterField( + model_name='msg', + name='db_receivers_scripts', + field=models.ManyToManyField(blank=True, help_text=b'script_receivers', related_name='receiver_script_set', to='scripts.ScriptDB'), + ), + migrations.AlterField( + model_name='msg', + name='db_sender_scripts', + field=models.ManyToManyField(blank=True, db_index=True, related_name='sender_script_set', to='scripts.ScriptDB', verbose_name=b'sender(script)'), + ), + migrations.AlterField( + model_name='channeldb', + name='db_object_subscriptions', + field=models.ManyToManyField(blank=True, db_index=True, related_name='object_subscription_set', to='objects.ObjectDB', verbose_name=b'object subscriptions'), + ), + migrations.AlterField( + model_name='msg', + name='db_receivers_scripts', + field=models.ManyToManyField(blank=True, help_text=b'script_receivers', related_name='receiver_script_set', to='scripts.ScriptDB'), + ), + migrations.AlterField( + model_name='msg', + name='db_sender_scripts', + field=models.ManyToManyField(blank=True, db_index=True, related_name='sender_script_set', to='scripts.ScriptDB', verbose_name=b'sender(script)'), + ), + ] + + if _table_exists(db_cursor, 'comms_msg_db_hide_from_players'): # OBS - this is run BEFORE migrations are run! # not a migration of an existing database - operations = [] - else: - operations = [ + operations += [ migrations.AddField( model_name='channeldb', name='db_account_subscriptions', @@ -50,19 +87,4 @@ class Migration(migrations.Migration): name='db_sender_accounts', field=models.ManyToManyField(blank=True, db_index=True, related_name='sender_account_set', to='accounts.AccountDB', verbose_name=b'sender(account)'), ), - migrations.AlterField( - model_name='channeldb', - name='db_object_subscriptions', - field=models.ManyToManyField(blank=True, db_index=True, related_name='object_subscription_set', to='objects.ObjectDB', verbose_name=b'object subscriptions'), - ), - migrations.AlterField( - model_name='msg', - name='db_receivers_scripts', - field=models.ManyToManyField(blank=True, help_text=b'script_receivers', related_name='receiver_script_set', to='scripts.ScriptDB'), - ), - migrations.AlterField( - model_name='msg', - name='db_sender_scripts', - field=models.ManyToManyField(blank=True, db_index=True, related_name='sender_script_set', to='scripts.ScriptDB', verbose_name=b'sender(script)'), - ), ] diff --git a/evennia/comms/migrations/0015_auto_20170706_2041.py b/evennia/comms/migrations/0015_auto_20170706_2041.py index 0aa6dae002..607cabef29 100644 --- a/evennia/comms/migrations/0015_auto_20170706_2041.py +++ b/evennia/comms/migrations/0015_auto_20170706_2041.py @@ -2,7 +2,17 @@ # Generated by Django 1.11.2 on 2017-07-06 20:41 from __future__ import unicode_literals -from django.db import migrations +from django.db import migrations, OperationalError, connection + + +def _table_exists(db_cursor, tablename): + "Returns bool if table exists or not" + sql_check_exists = "SELECT * from %s;" % tablename + try: + db_cursor.execute(sql_check_exists) + return True + except OperationalError: + return False class Migration(migrations.Migration): @@ -11,17 +21,23 @@ class Migration(migrations.Migration): ('comms', '0014_auto_20170705_1736'), ] - operations = [ - migrations.RemoveField( - model_name='channeldb', - name='db_subscriptions', - ), - migrations.RemoveField( - model_name='msg', - name='db_receivers_accounts', - ), - migrations.RemoveField( - model_name='msg', - name='db_sender_accounts', - ), - ] + db_cursor = connection.cursor() + + if not _table_exists(db_cursor, "channels.channeldb_db_receivers_players"): + # OBS - this is run BEFORE migrations are run! + operations = [] + else: + operations = [ + migrations.RemoveField( + model_name='channeldb', + name='db_subscriptions', # this is now db_account_subscriptions + ), + migrations.RemoveField( + model_name='msg', + name='db_receivers_players', + ), + migrations.RemoveField( + model_name='msg', + name='db_sender_players', + ), + ] diff --git a/evennia/comms/models.py b/evennia/comms/models.py index 914b81da5f..c0168150f1 100644 --- a/evennia/comms/models.py +++ b/evennia/comms/models.py @@ -483,7 +483,7 @@ class SubscriptionHandler(object): self._cache = None def _recache(self): - self._cache = {account : True for account in self.obj.db_subscriptions.all()} + self._cache = {account : True for account in self.obj.db_account_subscriptions.all()} self._cache.update({obj : True for obj in self.obj.db_object_subscriptions.all()}) def has(self, entity): @@ -528,7 +528,7 @@ class SubscriptionHandler(object): if clsname == "ObjectDB": self.obj.db_object_subscriptions.add(subscriber) elif clsname == "AccountDB": - self.obj.db_subscriptions.add(subscriber) + self.obj.db_account_subscriptions.add(subscriber) _CHANNELHANDLER.cached_cmdsets.pop(subscriber, None) self._recache() @@ -549,7 +549,7 @@ class SubscriptionHandler(object): clsname = subscriber.__dbclass__.__name__ # chooses the right type if clsname == "AccountDB": - self.obj.db_subscriptions.remove(entity) + self.obj.db_account_subscriptions.remove(entity) elif clsname == "ObjectDB": self.obj.db_object_subscriptions.remove(entity) _CHANNELHANDLER.cached_cmdsets.pop(subscriber, None) @@ -591,7 +591,7 @@ class SubscriptionHandler(object): Remove all subscribers from channel. """ - self.obj.db_subscriptions.clear() + self.obj.db_account_subscriptions.clear() self.obj.db_object_subscriptions.clear() self._cache = None @@ -604,8 +604,7 @@ class ChannelDB(TypedObject): The Channel class defines the following database fields beyond the ones inherited from TypedObject: - - db_subscriptions: The Account subscriptions (this is the most - usual case, named this way for legacy. + - db_account_subscriptions: The Account subscriptions. - db_object_subscriptions: The Object subscriptions. """ diff --git a/evennia/objects/migrations/0009_remove_objectdb_db_player.py b/evennia/objects/migrations/0009_remove_objectdb_db_player.py index 740d7eca66..1dd84cfded 100644 --- a/evennia/objects/migrations/0009_remove_objectdb_db_player.py +++ b/evennia/objects/migrations/0009_remove_objectdb_db_player.py @@ -2,7 +2,18 @@ # Generated by Django 1.11.2 on 2017-07-06 20:41 from __future__ import unicode_literals -from django.db import migrations +from django.db import migrations, connection +from django.db import OperationalError + + +def _table_exists(db_cursor, tablename): + "Returns bool if table exists or not" + sql_check_exists = "SELECT * from %s;" % tablename + try: + db_cursor.execute(sql_check_exists) + return True + except OperationalError: + return False class Migration(migrations.Migration): @@ -11,9 +22,15 @@ class Migration(migrations.Migration): ('objects', '0008_auto_20170705_1736'), ] - operations = [ - migrations.RemoveField( - model_name='objectdb', - name='db_account', - ), - ] + db_cursor = connection.cursor() + + if not _table_exists(db_cursor, "objectdb_db_player"): + # OBS - this is run BEFORE migrations are run! + operations = [] + else: + operations = [ + migrations.RemoveField( + model_name='objectdb', + name='db_player', + ), + ] diff --git a/evennia/scripts/migrations/0011_remove_scriptdb_db_player.py b/evennia/scripts/migrations/0011_remove_scriptdb_db_player.py index 66157f65f9..40f8269bb8 100644 --- a/evennia/scripts/migrations/0011_remove_scriptdb_db_player.py +++ b/evennia/scripts/migrations/0011_remove_scriptdb_db_player.py @@ -2,7 +2,17 @@ # Generated by Django 1.11.2 on 2017-07-06 20:41 from __future__ import unicode_literals -from django.db import migrations +from django.db import migrations, OperationalError, connection + + +def _table_exists(db_cursor, tablename): + "Returns bool if table exists or not" + sql_check_exists = "SELECT * from %s;" % tablename + try: + db_cursor.execute(sql_check_exists) + return True + except OperationalError: + return False class Migration(migrations.Migration): @@ -11,9 +21,14 @@ class Migration(migrations.Migration): ('scripts', '0010_auto_20170705_1736'), ] - operations = [ - migrations.RemoveField( - model_name='scriptdb', - name='db_account', - ), - ] + db_cursor = connection.cursor() + + if not _table_exists(db_cursor, 'scripts_scriptdb_db_player'): + operations = [] + else: + operations = [ + migrations.RemoveField( + model_name='scriptdb', + name='db_player', + ), + ]