<commit_before>package tables

import (
	"database/sql"

	"github.com/pkg/errors"
)

func init() {
	MigrationClient.AddMigration(Up_20171219164727, Down_20171219164727)
}

func Up_20171219164727(tx *sql.Tx) error {
	// Add query name column
	query := `
		ALTER TABLE scheduled_queries
		ADD COLUMN query_name varchar(255)
	`
	if _, err := tx.Exec(query); err != nil {
		return errors.Wrap(err, "adding query_name column")
	}

	// Populate query name column via join with query ID
	query = `
		UPDATE scheduled_queries
		SET query_name = (SELECT name from queries where id = query_id)
	`
	if _, err := tx.Exec(query); err != nil {
		return errors.Wrap(err, "populating query_name column")
	}

	// Add not null constraint
	query = `
		ALTER TABLE scheduled_queries
		MODIFY query_name varchar(255) NOT NULL
	`<commit_msg>Fix error when migrating packs and scheduled query has been deleted. (#1851)<commit_after>package tables

import (
	"database/sql"

	"github.com/pkg/errors"
)

func init() {
	MigrationClient.AddMigration(Up_20171219164727, Down_20171219164727)
}

func Up_20171219164727(tx *sql.Tx) error {
	// Add query name column
	query := `
		ALTER TABLE scheduled_queries
		ADD COLUMN query_name varchar(255)
	`
	if _, err := tx.Exec(query); err != nil {
		return errors.Wrap(err, "adding query_name column")
	}

	// Populate query name column via join with query ID
	query = `
		UPDATE scheduled_queries
		SET query_name = (SELECT name from queries where id = query_id)
	`
	if _, err := tx.Exec(query); err != nil {
		return errors.Wrap(err, "populating query_name column")
	}

	// Clear out any scheduled queries that didn't correspond to a query
	query = `
		DELETE FROM scheduled_queries
		WHERE query_name IS NULL
	`
	if _, err := tx.Exec(query); err != nil {
		return errors.Wrap(err, "clear invalid scheduled queries")
	}

	// Add not null constraint
	query = `
		ALTER TABLE scheduled_queries
		MODIFY query_name varchar(255) NOT NULL
	`
{'fraction_non_alphanumeric': 0.09989754098360656, 'fraction_numerical': 0.05122950819672131, 'mean_word_length': 4.007692307692308, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6991089', 'n_tokens_mistral': 740, 'n_tokens_neox': 651, 'n_words': 199}