TIF_E41253310/scripts/maintenance/truncate_all.py

41 lines
1.1 KiB
Python

import pymysql
conn = pymysql.connect(host='localhost', user='root', password='', database='tracerstudy_db', port=3306, charset='utf8mb4')
cursor = conn.cursor()
# Disable foreign key checks
cursor.execute('SET FOREIGN_KEY_CHECKS = 0')
print('Foreign key checks disabled')
# Tables to truncate
tables = ['clustering_result', 'tracer_responses', 'kompetensi', 'pekerjaan', 'alumni']
for table in tables:
cursor.execute(f'TRUNCATE TABLE {table}')
print(f'Truncated: {table}')
# Re-enable foreign key checks
cursor.execute('SET FOREIGN_KEY_CHECKS = 1')
print('Foreign key checks enabled')
conn.commit()
# Verify
cursor.execute('SELECT COUNT(*) FROM alumni')
alumni_count = cursor.fetchone()[0]
cursor.execute('SELECT COUNT(*) FROM tracer_responses')
tracer_count = cursor.fetchone()[0]
cursor.execute('SELECT COUNT(*) FROM pekerjaan')
pekerjaan_count = cursor.fetchone()[0]
print()
print('VERIFIKASI SETELAH TRUNCATE:')
print(f' Alumni: {alumni_count}')
print(f' Tracer Responses: {tracer_count}')
print(f' Pekerjaan: {pekerjaan_count}')
cursor.close()
conn.close()
print()
print('SEMUA DATA IMPORT BERHASIL DIHAPUS!')