浏览代码

cmake: Fix dbschema generation to match older makefile structure

Xenofon Karamanos 3 月之前
父节点
当前提交
e0dd39618f

+ 6 - 0
CMakeLists.txt

@@ -74,6 +74,12 @@ add_subdirectory(src)
 add_subdirectory(utils/kamctl)
 add_subdirectory(utils/kamcmd)
 
+# ----------
+# Db schema files
+# Include it here due to calling a script where we need the base binary dir
+# and scirpts are using {CMAKE_BINARY_DIR} from where they where first included.
+include(${CMAKE_SOURCE_DIR}/cmake/dbschema.cmake)
+
 # TODO: Packaging stuuf. These should be on different file propably
 set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
 set(CPACK_PACKAGE_NAME kamailio)

+ 49 - 0
cmake/dbschema-version-postprocess.sh

@@ -0,0 +1,49 @@
+#!/usr/bin/env bash
+
+# This script is executed by CMake after db_berkeley and db_text schema files are generated.
+# It appends the last line of each generated file (except 'version')
+# to the 'version' file and truncates each generated file to the n line.
+
+# Get the number of lines to move to version file from the first argument
+# Get the nubmer of lines to keep in the file from the second argument
+
+TAIL_NUMBER="$1"
+HEAD_NUMBER="$2"
+
+# echo " Tail number: $TAIL_NUMBER"
+# echo " Head number: $HEAD_NUMBER"
+
+# Loop through files, sorted alphabetically
+for FILE in $(ls * | sort); do
+    # Check if it's a regular file and not the version file
+    if [ -f "$FILE" ] && [ "$FILE" != "version" ]; then
+        # echo "  Processing: $FILE"
+
+        # Check if file has at least 1 line before tail
+        if [ -s "$FILE" ]; then # -s checks if file is not empty
+            # Append the last line to the version file
+            # Using "printf" to ensure a newline is added after the tail output
+            tail -n "$TAIL_NUMBER" "$FILE" >> version
+            if [ ${PIPESTATUS[0]} -ne 0 ]; then # Check tail command result
+                echo "Warning: tail command failed for $FILE"
+            fi
+
+            # Get the first line and overwrite the original file
+            head -n "$HEAD_NUMBER" "$FILE" > "$$FILE".tmp
+            if [ $? -ne 0 ]; then
+                 echo "Warning: head command failed for $FILE"
+            else
+                mv "$$FILE".tmp "$FILE"
+                if [ $? -ne 0 ]; then
+                     echo "Warning: mv command failed for $FILE"
+                fi
+            fi
+        else
+            echo "Warning: File $FILE is empty, skipping processing."
+        fi
+    fi
+done
+
+echo "Finished processing schema files in $PWD"
+
+exit 0 # Indicate success

+ 93 - 45
cmake/dbschema.cmake

@@ -26,6 +26,7 @@ function(add_db_target db_name xsl_file)
   if(NOT (db_name_folder IN_LIST added_modules))
     return()
   endif()
+
   find_group_name(${db_name_folder})
 
   add_custom_target(
@@ -35,35 +36,50 @@ function(add_db_target db_name xsl_file)
     COMMENT "Creating schemas for ${db_name}"
   )
 
