Fix migration edge cases with MySQL and PostgreSQL

This commit is contained in:
Griatch 2017-09-22 23:23:59 +02:00
parent fb773a9e75
commit 414bacf58b
7 changed files with 44 additions and 51 deletions

View file

@ -3,17 +3,11 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import migrations, models, connection from django.db import migrations, models, connection
from django.db import OperationalError
def _table_exists(db_cursor, tablename): def _table_exists(db_cursor, tablename):
"Returns bool if table exists or not" "Returns bool if table exists or not"
sql_check_exists = "SELECT * from %s;" % tablename return tablename in connection.introspection.table_names()
try:
db_cursor.execute(sql_check_exists)
return True
except OperationalError:
return False
class Migration(migrations.Migration): class Migration(migrations.Migration):

View file

@ -2,17 +2,12 @@
# Generated by Django 1.11.2 on 2017-07-06 20:41 # Generated by Django 1.11.2 on 2017-07-06 20:41
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import migrations, OperationalError, connection from django.db import migrations, connection
def _table_exists(db_cursor, tablename): def _table_exists(db_cursor, tablename):
"Returns bool if table exists or not" "Returns bool if table exists or not"
sql_check_exists = "SELECT * from %s;" % tablename return tablename in connection.introspection.table_names()
try:
db_cursor.execute(sql_check_exists)
return True
except OperationalError:
return False
class Migration(migrations.Migration): class Migration(migrations.Migration):

View file

@ -2,10 +2,15 @@
# Generated by Django 1.11.2 on 2017-07-05 17:27 # Generated by Django 1.11.2 on 2017-07-05 17:27
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models, connection
import django.db.models.deletion 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): class Migration(migrations.Migration):
dependencies = [ dependencies = [
@ -13,10 +18,15 @@ class Migration(migrations.Migration):
('objects', '0006_auto_20170606_1731'), ('objects', '0006_auto_20170606_1731'),
] ]
operations = [ db_cursor = connection.cursor()
migrations.AddField( operations = []
model_name='objectdb', if _table_exists(db_cursor, "players_playerdb"):
name='db_account', # OBS - this is run BEFORE migrations even start, so if we have a player table
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'), # 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'),
),
]

View file

@ -3,17 +3,11 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import migrations, connection from django.db import migrations, connection
from django.db import OperationalError
def _table_exists(db_cursor, tablename): def _table_exists(db_cursor, tablename):
"Returns bool if table exists or not" "Returns bool if table exists or not"
sql_check_exists = "SELECT * from %s;" % tablename return tablename in connection.introspection.table_names()
try:
db_cursor.execute(sql_check_exists)
return True
except OperationalError:
return False
class Migration(migrations.Migration): class Migration(migrations.Migration):

View file

@ -2,10 +2,15 @@
# Generated by Django 1.11.2 on 2017-07-05 17:27 # Generated by Django 1.11.2 on 2017-07-05 17:27
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models, connection
import django.db.models.deletion 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): class Migration(migrations.Migration):
dependencies = [ dependencies = [
@ -13,10 +18,15 @@ class Migration(migrations.Migration):
('scripts', '0008_auto_20170606_1731'), ('scripts', '0008_auto_20170606_1731'),
] ]
operations = [ db_cursor = connection.cursor()
migrations.AddField( operations = []
model_name='scriptdb', if _table_exists(db_cursor, "players_playerdb"):
name='db_account', # OBS - this is run BEFORE migrations even start, so if we have a player table
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'), # 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'),
),
]

View file

@ -2,17 +2,12 @@
# Generated by Django 1.11.2 on 2017-07-06 20:41 # Generated by Django 1.11.2 on 2017-07-06 20:41
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import migrations, OperationalError, connection from django.db import migrations, connection
def _table_exists(db_cursor, tablename): def _table_exists(db_cursor, tablename):
"Returns bool if table exists or not" "Returns bool if table exists or not"
sql_check_exists = "SELECT * from %s;" % tablename return tablename in connection.introspection.table_names()
try:
db_cursor.execute(sql_check_exists)
return True
except OperationalError:
return False
class Migration(migrations.Migration): class Migration(migrations.Migration):

View file

@ -2,17 +2,12 @@
# Generated by Django 1.11.3 on 2017-07-13 18:47 # Generated by Django 1.11.3 on 2017-07-13 18:47
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import migrations, OperationalError, connection from django.db import migrations, connection
def _table_exists(db_cursor, tablename): def _table_exists(db_cursor, tablename):
"Returns bool if table exists or not" "Returns bool if table exists or not"
sql_check_exists = "SELECT * from %s;" % tablename return tablename in connection.introspection.table_names()
try:
db_cursor.execute(sql_check_exists)
return True
except OperationalError:
return False
def _drop_table(db_cursor, table_name): def _drop_table(db_cursor, table_name):