Skip to content

Commit

Permalink
Merge pull request #12 from schemahero/generate
Browse files Browse the repository at this point in the history
Generate
  • Loading branch information
marccampbell committed May 20, 2019
2 parents 5596879 + 0580ad8 commit 15e94b5
Show file tree
Hide file tree
Showing 2,190 changed files with 290,768 additions and 9 deletions.
89 changes: 89 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Expand Up @@ -43,3 +43,7 @@ version="v1.4.7"
name = "github.com/stretchr/testify"
version = "1.3.0"


[[constraint]]
branch = "master"
name = "github.com/xo/dburl"
2 changes: 2 additions & 0 deletions config/crds/databases_v1alpha1_database.yaml
Expand Up @@ -25,6 +25,8 @@ spec:
properties:
postgres:
properties:
allowOrphanedTablesOnDelete:
type: boolean
disableDriftDetection:
type: boolean
uri:
Expand Down
30 changes: 30 additions & 0 deletions docs/migrating/README.md
@@ -0,0 +1,30 @@
# Migrating To SchemaHero

Like most of us, you probably already have a database (or several) running in production. You've invested in your current tooling and have a many database migrations. The process of converting these into YAML that can be managed with SchemaHero isn't difficult, but it's time consuming and tedious. SchemaHero has functionality that can help, if you can access a running instance of your database.

Note: We recommend that you run the import tool against a local, dev instance when possible.

## Schema Import

To start, get the `schemahero` binary (or use the Docker container) and provide a connection string to your database:

```
$ schemahero generate \
--driver postgres \
--uri postgres://user:pass@host:5432/dbname \
--dbname destired-schemahero-databasename \
--output-dir ./imported
```

or

```
$ docker run schemahero/schemahero -v `pwd`/imported:/out \
generate \
--driver postgres \
--uri postgres://user:pass@host:5432/dbname \
--dbname destired-schemahero-databasename \
--output-dir ./imported
```

This will create .yaml files (1 per table) that you can deploy to a cluster to recreate the schema
3 changes: 2 additions & 1 deletion pkg/apis/databases/v1alpha1/database_types.go
Expand Up @@ -47,7 +47,8 @@ type Database struct {

SchemaHero *SchemaHero `json:"schemahero,omitempty"`
Connection DatabaseConnection `json:"connection,omitempty"`
Status DatabaseStatus `json:"status,omitempty"`

Status DatabaseStatus `json:"status,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
5 changes: 3 additions & 2 deletions pkg/apis/databases/v1alpha1/postgres_types.go
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha1

type PostgresConnection struct {
URI ValueOrValueFrom `json:"uri,omitempty"`
DisableDriftDetection bool `json:"disableDriftDetection,omitempty"`
URI ValueOrValueFrom `json:"uri,omitempty"`
DisableDriftDetection bool `json:"disableDriftDetection,omitempty"`
AllowOrphanedTablesOnDelete bool `json:"allowOrphanedTablesOnDelete,omitempty"`
}
4 changes: 2 additions & 2 deletions pkg/apis/schemas/v1alpha1/table_types.go
Expand Up @@ -21,7 +21,7 @@ import (
)

type PostgresTableColumnConstraints struct {
NotNull bool `json:"notNull"`
NotNull bool `json:"notNull" yaml:"notNull"`
}

type PostgresTableColumn struct {
Expand All @@ -32,7 +32,7 @@ type PostgresTableColumn struct {
}

type PostgresTableSchema struct {
PrimaryKey []string `json:"primaryKey"`
PrimaryKey []string `json:"primaryKey" yaml:"primaryKey"`
Columns []*PostgresTableColumn `json:"columns"`
}

Expand Down
34 changes: 34 additions & 0 deletions pkg/cli/generate.go
@@ -0,0 +1,34 @@
package cli

import (
"github.com/schemahero/schemahero/pkg/generate"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func Generate() *cobra.Command {
cmd := &cobra.Command{
Use: "generate",
Short: "generate schemahero custom resources from a running database instance",
Long: `...`,
RunE: func(cmd *cobra.Command, args []string) error {
g := generate.NewGenerator()
return g.RunSync()
},
}

cmd.Flags().StringP("driver", "d", "", "name of the database driver to use")
cmd.Flags().StringP("uri", "u", "", "connection string uri")
cmd.Flags().StringP("namespace", "n", "default", "namespace to put the custom resources into")
cmd.Flags().StringP("dbname", "", "", "schemahero database name to write in the yaml")
cmd.Flags().StringP("output-dir", "o", "", "directory to write schema files to")

cmd.MarkFlagRequired("driver")
cmd.MarkFlagRequired("uri")
cmd.MarkFlagRequired("dbname")

viper.BindPFlags(cmd.Flags())

return cmd
}
1 change: 1 addition & 0 deletions pkg/cli/root.go
Expand Up @@ -23,6 +23,7 @@ func RootCmd() *cobra.Command {
cobra.OnInitialize(initConfig)

cmd.AddCommand(Watch())
cmd.AddCommand(Generate())

viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
return cmd
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/watch.go
Expand Up @@ -18,7 +18,7 @@ func Watch() *cobra.Command {
},
}

cmd.Flags().StringP("driver", "d", "", "name of the database to connect to")
cmd.Flags().StringP("driver", "d", "", "name of the database driver to use")
cmd.Flags().StringP("uri", "u", "", "connection string uri")
cmd.Flags().StringP("namespace", "n", "", "namespace of the spwaning object")
cmd.Flags().StringP("instance", "i", "", "instance name of the spawning object")
Expand Down
18 changes: 18 additions & 0 deletions pkg/database/postgres/column.go
Expand Up @@ -223,6 +223,24 @@ func unaliasParameterizedColumnType(requestedType string) string {
return ""
}

func PostgresColumnToSchemaColumn(column *Column) (*schemasv1alpha1.PostgresTableColumn, error) {
constraints := &schemasv1alpha1.PostgresTableColumnConstraints{
NotNull: column.Constraints.NotNull,
}

schemaColumn := &schemasv1alpha1.PostgresTableColumn{
Name: column.Name,
Type: column.DataType,
Constraints: constraints,
}

if column.ColumnDefault != nil {
schemaColumn.Default = *column.ColumnDefault
}

return schemaColumn, nil
}

func schemaColumnToPostgresColumn(schemaColumn *schemasv1alpha1.PostgresTableColumn) (*Column, error) {
column := &Column{}

Expand Down
15 changes: 12 additions & 3 deletions pkg/database/postgres/postgres.go
Expand Up @@ -2,14 +2,17 @@ package postgres

import (
"database/sql"
"strings"

// import the postgres driver
_ "github.com/lib/pq"
"github.com/xo/dburl"
)

type Postgres struct {
conn *sql.Conn
db *sql.DB
conn *sql.Conn
db *sql.DB
databaseName string
}

func Connect(uri string) (*Postgres, error) {
Expand All @@ -22,8 +25,14 @@ func Connect(uri string) (*Postgres, error) {
return nil, err
}

parsed, err := dburl.Parse(uri)
if err != nil {
return nil, err
}

postgres := Postgres{
db: db,
db: db,
databaseName: strings.TrimLeft(parsed.Path, "/"),
}

return &postgres, nil
Expand Down

0 comments on commit 15e94b5

Please sign in to comment.