Set sync_topic in migration
This commit is contained in:
		
							parent
							
								
									7e528d9c10
								
							
						
					
					
						commit
						2908c429a5
					
				
					 3 changed files with 26 additions and 2 deletions
				
			
		|  | @ -40,7 +40,6 @@ import ( | |||
| 			login/account endpoints | ||||
| 		reset daily Limits for users | ||||
| 			- set last_stats_reset in migration | ||||
| 		set sync_topic in migration | ||||
| 		update last_seen when API is accessed | ||||
| 		Make sure account endpoints make sense for admins | ||||
| 
 | ||||
|  |  | |||
|  | @ -232,6 +232,8 @@ const ( | |||
| 		DROP TABLE access; | ||||
| 		DROP TABLE user_old; | ||||
| 	` | ||||
| 	migrate1To2SelectAllUsersIDsNoTx = `SELECT id FROM user` | ||||
| 	migrate1To2UpdateSyncTopicNoTx   = `UPDATE user SET sync_topic = ? WHERE id = ?` | ||||
| ) | ||||
| 
 | ||||
| // Manager is an implementation of Manager. It stores users and access control list | ||||
|  | @ -481,7 +483,6 @@ func (a *Manager) AddUser(username, password string, role Role, createdBy string | |||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	// INSERT INTO user (user, pass, role, sync_topic, created_by, created_at, last_seen) | ||||
| 	syncTopic, now := util.RandomString(syncTopicLength), time.Now().Unix() | ||||
| 	if _, err = a.db.Exec(insertUserQuery, username, hash, role, syncTopic, createdBy, now, now); err != nil { | ||||
| 		return err | ||||
|  | @ -885,6 +886,27 @@ func migrateFrom1(db *sql.DB) error { | |||
| 	if _, err := tx.Exec(migrate1To2InsertFromOldTablesAndDropNoTx); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	rows, err := tx.Query(migrate1To2SelectAllUsersIDsNoTx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer rows.Close() | ||||
| 	syncTopics := make(map[int]string) | ||||
| 	for rows.Next() { | ||||
| 		var userID int | ||||
| 		if err := rows.Scan(&userID); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		syncTopics[userID] = util.RandomString(syncTopicLength) | ||||
| 	} | ||||
| 	if err := rows.Close(); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	for userID, syncTopic := range syncTopics { | ||||
| 		if _, err := tx.Exec(migrate1To2UpdateSyncTopicNoTx, syncTopic, userID); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
| 	if _, err := tx.Exec(updateSchemaVersion, 2); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  |  | |||
|  | @ -600,10 +600,13 @@ func TestSqliteCache_Migration_From1(t *testing.T) { | |||
| 
 | ||||
| 	require.Equal(t, "phil", phil.Name) | ||||
| 	require.Equal(t, RoleAdmin, phil.Role) | ||||
| 	require.Equal(t, syncTopicLength, len(phil.SyncTopic)) | ||||
| 	require.Equal(t, 0, len(philGrants)) | ||||
| 
 | ||||
| 	require.Equal(t, "ben", ben.Name) | ||||
| 	require.Equal(t, RoleUser, ben.Role) | ||||
| 	require.Equal(t, syncTopicLength, len(ben.SyncTopic)) | ||||
| 	require.NotEqual(t, ben.SyncTopic, phil.SyncTopic) | ||||
| 	require.Equal(t, 2, len(benGrants)) | ||||
| 	require.Equal(t, "stats", benGrants[0].TopicPattern) | ||||
| 	require.Equal(t, PermissionReadWrite, benGrants[0].Allow) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue