-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration-init
executable file
·42 lines (30 loc) · 1.23 KB
/
migration-init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python2.7
import migrations, subprocess
MIGRATION_SCHEMA = {
'UP' : """
CREATE SCHEMA "migrations";
CREATE TABLE "migrations"."history" (
"migration_id" serial PRIMARY KEY,
"next_migration_id" int DEFAULT NULL,
"hash" varchar(40) NOT NULL UNIQUE,
"applied" boolean DEFAULT false,
FOREIGN KEY ( "next_migration_id" ) REFERENCES
"migrations"."history" ( "migration_id" )
);
CREATE INDEX "history_migration_id_idx" ON "migrations"."history" ( "migration_id" DESC );
CREATE INDEX "history_next_migration_id_idx" ON "migrations"."history" ( "next_migration_id" DESC NULLS FIRST );
CREATE INDEX "history_applied_idx" ON "migrations"."history" ( "applied" );
""",
'DOWN' : 'DROP SCHEMA "migrations" CASCADE;'
}
class MigrationInit(migrations.MigrationCommand):
def main ( self, uninstall, DBNAME, DBUSER, **kwargs ):
migration = MIGRATION_SCHEMA['DOWN'] if uninstall else MIGRATION_SCHEMA['UP']
subprocess.call(('psql', DBNAME, DBUSER, '-1', '-c', migration))
if __name__ == '__main__':
command = MigrationInit(
).add_argument('--uninstall', action='store_true', help='uninstall the migrations schema instead'
).with_psql_arguments(
)
command()
# vim: filetype=python