Add telegram_communication app with dedicated DB routing
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 3s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 3s
- Register TelegramCommunicationConfig in INSTALLED_APPS and add telegram_db to DATABASES - Define TelegramData model mapped to the messages table - Fix allow_migrate in ProjectDatabaseRouter: redirect and telegram entries were returning the DB name string (always truthy) instead of a boolean comparison, causing their migrations to bleed into every database - Add telegram routing to db_for_read, db_for_write, allow_relation, and allow_migrate Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,4 +3,5 @@ from django.apps import AppConfig
|
|||||||
|
|
||||||
class TelegramCommunicationConfig(AppConfig):
|
class TelegramCommunicationConfig(AppConfig):
|
||||||
default_auto_field = 'django.db.models.BigAutoField'
|
default_auto_field = 'django.db.models.BigAutoField'
|
||||||
name = 'telegram_communication'
|
name = 'apps.telegram_communication'
|
||||||
|
label = 'telegram'
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
# Generated by Django 4.2.30 on 2026-06-30 12:13
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='TelegramData',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('message_id', models.IntegerField(max_length=100, null=True)),
|
||||||
|
('chat_id', models.IntegerField(max_length=100, null=True)),
|
||||||
|
('description', models.CharField(max_length=100, null=True)),
|
||||||
|
('creation_date', models.DateField(blank=True, null=True, verbose_name='Published')),
|
||||||
|
('expiration_date', models.DateField(blank=True, null=True, verbose_name='Published')),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
'db_table': 'messages',
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
||||||
+33
@@ -0,0 +1,33 @@
|
|||||||
|
# Generated by Django 4.2.30 on 2026-06-30 12:14
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('telegram', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='telegramdata',
|
||||||
|
name='chat_id',
|
||||||
|
field=models.IntegerField(),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='telegramdata',
|
||||||
|
name='creation_date',
|
||||||
|
field=models.DateField(blank=True, verbose_name='Published'),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='telegramdata',
|
||||||
|
name='expiration_date',
|
||||||
|
field=models.DateField(blank=True, verbose_name='Published'),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='telegramdata',
|
||||||
|
name='message_id',
|
||||||
|
field=models.IntegerField(),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -1,3 +1,12 @@
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
# Create your models here.
|
|
||||||
|
class TelegramData(models.Model):
|
||||||
|
message_id = models.IntegerField(null=False)
|
||||||
|
chat_id = models.IntegerField(null=False)
|
||||||
|
description = models.CharField(max_length=100, null=True)
|
||||||
|
creation_date = models.DateField(blank=True, null=False, verbose_name='Published')
|
||||||
|
expiration_date = models.DateField(blank=True, null=False, verbose_name='Published')
|
||||||
|
class Meta:
|
||||||
|
db_table = 'messages'
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ class ProjectDatabaseRouter:
|
|||||||
return 'redirect_db'
|
return 'redirect_db'
|
||||||
if model._meta.app_label == 'cloudmanager':
|
if model._meta.app_label == 'cloudmanager':
|
||||||
return 'cloudmanager_db'
|
return 'cloudmanager_db'
|
||||||
|
if model._meta.app_label == 'telegram':
|
||||||
|
return 'telegram_db'
|
||||||
return 'default'
|
return 'default'
|
||||||
|
|
||||||
def db_for_write(self, model, **hints):
|
def db_for_write(self, model, **hints):
|
||||||
@@ -28,14 +30,16 @@ class ProjectDatabaseRouter:
|
|||||||
return 'cloudmanager_db'
|
return 'cloudmanager_db'
|
||||||
if model._meta.app_label == 'redirect':
|
if model._meta.app_label == 'redirect':
|
||||||
return 'redirect_db'
|
return 'redirect_db'
|
||||||
|
if model._meta.app_label == 'telegram':
|
||||||
|
return 'telegram_db'
|
||||||
return 'default'
|
return 'default'
|
||||||
|
|
||||||
def allow_relation(self, obj1, obj2, **hints):
|
def allow_relation(self, obj1, obj2, **hints):
|
||||||
"""Allow relations if both models are in the same database."""
|
"""Allow relations if both models are in the same database."""
|
||||||
# Prevents accidental foreign key relationships across different physical DBs
|
# Prevents accidental foreign key relationships across different physical DBs
|
||||||
if (
|
if (
|
||||||
obj1._meta.app_label in ['simpsons', 'cloudmanager','redirect','job'] or
|
obj1._meta.app_label in ['simpsons', 'cloudmanager','redirect','job','telegram'] or
|
||||||
obj2._meta.app_label in ['simpsons', 'cloudmanager','redirect','job']
|
obj2._meta.app_label in ['simpsons', 'cloudmanager','redirect','job','telegram']
|
||||||
):
|
):
|
||||||
return obj1._meta.app_label == obj2._meta.app_label
|
return obj1._meta.app_label == obj2._meta.app_label
|
||||||
return None
|
return None
|
||||||
@@ -49,9 +53,11 @@ class ProjectDatabaseRouter:
|
|||||||
if app_label == 'cloudmanager':
|
if app_label == 'cloudmanager':
|
||||||
return db == 'cloudmanager_db'
|
return db == 'cloudmanager_db'
|
||||||
if app_label == 'redirect':
|
if app_label == 'redirect':
|
||||||
return 'redirect_db'
|
return db == 'redirect_db'
|
||||||
|
if app_label == 'telegram':
|
||||||
|
return db == 'telegram_db'
|
||||||
# Ensure default Django apps (auth, sessions, etc.) don't migrate into simpsons/cloudmanager
|
# Ensure default Django apps (auth, sessions, etc.) don't migrate into simpsons/cloudmanager
|
||||||
if db in ['simpsons_db', 'cloudmanager_db','redirect_db','job_db']:
|
if db in ['simpsons_db', 'cloudmanager_db','redirect_db','job_db','telegram_db']:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return None
|
return None
|
||||||
@@ -47,6 +47,7 @@ INSTALLED_APPS = [
|
|||||||
'apps.random_simpsons.apps.RandomSimpsonsConfig',
|
'apps.random_simpsons.apps.RandomSimpsonsConfig',
|
||||||
'apps.website_redirection.apps.redirectConfig',
|
'apps.website_redirection.apps.redirectConfig',
|
||||||
'apps.job_dashboard.apps.JobDashboardConfig',
|
'apps.job_dashboard.apps.JobDashboardConfig',
|
||||||
|
'apps.telegram_communication.apps.TelegramCommunicationConfig',
|
||||||
'import_export',
|
'import_export',
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -133,6 +134,14 @@ DATABASES = {
|
|||||||
'HOST': os.environ.get('DB_HOST'),
|
'HOST': os.environ.get('DB_HOST'),
|
||||||
'PORT': os.environ.get('DB_PORT'),
|
'PORT': os.environ.get('DB_PORT'),
|
||||||
},
|
},
|
||||||
|
'telegram_db': {
|
||||||
|
'ENGINE': 'django.db.backends.postgresql',
|
||||||
|
'NAME': 'telegram',
|
||||||
|
'USER': os.environ.get('DB_USERNAME'),
|
||||||
|
'PASSWORD': os.environ.get('DB_PASSWORD'),
|
||||||
|
'HOST': os.environ.get('DB_HOST'),
|
||||||
|
'PORT': os.environ.get('DB_PORT'),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
DATABASE_ROUTERS = ['core.routers.ProjectDatabaseRouter']
|
DATABASE_ROUTERS = ['core.routers.ProjectDatabaseRouter']
|
||||||
#DATABASE_ROUTERS = []
|
#DATABASE_ROUTERS = []
|
||||||
|
|||||||
Executable → Regular
Reference in New Issue
Block a user