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 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):

View file

@ -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):

View file

@ -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'),
),
]

View file

@ -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):

View file

@ -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'),
),
]

View file

@ -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):

View file

@ -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):