private static void TransferItemsWithId <T>(DbTorronto db, DbTorronto postgres, string tableName, bool resetSeq) where T : class { List <T> items; var offset = 0; var count = 100; var options = new BulkCopyOptions { KeepIdentity = true }; Console.WriteLine("TABLE {0}", tableName); do { items = db.GetTable <T>() .Skip(offset) .Take(count) .ToList(); postgres.BulkCopy(options, items); offset += count; Console.WriteLine("processed: {0}", offset); } while (items.Count > 0); if (resetSeq) { postgres.Execute(string.Format(@" SELECT setval(pg_get_serial_sequence('{0}', 'id'), (SELECT MAX(id) FROM {0})); ", tableName)); } }
private static void PgMigrate() { Console.WriteLine("Migration started"); using (var db = new DbTorronto()) using (var postgres = new DbTorronto("torrontopg")) { postgres.Execute("select truncate_tables('postgres')"); TransferItemsWithId <Movie>(db, postgres, "movies", true); TransferItemsWithId <Torrent>(db, postgres, "torrents", true); TransferItemsWithId <Genre>(db, postgres, "genres", true); TransferItemsWithId <Person>(db, postgres, "persons", true); TransferItemsWithId <User>(db, postgres, "users", true); TransferItemsWithId <MovieGenre>(db, postgres, "movies_genres", false); TransferItemsWithId <MoviePerson>(db, postgres, "movies_persons", false); TransferItemsWithId <MovieRecommendation>(db, postgres, "movies_recommendations", false); TransferItemsWithId <MovieUser>(db, postgres, "movies_users", true); TransferItemsWithId <TorrentUser>(db, postgres, "torrents_users", false); TransferItemsWithId <UserIdentity>(db, postgres, "user_identities", true); } Console.WriteLine("Migration finished"); }
public static void RefreshTopWeekMovies(this DbTorronto db) { db.Execute("REFRESH MATERIALIZED VIEW movies_top_week;"); }