def migrate(store=avatar_store, config=config, dryRun=False): schema = makeSchema(store, dryRun) if schema is None: print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" print "Migrations not supported for", getConnectionClassName(store) return schema.ensureSchemaTable() # Make sure the real schema is what schemup_tables says it is print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" print "Checking schema integrity..." mismatches = validator.findSchemaMismatches(schema) # NTA TODO: Pretty print if mismatches: print "Real schema & 'schemup_tables' are out of sync (did you change the schema outside of schemup?):" for mismatch in mismatches: print mismatch, "\n" raise Exception("Schema mismatches") loadMigrations(store, config) print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" print "Upgrading..." sqls = commands.upgrade(schema, stormSchema) # Sanity checking if not dryRun: commands.validate(schema, stormSchema) # This is needed because several schemup operations touch the DB # through queries, but only "ensureSchemaTable" and "upgrade" end # the transaction (when they need to persist data (committing)). # TODO: The correct fix would be putting transaction start/end in # single functions (either this or schemup's (both of which # requires changing schemup)). Preferrably we want to separate # actualy querying and transaction start/end management store.rollback() if not sqls: print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" print "Schema up-to-date" elif dryRun: print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" for sql in sqls: print "" print sql else: print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" print "Migrated successfully" print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
def validate(dbSchema, ormSchema): """ Check DB versions against ORM versions, returning mismatches. If there are version mismatches, check DB schemas against cache, returning mismatches there. """ mismatches = validator.findMismatches(dbSchema, ormSchema) if mismatches: raise errors.ORMValidationError( map(errors.ORMValidationMismatch, mismatches)) schemaMismatches = list(validator.findSchemaMismatches(dbSchema)) if schemaMismatches: raise errors.SchemaValidationError( map(errors.SchemaValidationMismatch, schemaMismatches))
def run(action='nothing'): dryRun = action != 'commit' dbConfig = config.POSTGRES dbConfigParams = { "database": dbConfig["database"], "host": dbConfig["host"], "user": dbConfig["username"], "password": dbConfig["password"], "port": dbConfig["port"] } pgConn = psycopg2.connect(**dbConfigParams) pgSchema = postgres.PostgresSchema(pgConn, dryRun=dryRun) dictSchema = DictSchema("schema/versions.json") pgSchema.ensureSchemaTable() # Ensure current DB's integrity schemaMismatches = findSchemaMismatches(pgSchema) if schemaMismatches: print "Real schema & 'schemup_tables' are out of sync" for mismatch in schemaMismatches: print mismatch, "\n" sys.exit(1) commands.load('schema/migrations') sqls = commands.upgrade(pgSchema, dictSchema) if dryRun and sqls: print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" for sql in sqls: print sql print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" sys.exit(1) commands.validate(pgSchema, dictSchema)
def getExpectedTableVersions(self): return sorted(self.versions.iteritems()) dbConfig = json.load(open("../config/db.json", "r")) pgConn = psycopg2.connect(**dbConfig) pgSchema = postgres.PostgresSchema(pgConn, dryRun=dryRun) dictSchema = DictSchema("versions.json") pgSchema.ensureSchemaTable() # Ensure current DB's integrity schemaMismatches = findSchemaMismatches(pgSchema) if schemaMismatches: print "Real schema & 'schemup_tables' are out of sync" for mismatch in schemaMismatches: print mismatch, "\n" sys.exit(1) commands.load('migrations') sqls = commands.upgrade(pgSchema, dictSchema) if dryRun and sqls: print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" for sql in sqls: print sql print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" sys.exit(1)
"host": dbConfig["host"], "user": dbConfig["username"], "password": dbConfig["password"], "port": dbConfig["port"] } pgConn = psycopg2.connect(**dbConfigParams) pgSchema = postgres.PostgresSchema(pgConn, dryRun=dryRun) dictSchema = DictSchema("versions.json") pgSchema.ensureSchemaTable() # Ensure current DB's integrity schemaMismatches = findSchemaMismatches(pgSchema) if schemaMismatches: print "Real schema & 'schemup_tables' are out of sync" for mismatch in schemaMismatches: print mismatch, "\n" sys.exit(1) commands.load('migrations') sqls = commands.upgrade(pgSchema, dictSchema) if dryRun and sqls: print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" for sql in sqls: print sql print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" sys.exit(1)