Skip to content

Commit 6d751d2

Browse files
authored
Merge pull request #82 from seatable/update_scheduler_init_db_process
update scheduler init db process & db_user priority
2 parents fa36f9a + 7063c35 commit 6d751d2

File tree

3 files changed

+50
-25
lines changed

3 files changed

+50
-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: 46 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"))
@@ -39,20 +36,54 @@ def wait_for_mysql():
3936
return
4037

4138

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

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)
73+
sql = 'mysql -h %s -u%s -p%s -e "CREATE DATABASE IF NOT EXISTS %s;"' % (
74+
shlex.quote(DB_HOST),
75+
shlex.quote(DB_ROOT_USER),
76+
shlex.quote(DB_ROOT_PASSWD),
77+
DATABASE_NAME,
78+
)
79+
os.system(sql)
80+
81+
check_and_create_mysql_user()
82+
5283
sql = "mysql -h %s -u%s -p%s %s </opt/scheduler/database/initial_tables.sql" % (
5384
shlex.quote(DB_HOST),
54-
shlex.quote(db_user),
55-
shlex.quote(db_passwd),
85+
shlex.quote(DB_ROOT_USER),
86+
shlex.quote(DB_ROOT_PASSWD),
5687
DATABASE_NAME,
5788
)
5889
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)