Просмотр исходного кода

added logs in migrate.py

tags/9.6-1.1.0
Robin Thoni 7 лет назад
Родитель
Сommit
8af5bc8033
2 измененных файлов: 18 добавлений и 5 удалений
  1. 2
    0
      Dockerfile
  2. 16
    5
      migrate.py

+ 2
- 0
Dockerfile Просмотреть файл

@@ -29,4 +29,6 @@ COPY ./migrate.py /usr/local/bin/
29 29
 
30 30
 RUN rm -rf /var/log/*
31 31
 
32
+RUN touch /var/log/migrate.py.logs && chmod 777 /var/log/migrate.py.logs
33
+
32 34
 VOLUME "/var/lib/postgresql/backup"

+ 16
- 5
migrate.py Просмотреть файл

@@ -7,6 +7,7 @@ import os.path
7 7
 import re
8 8
 import distutils.version
9 9
 import subprocess
10
+import datetime
10 11
 
11 12
 
12 13
 def file_runner_pgsql(folder, file):
@@ -60,7 +61,7 @@ def run_migration(folder, files, version, no_run, file_runners):
60 61
         if file.startswith('%s_' % (version)):
61 62
             run_file(folder, file, no_run, file_runners)
62 63
 
63
-def run_migrations(folder, version_from, version_to, no_run, file_runners):
64
+def run_migrations(folder, version_from, version_to, no_run, file_runners, log_file):
64 65
     files = list_files(folder)
65 66
     versions = list_versions(files)
66 67
 
@@ -82,9 +83,18 @@ def run_migrations(folder, version_from, version_to, no_run, file_runners):
82 83
         print('Not running because --no-run was specified')
83 84
 
84 85
     if version_from is None:
85
-        print('Initialising from %s (inclusive) to %s (inclusive)' % (versions[0], version_to))
86
+        msg = 'Initialising from %s (inclusive) to %s (inclusive)' % (versions[0], version_to)
86 87
     else:
87
-        print('Migrating from %s (exclusive) to %s (inclusive)' % (version_from, version_to))
88
+        msg = 'Migrating from %s (exclusive) to %s (inclusive)' % (version_from, version_to)
89
+
90
+    print(msg)
91
+
92
+    if not no_run:
93
+        try:
94
+            with open(log_file, "a") as f:
95
+                f.write('%s: %s in %s\n' % (datetime.datetime.now(), msg, folder))
96
+        except Exception as e:
97
+            print('Failed to write logs in %: %s' % (log_file, e))
88 98
 
89 99
     version_from_obj = distutils.version.StrictVersion('0.0.0' if version_from is None else version_from)
90 100
     version_to_obj = distutils.version.StrictVersion(version_to)
@@ -110,6 +120,7 @@ def main():
110 120
     parser.add_argument('--no-run', dest='no_run', default=False, action='store_true', help='Do not run migration, just print')
111 121
     parser.add_argument('--version-from', dest='version_from', default=None, help='Current version, required if using --migrate, can not be used --init')
112 122
     parser.add_argument('--version-to', dest='version_to', default=None, help='Final version, default to last available version is not given')
123
+    parser.add_argument('--log-file', dest='log_file', default='/var/lib/postgresql/data/migrate.py.log', help='File to log migrations')
113 124
 
114 125
     args = parser.parse_args()
115 126
 
@@ -126,12 +137,12 @@ def main():
126 137
         if args.version_from is not None:
127 138
             print('--version-from can not be used with --init')
128 139
             return 64
129
-        return run_migrations(args.folder, None, args.version_to, args.no_run, file_runners)
140
+        return run_migrations(args.folder, None, args.version_to, args.no_run, file_runners, args.log_file)
130 141
     elif args.is_migrate:
131 142
         if args.version_from is None:
132 143
             print('--version-from is required. Use --init to run all migrations')
133 144
             return 64
134
-        return run_migrations(args.folder, args.version_from, args.version_to, args.no_run, file_runners)
145
+        return run_migrations(args.folder, args.version_from, args.version_to, args.no_run, file_runners, args.log_file)
135 146
     else:
136 147
         print('Missing --init or --migrate')
137 148
         return 64

Загрузка…
Отмена
Сохранить