<commit_before>package application

import (
	"database/sql"
	"errors"
	"fmt"
	_ "github.com/mattn/go-sqlite3"
	"os"
)

var ErrInvalidConfiguration = errors.New("infrastructure configuration parameters are invalid")

// ErrFailedToCreatedDB for database error
type ErrFailedToCreatedDB struct {
	Msg string
}

// Error to satisfy the Error interface
func (e *ErrFailedToCreatedDB) Error() string {
	return fmt.Sprintf("failed to create database: %s", e.Msg)
}

// New creates a new instance of Infrastructure
func NewSchema(path string, name string, memory bool) (*Infrastructure, error) {

	if name == "" || path == "" {
		return &Infrastructure{}, ErrInvalidConfiguration
	}

	var db *sql.DB
	var err error

	if memory {
		db, err = sql.Open("sqlite3", ":memory:")
	} else {
		if err := validatePath(path); err != nil {
			return &Infrastructure{}, ErrInvalidConfiguration
		}
		db, err = sql.Open("sqlite3", path+"/"+name+".db")
	}
	if err != nil {
		return nil, err
	}

	// TODO: Move table creation to a proper place ---------------------------------------------------------------------
	// Create table in order to create the database file
	sqlStmt := `
	CREATE TABLE IF NOT EXISTS seller
	(
		slug TEXT NOT NULL PRIMARY KEY,
		name TEXT
	);
	`
	_, err = db.Exec(sqlStmt)
	if err != nil {
		return &Infrastructure{}, &ErrFailedToCreatedDB{Msg: err.Error()}
	}

	sqlStmt = `
	CREATE TABLE IF NOT EXISTS transaction_types
	(
		TYPE CHAR NOT NULL
	)
	`

	_, err = db.Exec(sqlStmt)
	if err != nil {
		return &Infrastructure{}, &ErrFailedToCreatedDB{Msg: err.Error()}
	}

	// Create transactions table in order to create the database file
	// 25-10-2017;25-10-2017;COMPRA CONTINENTE MAI ;77,52;;61,25;61,25;
	// id, seller_id, debt amount, credit amount, contabilistic, real
	sqlStmt = `
	CREATE TABLE IF NOT EXISTS transactions
	(
		ID int NOT NULL PRIMARY KEY,
		SELLER_ID int NOT NULL,
		AMOUNT int DEFAULT 0,
		TYPE 
		BALANCE int NOT NULL
	);
	`
	_, err = db.Exec(sqlStmt)
	if err != nil {
		return &Infrastructure{}, &ErrFailedToCreatedDB{Msg: err.Error()}
	}

	// -----------------------------------------------------------------------------------------------------------------

	s := &Infrastructure{db}
	return s, nil
}

// validatePath validates that the database directory or file exists and it is
// in a proper format to be used
func validatePath(path string) error {
	fileInfo, err := os.Stat(path)
	if err != nil && os.IsNotExist(err) {
		err := os.MkdirAll(path, 0755)
		if err != nil {
			return err
		}
	} else {
		if err != nil {
			return err
		}
		fileInfo, _ = os.Stat(path)
		if !fileInfo.Mode().IsDir() {
			return err
		}
	}

	return nil
}

// Infrastructure contains a Infrastructure database
type Infrastructure struct {
	db *sql.DB
}
<commit_msg>Updates SQL to create tables<commit_after>package application

import (
	"database/sql"
	"errors"
	"fmt"
	_ "github.com/mattn/go-sqlite3"
	"os"
)

var ErrInvalidConfiguration = errors.New("infrastructure configuration parameters are invalid")

// ErrFailedToCreatedDB for database error
type ErrFailedToCreatedDB struct {
	Msg string
}

// Error to satisfy the Error interface
func (e *ErrFailedToCreatedDB) Error() string {
	return fmt.Sprintf("failed to create database: %s", e.Msg)
}

// New creates a new instance of Infrastructure
func NewSchema(path string, name string, memory bool) (*Infrastructure, error) {

	if name == "" || path == "" {
		return &Infrastructure{}, ErrInvalidConfiguration
	}

	var db *sql.DB
	var err error

	if memory {
		db, err = sql.Open("sqlite3", ":memory:")
	} else {
		if err := validatePath(path); err != nil {
			return &Infrastructure{}, ErrInvalidConfiguration
		}
		db, err = sql.Open("sqlite3", path+"/"+name+".db")
	}
	if err != nil {
		return nil, err
	}

	// TODO: Move table creation to a proper place ---------------------------------------------------------------------
	// Create table in order to create the database file
	sqlStmt := `
CREATE TABLE IF NOT EXISTS seller (
  slug TEXT NOT NULL,
  name TEXT,
  UNIQUE (slug)
);
	`
	_, err = db.Exec(sqlStmt)
	if err != nil {
		return &Infrastructure{}, &ErrFailedToCreatedDB{Msg: err.Error()}
	}

	// Create transactions table in order to create the database file
	// 25-10-2017;25-10-2017;COMPRA CONTINENTE MAI ;77,52;;61,25;61,25;
	// id, seller_id, debt amount, credit amount, contabilistic, real
	sqlStmt = `
CREATE TABLE IF NOT EXISTS "transaction"(
  id INTEGER PRIMARY KEY,
  seller BIGINT NOT NULL,
  amount BIGINT NOT NULL,
  FOREIGN KEY (seller) REFERENCES seller(slug)
);
	`
	_, err = db.Exec(sqlStmt)
	if err != nil {
		return &Infrastructure{}, &ErrFailedToCreatedDB{Msg: err.Error()}
	}

	// -----------------------------------------------------------------------------------------------------------------

	s := &Infrastructure{db}
	return s, nil
}

// validatePath validates that the database directory or file exists and it is
// in a proper format to be used
func validatePath(path string) error {
	fileInfo, err := os.Stat(path)
	if err != nil && os.IsNotExist(err) {
		err := os.MkdirAll(path, 0755)
		if err != nil {
			return err
		}
	} else {
		if err != nil {
			return err
		}
		fileInfo, _ = os.Stat(path)
		if !fileInfo.Mode().IsDir() {
			return err
		}
	}

	return nil
}

// Infrastructure contains a Infrastructure database
type Infrastructure struct {
	db *sql.DB
}

{'fraction_non_alphanumeric': 0.17948240551107802, 'fraction_numerical': 0.013219139824986036, 'mean_word_length': 2.722799722799723, 'pattern_counts': {'":': 2, '<': 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': '2206807', 'n_tokens_mistral': 1808, 'n_tokens_neox': 1682, 'n_words': 542}