mirror of
https://github.com/TECHNOFAB11/dbmate.git
synced 2025-12-13 00:20:04 +01:00
Add dump command (#23)
Adds `dbmate dump` command to write the database schema to a file. The intent is for this file to be checked in to the codebase, similar to Rails' `schema.rb` (or `structure.sql`) file. This allows developers to share a single file documenting the database schema, and makes it considerably easier to review PRs which add (or change) migrations. The existing `up`, `migrate`, and `rollback` commands will automatically trigger a schema dump, unless `--no-dump-schema` is passed. Closes https://github.com/amacneil/dbmate/issues/5
This commit is contained in:
parent
54a9fbc859
commit
d855ee1ada
15 changed files with 578 additions and 34 deletions
|
|
@ -61,6 +61,40 @@ func TestSQLiteCreateDropDatabase(t *testing.T) {
|
|||
require.Equal(t, true, os.IsNotExist(err))
|
||||
}
|
||||
|
||||
func TestSQLiteDumpSchema(t *testing.T) {
|
||||
drv := SQLiteDriver{}
|
||||
u := sqliteTestURL(t)
|
||||
|
||||
// prepare database
|
||||
db := prepTestSQLiteDB(t)
|
||||
defer mustClose(db)
|
||||
err := drv.CreateMigrationsTable(db)
|
||||
require.Nil(t, err)
|
||||
|
||||
// insert migration
|
||||
err = drv.InsertMigration(db, "abc1")
|
||||
require.Nil(t, err)
|
||||
err = drv.InsertMigration(db, "abc2")
|
||||
require.Nil(t, err)
|
||||
|
||||
// DumpSchema should return schema
|
||||
schema, err := drv.DumpSchema(u, db)
|
||||
require.Nil(t, err)
|
||||
require.Contains(t, string(schema), "CREATE TABLE schema_migrations")
|
||||
require.Contains(t, string(schema), ");\n-- Dbmate schema migrations\n"+
|
||||
"INSERT INTO schema_migrations (version) VALUES\n"+
|
||||
" ('abc1'),\n"+
|
||||
" ('abc2');\n")
|
||||
|
||||
// DumpSchema should return error if command fails
|
||||
u.Path = "/."
|
||||
schema, err = drv.DumpSchema(u, db)
|
||||
require.Nil(t, schema)
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, "Error: unable to open database \".\": unable to open database file",
|
||||
err.Error())
|
||||
}
|
||||
|
||||
func TestSQLiteDatabaseExists(t *testing.T) {
|
||||
drv := SQLiteDriver{}
|
||||
u := sqliteTestURL(t)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue