| 42 | const postgresImportAdvisoryLockID int64 = 922337203685477001 |
| 43 | |
| 44 | func MigrateSQLiteToPostgres(ctx context.Context, opts SQLiteToPostgresMigrationOptions) (*SQLiteToPostgresMigrationReport, error) { |
| 45 | sqlitePath, pgDSN, err := validateMigrationOptions(opts) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | |
| 50 | sqliteDB, err := openSQLiteDB(sqlitePath) |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | defer sqliteDB.Close() |
| 55 | |
| 56 | tables, err := listSQLiteTables(ctx, sqliteDB) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | if len(tables) == 0 { |
| 61 | return nil, errors.New("sqlite database has no importable tables") |
| 62 | } |
| 63 | |
| 64 | sqliteCounts, err := countSQLiteRows(ctx, sqliteDB, tables) |
| 65 | if err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | |
| 69 | pool, err := openPostgresPool(ctx, pgDSN) |
| 70 | if err != nil { |
| 71 | return nil, err |
| 72 | } |
| 73 | defer pool.Close() |
| 74 | |
| 75 | if !opts.Apply { |
| 76 | return runDryRunReport(ctx, pool, tables, sqliteCounts) |
| 77 | } |
| 78 | |
| 79 | if err := bootstrapPostgresSchema(pgDSN); err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | |
| 83 | conn, tx, err := beginImportTx(ctx, pool) |
| 84 | if err != nil { |
| 85 | return nil, err |
| 86 | } |
| 87 | defer conn.Release() |
| 88 | |
| 89 | committed := false |
| 90 | defer func() { |
| 91 | if !committed { |
| 92 | _ = tx.Rollback(ctx) |
| 93 | } |
| 94 | }() |
| 95 | |
| 96 | orderedTables, err := orderPostgresImportTables(ctx, tx, tables) |
| 97 | if err != nil { |
| 98 | return nil, err |
| 99 | } |
| 100 | tables = orderedTables |
| 101 | |