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()
 | 
			
		||||
 | 
			
		||||
	// First find all years 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")
 | 
			
		||||
	// First find all days that still need episodes numbers
 | 
			
		||||
	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 {
 | 
			
		||||
		return fmt.Errorf("error retrieving rows: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Loop over each year and set the missing episode numbers
 | 
			
		||||
	for rows.Next() {
 | 
			
		||||
		var year, lastEpisode int
 | 
			
		||||
		err = rows.Scan(&year, &lastEpisode)
 | 
			
		||||
		log.Printf("Last episode is %d", lastEpisode)
 | 
			
		||||
		var uploadDate string
 | 
			
		||||
		var lastEpisode int
 | 
			
		||||
		err = rows.Scan(&uploadDate, &lastEpisode)
 | 
			
		||||
		log.Printf("Last episode for %s is %d", uploadDate, lastEpisode)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("error getting row: %w", err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 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 {
 | 
			
		||||
			return fmt.Errorf("error retrieving episodes: %w", err)
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
@@ -11,7 +11,13 @@ func TestCalculateEpisodeNumbers(t *testing.T) {
 | 
			
		||||
		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 {
 | 
			
		||||
		t.Error(err)
 | 
			
		||||
	}
 | 
			
		||||
@@ -26,6 +32,18 @@ func TestCalculateEpisodeNumbers(t *testing.T) {
 | 
			
		||||
		t.Error(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	expectedList := []struct {
 | 
			
		||||
		Title   string
 | 
			
		||||
		Episode int
 | 
			
		||||
	}{
 | 
			
		||||
		{"Video A", 10401},
 | 
			
		||||
		{"Video D", 10501},
 | 
			
		||||
		{"Video C", 10403},
 | 
			
		||||
		{"Video B", 10402},
 | 
			
		||||
		{"Video E", 100101},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, expected := range expectedList {
 | 
			
		||||
		var title string
 | 
			
		||||
		var episode int
 | 
			
		||||
		rows.Next()
 | 
			
		||||
@@ -34,23 +52,11 @@ func TestCalculateEpisodeNumbers(t *testing.T) {
 | 
			
		||||
			t.Error(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	if title != "Video A" {
 | 
			
		||||
		t.Errorf("Title is %s, expected Video A", title)
 | 
			
		||||
		if title != expected.Title {
 | 
			
		||||
			t.Errorf("Title is %s, expected %s", title, expected.Title)
 | 
			
		||||
		}
 | 
			
		||||
	if episode != 1 {
 | 
			
		||||
		t.Errorf("Episode is %d, expected 1", episode)
 | 
			
		||||
		if episode != expected.Episode {
 | 
			
		||||
			t.Errorf("Episode is %d, expected %d for video %s", episode, expected.Episode, title)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	rows.Next()
 | 
			
		||||
	err = rows.Scan(&title, &episode)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Error(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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