File size: 8,996 Bytes
dddd1c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
SDU-Shift SQLite から Supabase (PostgreSQL) へのデータ移行スクリプト。

使い方:
    python scripts/migrate_to_supabase.py --sqlite-path /path/to/shift_tool.db --pg-url "postgresql://..."
"""

import os
import sys
import argparse
import sqlite3
import psycopg2
from pathlib import Path

# プロジェクトのルートディレクトリを検索パスに追加
project_root = Path(__file__).resolve().parents[1]
sys.path.append(str(project_root))

from db.database import initialize_db


def get_default_sqlite_path():
    """OSごとのデフォルトの SQLite ファイルパスを取得する"""
    if sys.platform == "darwin":
        return Path.home() / "Library" / "Application Support" / "SDU-Shift" / "shift_tool.db"
    elif sys.platform == "win32":
        return Path(os.environ.get("APPDATA", str(Path.home()))) / "SDU-Shift" / "shift_tool.db"
    else:
        return Path.home() / ".shift_tool" / "shift_tool.db"


def main():
    parser = argparse.ArgumentParser(description="SDU-Shift SQLite to Supabase (PostgreSQL) Data Migrator")
    parser.add_argument(
        "--sqlite-path",
        type=str,
        default=str(get_default_sqlite_path()),
        help="移行元の SQLite データベースファイルへのパス"
    )
    parser.add_argument(
        "--pg-url",
        type=str,
        required=True,
        help="移行先の Supabase/PostgreSQL 接続文字列 (DATABASE_URL)"
    )
    parser.add_argument(
        "--yes",
        action="store_true",
        help="確認プロンプトをスキップする"
    )

    args = parser.parse_args()

    sqlite_path = Path(args.sqlite_path)
    pg_url = args.pg_url

    # 1. 接続確認と事前チェック
    if not sqlite_path.exists():
        print(f"Error: 移行元の SQLite ファイルが見つかりません: {sqlite_path}")
        sys.exit(1)

    print("=== SDU-Shift Supabase 移行ツール ===")
    print(f"移行元 (SQLite): {sqlite_path}")
    # パスワードなどをマスクして表示
    try:
        from urllib.parse import urlparse
        parsed = urlparse(pg_url)
        masked_pg_url = f"{parsed.scheme}://{parsed.username}:******@{parsed.hostname}:{parsed.port}{parsed.path}"
        print(f"移行先 (PostgreSQL): {masked_pg_url}")
    except Exception:
        print("移行先 (PostgreSQL): [接続文字列の解析に失敗しました]")

    if not args.yes:
        confirm = input("\n移行先の PostgreSQL データベースは初期化され、既存のデータはすべて削除されます。続行しますか? (y/N): ")
        if confirm.lower() != 'y':
            print("移行をキャンセルしました。")
            sys.exit(0)

    # 2. PostgreSQL 側のテーブル初期化
    print("\n[1/4] PostgreSQL 側のデータベーススキーマを初期化中...")
    original_db_url = os.environ.get("DATABASE_URL")
    try:
        os.environ["DATABASE_URL"] = pg_url
        initialize_db()
        print("-> スキーマの初期化およびマイグレーションが完了しました。")
    except Exception as e:
        print(f"Error: PostgreSQL の初期化に失敗しました: {e}")
        sys.exit(1)
    finally:
        if original_db_url is not None:
            os.environ["DATABASE_URL"] = original_db_url
        else:
            os.environ.pop("DATABASE_URL", None)

    # 3. データの移行処理
    print("\n[2/4] データを移行中...")
    
    # 接続の確立
    try:
        sqlite_conn = sqlite3.connect(str(sqlite_path))
        sqlite_conn.row_factory = sqlite3.Row
        sqlite_cur = sqlite_conn.cursor()
    except Exception as e:
        print(f"Error: SQLite への接続に失敗しました: {e}")
        sys.exit(1)

    try:
        # psycopg2 の接続を確立
        # トランザクションプーラー経由でも確実に動作するように autocommit=False にする
        pg_conn = psycopg2.connect(pg_url)
        pg_cur = pg_conn.cursor()
    except Exception as e:
        print(f"Error: PostgreSQL への接続に失敗しました: {e}")
        sqlite_conn.close()
        sys.exit(1)

    # 移行対象のテーブル(外部キーの依存関係順にソート)
    # 親テーブルを先に移行し、子テーブルを後に移行する
    tables_to_migrate = [
        "employees",
        "schedule_periods",
        "shift_requests",
        "shift_assignments",
        "shift_constraints",
        "schedule_notes",
        "staff_notes",
        "reservation_counts",
        "app_settings",
        "breakfast_band_constraints",
        "assignment_log",
        "cell_notes",
        "customer_count_history",
        "weather_cache",
        "google_tokens"
    ]

    try:
        # PostgreSQL 側の既存データを CASCADE で TRUNCATE する
        print("-> PostgreSQL 側の既存データを削除中...")
        truncate_query = f"TRUNCATE TABLE {', '.join(tables_to_migrate)} CASCADE;"
        pg_cur.execute(truncate_query)
        pg_conn.commit()
        print("-> 既存データのクリーンアップが完了しました。")
    except Exception as e:
        pg_conn.rollback()
        print(f"Warning: テーブルの TRUNCATE に失敗しました。個別にDELETEを試みます: {e}")
        # 個別に DELETE を逆順で実行
        try:
            for table in reversed(tables_to_migrate):
                pg_cur.execute(f"DELETE FROM {table};")
            pg_conn.commit()
            print("-> 個別の DELETE によるクリーンアップが完了しました。")
        except Exception as delete_err:
            pg_conn.rollback()
            print(f"Error: データベースのクリアに失敗しました: {delete_err}")
            sqlite_conn.close()
            pg_conn.close()
            sys.exit(1)

    # 各テーブルの移行
    for table in tables_to_migrate:
        try:
            # SQLite から全データを取得
            sqlite_cur.execute(f"SELECT * FROM {table}")
            rows = sqlite_cur.fetchall()
            
            if not rows:
                print(f"-> {table}: 移行データなし (0件)")
                continue

            # カラム名の取得
            colnames = list(rows[0].keys())
            
            # PostgreSQL へのインサートクエリを作成
            # %s をプレースホルダとして使用する
            placeholders = ", ".join(["%s"] * len(colnames))
            columns_str = ", ".join(colnames)
            insert_query = f"INSERT INTO {table} ({columns_str}) VALUES ({placeholders})"
            
            # データ挿入
            inserted_count = 0
            for row in rows:
                values = [row[col] for col in colnames]
                pg_cur.execute(insert_query, values)
                inserted_count += 1
            
            pg_conn.commit()
            print(f"-> {table}: {inserted_count} 件のデータを移行しました。")

        except Exception as e:
            pg_conn.rollback()
            print(f"Error: {table} の移行中にエラーが発生しました: {e}")
            sqlite_conn.close()
            pg_conn.close()
            sys.exit(1)

    # 4. PostgreSQL のシーケンス(SEQUENCE)のリセット
    print("\n[3/4] PostgreSQL のシーケンス (ID 自動採番) を同期中...")
    # SERIAL / PRIMARY KEY AUTOINCREMENT で定義されたテーブル
    sequence_tables = [
        "employees",
        "schedule_periods",
        "shift_requests",
        "shift_assignments",
        "schedule_notes",
        "staff_notes",
        "reservation_counts",
        "assignment_log",
        "cell_notes"
    ]

    for table in sequence_tables:
        try:
            # シーケンスの最大値に基づいてシーケンスを更新
            # これをやらないと、新規登録時に重複キーエラーが発生する
            seq_query = f"""
                SELECT setval(
                    pg_get_serial_sequence('{table}', 'id'),
                    coalesce(max(id), 1),
                    max(id) IS NOT NULL
                ) FROM {table};
            """
            pg_cur.execute(seq_query)
            pg_conn.commit()
            print(f"-> {table} のシーケンスを同期しました。")
        except Exception as e:
            pg_conn.rollback()
            print(f"Warning: {table} のシーケンス同期中にエラーが発生しました: {e}")

    # 接続のクローズ
    sqlite_conn.close()
    pg_conn.close()

    print("\n[4/4] 移行処理が正常に完了しました! 🎉")
    print("Supabase のダッシュボードでデータが正しく移行されていることを確認してください。")
    print("Web版を起動する際は、環境変数 DATABASE_URL に Supabase の接続文字列を設定してください。")


if __name__ == "__main__":
    main()