Skip to content

adding the abillity to config to all users mutiple postgresql server #8897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions web/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,33 @@
##########################################################################
CONFIG_DATABASE_URI = ''

##########################################################################
# add default server to all users
# exemple:
# postgresql_servers = [
# {
# "name": "Serveur A",
# "host": "localhost",
# "port": 5432,
# "username": "admin",
# "password": "",
# "maintenance_db": "postgres",
# "comment": "ajout auto"
# },
# {
# "name": "Serveur B",
# "host": "db2",
# "port": 5433,
# "username": "admin",
# "password": "secret",
# "maintenance_db": "postgres",
# "comment": ""
# }
# ]
##########################################################################

postgresql_servers = []

##########################################################################
# User account and settings storage
##########################################################################
Expand Down
60 changes: 60 additions & 0 deletions web/pgadmin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,12 +546,72 @@ def run_migration_for_others():
paranoid = Paranoid(app)
paranoid.redirect_view = _INDEX_PATH

##########################################################################
# add all default server
##########################################################################

def add_servers_to_all_users_from_config(postgresql_servers):
users = User.query.all()
server_groups = ServerGroup.query.all()

for user in users:
user_id = user.id
servergroup_id = server_groups.get(user_id)

if not servergroup_id:
print(f"⚠️ Aucun servergroup trouvé pour user_id {user_id}.")
continue

for config in postgresql_servers:
name = config.get("name")
host = config.get("host", "localhost")
port = config.get("port", 5432)
username = config.get("username", "postgres")
password = config.get("password", "")
maintenance_db = config.get("maintenance_db", "postgres")
comment = config.get("comment", "")

existing = Server.query.filter_by(
user_id=user_id,
name=name,
host=host,
port=port
).first()

if existing:
continue # déjà ajouté

svr = Server(
user_id=user_id,
servergroup_id=servergroup_id,
name=name,
host=host,
port=port,
maintenance_db=maintenance_db,
username=username,
password=password,
comment=comment,
connection_params={
"sslmode": "prefer",
"connect_timeout": 10
}
)

db.session.add(svr)
print(f"✅ Ajouté: {name} pour user {user.email}")

db.session.commit()

import postgresql_servers from "config"
add_servers_to_all_users_from_config(postgresql_servers)

##########################################################################
# Load all available server drivers
##########################################################################
driver.init_app(app)
authenticate.init_app(app)
heartbeat.init_app(app)


##########################################################################
# Register language to the preferences after login
Expand Down