Browse Source

added logs in migrate.py

tags/9.6-1.1.0
Robin Thoni 7 years ago
parent
commit
8af5bc8033
2 changed files with 18 additions and 5 deletions
  1. 2
    0
      Dockerfile
  2. 16
    5
      migrate.py

+ 2
- 0
Dockerfile View File

29
 
29
 
30
 RUN rm -rf /var/log/*
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
 VOLUME "/var/lib/postgresql/backup"
34
 VOLUME "/var/lib/postgresql/backup"

+ 16
- 5
migrate.py View File

7
 import re
7
 import re
8
 import distutils.version
8
 import distutils.version
9
 import subprocess
9
 import subprocess
10
+import datetime
10
 
11
 
11
 
12
 
12
 def file_runner_pgsql(folder, file):
13
 def file_runner_pgsql(folder, file):
60
         if file.startswith('%s_' % (version)):
61
         if file.startswith('%s_' % (version)):
61
             run_file(folder, file, no_run, file_runners)
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
     files = list_files(folder)
65
     files = list_files(folder)
65
     versions = list_versions(files)
66
     versions = list_versions(files)
66
 
67
 
82
         print('Not running because --no-run was specified')
83
         print('Not running because --no-run was specified')
83
 
84
 
84
     if version_from is None:
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
     else:
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
     version_from_obj = distutils.version.StrictVersion('0.0.0' if version_from is None else version_from)
99
     version_from_obj = distutils.version.StrictVersion('0.0.0' if version_from is None else version_from)
90
     version_to_obj = distutils.version.StrictVersion(version_to)
100
     version_to_obj = distutils.version.StrictVersion(version_to)
110
     parser.add_argument('--no-run', dest='no_run', default=False, action='store_true', help='Do not run migration, just print')
120
     parser.add_argument('--no-run', dest='no_run', default=False, action='store_true', help='Do not run migration, just print')
111
     parser.add_argument('--version-from', dest='version_from', default=None, help='Current version, required if using --migrate, can not be used --init')
121
     parser.add_argument('--version-from', dest='version_from', default=None, help='Current version, required if using --migrate, can not be used --init')
112
     parser.add_argument('--version-to', dest='version_to', default=None, help='Final version, default to last available version is not given')
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
     args = parser.parse_args()
125
     args = parser.parse_args()
115
 
126
 
126
         if args.version_from is not None:
137
         if args.version_from is not None:
127
             print('--version-from can not be used with --init')
138
             print('--version-from can not be used with --init')
128
             return 64
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
     elif args.is_migrate:
141
     elif args.is_migrate:
131
         if args.version_from is None:
142
         if args.version_from is None:
132
             print('--version-from is required. Use --init to run all migrations')
143
             print('--version-from is required. Use --init to run all migrations')
133
             return 64
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
     else:
146
     else:
136
         print('Missing --init or --migrate')
147
         print('Missing --init or --migrate')
137
         return 64
148
         return 64

Loading…
Cancel
Save