Pridať komentár

Príspevok, na ktorý odpovedáte

RE: Pre administrátorov 03.09.2019 | 10:23
Avatar Miroslav Bendík Gentoo  Administrátor

Neviem, či to má nejaký zmysel ale budiž. Pred mazaním poprosím zálohovať databázu (su - postgres, bez hesla, alebo z heslom z web/settings_production.py).

Príkazy

cd /srv/linuxos/app
. ./scripts/setenv.server
django-admin.py shell_plus
neaktivny = Q(last_login__isnull=True) | Q(last_login__lt=timezone.now() - timezone.timedelta(365))
content_types = (
    (Article, 'author', 'ma_clanky'),
    (Blog, 'author', 'ma_blog'),
    (Comment, 'user', 'ma_komentare'),
    (Desktop, 'author', 'ma_desktop'),
    (News, 'author', 'ma_spravy'),
    (Node, 'author_id', 'ma_blackhole_clanky'),
    (NodeRevision, 'author_id', 'ma_blackhole_revizie'),
    (Page, 'last_author', 'ma_wiki_stranky'),
    (Topic, 'author', 'ma_temy_fora'),
    (Tweet, 'author', 'ma_tweety'),
)
anotacie = {}
bez_obsahu = Q()
for model, related_name, column_name in content_types:
    anotacie[column_name] = Exists(model._default_manager.filter(**{related_name: OuterRef('pk')}).values('pk'))
    bez_obsahu = bez_obsahu & Q(**{column_name: False})



# Výpis ID
User.objects.annotate(**anotacie).filter(neaktivny & bez_obsahu).values_list('pk', flat=True)

# Vymazanie
User.objects.annotate(**anotacie).filter(neaktivny & bez_obsahu).delete()

Príslušné SQL:

SELECT "auth_user"."id"
FROM "auth_user"
WHERE (("auth_user"."last_login" IS NULL
        OR "auth_user"."last_login" < 2018-09-03 08:19:42.645751+00:00)
       AND EXISTS
         (SELECT U0."id"
          FROM "article_article" U0
          WHERE U0."author_id" = ("auth_user"."id")) = FALSE
       AND EXISTS
         (SELECT U0."id"
          FROM "blog_blog" U0
          WHERE U0."author_id" = ("auth_user"."id")) = FALSE
       AND EXISTS
         (SELECT U0."id"
          FROM "comments_comment" U0
          WHERE U0."user_id" = ("auth_user"."id")) = FALSE
       AND EXISTS
         (SELECT U0."id"
          FROM "desktops_desktop" U0
          WHERE U0."author_id" = ("auth_user"."id")) = FALSE
       AND EXISTS
         (SELECT U0."id"
          FROM "news_news" U0
          WHERE U0."author_id" = ("auth_user"."id")) = FALSE
       AND EXISTS
         (SELECT U0."id"
          FROM "blackhole_node" U0
          WHERE U0."author_id" = ("auth_user"."id")) = FALSE
       AND EXISTS
         (SELECT U0."id"
          FROM "blackhole_noderevision" U0
          WHERE U0."author_id" = ("auth_user"."id")) = FALSE
       AND EXISTS
         (SELECT U0."id"
          FROM "wiki_page" U0
          WHERE U0."last_author_id" = ("auth_user"."id")) = FALSE
       AND EXISTS
         (SELECT U0."id"
          FROM "forum_topic" U0
          WHERE U0."author_id" = ("auth_user"."id")) = FALSE
       AND EXISTS
         (SELECT U0."id"
          FROM "tweets_tweet" U0
          WHERE U0."author_id" = ("auth_user"."id")) = FALSE)
12 - 6 plus tisíc (číslom)

Maximálna veľkosť je: 2,0 MB