Skip to content

Commit afa9f7d

Browse files
committed
update scheduler init db process & db_user priority
1 parent fa36f9a commit afa9f7d

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

scheduler/app/database/__init__.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,12 @@
1111
DB_PASSWD = os.getenv("DB_PASSWD")
1212
db_user = ""
1313
db_passwd = ""
14-
if DB_ROOT_USER and DB_ROOT_PASSWD:
15-
db_user = "root"
16-
db_passwd = DB_ROOT_PASSWD
17-
elif DB_USER and DB_PASSWD:
14+
if DB_USER:
1815
db_user = DB_USER
1916
db_passwd = DB_PASSWD
2017
else:
2118
db_user = "root"
22-
db_passwd = ""
19+
db_passwd = DB_ROOT_PASSWD
2320

2421
DB_HOST = os.getenv("DB_HOST", "seatable-mysql")
2522
DB_PORT = os.getenv("DB_PORT", "3306")

scheduler/app/database/init_db.py

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@
99
DB_PASSWD = os.getenv("DB_PASSWD")
1010
db_user = ""
1111
db_passwd = ""
12-
if DB_ROOT_USER and DB_ROOT_PASSWD:
13-
db_user = "root"
14-
db_passwd = DB_ROOT_PASSWD
15-
elif DB_USER and DB_PASSWD:
12+
if DB_USER:
1613
db_user = DB_USER
1714
db_passwd = DB_PASSWD
1815
else:
1916
db_user = "root"
20-
db_passwd = ""
17+
db_passwd = DB_ROOT_PASSWD
2118

2219
DB_HOST = os.getenv("DB_HOST", "seatable-mysql")
2320
DB_PORT = int(os.getenv("DB_PORT", "3306"))
@@ -38,21 +35,49 @@ def wait_for_mysql():
3835
connection.close()
3936
return
4037

38+
def check_and_create_mysql_user():
39+
connection = pymysql.connect(host=DB_HOST, port=DB_PORT, user=DB_ROOT_USER, passwd=DB_ROOT_PASSWD)
40+
41+
try:
42+
with connection.cursor() as cursor:
43+
if db_user != 'root':
44+
query_user_exists = f"SELECT EXISTS(SELECT 1 FROM mysql.user WHERE User='{db_user}' AND Host='%') AS user_exists;"
45+
cursor.execute(query_user_exists)
46+
result = cursor.fetchone()
47+
user_exists = result[0] == 1
48+
if not user_exists:
49+
create_user_sql = f"CREATE USER '{db_user}'@'%' IDENTIFIED BY '{db_passwd}';"
50+
cursor.execute(create_user_sql)
51+
print(f"Create user '{db_user}'@'%' sucessfully.")
52+
53+
grant_privileges_sql = (
54+
f"GRANT ALL PRIVILEGES ON {DATABASE_NAME}.* TO '{db_user}'@'%';"
55+
)
56+
for stmt in grant_privileges_sql.split(';'):
57+
if stmt.strip():
58+
cursor.execute(stmt)
59+
print(f"Granted user '{db_user}'@'%' privileges sucessfully.")
60+
61+
cursor.execute("FLUSH PRIVILEGES;")
62+
finally:
63+
connection.close()
4164

4265
wait_for_mysql()
4366

44-
if db_user == "root":
45-
sql = 'mysql -h %s -u%s -p%s -e "CREATE DATABASE IF NOT EXISTS %s;"' % (
46-
shlex.quote(DB_HOST),
47-
shlex.quote(db_user),
48-
shlex.quote(db_passwd),
49-
DATABASE_NAME,
50-
)
51-
os.system(sql)
67+
sql = 'mysql -h %s -u%s -p%s -e "CREATE DATABASE IF NOT EXISTS %s;"' % (
68+
shlex.quote(DB_HOST),
69+
shlex.quote(DB_ROOT_USER),
70+
shlex.quote(DB_ROOT_PASSWD),
71+
DATABASE_NAME,
72+
)
73+
os.system(sql)
74+
75+
check_and_create_mysql_user()
76+
5277
sql = "mysql -h %s -u%s -p%s %s </opt/scheduler/database/initial_tables.sql" % (
5378
shlex.quote(DB_HOST),
54-
shlex.quote(db_user),
55-
shlex.quote(db_passwd),
79+
shlex.quote(DB_ROOT_USER),
80+
shlex.quote(DB_ROOT_PASSWD),
5681
DATABASE_NAME,
5782
)
5883
os.system(sql)

scheduler/app/upgrade/upgrade.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,12 @@
2323
DB_PASSWD = os.getenv("DB_PASSWD")
2424
db_user = ""
2525
db_passwd = ""
26-
if DB_ROOT_USER and DB_ROOT_PASSWD:
27-
db_user = "root"
28-
db_passwd = DB_ROOT_PASSWD
29-
elif DB_USER and DB_PASSWD:
26+
if DB_USER:
3027
db_user = DB_USER
3128
db_passwd = DB_PASSWD
3229
else:
3330
db_user = "root"
34-
db_passwd = ""
31+
db_passwd = DB_ROOT_PASSWD
3532

3633
DB_HOST = os.getenv("DB_HOST", "seatable-mysql")
3734
DB_PORT = os.getenv("DB_PORT", "3306")

0 commit comments

Comments
 (0)