Skip to content

Commit

Permalink
Merge pull request #1707 from gtech-mulearn/dev
Browse files Browse the repository at this point in the history
fix(alter):Added alter script 1.39
  • Loading branch information
Aashish Vinu authored Dec 2, 2023
2 parents 0f2fd2d + 84c42a8 commit ac5aa12
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
15 changes: 6 additions & 9 deletions alter-scripts/alter-1.39.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
from django.core.files.storage import FileSystemStorage
import django
import sys
from connection import execute

os.chdir('..')
sys.path.append(os.getcwd())
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mulearnbackend.settings')
django.setup()

from db.user import User
from db.settings import SystemSetting


def save_image(user_id, url):
try:
response = requests.get(url)
response.raise_for_status()

extension = splitext(url)[-1].split("?")[0]
pic = BytesIO(response.content)
fs = FileSystemStorage()
Expand All @@ -29,10 +28,8 @@ def save_image(user_id, url):


if __name__ == "__main__":
users = User.objects.filter(profile_pic__isnull=False)
for user in users:
save_image(user.id, user.profile_pic)
data = execute("SELECT id, profile_pic FROM user WHERE profile_pic IS NOT NULL;")
for user_id, profile_pic in data:
save_image(user_id, profile_pic)

settings = SystemSetting.objects.filter(key="db.version").first()
settings.value = "1.39"
settings.save()
execute("UPDATE system_setting SET value = '1.39', updated_at = now() WHERE `key` = 'db.version';")
16 changes: 16 additions & 0 deletions alter-scripts/connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pymysql
from decouple import config

db_config = {
'host': config('DATABASE_HOST'),
'user': config('DATABASE_USER'),
'password': config('DATABASE_PASSWORD'),
'db': config('DATABASE_NAME'),
}


def execute(query):
with pymysql.connect(**db_config) as connection:
with connection.cursor() as cursor:
cursor.execute(query)
return cursor.fetchall()
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ requests==2.31.0
service-identity==23.1.0
tzdata==2023.3
zope.interface==6.1
qrcode==7.4.2
qrcode==7.4.2
pymysql==1.0.2

0 comments on commit ac5aa12

Please sign in to comment.