Skip to content

Commit

Permalink
Merge pull request #142 from schemahero/0.8.0-alpha.2
Browse files Browse the repository at this point in the history
Adding support to "plan" to a file and then "apply"
  • Loading branch information
marccampbell committed Apr 5, 2020
2 parents f1ecf35 + 24a2cfa commit 1e66c8f
Show file tree
Hide file tree
Showing 28 changed files with 166 additions and 111 deletions.
4 changes: 2 additions & 2 deletions integration/tests/mysql/column-set-default/expect.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
alter table `users` modify column `account_type` varchar (10) not null default "trial"
alter table `users` modify column `num_seats` int (11) not null default "5"
alter table `users` modify column `account_type` varchar (10) not null default "trial";
alter table `users` modify column `num_seats` int (11) not null default "5";
4 changes: 2 additions & 2 deletions integration/tests/mysql/column-unset-default/expect.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
alter table `users` modify column `account_type` varchar (10)
alter table `users` modify column `num_seats` int (11)
alter table `users` modify column `account_type` varchar (10);
alter table `users` modify column `num_seats` int (11);
2 changes: 1 addition & 1 deletion integration/tests/mysql/create-table/expect.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
create table `users` (`id` int (11), `login` varchar (255), `name` varchar (255) not null default 'ethan', primary key (`id`))
create table `users` (`id` int (11), `login` varchar (255), `name` varchar (255) not null default 'ethan', primary key (`id`));
2 changes: 1 addition & 1 deletion integration/tests/mysql/foreign-key-action/expect.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
create table `user_project` (`user_id` int (11), `project_id` int (11), `misc_id` varchar (255) not null, primary key (`user_id`, `project_id`), constraint user_project_user_id_fkey foreign key (user_id) references users (id), constraint user_project_project_id_fkey foreign key (project_id) references projects (id), constraint misc_named_fk foreign key (misc_id) references misc (pk) on delete cascade)
create table `user_project` (`user_id` int (11), `project_id` int (11), `misc_id` varchar (255) not null, primary key (`user_id`, `project_id`), constraint user_project_user_id_fkey foreign key (user_id) references users (id), constraint user_project_project_id_fkey foreign key (project_id) references projects (id), constraint misc_named_fk foreign key (misc_id) references misc (pk) on delete cascade);
2 changes: 1 addition & 1 deletion integration/tests/mysql/foreign-key-alter/expect.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
alter table issues add constraint renamed_fkey foreign key (project_id) references projects (id)
alter table issues add constraint renamed_fkey foreign key (project_id) references projects (id);
2 changes: 1 addition & 1 deletion integration/tests/mysql/foreign-key-create/expect.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
create table `user_project` (`user_id` int (11), `project_id` int (11), `misc_id` varchar (255) not null, primary key (`user_id`, `project_id`), constraint user_project_user_id_fkey foreign key (user_id) references users (id), constraint user_project_project_id_fkey foreign key (project_id) references projects (id), constraint misc_named_fk foreign key (misc_id) references misc (pk))
create table `user_project` (`user_id` int (11), `project_id` int (11), `misc_id` varchar (255) not null, primary key (`user_id`, `project_id`), constraint user_project_user_id_fkey foreign key (user_id) references users (id), constraint user_project_project_id_fkey foreign key (project_id) references projects (id), constraint misc_named_fk foreign key (misc_id) references misc (pk));
2 changes: 1 addition & 1 deletion integration/tests/mysql/foreign-key-drop/expect.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
alter table `org` modify column `project_id` int (11) not null
alter table `org` modify column `project_id` int (11) not null;
2 changes: 1 addition & 1 deletion integration/tests/mysql/index-create/expect.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
create unique index idx_users_email on users (email)
create unique index idx_users_email on users (email);
2 changes: 1 addition & 1 deletion integration/tests/mysql/not-null/expect.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
alter table `projects` modify column `icon_uri` varchar (255)
alter table `projects` modify column `icon_uri` varchar (255);
2 changes: 1 addition & 1 deletion integration/tests/mysql/primary-key-add/expect.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
alter table `user_projects` add constraint `user_projects_pkey` primary key (`user_id`, `project_id`)
alter table `user_projects` add constraint `user_projects_pkey` primary key (`user_id`, `project_id`);
2 changes: 1 addition & 1 deletion integration/tests/mysql/primary-key-drop/expect.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
alter table `user_projects` drop primary key
alter table `user_projects` drop primary key;
4 changes: 2 additions & 2 deletions integration/tests/postgres/column-set-default/expect.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
alter table "users" alter column "account_type" set default 'trial', alter column "account_type" set not null
alter table "users" alter column "num_seats" set default '5', alter column "num_seats" set not null
alter table "users" alter column "account_type" set default 'trial', alter column "account_type" set not null;
alter table "users" alter column "num_seats" set default '5', alter column "num_seats" set not null;
4 changes: 2 additions & 2 deletions integration/tests/postgres/column-unset-default/expect.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
alter table "users" alter column "account_type" drop default, alter column "account_type" drop not null
alter table "users" alter column "num_seats" drop default, alter column "num_seats" drop not null
alter table "users" alter column "account_type" drop default, alter column "account_type" drop not null;
alter table "users" alter column "num_seats" drop default, alter column "num_seats" drop not null;
2 changes: 1 addition & 1 deletion integration/tests/postgres/create-table/expect.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
create table "users" ("id" integer, "login" character varying (255), "name" character varying (255) not null default 'ethan', primary key ("id"))
create table "users" ("id" integer, "login" character varying (255), "name" character varying (255) not null default 'ethan', primary key ("id"));
2 changes: 1 addition & 1 deletion integration/tests/postgres/foreign-key-action/expect.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
create table "user_project" ("user_id" integer, "project_id" integer, "misc_id" character varying (255) not null, primary key ("user_id", "project_id"), constraint user_project_user_id_fkey foreign key (user_id) references users (id), constraint user_project_project_id_fkey foreign key (project_id) references projects (id), constraint misc_named_fk foreign key (misc_id) references misc (pk) on delete cascade)
create table "user_project" ("user_id" integer, "project_id" integer, "misc_id" character varying (255) not null, primary key ("user_id", "project_id"), constraint user_project_user_id_fkey foreign key (user_id) references users (id), constraint user_project_project_id_fkey foreign key (project_id) references projects (id), constraint misc_named_fk foreign key (misc_id) references misc (pk) on delete cascade);
4 changes: 2 additions & 2 deletions integration/tests/postgres/foreign-key-alter/expect.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
alter table issues drop constraint "issues_project_id_fkey"
alter table issues add constraint renamed_fkey foreign key (project_id) references projects (id)
alter table issues drop constraint "issues_project_id_fkey";
alter table issues add constraint renamed_fkey foreign key (project_id) references projects (id);
2 changes: 1 addition & 1 deletion integration/tests/postgres/foreign-key-create/expect.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
create table "user_project" ("user_id" integer, "project_id" integer, "misc_id" character varying (255) not null, primary key ("user_id", "project_id"), constraint user_project_user_id_fkey foreign key (user_id) references users (id), constraint user_project_project_id_fkey foreign key (project_id) references projects (id), constraint misc_named_fk foreign key (misc_id) references misc (pk))
create table "user_project" ("user_id" integer, "project_id" integer, "misc_id" character varying (255) not null, primary key ("user_id", "project_id"), constraint user_project_user_id_fkey foreign key (user_id) references users (id), constraint user_project_project_id_fkey foreign key (project_id) references projects (id), constraint misc_named_fk foreign key (misc_id) references misc (pk));
4 changes: 2 additions & 2 deletions integration/tests/postgres/foreign-key-drop/expect.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
alter table "org" alter column "project_id" set not null
alter table org drop constraint "org_project_id_fkey"
alter table "org" alter column "project_id" set not null;
alter table org drop constraint "org_project_id_fkey";
2 changes: 1 addition & 1 deletion integration/tests/postgres/index-create/expect.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
create unique index idx_users_email on users (email)
create unique index idx_users_email on users (email);
2 changes: 1 addition & 1 deletion integration/tests/postgres/not-null/expect.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
alter table "projects" alter column "icon_uri" drop not null
alter table "projects" alter column "icon_uri" drop not null;
2 changes: 1 addition & 1 deletion integration/tests/postgres/primary-key-add/expect.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
alter table user_projects add constraint user_projects_pkey primary key (user_id, project_id)
alter table user_projects add constraint user_projects_pkey primary key (user_id, project_id);
2 changes: 1 addition & 1 deletion integration/tests/postgres/primary-key-drop/expect.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
alter table user_projects drop constraint "user_projects_pkey"
alter table user_projects drop constraint "user_projects_pkey";
56 changes: 51 additions & 5 deletions pkg/cli/schemaherocli/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import (
"os"
"path/filepath"

"github.com/pkg/errors"
"github.com/schemahero/schemahero/pkg/database"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func Plan() *cobra.Command {
cmd := &cobra.Command{
Use: "plan",
Short: "plan a spec application against a database",
Long: `...`,
Use: "plan",
Short: "plan a spec application against a database",
Long: `...`,
SilenceUsage: true,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
},
Expand Down Expand Up @@ -46,28 +48,72 @@ func Plan() *cobra.Command {
return err
}

if _, err = os.Stat(v.GetString("out")); err == nil {
return errors.Errorf("file %s already exists", v.GetString("out"))
}

var f *os.File
if v.GetString("out") != "" {
f, err = os.OpenFile(v.GetString("out"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
}

db := database.NewDatabase()
if fi.Mode().IsDir() {
err := filepath.Walk(v.GetString("spec-file"), func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
if err := db.PlanSync(path); err != nil {
statements, err := db.PlanSync(path)
if err != nil {
return err
}

if f != nil {
for _, statement := range statements {
if _, err := f.WriteString(fmt.Sprintf("%s;\n", statement)); err != nil {
return err
}
}
} else {
for _, statement := range statements {
fmt.Printf("%s;\n", statement)
}
}
}

return nil
})

return err
} else {
return db.PlanSync(v.GetString("spec-file"))
statements, err := db.PlanSync(v.GetString("spec-file"))
if err != nil {
return err
}

if f != nil {
for _, statement := range statements {
if _, err := f.WriteString(fmt.Sprintf("%s;\n", statement)); err != nil {
return err
}
}
} else {
for _, statement := range statements {
fmt.Printf("%s;\n", statement)
}
}

return nil
}
},
}

cmd.Flags().String("driver", "", "name of the database driver to use")
cmd.Flags().String("uri", "", "connection string uri to use")
cmd.Flags().String("spec-file", "", "filename or directory name containing the spec(s) to apply")
cmd.Flags().String("out", "", "filename to write DDL statements to, if not present output file be written to stdout")

return cmd
}
18 changes: 8 additions & 10 deletions pkg/database/database.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package database

