Update README and add support for configuration file
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
export $PSQL_OPTS=
|
||||||
|
export $DB_NAME=
|
||||||
@@ -2,12 +2,53 @@
|
|||||||
|
|
||||||
[Startup Engineering - Roll your own Database Migrations](http://ian-shafer.github.io/2016/02/02/db-migrations/)
|
[Startup Engineering - Roll your own Database Migrations](http://ian-shafer.github.io/2016/02/02/db-migrations/)
|
||||||
|
|
||||||
### Required table
|
## Setup
|
||||||
|
|
||||||
|
#### Configuration
|
||||||
|
All configuration needed is done in .configuration.sh.
|
||||||
|
Note that a
|
||||||
|
[.pgpass](http://www.postgresql.org/docs/9.3/static/libpq-pgpass.html) file will
|
||||||
|
do most of the times as well and therefore configuration is not mandatory.
|
||||||
|
|
||||||
|
#### Migration files
|
||||||
|
The files in the /migrations directory are changes to the scheme, not the full
|
||||||
|
scheme.
|
||||||
|
|
||||||
|
The naming convention for migration files are:
|
||||||
|
- 000.000
|
||||||
|
- 000.001
|
||||||
|
- 000.002
|
||||||
|
- 000.003
|
||||||
|
- 000.004
|
||||||
|
- ...and so on
|
||||||
|
|
||||||
|
**Example**
|
||||||
|
The first file, `migration/000.000` may look like this:
|
||||||
|
```
|
||||||
|
create table users (
|
||||||
|
name varchar(32) not null,
|
||||||
|
primary key (name)
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
The second file, `migration/000.001` may look like this:
|
||||||
|
```
|
||||||
|
alter table users
|
||||||
|
add email varchar(256) not null;
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Prerequisites
|
||||||
|
One table needs to be created for the database to make this work:
|
||||||
|
|
||||||
```
|
```
|
||||||
create table database_versions (
|
CREATE TABLE database_versions (
|
||||||
version varchar(32) not null,
|
version varchar(32) not null,
|
||||||
is_active boolean not null default false,
|
is_active boolean not null default false,
|
||||||
creation_date timestamp not null default current_timestamp
|
creation_date timestamp not null default current_timestamp
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
To do the actual migration, then just run:
|
||||||
|
```
|
||||||
|
# ./db-upgrade
|
||||||
|
```
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -e
|
set -e
|
||||||
# source $(dirname $0)/../.include
|
source .configuration.sh
|
||||||
|
|
||||||
function add-version() {
|
function add-version() {
|
||||||
psql $PSQL_OPTS -c "update database_versions set is_active = false where is_active = true" $DB_NAME
|
psql $PSQL_OPTS -c "update database_versions set is_active = false where is_active = true" $DB_NAME
|
||||||
|
|||||||
Reference in New Issue
Block a user