Search code examples
pythondjangopymongo

Django Remove DB (Python manage migrate)


I'm using Django.

After running python manage migrate, the DB is created normally. However, after some time, the DB suddenly disappears. The drop command does not exist in my code.

I am curious as to why the DB is deleted. All data removed. (DB, Collection, Field)

Does anyone know the cause?

This is my code

below code is app/models

model

from djongo import models

class RealTime(models.Model):
    _id = models.CharField(max_length=255, primary_key=True)
    site = models.CharField(max_length=125)
    title = models.CharField(max_length=255)
    url = models.URLField()
    create_time = models.DateTimeField()
    GPTAnswer = models.TextField()
    
    class Meta:
        db_table = 'realtimebest'

class Daily(models.Model):
    rank = models.IntegerField()
    title = models.CharField(max_length=255)
    url = models.URLField()
    create_time = models.DateTimeField()

Below code is Schema:

schema

import graphene
from graphene_django.types import DjangoObjectType
from graphene import Mutation

from .views import board_summary
from .communityWebsite.models import RealTime, Daily

class RealTimeType(DjangoObjectType):
    class Meta:
        model = RealTime

class DailyType(DjangoObjectType):
    class Meta:
        model = Daily

class Query(graphene.ObjectType):
    all_realtime = graphene.List(RealTimeType)
    all_daily = graphene.List(DailyType)

    def resolve_all_realtime(self, info, **kwargs):
        return RealTime.objects.all()

    def resolve_all_daily(self, info, **kwargs):
        return Daily.objects.all()

class SummaryBoardMutation(Mutation):
    class Arguments:
        board_id = graphene.String(required=True)

    response = graphene.String()

    def mutate(self, info, board_id):
        response = board_summary(board_id)
        return SummaryBoardMutation(response=response)

class Mutation(graphene.ObjectType):
    summary_board = SummaryBoardMutation.Field()

schema = graphene.Schema(query=Query, mutation=Mutation)

settings.py

# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
     "corsheaders",

    # Graph QL
    'graphene_django',
    'graphene_mongo',
    
    'webCrwaling',
    'kingwangjjang',
    'chatGPT'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'kingwangjjang.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        # 'DIRS': [],
        "DIRS": [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'kingwangjjang.wsgi.application'

# Database
# MongoDB settings
DB_URI = 'mongodb://'+ DB_HOST + '/' + DB_USER + ':' + DB_PASSWORD
DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': DB_NAME,
        'ENFORCE_SCHEMA': True,
        'CLIENT': {
            'host': DB_URI
        }  
    }
}

# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/

LANGUAGE_CODE = 'ko-kr'

TIME_ZONE = 'Asia/Seoul'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# GRAPHENE = {
#     "SCHEMA": "webCrwaling.schema.schema"
# }

Insert Data

def get_real_time_best(self):
    req = requests.get('https://www.dcinside.com/', headers=self.g_headers[0])
    html_content = req.text
    soup = BeautifulSoup(html_content, 'html.parser')
    li_elements = soup.select('#dcbest_list_date li')
    already_exists_post = []

    for li in li_elements:
        p_element = li.select_one('.box.besttxt p')
        a_element = li.select_one('.main_log')
        time_element = li.select_one('.box.best_info .time')

        if p_element and a_element and time_element:
            p_text = p_element.get_text(strip=True)
            a_href = a_element['href']
            no_value = a_href.split('no=')[-1]
            time_text = time_element.get_text(strip=True)

            if(time_text.find('-') > 0): 
                break  # 오늘 것만 추가 (이전 글은 제외 (DB에서 확인))

            # 시간 13:40 -> 2024.01.29 13:40 로 수정
            now = datetime.now()
            hour, minute = map(int, time_text.split(':'))
            # 시간 설정 및 datetime 객체 생성
            target_datetime = datetime(now.year, now.month, now.day, hour, minute)

            try:
                existing_instance = RealTime.objects.filter(_id=no_value).first()
                if existing_instance:
                    already_exists_post.append(no_value)
                    continue
                else:
                    RealTime.objects.get_or_create(
                        _id=no_value,
                        defaults={
                            'site' : 'dcinside',
                            'title': p_text,
                            'url': a_href,
                            'create_time': target_datetime,
                            'GPTAnswer': DEFAILT_GPT_ANSWER
                        }
                    )
            except IntegrityError:
                continue

    print("already exists post", already_exists_post)

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' Could this be a problem?

What part needs to be modified to solve the problem of data completely disappearing?

  1. Time Zone = False

But Python manage migrate is normal


Solution

  • I added an authentication process to mongodb which solved the problem of db disappearing. Checking the log as suggested in the comments let me to this solution.