+  # db_name for old makefiles are different.
+  #     old       ->     new (module names)
+  # db_berkeley   -> db_berkeley
+  # mongodb       -> db_mongodb
+  # mysql         -> db_mysql
+  # db_oracle     -> db_oracle
+  # postgres      -> db_postgres
+  # db_redis      -> db_redis
+  # db_sqlite     -> db_sqlite
+  # dbtext        -> db_text
+  # pi_framework  -> xhttp_pi (this was not provided at all in the old makefiles)
+  # For consistency, we are now using the new names.
+  # For compatibility with tools, we are still using the old names for install folder
+
+  # Determine the prefix/suffix
+  if(db_name STREQUAL "db_berkeley"
+     OR db_name STREQUAL "db_redis"
+     OR db_name STREQUAL "db_text"
+     OR db_name STREQUAL "db_mongodb"
+  )
+    set(prefix '')
+    set(folder_suffix "${MAIN_NAME}")
+  else()
+    set(prefix "${table}-")
+    set(folder_suffix '')
+  endif()
+
+  # install folder based on db_name
+  if(db_name STREQUAL "db_mongodb")
+    set(install_folder "mongodb")
+  elseif(db_name STREQUAL "db_mysql")
+    set(install_folder "mysql")
+  elseif(db_name STREQUAL "db_postgres")
+    set(install_folder "mysql")
+  elseif(db_name STREQUAL "db_text")
+    set(install_folder "dbtext")
+  else()
+    set(install_folder "${db_name}")
+  endif()
+
   # Loop through each table and add a command for xsltproc
   foreach(table ${EXTRACTED_TABLES})
-    # Determine the prefix based on db_name
-    if(db_name STREQUAL "db_berkeley"
-       OR db_name STREQUAL "db_redis"
-       OR db_name STREQUAL "db_text"
-       OR db_name STREQUAL "db_mongodb"
-    )
-      set(prefix '')
-      set(folder_suffix "${MAIN_NAME}")
-    else()
-      set(prefix "${table}-")
-      set(folder_suffix '')
-    endif()
-
-    # db_name for old makefiles are different.
-    # old -> new (module names)
-    # db_berkeley   -> db_berkeley
-    # mongodb       -> db_mongodb
-    # mysql         -> db_mysql
-    # db_oracle     -> db_oracle
-    # postgres      -> db_postgres
-    # db_redis      -> db_redis
-    # db_sqlite     -> db_sqlite
-    # dbtext        -> db_text
-    # pi_framework  -> xhttp_pi
-    # TODO: Not sure if these are used somewhere else.
-    # For consistency, we are now using the new names.
-    # We should probably change the old names if something breaks.
+
+    # Stringparam db is the db_* module name
     add_custom_command(
       TARGET dbschema_${db_name}
       PRE_BUILD
@@ -76,6 +92,31 @@ function(add_db_target db_name xsl_file)
     )
   endforeach()
 
