Compare commits

..

8 Commits

Author SHA1 Message Date
1c87721611 Merge pull request 'migration fix' (#6) from v2 into main
Some checks failed
continuous-integration/drone/push Build is failing
Reviewed-on: #6
2025-09-05 05:23:12 +00:00
c6574efee3 Merge pull request 'model channels' (#5) from v2 into main
Some checks failed
continuous-integration/drone/push Build is failing
Reviewed-on: #5
2025-09-05 05:17:27 +00:00
fb620e5d38 Merge branch 'v2'
Some checks failed
continuous-integration/drone/push Build is failing
2025-09-05 14:06:13 +09:00
3788fca8ee ssMerge branch 'main' of ssh://git.smartsoltech.kr:2222/trevor/post_bot
Some checks failed
continuous-integration/drone/push Build is failing
2025-09-05 05:01:39 +00:00
fb82b7f270 chmod +x lacally added 2025-09-05 05:01:02 +00:00
5f3bac5bf5 Merge pull request 'script fix docker-compose vs docker compose' (#3) from v2 into main
Some checks failed
continuous-integration/drone/push Build is failing
Reviewed-on: #3
2025-09-05 05:00:07 +00:00
a39a7bf910 Merge pull request 'script fix' (#2) from v2 into main
Some checks failed
continuous-integration/drone/push Build is failing
Reviewed-on: #2
2025-09-05 04:57:49 +00:00
aa88880a52 Merge pull request 'migrations + util scripts' (#1) from v2 into main
Some checks failed
continuous-integration/drone/push Build is failing
Reviewed-on: #1
2025-09-05 04:56:06 +00:00
6 changed files with 85 additions and 74 deletions

View File

@@ -0,0 +1,32 @@
"""init
Revision ID: 69ef23ef1ed1
Revises:
Create Date: 2025-09-05 13:53:02.737876
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '69ef23ef1ed1'
down_revision: Union[str, Sequence[str], None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###

View File

@@ -0,0 +1,46 @@
"""channel table
Revision ID: 7506a3320699
Revises: 69ef23ef1ed1
Create Date: 2025-09-05 14:12:37.430983
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.sql import table, column
# revision identifiers, used by Alembic.
revision: str = '7506a3320699'
down_revision: Union[str, Sequence[str], None] = '69ef23ef1ed1'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# Для SQLite: создаём новую таблицу, копируем данные, удаляем старую, переименовываем новую
conn = op.get_bind()
# 1. Создать новую таблицу
op.create_table(
'channels_new',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=True),
sa.Column('link', sa.String(length=255), nullable=True),
sa.Column('admin_id', sa.Integer(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
# 2. Если старая таблица есть, скопировать данные
result = conn.execute(sa.text("SELECT name FROM sqlite_master WHERE type='table' AND name='channels'")).fetchone()
if result:
conn.execute(sa.text("INSERT INTO channels_new (id, name, link, admin_id) SELECT id, name, link, admin_id FROM channels"))
op.drop_table('channels')
# 3. Переименовать новую таблицу
conn.execute(sa.text("ALTER TABLE channels_new RENAME TO channels"))
def downgrade() -> None:
"""Downgrade schema."""
op.drop_table('channels')

View File

@@ -1,57 +0,0 @@
"""init
Revision ID: eeb6744b9452
Revises:
Create Date: 2025-09-05 14:55:12.005564
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'eeb6744b9452'
down_revision: Union[str, Sequence[str], None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Создание всех таблиц согласно моделям."""
op.create_table(
'admins',
sa.Column('id', sa.Integer(), primary_key=True),
sa.Column('tg_id', sa.Integer(), unique=True, nullable=False),
)
op.create_table(
'channels',
sa.Column('id', sa.Integer(), primary_key=True),
sa.Column('name', sa.String, nullable=True),
sa.Column('link', sa.String, nullable=True),
sa.Column('admin_id', sa.Integer(), sa.ForeignKey('admins.id'), nullable=True),
)
op.create_table(
'groups',
sa.Column('id', sa.Integer(), primary_key=True),
sa.Column('name', sa.String, nullable=False),
sa.Column('link', sa.String, nullable=False),
sa.Column('admin_id', sa.Integer(), sa.ForeignKey('admins.id'), nullable=True),
)
op.create_table(
'buttons',
sa.Column('id', sa.Integer(), primary_key=True),
sa.Column('name', sa.String, nullable=False),
sa.Column('url', sa.String, nullable=False),
sa.Column('channel_id', sa.Integer(), sa.ForeignKey('channels.id'), nullable=True),
sa.Column('group_id', sa.Integer(), sa.ForeignKey('groups.id'), nullable=True),
)
def downgrade() -> None:
"""Удаление всех таблиц."""
op.drop_table('buttons')
op.drop_table('groups')
op.drop_table('channels')
op.drop_table('admins')

10
bin/update.sh Normal file → Executable file
View File

@@ -1,16 +1,6 @@
#!/bin/bash
set -e
echo "[update.sh] Проверка bot.db..."
if [ -d "bot.db" ]; then
echo "Удаляю папку bot.db..."
rm -rf bot.db
fi
if [ ! -f "bot.db" ]; then
echo "Создаю пустой файл bot.db..."
touch bot.db
fi
echo "[update.sh] Получение свежего кода..."
git pull

10
db.py
View File

@@ -9,13 +9,15 @@ DATABASE_URL = os.getenv("DATABASE_URL", "sqlite+aiosqlite:///bot.db")
if DATABASE_URL.startswith("sqlite+aiosqlite:///"):
db_path = DATABASE_URL.replace("sqlite+aiosqlite:///", "")
# Убираем лишний слэш в конце, если есть
if db_path.endswith(os.sep):
db_path = db_path.rstrip(os.sep)
abs_db_path = os.path.abspath(db_path)
db_dir = os.path.dirname(abs_db_path)
# Создаём директорию только если она не равна текущей ('.') и не пустая
if db_dir and db_dir != os.path.abspath("") and db_dir != '.' and not os.path.exists(db_dir):
if db_dir and not os.path.exists(db_dir):
os.makedirs(db_dir, exist_ok=True)
# Если по этому пути уже есть папка, удаляем её
if os.path.exists(abs_db_path) and os.path.isdir(abs_db_path):
# Если по этому пути уже есть папка, удаляем её и создаём файл
if os.path.isdir(abs_db_path):
import shutil
shutil.rmtree(abs_db_path)
# Если файла нет, создаём пустой файл

View File

@@ -12,15 +12,13 @@ class Channel(Base):
id = Column(Integer, primary_key=True)
name = Column(String)
link = Column(String)
admin_id = Column(Integer, ForeignKey('admins.id'))
buttons = relationship('Button', back_populates='channel')
admin_id = Column(Integer, ForeignKey('admins.id')) # если есть таблица admins
class Group(Base):
__tablename__ = 'groups'
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
link = Column(String, nullable=False)
admin_id = Column(Integer, ForeignKey('admins.id'))
buttons = relationship('Button', back_populates='group')
class Button(Base):