-
-
Notifications
You must be signed in to change notification settings - Fork 891
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
admin: store version history (#11520)
Co-authored-by: Jens Langhammer <jens@goauthentik.io>
- Loading branch information
Showing
7 changed files
with
225 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from rest_framework.permissions import IsAdminUser | ||
from rest_framework.viewsets import ReadOnlyModelViewSet | ||
|
||
from authentik.admin.models import VersionHistory | ||
from authentik.core.api.utils import ModelSerializer | ||
|
||
|
||
class VersionHistorySerializer(ModelSerializer): | ||
"""VersionHistory Serializer""" | ||
|
||
class Meta: | ||
model = VersionHistory | ||
fields = [ | ||
"id", | ||
"timestamp", | ||
"version", | ||
"build", | ||
] | ||
|
||
|
||
class VersionHistoryViewSet(ReadOnlyModelViewSet): | ||
"""VersionHistory Viewset""" | ||
|
||
queryset = VersionHistory.objects.all() | ||
serializer_class = VersionHistorySerializer | ||
permission_classes = [IsAdminUser] | ||
filterset_fields = [ | ||
"version", | ||
"build", | ||
] | ||
search_fields = ["version", "build"] | ||
ordering = ["-timestamp"] | ||
pagination_class = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"""authentik admin models""" | ||
|
||
from django.db import models | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
|
||
class VersionHistory(models.Model): | ||
id = models.BigAutoField(primary_key=True) | ||
timestamp = models.DateTimeField() | ||
version = models.TextField() | ||
build = models.TextField() | ||
|
||
class Meta: | ||
managed = False | ||
db_table = "authentik_version_history" | ||
ordering = ("-timestamp",) | ||
verbose_name = _("Version history") | ||
verbose_name_plural = _("Version history") | ||
default_permissions = [] | ||
|
||
def __str__(self): | ||
return f"{self.version}.{self.build} ({self.timestamp})" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# flake8: noqa | ||
from lifecycle.migrate import BaseMigration | ||
|
||
|
||
class Migration(BaseMigration): | ||
def needs_migration(self) -> bool: | ||
self.cur.execute( | ||
"SELECT * FROM information_schema.tables WHERE table_name = 'authentik_version_history';" | ||
) | ||
return not bool(self.cur.rowcount) | ||
|
||
def run(self): | ||
self.cur.execute( | ||
""" | ||
BEGIN TRANSACTION; | ||
CREATE TABLE IF NOT EXISTS authentik_version_history ( | ||
id BIGSERIAL PRIMARY KEY, | ||
"timestamp" timestamp with time zone NOT NULL, | ||
version text NOT NULL, | ||
build text NOT NULL | ||
); | ||
COMMIT; | ||
""" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# flake8: noqa | ||
from lifecycle.migrate import BaseMigration | ||
from datetime import datetime | ||
|
||
from authentik import __version__, get_build_hash | ||
|
||
|
||
class Migration(BaseMigration): | ||
def needs_migration(self) -> bool: | ||
self.cur.execute( | ||
""" | ||
SELECT * FROM authentik_version_history | ||
WHERE version = %s AND build = %s | ||
ORDER BY "timestamp" DESC | ||
LIMIT 1 | ||
""", | ||
(__version__, get_build_hash()), | ||
) | ||
return not bool(self.cur.rowcount) | ||
|
||
def run(self): | ||
self.cur.execute( | ||
""" | ||
INSERT INTO authentik_version_history ("timestamp", version, build) | ||
VALUES (%s, %s, %s) | ||
""", | ||
(datetime.now(), __version__, get_build_hash()), | ||
) | ||
self.cur.execute( | ||
""" | ||
DELETE FROM authentik_version_history WHERE id NOT IN ( | ||
SELECT id FROM authentik_version_history | ||
ORDER BY "timestamp" DESC | ||
LIMIT 1000 | ||
) | ||
""" | ||
) | ||
self.con.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters