forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request gobuffalo#6 from markbates/fizz
Fizz
- Loading branch information
Showing
63 changed files
with
2,568 additions
and
618 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,4 @@ bin/* | |
gin-bin | ||
*.sqlite | ||
vendor/ | ||
tsoda |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# Fizz | ||
|
||
## A Common DSL for Migrating Databases | ||
|
||
## Create a Table | ||
|
||
``` javascript | ||
create_table("users", func(t) { | ||
t.Column("email", "string", {}) | ||
t.Column("twitter_handle", "string", {"size": 50}) | ||
t.Column("age", "integer", {"default": 0}) | ||
t.Column("admin", "boolean", {"default": false}) | ||
t.Column("bio", "text", {"null": true}) | ||
t.Column("joined_at", "timestamp", {}) | ||
}) | ||
``` | ||
|
||
The `create_table` function will also generate an `id` column of type `integer` that will auto-increment. It will also generate two `timestamp` columns; `created_at` and `updated_at`. | ||
|
||
Columns all have the same syntax. First is the name of the column. Second is the type of the field. Third is any options you want to set on that column. | ||
|
||
#### <a name="column-info"></a> "Common" Types: | ||
|
||
* `string` | ||
* `text` | ||
* `timestamp` | ||
* `integer` | ||
* `boolean` | ||
|
||
Any other type passed it will be be passed straight through to the underlying database. For example for PostgreSQL you could pass `jsonb`and it will be supported, however, SQLite will yell very loudly at you if you do the same thing! | ||
|
||
#### Supported Options: | ||
|
||
* `size` - The size of the column. For example if you wanted a `varchar(50)` in Postgres you would do: `t.Column("column_name", "string", {"size": 50})` | ||
* `null` - By default columns are not allowed to be `null`. | ||
`default` - The default value you want for this column. By default this is `null`. | ||
|
||
## Drop a Table | ||
|
||
``` javascript | ||
drop_table("table_name") | ||
``` | ||
|
||
## Rename a Table | ||
|
||
``` javascript | ||
rename_table("old_table_name", "new_table_name") | ||
``` | ||
|
||
## Add a Column | ||
|
||
``` javascript | ||
add_column("table_name", "column_name", "string", {}) | ||
``` | ||
|
||
See [above](#column-info) for more details on column types and options. | ||
|
||
## Rename a Column | ||
|
||
``` javascript | ||
rename_column("table_name", "old_column_name", "new_column_name") | ||
``` | ||
|
||
## Drop a Column | ||
|
||
``` javascript | ||
drop_column("table_name", "column_name") | ||
``` | ||
|
||
## Add an Index | ||
|
||
#### Supported Options: | ||
|
||
* `name` - This defaults to `table_name_column_name_idx` | ||
* `unique` | ||
|
||
### Simple Index: | ||
|
||
``` javascript | ||
add_index("table_name", "column_name", {}) | ||
``` | ||
|
||
### Multi-Column Index: | ||
|
||
``` javascript | ||
add_index("table_name", ["column_1", "column_2"], {}) | ||
``` | ||
|
||
### Unique Index: | ||
|
||
``` javascript | ||
add_index("table_name", "column_name", {"unique": true}) | ||
``` | ||
|
||
### Index Names: | ||
|
||
``` javascript | ||
add_index("table_name", "column_name", {}) # name => table_name_column_name_idx | ||
add_index("table_name", "column_name", {"name": "custom_index_name"}) | ||
``` | ||
|
||
## Rename an Index | ||
|
||
``` javascript | ||
rename_index("table_name", "old_index_name", "new_index_name") | ||
``` | ||
|
||
## Drop an Index | ||
|
||
``` javascript | ||
drop_index("table_name", "index_name") | ||
``` | ||
|
||
## Raw SQL | ||
|
||
``` javascript | ||
raw("select * from users;") | ||
``` | ||
|
||
*All calls to `raw` must end with a `;`!* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package fizz | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/mattn/anko/vm" | ||
|
||
core "github.com/mattn/anko/builtins" | ||
) | ||
|
||
type BubbleType int | ||
|
||
type Bubbler struct { | ||
Translator | ||
data []string | ||
} | ||
|
||
func NewBubbler(t Translator) *Bubbler { | ||
return &Bubbler{ | ||
Translator: t, | ||
data: []string{}, | ||
} | ||
} | ||
|
||
func (b *Bubbler) String() string { | ||
return strings.Join(b.data, "\n") | ||
} | ||
|
||
func (b *Bubbler) Bubble(s string) (string, error) { | ||
env := core.Import(vm.NewEnv()) | ||
|
||
f := fizzer{b} | ||
|
||
// columns: | ||
env.Define("add_column", f.AddColumn()) | ||
env.Define("drop_column", f.DropColumn()) | ||
env.Define("rename_column", f.RenameColumn()) | ||
|
||
env.Define("raw", f.RawSql()) | ||
|
||
// indexes: | ||
env.Define("add_index", f.AddIndex()) | ||
env.Define("drop_index", f.DropIndex()) | ||
env.Define("rename_index", f.RenameIndex()) | ||
|
||
// tables: | ||
env.Define("create_table", f.CreateTable()) | ||
env.Define("drop_table", f.DropTable()) | ||
env.Define("rename_table", f.RenameTable()) | ||
|
||
_, err := env.Execute(s) | ||
if err != nil { | ||
fmt.Printf("ParseError: err -> %#v\n", err) | ||
} | ||
return b.String(), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package fizz_test |
Oops, something went wrong.