+  # Create version table for db_text and db_berkeley
+  # Use bash script
+  set(POSTPROCESS_SCRIPT "${CMAKE_SOURCE_DIR}/cmake/dbschema-version-postprocess.sh")
+
+  if(db_name STREQUAL "db_text")
+    add_custom_command(
+      TARGET dbschema_${db_name}
+      POST_BUILD
+      COMMAND ${POSTPROCESS_SCRIPT} 1 1
+              # ${CMAKE_BINARY_DIR}/utils/kamctl/${db_name_folder}/${folder_suffix}
+      COMMENT "Creating version table for ${db_name}"
+      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/utils/kamctl/${db_name_folder}/${folder_suffix}
+    )
+  elseif(db_name STREQUAL "db_berkeley")
+    add_custom_command(
+      TARGET dbschema_${db_name}
+      POST_BUILD
+      COMMAND ${POSTPROCESS_SCRIPT} 2 10
+              # ${CMAKE_BINARY_DIR}/utils/kamctl/${db_name_folder}/${folder_suffix}
+      COMMENT "Creating version table for ${db_name}"
+      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/utils/kamctl/${db_name_folder}/${folder_suffix}
+    )
+
+  endif()
+
   add_custom_target(
     dbschema_${db_name}_clean
     COMMAND ${CMAKE_COMMAND} -E remove_directory
@@ -88,9 +129,12 @@ function(add_db_target db_name xsl_file)
 
   # message(WARNING "group name is ${group_name}")
   # Before installing, ensure the target is built `dbschema_${db_name}`
+  # install as previously done in makefile folder. see naming above
+  # TODO: when tools adopt to new folder structure, remove the install_folder variable
+
   install(
-    DIRECTORY ${CMAKE_BINARY_DIR}/utils/kamctl/${db_name_folder}
-    DESTINATION ${CMAKE_INSTALL_DATADIR}/${MAIN_NAME}
+    DIRECTORY ${CMAKE_BINARY_DIR}/utils/kamctl/${db_name_folder}/
+    DESTINATION ${CMAKE_INSTALL_DATADIR}/${MAIN_NAME}/${install_folder}
     OPTIONAL
     COMPONENT ${group_name}
   )
@@ -132,6 +176,7 @@ else()
     string(REPLACE "kamailio-" "" TABLE_NAME "${TABLE_NAME}")
     string(REPLACE ".xml" "" TABLE_NAME "${TABLE_NAME}")
     list(APPEND EXTRACTED_TABLES "${TABLE_NAME}")
+    list(SORT EXTRACTED_TABLES)
   endforeach()
   # Output the extracted table names
   if(VERBOSE)
@@ -143,7 +188,7 @@ endif()
 add_db_target(db_berkeley "${STYLESHEETS}/db_berkeley.xsl")
 
 #---- DB mongo
-add_db_target(db_mongodb "${STYLESHEETS}/mongodb.xsl")
+add_db_target(db_mongodb "${STYLESHEETS}/db_mongodb.xsl")
 # Create the version-create.mongo script
 # After processing the JSON files, create the version-create.mongo script
 # Usage of generate_version_create_mongo.sh:
@@ -155,29 +200,29 @@ if(TARGET dbschema_db_mongodb)
     POST_BUILD
     COMMAND
       bash generate_version_create_mongo.sh
-      "${CMAKE_BINARY_DIR}/utils/kamctl/mongodb/kamailio/version-create.mongo"
-      "${CMAKE_BINARY_DIR}/utils/kamctl/mongodb/kamailio"
+      "${CMAKE_BINARY_DIR}/utils/kamctl/db_mongodb/kamailio/version-create.mongo"
+      "${CMAKE_BINARY_DIR}/utils/kamctl/db_mongodb/kamailio"
     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/utils/kamctl
     COMMENT "Creating version-create.mongo from JSON files"
   )
-endif()
 
-find_group_name("db_mongodb")
-install(
-  FILES ${CMAKE_BINARY_DIR}/utils/kamctl/mongodb/kamailio/version-create.mongo
-  DESTINATION ${CMAKE_INSTALL_DATADIR}/${MAIN_NAME}/mongodb/${MAIN_NAME}
-  OPTIONAL
-  COMPONENT ${group_name}
-)
+  find_group_name("db_mongodb")
+  install(
+    FILES ${CMAKE_BINARY_DIR}/utils/kamctl/db_mongodb/kamailio/version-create.mongo
+    DESTINATION ${CMAKE_INSTALL_DATADIR}/${MAIN_NAME}/mongodb/${MAIN_NAME}
+    OPTIONAL
+    COMPONENT ${group_name}
+  )
+endif()
 
 #---- DB mysql
-add_db_target(db_mysql "${STYLESHEETS}/mysql.xsl")
+add_db_target(db_mysql "${STYLESHEETS}/db_mysql.xsl")
 
 #---- DB Oracle
-add_db_target(db_oracle "${STYLESHEETS}/oracle.xsl")
+add_db_target(db_oracle "${STYLESHEETS}/db_oracle.xsl")
 
 #---- DB postgres
-add_db_target(db_postgres "${STYLESHEETS}/postgres.xsl")
+add_db_target(db_postgres "${STYLESHEETS}/db_postgres.xsl")
 
 #---- DB redis
 add_db_target(db_redis "${STYLESHEETS}/db_redis.xsl")
@@ -186,14 +231,17 @@ add_db_target(db_redis "${STYLESHEETS}/db_redis.xsl")
 add_db_target(db_sqlite "${STYLESHEETS}/db_sqlite.xsl")
 
 #---- DB text
