<commit_before>package main

import (
	"errors"
	"fmt"
	"github.com/JackC/cli"
	"github.com/JackC/pgx"
	"github.com/JackC/tern/migrate"
	"github.com/vaughan0/go-ini"
	"os"
	"path/filepath"
	"strconv"
	"time"
)

const VERSION = "1.2.1"

var defaultConf = `[database]
# socket or host is required
# socket =
# host =
# port = 5432
# database is required
# database =
# user defaults to OS user
# user =
# version_table = schema_version

[data]
# Any fields in the data section are available in migration templates
# prefix = foo
`

var sampleMigration = `-- This is a sample migration.

create table people(
  id serial primary key,
  first_name varchar not null,
  last_name varchar not null
);

---- create above / drop below ----

drop table people;
`

var newMigrationText = `-- Write your migrate up statements here

---- create above / drop below ----

-- Write your migrate down statements here. If this migration is irreversible
-- Then delete the separator line above.
`

type Config struct {
	ConnectionParameters pgx.ConnectionParameters
	VersionTable         string
	Data                 map[string]interface{}
}

func (c *Config) Validate() error {
	if c.ConnectionParameters.Host == "" && c.ConnectionParameters.Socket == "" {
		return errors.New("Config must contain host or socket but it does not")
	}

	if c.ConnectionParameters.Database == "" {
		return errors.New("Config must contain database but it does not")
	}

	return nil
}

func main() {
	app := cli.NewApp()
	app.Name = "tern"
	app.Usage = "PostgreSQL database migrator"
	app.Version = VERSION
	app.Author = "<NAME>"
	app.Email = "<EMAIL>"

	app.Commands = []cli.Command{
		{
			Name:        "init",
			ShortName:   "i",
			Usage:       "init a new tern project",
			Synopsis:    "[directory]",
			Description: "Initialize a new tern project in directory",
			Action:      Init,
		},
		{
			Name:        "migrate",
			ShortName:   "m",
			Usage:       "migrate the database",
			Synopsis:    "[command options]",
			Description: "migrate the database to destination version",
			Flags: []cli.Flag{
				cli.StringFlag{"destination, d", "last", "Destination migration version"},
				cli.StringFlag{"migrations, m", ".", "Migrations path"},
				cli.StringFlag{"config, c", "tern.conf", "Config path"},
			},
			Action: Migrate,
		},
		{
			Name:        "new",
			ShortName:   "m",
			Usage:       "generate a new migration",
			Synopsis:    "[command options] name",
			Description: "generate a new migration with the next sequence number and provided name",
			Flags: []cli.Flag{
				cli.StringFlag{"migrations, m", ".", "Migrations path"},
			},
			Action: NewMigration,
		},
	}

	app.Run(os.Args)
}

func Init(c *cli.Context) {
	var directory string
	switch len(c.Args()) {
	case 0:
		directory = "."
	case 1:
		directory = c.Args()[0]
		err := os.Mkdir(directory, os.ModePerm)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)<commit_msg>Fix new migration short name<commit_after>package main

import (
	"errors"
	"fmt"
	"github.com/JackC/cli"
	"github.com/JackC/pgx"
	"github.com/JackC/tern/migrate"
	"github.com/vaughan0/go-ini"
	"os"
	"path/filepath"
	"strconv"
	"time"
)

const VERSION = "1.2.1"

var defaultConf = `[database]
# socket or host is required
# socket =
# host =
# port = 5432
# database is required
# database =
# user defaults to OS user
# user =
# version_table = schema_version

[data]
# Any fields in the data section are available in migration templates
# prefix = foo
`

var sampleMigration = `-- This is a sample migration.

create table people(
  id serial primary key,
  first_name varchar not null,
  last_name varchar not null
);

---- create above / drop below ----

drop table people;
`

var newMigrationText = `-- Write your migrate up statements here

---- create above / drop below ----

-- Write your migrate down statements here. If this migration is irreversible
-- Then delete the separator line above.
`

type Config struct {
	ConnectionParameters pgx.ConnectionParameters
	VersionTable         string
	Data                 map[string]interface{}
}

func (c *Config) Validate() error {
	if c.ConnectionParameters.Host == "" && c.ConnectionParameters.Socket == "" {
		return errors.New("Config must contain host or socket but it does not")
	}

	if c.ConnectionParameters.Database == "" {
		return errors.New("Config must contain database but it does not")
	}

	return nil
}

func main() {
	app := cli.NewApp()
	app.Name = "tern"
	app.Usage = "PostgreSQL database migrator"
	app.Version = VERSION
	app.Author = "<NAME>"
	app.Email = "<EMAIL>"

	app.Commands = []cli.Command{
		{
			Name:        "init",
			ShortName:   "i",
			Usage:       "init a new tern project",
			Synopsis:    "[directory]",
			Description: "Initialize a new tern project in directory",
			Action:      Init,
		},
		{
			Name:        "migrate",
			ShortName:   "m",
			Usage:       "migrate the database",
			Synopsis:    "[command options]",
			Description: "migrate the database to destination version",
			Flags: []cli.Flag{
				cli.StringFlag{"destination, d", "last", "Destination migration version"},
				cli.StringFlag{"migrations, m", ".", "Migrations path"},
				cli.StringFlag{"config, c", "tern.conf", "Config path"},
			},
			Action: Migrate,
		},
		{
			Name:        "new",
			ShortName:   "n",
			Usage:       "generate a new migration",
			Synopsis:    "[command options] name",
			Description: "generate a new migration with the next sequence number and provided name",
			Flags: []cli.Flag{
				cli.StringFlag{"migrations, m", ".", "Migrations path"},
			},
			Action: NewMigration,
		},
	}

	app.Run(os.Args)
}

func Init(c *cli.Context) {
	var directory string
	switch len(c.Args()) {
	case 0:
		directory = "."
	case 1:
		directory = c.Args()[0]
		err := os.Mkdir(directory, os.ModePerm)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
{'fraction_non_alphanumeric': 0.133150778709567, 'fraction_numerical': 0.0037651891151805577, 'mean_word_length': 3.171306209850107, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2777405', 'n_tokens_mistral': 2047, 'n_tokens_neox': 1885, 'n_words': 579}