Skip to content

Commit 1ba29ce

Browse files
authored
Merge pull request #83 from seatable/revert_modification
Revert modification
2 parents 6d751d2 + 4c207f2 commit 1ba29ce

File tree

3 files changed

+25
-50
lines changed

3 files changed

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

3841

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-
7142
wait_for_mysql()
7243

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-
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)
8352
sql = "mysql -h %s -u%s -p%s %s </opt/scheduler/database/initial_tables.sql" % (
8453
shlex.quote(DB_HOST),
85-
shlex.quote(DB_ROOT_USER),
86-
shlex.quote(DB_ROOT_PASSWD),
54+
shlex.quote(db_user),
55+
shlex.quote(db_passwd),
8756
DATABASE_NAME,
8857
)
8958
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)