-add_db_target(db_text "${STYLESHEETS}/dbtext.xsl")
+add_db_target(db_text "${STYLESHEETS}/db_text.xsl")
 
 #---- DB xhttp_pi
 add_db_target(pi_framework_table "${STYLESHEETS}/pi_framework_table.xsl")
 add_db_target(pi_framework_mod "${STYLESHEETS}/pi_framework_mod.xsl")
 
 # Add alias targets that match the dbschema
-if(XSLTPROC_EXECUTABLE)
+if(XSLTPROC_EXECUTABLE
+   AND TARGET dbschema_pi_framework_table
+   AND TARGET dbschema_pi_framework_mod
+)
   add_custom_target(dbschema_xhttp_pi)
   add_dependencies(dbschema_xhttp_pi dbschema_pi_framework_table dbschema_pi_framework_mod)
 

+ 1 - 1
doc/stylesheets/dbschema_k/xsl/common.xsl

@@ -43,7 +43,7 @@
 
     <xsl:template name="quotechar">
 	<xsl:choose>
-	    <xsl:when test="$db='mysql'">
+	    <xsl:when test="$db='db_mysql'">
 			<xsl:text>`</xsl:text>
 	    </xsl:when>
 	    <xsl:otherwise>

+ 0 - 0
doc/stylesheets/dbschema_k/xsl/mongodb.xsl → doc/stylesheets/dbschema_k/xsl/db_mongodb.xsl


+ 0 - 0
doc/stylesheets/dbschema_k/xsl/mysql.xsl → doc/stylesheets/dbschema_k/xsl/db_mysql.xsl


+ 0 - 0
doc/stylesheets/dbschema_k/xsl/oracle.xsl → doc/stylesheets/dbschema_k/xsl/db_oracle.xsl


+ 0 - 0
doc/stylesheets/dbschema_k/xsl/postgres.xsl → doc/stylesheets/dbschema_k/xsl/db_postgres.xsl


+ 0 - 0
doc/stylesheets/dbschema_k/xsl/dbtext.xsl → doc/stylesheets/dbschema_k/xsl/db_text.xsl


+ 1 - 1
doc/stylesheets/dbschema_k/xsl/pi_framework_table.xsl

@@ -65,7 +65,7 @@
 			<xsl:call-template name="get-type"/>
 		</xsl:variable>
 		<xsl:choose>
-			<xsl:when test="type[@db='mysql']">
+			<xsl:when test="type[@db='db_mysql']">
 			<xsl:value-of select="normalize-space(type[@db='mysql'])"/>
 			</xsl:when>
 			<xsl:when test="$type='char'">

+ 0 - 4
src/CMakeLists.txt

@@ -215,10 +215,6 @@ install(
   OPTIONAL
 )
 
-# ----------
-# Db schema files
-include(${CMAKE_SOURCE_DIR}/cmake/dbschema.cmake)
-
 # ----------
 # Install the configuration file (kamailio.cfg) ${CFG_NAME} using a CODE block
 # to check  existence at install time instead of configure time

+ 5 - 4
src/lib/srdb1/schema/version.xml

@@ -6,12 +6,13 @@
 
 ]>
 
-<table id="version" xmlns:db="http://docbook.org/ns/docbook">>
+<table id="version"
+    xmlns:db="http://docbook.org/ns/docbook">>
     <name>version</name>
     <version>1</version>
-    <type db="mysql">&MYSQL_TABLE_TYPE;</type>
+    <type db="db_mysql">&MYSQL_TABLE_TYPE;</type>
     <description>
-		<db:para>
+        <db:para>
 			This table is used by Kamailio modules to check if the structure
 			of database tables has the expected number.
         </db:para>
@@ -23,7 +24,7 @@
         <size>&table_id_len;</size>
         <autoincrement/>
         <primary/>
-        <type db="dbtext">int,auto</type>
+        <type db="db_text">int,auto</type>
         <description>Unique ID</description>
     </column>