2015-06-30 00:51:55 -05:00
|
|
|
#########################################################################
|
2015-01-22 09:56:23 -06:00
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2022-01-04 02:24:25 -06:00
|
|
|
# Copyright (C) 2013 - 2022, The pgAdmin Development Team
|
2015-01-22 09:56:23 -06:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
"""Perform the initial setup of the application, by creating the auth
|
|
|
|
and settings database."""
|
|
|
|
|
2018-11-21 10:09:05 -06:00
|
|
|
import argparse
|
2016-06-21 08:12:14 -05:00
|
|
|
import os
|
|
|
|
import sys
|
2022-09-06 01:18:55 -05:00
|
|
|
|
|
|
|
# We need to include the root directory in sys.path to ensure that we can
|
|
|
|
# find everything we need when running in the standalone runtime.
|
|
|
|
root = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
if sys.path[0] != root:
|
|
|
|
sys.path.insert(0, root)
|
|
|
|
|
2020-04-30 06:52:48 -05:00
|
|
|
import builtins
|
2022-07-01 07:42:00 -05:00
|
|
|
import config
|
2015-10-20 02:03:18 -05:00
|
|
|
|
2017-08-25 05:56:44 -05:00
|
|
|
# Grab the SERVER_MODE if it's been set by the runtime
|
|
|
|
if 'SERVER_MODE' in globals():
|
|
|
|
builtins.SERVER_MODE = globals()['SERVER_MODE']
|
|
|
|
else:
|
|
|
|
builtins.SERVER_MODE = None
|
2016-06-21 08:12:14 -05:00
|
|
|
|
2022-01-04 00:57:17 -06:00
|
|
|
from pgadmin.model import db, Version, SCHEMA_VERSION as CURRENT_SCHEMA_VERSION
|
2021-04-27 02:24:57 -05:00
|
|
|
from pgadmin import create_app
|
2022-01-04 00:57:17 -06:00
|
|
|
from pgadmin.utils import clear_database_servers, dump_database_servers,\
|
|
|
|
load_database_servers
|
2022-07-01 07:42:00 -05:00
|
|
|
from pgadmin.setup import db_upgrade, create_app_data_directory
|
2018-11-21 10:09:05 -06:00
|
|
|
|
|
|
|
|
|
|
|
def dump_servers(args):
|
|
|
|
"""Dump the server groups and servers.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
args (ArgParser): The parsed command line options
|
|
|
|
"""
|
|
|
|
|
2020-06-01 00:52:38 -05:00
|
|
|
# What user?
|
|
|
|
if args.user is not None:
|
|
|
|
dump_user = args.user
|
|
|
|
else:
|
|
|
|
dump_user = config.DESKTOP_USER
|
|
|
|
|
|
|
|
# And the sqlite path
|
|
|
|
if args.sqlite_path is not None:
|
|
|
|
config.SQLITE_PATH = args.sqlite_path
|
|
|
|
|
|
|
|
print('----------')
|
|
|
|
print('Dumping servers with:')
|
|
|
|
print('User:', dump_user)
|
|
|
|
print('SQLite pgAdmin config:', config.SQLITE_PATH)
|
|
|
|
print('----------')
|
2018-11-21 10:09:05 -06:00
|
|
|
|
2020-06-01 00:52:38 -05:00
|
|
|
app = create_app(config.APP_NAME + '-cli')
|
2022-08-31 03:58:48 -05:00
|
|
|
with app.test_request_context():
|
2022-01-04 00:57:17 -06:00
|
|
|
dump_database_servers(args.dump_servers, args.servers, dump_user, True)
|
2020-09-02 04:09:54 -05:00
|
|
|
|
|
|
|
|
2018-11-21 10:09:05 -06:00
|
|
|
def load_servers(args):
|
|
|
|
"""Load server groups and servers.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
args (ArgParser): The parsed command line options
|
|
|
|
"""
|
2020-06-01 00:52:38 -05:00
|
|
|
|
|
|
|
# What user?
|
2020-09-02 04:09:54 -05:00
|
|
|
load_user = args.user if args.user is not None else config.DESKTOP_USER
|
2020-06-01 00:52:38 -05:00
|
|
|
|
|
|
|
# And the sqlite path
|
|
|
|
if args.sqlite_path is not None:
|
|
|
|
config.SQLITE_PATH = args.sqlite_path
|
|
|
|
|
|
|
|
print('----------')
|
|
|
|
print('Loading servers with:')
|
|
|
|
print('User:', load_user)
|
|
|
|
print('SQLite pgAdmin config:', config.SQLITE_PATH)
|
|
|
|
print('----------')
|
|
|
|
|
|
|
|
app = create_app(config.APP_NAME + '-cli')
|
2022-08-31 03:58:48 -05:00
|
|
|
with app.test_request_context():
|
2022-01-04 00:57:17 -06:00
|
|
|
load_database_servers(args.load_servers, None, load_user, True)
|
2018-11-21 10:09:05 -06:00
|
|
|
|
|
|
|
|
2022-07-01 04:20:12 -05:00
|
|
|
def setup_db(app):
|
2018-11-21 10:09:05 -06:00
|
|
|
"""Setup the configuration database."""
|
2018-01-26 10:54:21 -06:00
|
|
|
|
2017-04-23 22:06:55 -05:00
|
|
|
create_app_data_directory(config)
|
|
|
|
|
2020-08-31 06:15:31 -05:00
|
|
|
print("pgAdmin 4 - Application Initialisation")
|
|
|
|
print("======================================\n")
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
|
2017-07-10 10:08:35 -05:00
|
|
|
with app.app_context():
|
2017-08-29 09:03:02 -05:00
|
|
|
# Run migration for the first time i.e. create database
|
|
|
|
from config import SQLITE_PATH
|
|
|
|
if not os.path.exists(SQLITE_PATH):
|
|
|
|
db_upgrade(app)
|
|
|
|
else:
|
|
|
|
version = Version.query.filter_by(name='ConfigDB').first()
|
|
|
|
schema_version = version.value
|
|
|
|
|
|
|
|
# Run migration if current schema version is greater than the
|
|
|
|
# schema version stored in version table
|
|
|
|
if CURRENT_SCHEMA_VERSION >= schema_version:
|
|
|
|
db_upgrade(app)
|
|
|
|
|
|
|
|
# Update schema version to the latest
|
|
|
|
if CURRENT_SCHEMA_VERSION > schema_version:
|
|
|
|
version = Version.query.filter_by(name='ConfigDB').first()
|
|
|
|
version.value = CURRENT_SCHEMA_VERSION
|
|
|
|
db.session.commit()
|
2018-11-21 10:09:05 -06:00
|
|
|
|
2019-05-20 07:53:08 -05:00
|
|
|
if os.name != 'nt':
|
|
|
|
os.chmod(config.SQLITE_PATH, 0o600)
|
2019-04-17 10:57:34 -05:00
|
|
|
|
2018-11-21 10:09:05 -06:00
|
|
|
|
2021-03-01 06:29:47 -06:00
|
|
|
def clear_servers():
|
|
|
|
"""Clear groups and servers configurations.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
args (ArgParser): The parsed command line options
|
|
|
|
"""
|
|
|
|
|
|
|
|
# What user?
|
|
|
|
load_user = args.user if args.user is not None else config.DESKTOP_USER
|
|
|
|
|
|
|
|
# And the sqlite path
|
|
|
|
if args.sqlite_path is not None:
|
|
|
|
config.SQLITE_PATH = args.sqlite_path
|
|
|
|
|
|
|
|
app = create_app(config.APP_NAME + '-cli')
|
|
|
|
with app.app_context():
|
2022-01-04 00:57:17 -06:00
|
|
|
clear_database_servers(load_user, True)
|
2021-03-01 06:29:47 -06:00
|
|
|
|
|
|
|
|
2018-11-21 10:09:05 -06:00
|
|
|
if __name__ == '__main__':
|
|
|
|
# Configuration settings
|
|
|
|
parser = argparse.ArgumentParser(description='Setup the pgAdmin config DB')
|
|
|
|
|
2020-06-01 00:52:38 -05:00
|
|
|
exp_group = parser.add_argument_group('Dump server config')
|
2018-11-21 10:09:05 -06:00
|
|
|
exp_group.add_argument('--dump-servers', metavar="OUTPUT_FILE",
|
|
|
|
help='Dump the servers in the DB', required=False)
|
|
|
|
exp_group.add_argument('--servers', metavar="SERVERS", nargs='*',
|
|
|
|
help='One or more servers to dump', required=False)
|
|
|
|
|
2020-06-01 00:52:38 -05:00
|
|
|
imp_group = parser.add_argument_group('Load server config')
|
2018-11-21 10:09:05 -06:00
|
|
|
imp_group.add_argument('--load-servers', metavar="INPUT_FILE",
|
|
|
|
help='Load servers into the DB', required=False)
|
2021-03-01 06:29:47 -06:00
|
|
|
imp_group.add_argument('--replace', dest='replace', action='store_true',
|
|
|
|
help='replace server configurations',
|
|
|
|
required=False)
|
2018-11-21 10:09:05 -06:00
|
|
|
|
2021-03-01 06:29:47 -06:00
|
|
|
imp_group.set_defaults(replace=False)
|
2020-06-01 00:52:38 -05:00
|
|
|
# Common args
|
|
|
|
parser.add_argument('--sqlite-path', metavar="PATH",
|
|
|
|
help='Dump/load with the specified pgAdmin config DB'
|
|
|
|
' file. This is particularly helpful when there'
|
|
|
|
' are multiple pgAdmin configurations. It is also'
|
|
|
|
' recommended to use this option when running'
|
|
|
|
' pgAdmin in desktop mode.', required=False)
|
|
|
|
parser.add_argument('--user', metavar="USER_NAME",
|
|
|
|
help='Dump/load servers for the specified username',
|
|
|
|
required=False)
|
2018-11-21 10:09:05 -06:00
|
|
|
|
|
|
|
args, extra = parser.parse_known_args()
|
|
|
|
|
2022-07-01 07:42:00 -05:00
|
|
|
config.SETTINGS_SCHEMA_VERSION = CURRENT_SCHEMA_VERSION
|
2018-11-21 10:09:05 -06:00
|
|
|
if "PGADMIN_TESTING_MODE" in os.environ and \
|
|
|
|
os.environ["PGADMIN_TESTING_MODE"] == "1":
|
|
|
|
config.SQLITE_PATH = config.TEST_SQLITE_PATH
|
|
|
|
|
|
|
|
# What to do?
|
|
|
|
if args.dump_servers is not None:
|
2020-06-01 00:52:38 -05:00
|
|
|
try:
|
|
|
|
dump_servers(args)
|
|
|
|
except Exception as e:
|
|
|
|
print(str(e))
|
2018-11-21 10:09:05 -06:00
|
|
|
elif args.load_servers is not None:
|
2020-06-01 00:52:38 -05:00
|
|
|
try:
|
2021-03-01 06:29:47 -06:00
|
|
|
if args.replace:
|
|
|
|
clear_servers()
|
2020-06-01 00:52:38 -05:00
|
|
|
load_servers(args)
|
|
|
|
except Exception as e:
|
|
|
|
print(str(e))
|
2018-11-21 10:09:05 -06:00
|
|
|
else:
|
2022-07-01 04:20:12 -05:00
|
|
|
app = create_app()
|
|
|
|
setup_db(app)
|