Skip to content

Commit 03346ed

Browse files
committed
Documentation.
1 parent 1a100b3 commit 03346ed

1 file changed

Lines changed: 46 additions & 9 deletions

File tree

README.md

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@
6565
DMorph
6666
======
6767

68-
*DMorph* is a database migration library. Programs that use a database and have to preserve the data
69-
between versions can utilize *DMorph* to apply the necessary migration steps. If a program can
70-
afford to lose all data between version upgrades, this library is not necessary.
68+
*DMorph* (pronounced [diˈmɔʁf]) is a database migration library. Programs that use a database and
69+
have to preserve the data between versions can utilize *DMorph* to apply the necessary migration
70+
steps. If a program can afford to lose all data between version upgrades, this library is not
71+
necessary.
7172

7273
Includes direct support for the following relational database management systems:
7374

@@ -94,8 +95,8 @@ $ go get github.com/AlphaOne1/dmorph
9495
Getting Started
9596
---------------
9697

97-
*DMorph* applies migrations to a database. A migration is a series of steps, defined either in an
98-
SQL file or programmatically.
98+
*DMorph* applies migrations to a database.
99+
A migration is a series of steps, defined either in an SQL file or programmatically.
99100

100101

101102
### Migration from File
@@ -142,6 +143,8 @@ func migrate(db *sql.DB) error {
142143

143144
In this example just one file is used, the `WithMigrationFromFile` can be given multiple times.
144145
Migrations are executed in alphabetical order of their key. For files the key is the file's name.
146+
The `WithDialect` option is used to select the correct SQL dialect, as *DMorph* does not have
147+
a means to get that information (yet).
145148

146149

147150
### Migrations from Folder
@@ -200,11 +203,11 @@ migration fulfilling this interface could look like this:
200203
```go
201204
type CustomMigration struct {}
202205

203-
func (m *CustomMigration) Key() string {
206+
func (m CustomMigration) Key() string {
204207
return "0001_custom"
205208
}
206209

207-
func (m *CustomMigration) Migrate(tx *sql.Tx) error {
210+
func (m CustomMigration) Migrate(tx *sql.Tx) error {
208211
_, err := tx.Exec(`CREATE TABLE tab0(id INTEGER PRIMARY KEY)`)
209212
return err
210213
}
@@ -220,6 +223,40 @@ This newly created migration can then be passed to *DMorph* as follows:
220223
func migrate(db *sql.DB) error {
221224
return dmorph.Run(db,
222225
dmorph.WithDialect(dmorph.DialectSQLite()),
223-
dmorph.WithMigration(CustomMigration{}))
226+
dmorph.WithMigrations(CustomMigration{}))
224227
}
225-
```
228+
```
229+
230+
### New Database SQL Dialect
231+
232+
*DMorph* uses the Dialect interface to adapt to different database management systems:
233+
234+
```go
235+
type Dialect interface {
236+
EnsureMigrationTableExists(db *sql.DB, tableName string) error
237+
AppliedMigrations(db *sql.DB, tableName string) ([]string, error)
238+
RegisterMigration(tx *sql.Tx, id string, tableName string) error
239+
}
240+
```
241+
242+
It contains a convenience wrapper, `BaseDialect`, that fits most database systems. It implements the
243+
above functions using a set of user supplied SQL statements:
244+
245+
```go
246+
type BaseDialect struct {
247+
CreateTemplate string
248+
AppliedTemplate string
249+
RegisterTemplate string
250+
}
251+
```
252+
253+
All the included SQL dialects use the `BaseDialect` to implement their functionality. The tests for
254+
*DMorph* are done using the [SQLite dialect](dialect_sqlite.go).
255+
256+
As the migration table name can be user supplied, the statements need to have placeholders that will
257+
fill the final table name. As there might be special characters, it is always enclosed in the
258+
identifier enclosing characters of the database.
259+
260+
*DMorph* uses the `ValidTableNameRex` regular expression, to check if a table name is principally
261+
valid. The regular expression may be adapted, but it is strongly advised to only do so in pressing
262+
circumstances.

0 commit comments

Comments
 (0)