mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2025-02-25 18:55:31 -06:00
Add option to generate SQL with DROP table DDL in ERD Tool. #4997
This commit is contained in:
@@ -28,7 +28,7 @@ from pgadmin.browser.server_groups.servers.databases.schemas.utils \
|
||||
from pgadmin.browser.server_groups.servers.databases.schemas.tables. \
|
||||
constraints.foreign_key import utils as fkey_utils
|
||||
from pgadmin.utils.constants import PREF_LABEL_KEYBOARD_SHORTCUTS, \
|
||||
PREF_LABEL_DISPLAY
|
||||
PREF_LABEL_DISPLAY, PREF_LABEL_OPTIONS
|
||||
from .utils import ERDHelper
|
||||
from pgadmin.utils.exception import ConnectionLost
|
||||
|
||||
@@ -371,6 +371,19 @@ class ERDModule(PgAdminModule):
|
||||
fields=shortcut_fields
|
||||
)
|
||||
|
||||
self.preference.register(
|
||||
'options',
|
||||
'sql_with_drop',
|
||||
gettext('SQL With DROP Table'),
|
||||
'boolean',
|
||||
False,
|
||||
category_label=PREF_LABEL_OPTIONS,
|
||||
help_str=gettext(
|
||||
'If enabled, the SQL generated by the ERD Tool will add '
|
||||
'DROP table DDL before each CREATE table DDL.'
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
blueprint = ERDModule(MODULE_NAME, __name__, static_url_path='/static')
|
||||
|
||||
@@ -562,6 +575,10 @@ def translate_foreign_keys(tab_fks, tab_data, all_nodes):
|
||||
@login_required
|
||||
def sql(trans_id, sgid, sid, did):
|
||||
data = json.loads(request.data, encoding='utf-8')
|
||||
with_drop = False
|
||||
if request.args and 'with_drop' in request.args:
|
||||
with_drop = True if request.args.get('with_drop') == 'true' else False
|
||||
|
||||
helper = ERDHelper(trans_id, sid, did)
|
||||
conn = _get_connection(sid, did, trans_id)
|
||||
|
||||
@@ -572,7 +589,7 @@ def sql(trans_id, sgid, sid, did):
|
||||
tab_fks = tab_data.pop('foreign_key', [])
|
||||
tab_foreign_keys.extend(translate_foreign_keys(tab_fks, tab_data,
|
||||
all_nodes))
|
||||
sql += '\n\n' + helper.get_table_sql(tab_data)
|
||||
sql += '\n\n' + helper.get_table_sql(tab_data, with_drop=with_drop)
|
||||
|
||||
for tab_fk in tab_foreign_keys:
|
||||
fk_sql, name = fkey_utils.get_sql(conn, tab_fk, None)
|
||||
|
||||
Reference in New Issue
Block a user