From 414bacf58bc8abf7ae641b9d7e73ae5857532450 Mon Sep 17 00:00:00 2001 From: Griatch Date: Fri, 22 Sep 2017 23:23:59 +0200 Subject: [PATCH] Fix migration edge cases with MySQL and PostgreSQL --- .../migrations/0013_auto_20170705_1726.py | 8 +----- .../migrations/0015_auto_20170706_2041.py | 9 ++----- .../migrations/0007_objectdb_db_account.py | 26 +++++++++++++------ .../0009_remove_objectdb_db_player.py | 8 +----- .../migrations/0009_scriptdb_db_account.py | 26 +++++++++++++------ .../0011_remove_scriptdb_db_player.py | 9 ++----- .../0010_delete_old_player_tables.py | 9 ++----- 7 files changed, 44 insertions(+), 51 deletions(-) diff --git a/evennia/comms/migrations/0013_auto_20170705_1726.py b/evennia/comms/migrations/0013_auto_20170705_1726.py index 527cd22a57..db42debd2e 100644 --- a/evennia/comms/migrations/0013_auto_20170705_1726.py +++ b/evennia/comms/migrations/0013_auto_20170705_1726.py @@ -3,17 +3,11 @@ 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 - try: - db_cursor.execute(sql_check_exists) - return True - except OperationalError: - return False + return tablename in connection.introspection.table_names() class Migration(migrations.Migration): diff --git a/evennia/comms/migrations/0015_auto_20170706_2041.py b/evennia/comms/migrations/0015_auto_20170706_2041.py index 607cabef29..62b793646b 100644 --- a/evennia/comms/migrations/0015_auto_20170706_2041.py +++ b/evennia/comms/migrations/0015_auto_20170706_2041.py @@ -2,17 +2,12 @@ # Generated by Django 1.11.2 on 2017-07-06 20:41 from __future__ import unicode_literals -from django.db import migrations, OperationalError, connection +from django.db import migrations, 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 + return tablename in connection.introspection.table_names() class Migration(migrations.Migration): diff --git a/evennia/objects/migrations/0007_objectdb_db_account.py b/evennia/objects/migrations/0007_objectdb_db_account.py index 4245a669bb..6e40252e8e 100644 --- a/evennia/objects/migrations/0007_objectdb_db_account.py +++ b/evennia/objects/migrations/0007_objectdb_db_account.py @@ -2,10 +2,15 @@ # Generated by Django 1.11.2 on 2017-07-05 17:27 from __future__ import unicode_literals -from django.db import migrations, models +from django.db import migrations, models, connection import django.db.models.deletion +def _table_exists(db_cursor, tablename): + "Returns bool if table exists or not" + return tablename in connection.introspection.table_names() + + class Migration(migrations.Migration): dependencies = [ @@ -13,10 +18,15 @@ class Migration(migrations.Migration): ('objects', '0006_auto_20170606_1731'), ] - operations = [ - migrations.AddField( - model_name='objectdb', - name='db_account', - field=models.ForeignKey(help_text=b'an Account connected to this object, if any.', null=True, on_delete=django.db.models.deletion.SET_NULL, to='accounts.AccountDB', verbose_name=b'account'), - ), - ] + db_cursor = connection.cursor() + operations = [] + if _table_exists(db_cursor, "players_playerdb"): + # OBS - this is run BEFORE migrations even start, so if we have a player table + # here we are not starting from scratch. + operations = [ + migrations.AddField( + model_name='objectdb', + name='db_account', + field=models.ForeignKey(help_text=b'an Account connected to this object, if any.', null=True, on_delete=django.db.models.deletion.SET_NULL, to='accounts.AccountDB', verbose_name=b'account'), + ), + ] diff --git a/evennia/objects/migrations/0009_remove_objectdb_db_player.py b/evennia/objects/migrations/0009_remove_objectdb_db_player.py index 1dd84cfded..10fb2252f4 100644 --- a/evennia/objects/migrations/0009_remove_objectdb_db_player.py +++ b/evennia/objects/migrations/0009_remove_objectdb_db_player.py @@ -3,17 +3,11 @@ from __future__ import unicode_literals 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 + return tablename in connection.introspection.table_names() class Migration(migrations.Migration): diff --git a/evennia/scripts/migrations/0009_scriptdb_db_account.py b/evennia/scripts/migrations/0009_scriptdb_db_account.py index 427fb9c3f6..23f6df92c1 100644 --- a/evennia/scripts/migrations/0009_scriptdb_db_account.py +++ b/evennia/scripts/migrations/0009_scriptdb_db_account.py @@ -2,10 +2,15 @@ # Generated by Django 1.11.2 on 2017-07-05 17:27 from __future__ import unicode_literals -from django.db import migrations, models +from django.db import migrations, models, connection import django.db.models.deletion +def _table_exists(db_cursor, tablename): + "Returns bool if table exists or not" + return tablename in connection.introspection.table_names() + + class Migration(migrations.Migration): dependencies = [ @@ -13,10 +18,15 @@ class Migration(migrations.Migration): ('scripts', '0008_auto_20170606_1731'), ] - operations = [ - migrations.AddField( - model_name='scriptdb', - name='db_account', - field=models.ForeignKey(blank=True, help_text=b'the account to store this script on (should not be set if db_obj is set)', null=True, on_delete=django.db.models.deletion.CASCADE, to='accounts.AccountDB', verbose_name=b'scripted account'), - ), - ] + db_cursor = connection.cursor() + operations = [] + if _table_exists(db_cursor, "players_playerdb"): + # OBS - this is run BEFORE migrations even start, so if we have a player table + # here we are not starting from scratch. + operations = [ + migrations.AddField( + model_name='scriptdb', + name='db_account', + field=models.ForeignKey(blank=True, help_text=b'the account to store this script on (should not be set if db_obj is set)', null=True, on_delete=django.db.models.deletion.CASCADE, to='accounts.AccountDB', verbose_name=b'scripted account'), + ), + ] diff --git a/evennia/scripts/migrations/0011_remove_scriptdb_db_player.py b/evennia/scripts/migrations/0011_remove_scriptdb_db_player.py index 40f8269bb8..20fa63fd50 100644 --- a/evennia/scripts/migrations/0011_remove_scriptdb_db_player.py +++ b/evennia/scripts/migrations/0011_remove_scriptdb_db_player.py @@ -2,17 +2,12 @@ # Generated by Django 1.11.2 on 2017-07-06 20:41 from __future__ import unicode_literals -from django.db import migrations, OperationalError, connection +from django.db import migrations, 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 + return tablename in connection.introspection.table_names() class Migration(migrations.Migration): diff --git a/evennia/typeclasses/migrations/0010_delete_old_player_tables.py b/evennia/typeclasses/migrations/0010_delete_old_player_tables.py index 72dc691f7c..b3881b68ae 100644 --- a/evennia/typeclasses/migrations/0010_delete_old_player_tables.py +++ b/evennia/typeclasses/migrations/0010_delete_old_player_tables.py @@ -2,17 +2,12 @@ # Generated by Django 1.11.3 on 2017-07-13 18:47 from __future__ import unicode_literals -from django.db import migrations, OperationalError, connection +from django.db import migrations, 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 + return tablename in connection.introspection.table_names() def _drop_table(db_cursor, table_name):