Use month-day-count numbering system
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				Build / build (push) Successful in 2m59s
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	Build / build (push) Successful in 2m59s
				
			This commit is contained in:
		
							
								
								
									
										18
									
								
								episodes.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								episodes.go
									
									
									
									
									
								
							@@ -16,23 +16,29 @@ func CalculateEpisodeNumbers(db *sql.DB) error {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	defer tx.Rollback()
 | 
						defer tx.Rollback()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// First find all years that still need episodes numbers
 | 
						// First find all days that still need episodes numbers
 | 
				
			||||||
	rows, err := tx.Query("select v.year, max((select max(episode) from videos where year = v.year)) from videos v where episode is null group by year")
 | 
						rows, err := tx.Query(`
 | 
				
			||||||
 | 
							select v.upload_date,
 | 
				
			||||||
 | 
								ifnull(
 | 
				
			||||||
 | 
									(select max(episode) from videos where upload_date = v.upload_date),
 | 
				
			||||||
 | 
									substr(upload_date, 6, 2) || substr(upload_date, 9, 2) || '00')
 | 
				
			||||||
 | 
							from videos v where episode is null group by upload_date`)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return fmt.Errorf("error retrieving rows: %w", err)
 | 
							return fmt.Errorf("error retrieving rows: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Loop over each year and set the missing episode numbers
 | 
						// Loop over each year and set the missing episode numbers
 | 
				
			||||||
	for rows.Next() {
 | 
						for rows.Next() {
 | 
				
			||||||
		var year, lastEpisode int
 | 
							var uploadDate string
 | 
				
			||||||
		err = rows.Scan(&year, &lastEpisode)
 | 
							var lastEpisode int
 | 
				
			||||||
		log.Printf("Last episode is %d", lastEpisode)
 | 
							err = rows.Scan(&uploadDate, &lastEpisode)
 | 
				
			||||||
 | 
							log.Printf("Last episode for %s is %d", uploadDate, lastEpisode)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return fmt.Errorf("error getting row: %w", err)
 | 
								return fmt.Errorf("error getting row: %w", err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Find all episodes that need updating
 | 
							// Find all episodes that need updating
 | 
				
			||||||
		episodes, err := tx.Query("select id from videos where year = ? and episode is null order by run, id desc", year)
 | 
							episodes, err := tx.Query("select id from videos where upload_date = ? and episode is null order by run, id desc", uploadDate)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return fmt.Errorf("error retrieving episodes: %w", err)
 | 
								return fmt.Errorf("error retrieving episodes: %w", err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,7 +11,13 @@ func TestCalculateEpisodeNumbers(t *testing.T) {
 | 
				
			|||||||
		t.Error(err)
 | 
							t.Error(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	_, err = db.Exec("insert into videos (title, url, year, episode, run) values ('Video A', 'a', 2025, 1, 1), ('Video B', 'b', 2025, null, 2), ('Old Video', 'c', 2020, 100, 1)")
 | 
						_, err = db.Exec(`insert into videos (title, url, upload_date, year, episode, run) values
 | 
				
			||||||
 | 
							('Video A', 'a', '2025-01-04', 2025, 10401, 1),
 | 
				
			||||||
 | 
							('Video D', 'd', '2025-01-05', 2025, null, 2),
 | 
				
			||||||
 | 
							('Video C', 'c', '2025-01-04', 2025, null, 2),
 | 
				
			||||||
 | 
							('Video B', 'b', '2025-01-04', 2025, null, 2),
 | 
				
			||||||
 | 
							('Video E', 'e', '2025-10-01', 2025, null, 3),
 | 
				
			||||||
 | 
							('Old Video', 'z', '2020-12-31', 2020, 100, 1)`)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Error(err)
 | 
							t.Error(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -26,31 +32,31 @@ func TestCalculateEpisodeNumbers(t *testing.T) {
 | 
				
			|||||||
		t.Error(err)
 | 
							t.Error(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var title string
 | 
						expectedList := []struct {
 | 
				
			||||||
	var episode int
 | 
							Title   string
 | 
				
			||||||
	rows.Next()
 | 
							Episode int
 | 
				
			||||||
	err = rows.Scan(&title, &episode)
 | 
						}{
 | 
				
			||||||
	if err != nil {
 | 
							{"Video A", 10401},
 | 
				
			||||||
		t.Error(err)
 | 
							{"Video D", 10501},
 | 
				
			||||||
 | 
							{"Video C", 10403},
 | 
				
			||||||
 | 
							{"Video B", 10402},
 | 
				
			||||||
 | 
							{"Video E", 100101},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if title != "Video A" {
 | 
						for _, expected := range expectedList {
 | 
				
			||||||
		t.Errorf("Title is %s, expected Video A", title)
 | 
							var title string
 | 
				
			||||||
	}
 | 
							var episode int
 | 
				
			||||||
	if episode != 1 {
 | 
							rows.Next()
 | 
				
			||||||
		t.Errorf("Episode is %d, expected 1", episode)
 | 
							err = rows.Scan(&title, &episode)
 | 
				
			||||||
	}
 | 
							if err != nil {
 | 
				
			||||||
 | 
								t.Error(err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	rows.Next()
 | 
							if title != expected.Title {
 | 
				
			||||||
	err = rows.Scan(&title, &episode)
 | 
								t.Errorf("Title is %s, expected %s", title, expected.Title)
 | 
				
			||||||
	if err != nil {
 | 
							}
 | 
				
			||||||
		t.Error(err)
 | 
							if episode != expected.Episode {
 | 
				
			||||||
	}
 | 
								t.Errorf("Episode is %d, expected %d for video %s", episode, expected.Episode, title)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	if title != "Video B" {
 | 
					 | 
				
			||||||
		t.Errorf("Title is %s, expected Video B", title)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if episode != 2 {
 | 
					 | 
				
			||||||
		t.Errorf("Episode is %d, expected 2", episode)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										1
									
								
								migrations/2_renumber.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								migrations/2_renumber.sql
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					UPDATE videos SET episode=NULL, state='pending', cast=NULL;
 | 
				
			||||||
		Reference in New Issue
	
	Block a user