
   
ifneq (,$(wildcard ./.env))
	include .env
	export
	ENV_FILE_PARAM = --env-file .env
endif

build:
	docker-compose up --build -d --remove-orphans
up:
	docker-compose up -d
down:
	docker-compose down
logs:
	docker-compose logs
migrate:
	docker-compose exec api python3 manage.py migrate --noinput
makemigrations:
	docker-compose exec api python3 manage.py makemigrations

superuser:
	docker-compose exec api python3 manage.py createsuperuser	

down-v:
	docker-compose down -v

volume:
	docker volume inspect rog_src_postgres_data	

shell:
	docker-compose exec api python3 manage.py shell

# 移行関連タスク
migration-stats:
	docker compose exec app python migration_statistics.py

migration-run:
	docker compose exec app python migration_final_simple.py

migration-location2025:
	docker compose exec app python migration_location2025_support.py

migration-data-protection:
	docker compose exec app python migration_data_protection.py

# Old RogDB → RogDB 移行
migrate-old-rogdb:
	docker compose exec app python migrate_old_rogdb_to_rogdb.py

# rog_team 専用移行 (構造変換)
migrate-rog-team:
	docker compose exec app python migrate_rog_team_enhanced.py

# rog_entry 専用移行 (camelCase対応)
migrate-rog-entry:
	docker compose exec app python migrate_rog_entry_enhanced.py

# rog_goalimages 専用移行 (team_name→zekken_number変換)
migrate-rog-goalimages:
	docker compose exec app python migrate_rog_goalimages_enhanced.py

# 完全移行（通常テーブル + 特殊テーブル）
migrate-full:
	@echo "=== 1. 通常テーブル移行 (特殊テーブル除く) ==="
	$(MAKE) migrate-old-rogdb
	@echo "=== 2. rog_team構造変換移行 ==="
	$(MAKE) migrate-rog-team
	@echo "=== 3. rog_entry camelCase対応移行 ==="
	$(MAKE) migrate-rog-entry
	@echo "=== 4. rog_goalimages team_name→zekken変換移行 ==="
	$(MAKE) migrate-rog-goalimages
	@echo "=== 移行完了 ==="

# カラム名チェック
check-columns:
	docker compose exec app python check_column_names.py

# NULL値チェック
check-null-values:
	docker compose exec app python check_null_values.py

# 完全な移行前チェック
pre-migration-check:
	@echo "=== カラム名チェック ==="
	docker compose exec app python check_column_names.py
	@echo "=== NULL値チェック ==="
	docker compose exec app python check_null_values.py

# 移行前準備（カラム名チェック + 移行実行）
migrate-old-rogdb-safe:
	@echo "=== カラム名チェック実行 ==="
	docker compose exec app python check_column_names.py
	@echo "=== 移行実行 ==="
	docker compose exec app python migrate_old_rogdb_to_rogdb.py

migrate-old-rogdb-stats:
	docker compose exec app python -c "from migrate_old_rogdb_to_rogdb import RogTableMigrator; m = RogTableMigrator(); m.connect_databases(); m.get_rog_tables()"

migrate-old-rogdb-dryrun:
	docker compose exec -e EXCLUDE_TABLES=all app python migrate_old_rogdb_to_rogdb.py

migrate-old-rogdb-exclude-users:
	docker compose exec -e EXCLUDE_TABLES=rog_customuser,rog_session app python migrate_old_rogdb_to_rogdb.py

# データベース関連
db-shell:
	docker compose exec postgres-db psql -U $(POSTGRES_USER) -d $(POSTGRES_DBNAME)

db-backup:
	docker compose exec postgres-db pg_dump -U $(POSTGRES_USER) $(POSTGRES_DBNAME) > backup_$(shell date +%Y%m%d_%H%M%S).sql

# ログ確認
app-logs:
	docker compose logs app --tail=100 -f

db-logs:
	docker compose logs postgres-db --tail=50 -f

