<commit_before>package schemamagic

import (
	"fmt"

	"strings"

	"gopkg.in/jackc/pgx.v2"
)

// Table holds the table details as well as all the columns inside the table
type Table struct {
	Name          string
	DefaultSchema string
	Database      string
	Tx            *pgx.Tx
	Autocommit    bool
	Columns       []Column
	constraints   []Constraint
}

// NewTable creates and returns an instance of a postgres table
func NewTable(t Table) *Table {
	table := new(Table)
	table.Name = t.Name
	table.DefaultSchema = t.DefaultSchema
	table.Database = t.Database
	table.Autocommit = t.Autocommit
	table.Columns = t.Columns
	table.Tx = t.Tx
	return table
}

// Append method accepts a column and appends it to the list of columns of the table
func (t *Table) Append(col Column) {
	t.Columns = append(t.Columns, col)
}

// AddConstraint accepts a constraint and appends it to the list of constraints that need to be applied on the table
func (t *Table) AddConstraint(constraint Constraint) {
	t.constraints = append(t.constraints, constraint)
}

// Begin method initiates a DB transaction and checks if table (Name) exists in the DB. If it does, then it calls updateTable(). If it doesn't, it calls createTable(), and then updateTable()"""
func (t *Table) Begin() {
	log.Infoln("Operating on table --> ", t.Name)
	// Create the schema here
	schemaStatement := fmt.Sprintf("CREATE SCHEMA IF NOT EXISTS %s", t.DefaultSchema)
	_, err := t.Tx.Exec(schemaStatement)
	if err != nil {
		t.Tx.Rollback()
		log.Warningln("Couldn't create schema --> ", t.DefaultSchema, " with error being --> ", err)
	}
	//  Check if table exists in the database
	presence := t.checkTableExistence()
	if !presence {
		// Table does not exist --> need to create it
		t.createTable()
	}
	// Loop over all the available columns and call updateTable() on each column
	for _, col := range t.Columns {
		log.Debugln("-----------------------------------------------")
		t.updateTable(col)
		log.Debugln("-----------------------------------------------")
	}

	// Iterate over the available constraints and apply them
	for _, constraint := range t.constraints {
		// 1. drop them first
		dropRule := constraint.createDropRule(t.Name)
		log.Warningln("Constraint drop rule is ", dropRule)
		err := t.executeSQL(dropRule)
		if err != nil {
			log.Fatalln("While trying to drop constraint rule --> ", dropRule, "\n the error is ", err.Error())
		}
		// 2. add them
		addRule := constraint.createAddRule(t.Name)
		log.Warningln("Constraint add rule is ", addRule)
		err = t.executeSQL(addRule)
		if err != nil {
			log.Fatalln("While trying to add constraint rule -->  ", addRule, "\n the error is ", err.Error())
		}
	}

	if t.Autocommit {
		commitErr := t.Tx.Commit()
		if commitErr != nil {
			t.Tx.Rollback()
			log.Warningln("Couldn't commit changes to the TABLE --> ", t.Name, " with error being --> ", commitErr)<commit_msg>Replaced warning logs with debug logs at unimportant locations.<commit_after>package schemamagic

import (
	"fmt"

	"strings"

	"gopkg.in/jackc/pgx.v2"
)

// Table holds the table details as well as all the columns inside the table
type Table struct {
	Name          string
	DefaultSchema string
	Database      string
	Tx            *pgx.Tx
	Autocommit    bool
	Columns       []Column
	constraints   []Constraint
}

// NewTable creates and returns an instance of a postgres table
func NewTable(t Table) *Table {
	table := new(Table)
	table.Name = t.Name
	table.DefaultSchema = t.DefaultSchema
	table.Database = t.Database
	table.Autocommit = t.Autocommit
	table.Columns = t.Columns
	table.Tx = t.Tx
	return table
}

// Append method accepts a column and appends it to the list of columns of the table
func (t *Table) Append(col Column) {
	t.Columns = append(t.Columns, col)
}

// AddConstraint accepts a constraint and appends it to the list of constraints that need to be applied on the table
func (t *Table) AddConstraint(constraint Constraint) {
	t.constraints = append(t.constraints, constraint)
}

// Begin method initiates a DB transaction and checks if table (Name) exists in the DB. If it does, then it calls updateTable(). If it doesn't, it calls createTable(), and then updateTable()"""
func (t *Table) Begin() {
	log.Infoln("Operating on table --> ", t.Name)
	// Create the schema here
	schemaStatement := fmt.Sprintf("CREATE SCHEMA IF NOT EXISTS %s", t.DefaultSchema)
	_, err := t.Tx.Exec(schemaStatement)
	if err != nil {
		t.Tx.Rollback()
		log.Warningln("Couldn't create schema --> ", t.DefaultSchema, " with error being --> ", err)
	}
	//  Check if table exists in the database
	presence := t.checkTableExistence()
	if !presence {
		// Table does not exist --> need to create it
		t.createTable()
	}
	// Loop over all the available columns and call updateTable() on each column
	for _, col := range t.Columns {
		log.Debugln("-----------------------------------------------")
		t.updateTable(col)
		log.Debugln("-----------------------------------------------")
	}

	// Iterate over the available constraints and apply them
	for _, constraint := range t.constraints {
		// 1. drop them first
		dropRule := constraint.createDropRule(t.Name)
		log.Debugln("Constraint drop rule is ", dropRule)
		err := t.executeSQL(dropRule)
		if err != nil {
			log.Fatalln("While trying to drop constraint rule --> ", dropRule, "\n the error is ", err.Error())
		}
		// 2. add them
		addRule := constraint.createAddRule(t.Name)
		log.Debugln("Constraint add rule is ", addRule)
		err = t.executeSQL(addRule)
		if err != nil {
			log.Fatalln("While trying to add constraint rule -->  ", addRule, "\n the error is ", err.Error())
		}
	}

	if t.Autocommit {
		commitErr := t.Tx.Commit()
		if commitErr != nil {
			t.Tx.Rollback()
			log.Warningln("Couldn't commit changes to the TABLE --> ", t.Name, " with error being --> ", commitErr)
{'fraction_non_alphanumeric': 0.14484536082474228, 'fraction_numerical': 0.0010309278350515464, 'mean_word_length': 2.970668485675307, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '7403880', 'n_tokens_mistral': 1834, 'n_tokens_neox': 1757, 'n_words': 625}