Skip to content

Commit 4c207f2

Browse files
committed
Revert: update scheduler init db process & db_user priority
This reverts commit afa9f7d.
1 parent 7da98b5 commit 4c207f2

File tree

3 files changed

+25
-44
lines changed

3 files changed

+25
-44
lines changed

scheduler/app/database/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
DB_PASSWD = os.getenv("DB_PASSWD")
1212
db_user = ""
1313
db_passwd = ""
14-
if DB_USER:
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:
1518
db_user = DB_USER
1619
db_passwd = DB_PASSWD
1720
else:
1821
db_user = "root"
19-
db_passwd = DB_ROOT_PASSWD
22+
db_passwd = ""
2023

2124
DB_HOST = os.getenv("DB_HOST", "seatable-mysql")
2225
DB_PORT = os.getenv("DB_PORT", "3306")

scheduler/app/database/init_db.py

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
DB_PASSWD = os.getenv("DB_PASSWD")
1010
db_user = ""
1111
db_passwd = ""
12-
if DB_USER:
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:
1316
db_user = DB_USER
1417
db_passwd = DB_PASSWD
1518
else:
1619
db_user = "root"
17-
db_passwd = DB_ROOT_PASSWD
20+
db_passwd = ""
1821

1922
DB_HOST = os.getenv("DB_HOST", "seatable-mysql")
2023
DB_PORT = int(os.getenv("DB_PORT", "3306"))
@@ -35,49 +38,21 @@ def wait_for_mysql():
3538
connection.close()
3639
return
3740

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()
6441

6542
wait_for_mysql()
6643

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-
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)
7752
sql = "mysql -h %s -u%s -p%s %s </opt/scheduler/database/initial_tables.sql" % (
7853
shlex.quote(DB_HOST),
79-
shlex.quote(DB_ROOT_USER),
80-
shlex.quote(DB_ROOT_PASSWD),
54+
shlex.quote(db_user),
55+
shlex.quote(db_passwd),
8156
DATABASE_NAME,
8257
)
8358
os.system(sql)

scheduler/app/upgrade/upgrade.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@
2323
DB_PASSWD = os.getenv("DB_PASSWD")
2424
db_user = ""
2525
db_passwd = ""
26-
if DB_USER:
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:
2730
db_user = DB_USER
2831
db_passwd = DB_PASSWD
2932
else:
3033
db_user = "root"
31-
db_passwd = DB_ROOT_PASSWD
34+
db_passwd = ""
3235

3336
DB_HOST = os.getenv("DB_HOST", "seatable-mysql")
3437
DB_PORT = os.getenv("DB_PORT", "3306")

0 commit comments

Comments
 (0)