def test_schema_version_from_lines(): with pytest.raises(McSchemaVersionFromLinesException): schema_version_from_lines('no version') # noinspection SqlDialectInspection,SqlNoDataSourceInspection assert schema_version_from_lines(""" CREATE OR REPLACE FUNCTION set_database_schema_version() RETURNS boolean AS $$ DECLARE -- Database schema version number (same as a SVN revision number) -- Increase it by 1 if you make major database schema changes. MEDIACLOUD_DATABASE_SCHEMA_VERSION CONSTANT INT := 4588; BEGIN -- Update / set database schema version DELETE FROM database_variables WHERE name = 'database-schema-version'; INSERT INTO database_variables (name, value) VALUES ('database-schema-version', MEDIACLOUD_DATABASE_SCHEMA_VERSION::int); return true; END; $$ LANGUAGE 'plpgsql'; """) == 4588
def schema_is_up_to_date(self) -> bool: """Checks if the database schema is up-to-date""" # Check if the database is empty db_vars_table_exists = len(self.query(""" select 1 from pg_tables where tablename = 'database_variables' and schemaname = 'public' """).flat()) > 0 if not db_vars_table_exists: log.info( "Database table 'database_variables' does not exist, probably the database is empty at this point.") return True # Current schema version (current_schema_version,) = self.query(""" SELECT value AS schema_version FROM database_variables WHERE name = 'database-schema-version' LIMIT 1 """).flat() current_schema_version = int(current_schema_version) if current_schema_version == 0: raise McSchemaIsUpToDateException("Current schema version is 0") # Target schema version sql = open(mc_sql_schema_path(), 'r').read() target_schema_version = schema_version_from_lines(sql) if not target_schema_version: raise McSchemaIsUpToDateException("Invalid target schema version.") # Check if the current schema is up-to-date if current_schema_version != target_schema_version: return DatabaseHandler.__should_continue_with_outdated_schema(current_schema_version, target_schema_version) else: # Things are fine at this point. return True
def schema_is_up_to_date(self) -> bool: """Checks if the database schema is up-to-date""" # Check if the database is empty db_vars_table_exists = len(self.query(""" select 1 from pg_tables where tablename = 'database_variables' and schemaname = 'public' """).flat()) > 0 if not db_vars_table_exists: log.info( "Database table 'database_variables' does not exist, probably the database is empty at this point.") return True # Current schema version (current_schema_version,) = self.query(""" SELECT value AS schema_version FROM database_variables WHERE name = 'database-schema-version' LIMIT 1 """).flat() current_schema_version = int(current_schema_version) if current_schema_version == 0: raise McSchemaIsUpToDateException("Current schema version is 0") # Target schema version sql = open(mc_sql_schema_path(), 'r').read() target_schema_version = schema_version_from_lines(sql) if not target_schema_version: raise McSchemaIsUpToDateException("Invalid target schema version.") # Check if the current schema is up-to-date if current_schema_version != target_schema_version: return DatabaseHandler.__should_continue_with_outdated_schema(current_schema_version, target_schema_version) else: # Things are fine at this point. return True
def schema_is_up_to_date(self) -> bool: """Checks if the database schema is up-to-date""" root_dir = mc_root_path() # Check if the database is empty db_vars_table_exists = len( self.query(""" -- noinspection SqlResolve SELECT * FROM information_schema.tables WHERE table_name = 'database_variables' """).flat()) > 0 if not db_vars_table_exists: l.info( "Database table 'database_variables' does not exist, probably the database is empty at this point." ) return True # Current schema version (current_schema_version, ) = self.query(""" SELECT value AS schema_version FROM database_variables WHERE name = 'database-schema-version' LIMIT 1 """).flat() current_schema_version = int(current_schema_version) if current_schema_version == 0: raise McSchemaIsUpToDateException("Current schema version is 0") # Target schema version sql = open(os.path.join(root_dir, 'schema', 'mediawords.sql'), 'r').read() target_schema_version = schema_version_from_lines(sql) if not target_schema_version: raise McSchemaIsUpToDateException("Invalid target schema version.") # Check if the current schema is up-to-date if current_schema_version != target_schema_version: return self.__should_continue_with_outdated_schema( current_schema_version, target_schema_version) else: # Things are fine at this point. return True
def schema_is_up_to_date(self) -> bool: """Checks if the database schema is up-to-date""" root_dir = mc_root_path() # Check if the database is empty db_vars_table_exists = len(self.query(""" -- noinspection SqlResolve SELECT * FROM information_schema.tables WHERE table_name = 'database_variables' """).flat()) > 0 if not db_vars_table_exists: l.info("Database table 'database_variables' does not exist, probably the database is empty at this point.") return True # Current schema version (current_schema_version,) = self.query(""" SELECT value AS schema_version FROM database_variables WHERE name = 'database-schema-version' LIMIT 1 """).flat() current_schema_version = int(current_schema_version) if current_schema_version == 0: raise Exception("Current schema version is 0") # Target schema version sql = open(os.path.join(root_dir, 'schema', 'mediawords.sql'), 'r').read() target_schema_version = schema_version_from_lines(sql) if not target_schema_version: raise Exception("Invalid target schema version.") # Check if the current schema is up-to-date if current_schema_version != target_schema_version: return self.__should_continue_with_outdated_schema(current_schema_version, target_schema_version) else: # Things are fine at this point. return True