1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-04-28 11:15:58 +00:00
seahub/scripts/migrate_ldapusers.py
2023-07-07 09:39:38 +08:00

55 lines
1.6 KiB
Python

# -*- coding: utf-8 -*-
import os
import configparser
import pymysql
pymysql.install_as_MySQLdb()
install_path = os.path.dirname(os.path.abspath(__file__))
top_dir = os.path.dirname(install_path)
ccnet_conf = os.path.join(top_dir, 'conf', 'ccnet.conf')
sql = "INSERT IGNORE INTO EmailUser (email, passwd, is_staff, is_active, ctime) SELECT email, '!', is_staff, is_active, REPLACE(UNIX_TIMESTAMP(CURRENT_TIMESTAMP(6)),'.','') FROM LDAPUsers"
def migrate_ldapusers():
print('Start migrate LDAPUsers')
config = configparser.ConfigParser()
try:
config.read(ccnet_conf)
db_user = config.get('Database', 'USER')
db_host = config.get('Database', 'HOST')
db_port = config.getint('Database', 'PORT')
db_password = config.get('Database', 'PASSWD')
db_name = config.get('Database', 'DB')
except Exception as e:
print("Failed to read ccnet config file %s: %s" % (ccnet_conf, e))
return
try:
conn = pymysql.connect(user=db_user, host=db_host, port=db_port, password=db_password, database=db_name)
conn.autocommit(True)
cursor = conn.cursor()
except Exception as e:
print('Failed to connect to mysql database: %s' % e)
return
try:
cursor.execute(sql)
print('Migrated %s records' % cursor.rowcount)
print('Finish migrate LDAPUsers.')
except Exception as e:
print('Failed to exec sql: %s' % e)
return
finally:
if cursor:
cursor.close()
if conn:
conn.close()
if __name__ == '__main__':
migrate_ldapusers()