import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/pkg/errors"
schemasv1alpha3 "github.com/schemahero/schemahero/pkg/apis/schemas/v1alpha3"
"github.com/schemahero/schemahero/pkg/database/mysql"
"github.com/schemahero/schemahero/pkg/database/postgres"
Expand Down Expand Up @@ -65,13 +65,12 @@ func (d *Database) CreateFixturesSync() error {
}

if spec.Schema == nil {
fmt.Printf("skipping file %s because there is no schema\n", info.Name())
return nil
}

if d.Viper.GetString("driver") == "postgres" {
if spec.Schema.Postgres == nil {
fmt.Printf("skipping file %s because there is no postgres spec\n", info.Name())
return nil
}

statement, err := postgres.CreateTableStatement(spec.Name, spec.Schema.Postgres)
Expand All @@ -82,7 +81,7 @@ func (d *Database) CreateFixturesSync() error {
statements = append(statements, statement)
} else if d.Viper.GetString("driver") == "mysql" {
if spec.Schema.Mysql == nil {
fmt.Printf("skipping file %s because there is no mysql spec\n", info.Name())
return nil
}

statement, err := mysql.CreateTableStatement(spec.Name, spec.Schema.Mysql)
Expand Down Expand Up @@ -118,10 +117,10 @@ func (d *Database) CreateFixturesSync() error {
return nil
}

func (d *Database) PlanSync(filename string) error {
func (d *Database) PlanSync(filename string) ([]string, error) {
specContents, err := ioutil.ReadFile(filename)
if err != nil {
return err
return nil, errors.Wrap(err, "failed to read file")
}

var spec *schemasv1alpha3.TableSpec
Expand All @@ -135,15 +134,14 @@ func (d *Database) PlanSync(filename string) error {
if spec == nil {
plainSpec := schemasv1alpha3.TableSpec{}
if err := yaml.Unmarshal(specContents, &plainSpec); err != nil {
return err
return nil, errors.Wrap(err, "failed to unmarshal spec")
}

spec = &plainSpec
}

if spec.Schema == nil {
fmt.Printf("skipping file %s because there is no schema\n", filename)
return nil
return []string{}, nil
}

if d.Viper.GetString("driver") == "postgres" {
Expand All @@ -152,7 +150,7 @@ func (d *Database) PlanSync(filename string) error {
return mysql.PlanMysqlTable(d.Viper.GetString("uri"), spec.Name, spec.Schema.Mysql)
}

return errors.New("unknown database driver")
return nil, errors.New("unknown database driver")
}

func (d *Database) ApplySync(statements []string) error {
Expand Down

0 comments on commit 1e66c8f

Please sign in to comment.