IFLKNMMP2NMJG46W3MKRLCUSPAW73N7QSDXQLJBAWPYNVEQP6KXQC AVQ66WO4R4KVXAVP4YPEF65CPHJJY55H7ZOVPZ2BHFMGEBTWRUQQC KZKLAINJJWZ64T5MUZT34LJVQIKBTKZ6EJGD7C7TTSSDGCHEDPMAC JAT3DXOLENZZGXE2NYFF3TVQAQIXMMNYO234ETKQGC2CRHJVZERQC T2WZBTVFHVWPKL6AKEWSEVQBR3HWWWUPUNUP2MULF4WXEAZP46KQC RUVJ3V4N5V4Z3HSH2YYESKQF5G7RIHBFB5TLV2IPDWXSGJDRD54AC KLUEQ6X5CXVBV3KLJKEHWQYHIU6AYPP2WT4PWKM2QZJ7SNACCJ6QC E27ZWCDPESXDEHYZONCAKYL2U4K4ZLVXWX4453ICWSH4TGMQI4KQC 2P27XV3DGJCRA4SNJENCJYZLPR2XWZMTY7CGYYSJOY4UMDVVO25AC GPQSOVBPY7VTPHD75R6VWSNITPOL3AECF4DHJB32MF5Z72NV7YMQC LQLC7S3ADBR4O2JYVUSQJD65U3HG4ADOQBGB4F7KQCXUMNKMNEKAC }// resolveDBPath returns the DBPath from the input if set, otherwise falls back// to the package-level dbPath. This supports the incremental migration from// the global variable to explicit input fields.func resolveDBPath(inputPath string) string {if inputPath != "" {return inputPath}return dbPath
}// verifyPatternExistsAndActive checks that a pattern exists and is active.func verifyPatternExistsAndActive(database *sql.DB, patternID string) error {var exists, active boolerr := database.QueryRow("SELECT EXISTS(SELECT 1 FROM cyclic_recording_pattern WHERE id = ?), COALESCE((SELECT active FROM cyclic_recording_pattern WHERE id = ?), false)",patternID, patternID,).Scan(&exists, &active)if err != nil {return fmt.Errorf("failed to query pattern: %w", err)}if !exists {return fmt.Errorf("pattern not found: %s", patternID)}if !active {return fmt.Errorf("pattern '%s' is not active (cannot update inactive patterns)", patternID)}return nil
err := db.WithWriteTx(ctx, dbPath, "create_or_update_pattern", func(database *sql.DB, tx *db.LoggedTx) error {if err := verifyPatternExistsAndActive(database, *input.ID); err != nil {
err := db.WithWriteTx(ctx, resolveDBPath(input.DBPath), "create_or_update_pattern", func(database *sql.DB, tx *db.LoggedTx) error {if err := db.PatternExistsAndActive(database, *input.ID); err != nil {
err := db.WithWriteTx(ctx, dbPath, "create_or_update_location", func(database *sql.DB, tx *db.LoggedTx) error {if err := verifyDatasetExistsAndActive(ctx, tx, *input.DatasetID); err != nil {
err := db.WithWriteTx(ctx, resolveDBPath(input.DBPath), "create_or_update_location", func(database *sql.DB, tx *db.LoggedTx) error {if _, err := db.DatasetExistsAndActive(tx, *input.DatasetID); err != nil {
// verifyDatasetExistsAndActive checks that a dataset exists and is active.func verifyDatasetExistsAndActive(ctx context.Context, q db.Querier, datasetID string) error {_, err := db.DatasetExistsAndActive(q, datasetID)return err}
err := db.WithWriteTx(ctx, dbPath, "create_or_update_location", func(database *sql.DB, tx *db.LoggedTx) error {if err := verifyLocationExistsAndActive(database, locationID); err != nil {
err := db.WithWriteTx(ctx, resolveDBPath(input.DBPath), "create_or_update_location", func(database *sql.DB, tx *db.LoggedTx) error {if err := db.LocationExistsAndActive(database, locationID); err != nil {
}// verifyLocationExistsAndActive checks that a location exists and is active.func verifyLocationExistsAndActive(queryer interface {QueryRow(string, ...any) *sql.Row}, locationID string) error {var exists, active boolerr := queryer.QueryRow("SELECT EXISTS(SELECT 1 FROM location WHERE id = ?), COALESCE((SELECT active FROM location WHERE id = ?), false)",locationID, locationID,).Scan(&exists, &active)if err != nil {return fmt.Errorf("failed to query location: %w", err)}if !exists {return fmt.Errorf("location not found: %s", locationID)}if !active {return fmt.Errorf("location '%s' is not active (cannot update inactive locations)", locationID)}return nil
// verifyExportDataset checks the dataset exists, is active, and is structured.func verifyExportDataset(ctx context.Context, sourceDB *sql.DB, input ExportDatasetInput) (string, error) {var datasetName, datasetType stringerr := sourceDB.QueryRowContext(ctx,"SELECT name, type FROM dataset WHERE id = ? AND active = true",input.DatasetID,).Scan(&datasetName, &datasetType)if err != nil {return "", fmt.Errorf("dataset not found: %s", input.DatasetID)}if datasetType != "structured" {return "", fmt.Errorf("cannot export dataset of type '%s': only structured datasets are supported", datasetType)}return datasetName, nil}
func copyDataToOutput(ctx context.Context, outputDB *sql.DB, tables []TableRelationship, datasetID string) error {_, err := outputDB.ExecContext(ctx, fmt.Sprintf("ATTACH '%s' AS source", dbPath))
func copyDataToOutput(ctx context.Context, outputDB *sql.DB, tables []TableRelationship, datasetID string, sourceDBPath string) error {_, err := outputDB.ExecContext(ctx, fmt.Sprintf("ATTACH '%s' AS source", sourceDBPath))
// verifyDatasetActive checks that a dataset exists and is active.func verifyDatasetActive(database *sql.DB, datasetID string) error {var exists, active boolerr := database.QueryRow("SELECT EXISTS(SELECT 1 FROM dataset WHERE id = ?), COALESCE((SELECT active FROM dataset WHERE id = ?), false)", datasetID, datasetID).Scan(&exists, &active)if err != nil {return fmt.Errorf("failed to query dataset: %w", err)}if !exists {return fmt.Errorf("dataset not found: %s", datasetID)}if !active {return fmt.Errorf("dataset '%s' is not active (cannot update inactive datasets)", datasetID)}return nil}
}// verifyPatternExists verifies a cyclic recording pattern exists and is activefunc verifyPatternExists(ctx context.Context, tx *db.LoggedTx, patternID string) error {var exists, active boolerr := tx.QueryRowContext(ctx,"SELECT EXISTS(SELECT 1 FROM cyclic_recording_pattern WHERE id = ?), COALESCE((SELECT active FROM cyclic_recording_pattern WHERE id = ?), false)",patternID, patternID,).Scan(&exists, &active)if err != nil {return fmt.Errorf("failed to verify cyclic recording pattern: %w", err)}if !exists {return fmt.Errorf("cyclic recording pattern with ID '%s' does not exist", patternID)}if !active {return fmt.Errorf("cyclic recording pattern with ID '%s' is not active", patternID)}return nil
var exists, active boolerr := database.QueryRow("SELECT EXISTS(SELECT 1 FROM cluster WHERE id = ?), COALESCE((SELECT active FROM cluster WHERE id = ?), false)",clusterID, clusterID,).Scan(&exists, &active)if err != nil {return fmt.Errorf("failed to query cluster: %w", err)}if !exists {return fmt.Errorf("cluster not found: %s", clusterID)}if !active {return fmt.Errorf("cluster '%s' is not active (cannot update inactive clusters)", clusterID)}return nil
return db.ClusterExistsAndActive(database, clusterID)
// validateCyclicPattern checks that a cyclic recording pattern exists and is active.func validateCyclicPattern(database *sql.DB, patternID string) error {var exists, active boolerr := database.QueryRow("SELECT EXISTS(SELECT 1 FROM cyclic_recording_pattern WHERE id = ?), COALESCE((SELECT active FROM cyclic_recording_pattern WHERE id = ?), false)",patternID, patternID,).Scan(&exists, &active)if err != nil {return fmt.Errorf("failed to verify cyclic recording pattern: %w", err)}if !exists {return fmt.Errorf("cyclic recording pattern not found: %s", patternID)}if !active {return fmt.Errorf("cyclic recording pattern '%s' is not active", patternID)}return nil}
}// ClusterExistsAndActive checks that a cluster exists and is active.func ClusterExistsAndActive(q Querier, clusterID string) error {var exists, active boolerr := q.QueryRow("SELECT EXISTS(SELECT 1 FROM cluster WHERE id = ?), COALESCE((SELECT active FROM cluster WHERE id = ?), false)",clusterID, clusterID,).Scan(&exists, &active)if err != nil {return fmt.Errorf("failed to verify cluster: %w", err)}if !exists {return fmt.Errorf("cluster not found: %s", clusterID)}if !active {return fmt.Errorf("cluster '%s' is not active (cannot update inactive clusters)", clusterID)}return nil
// PatternExistsAndActive checks that a cyclic recording pattern exists and is active.func PatternExistsAndActive(q Querier, patternID string) error {var exists, active boolerr := q.QueryRow("SELECT EXISTS(SELECT 1 FROM cyclic_recording_pattern WHERE id = ?), COALESCE((SELECT active FROM cyclic_recording_pattern WHERE id = ?), false)",patternID, patternID,).Scan(&exists, &active)if err != nil {return fmt.Errorf("failed to verify cyclic recording pattern: %w", err)}if !exists {return fmt.Errorf("cyclic recording pattern with ID '%s' does not exist", patternID)}if !active {return fmt.Errorf("cyclic recording pattern with ID '%s' is not active", patternID)}return nil}// LocationExistsAndActive checks that a location exists and is active.func LocationExistsAndActive(q Querier, locationID string) error {var exists, active boolerr := q.QueryRow("SELECT EXISTS(SELECT 1 FROM location WHERE id = ?), COALESCE((SELECT active FROM location WHERE id = ?), false)",locationID, locationID,).Scan(&exists, &active)if err != nil {return fmt.Errorf("failed to verify location: %w", err)}if !exists {return fmt.Errorf("location not found: %s", locationID)}if !active {return fmt.Errorf("location '%s' is not active (cannot update inactive locations)", locationID)}return nil}// ValidateDatasetTypeForExport checks that a dataset exists, is active, and is 'structured'.// Returns the dataset name if valid.func ValidateDatasetTypeForExport(q Querier, datasetID string) (string, error) {name, err := DatasetExistsAndActive(q, datasetID)if err != nil {return "", err}datasetType, exists, err := GetDatasetType(q, datasetID)if err != nil {return "", fmt.Errorf("failed to query dataset type: %w", err)}if !exists {return "", fmt.Errorf("dataset not found: %s", datasetID)}if datasetType != "structured" {return "", fmt.Errorf("cannot export dataset of type '%s': only structured datasets are supported", datasetType)}return name, nil}