SchemaMigration.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /***************************************************************************
  2. *
  3. * Project _____ __ ____ _ _
  4. * ( _ ) /__\ (_ _)_| |_ _| |_
  5. * )(_)( /(__)\ )( (_ _)(_ _)
  6. * (_____)(__)(__)(__) |_| |_|
  7. *
  8. *
  9. * Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ***************************************************************************/
  24. #ifndef oatpp_orm_SchemaMigration_hpp
  25. #define oatpp_orm_SchemaMigration_hpp
  26. #include "Executor.hpp"
  27. namespace oatpp { namespace orm {
  28. /**
  29. * Database schema migration helper.
  30. */
  31. class SchemaMigration {
  32. private:
  33. static constexpr v_int32 SOURCE_TEXT = 0;
  34. static constexpr v_int32 SOURCE_FILE = 1;
  35. struct Source {
  36. v_int64 version;
  37. v_int32 type;
  38. oatpp::String param;
  39. };
  40. private:
  41. base::ObjectHandle<Executor> m_executor;
  42. oatpp::String m_suffix;
  43. std::vector<Source> m_scripts;
  44. public:
  45. /**
  46. * Constructor.
  47. * @param executor - &id:oatpp::orm::Executor;.
  48. * @param suffix - suffix of schema version control table.
  49. */
  50. SchemaMigration(const base::ObjectHandle<Executor>& executor, const oatpp::String& suffix = nullptr);
  51. /**
  52. * Default virtual destructor.
  53. */
  54. virtual ~SchemaMigration() = default;
  55. /**
  56. * Add migration script as text.
  57. * @param version - schema version corresponding to this script.
  58. * @param script - script text.
  59. */
  60. void addText(v_int64 version, const oatpp::String& script);
  61. /**
  62. * Add migration script file.
  63. * @param version - schema version corresponding to this script.
  64. * @param script - path to script file.
  65. */
  66. void addFile(v_int64 version, const oatpp::String& filename);
  67. /**
  68. * Run database schema migration.
  69. */
  70. void migrate();
  71. /**
  72. * Get current database schema version.
  73. * @return - schema version.
  74. */
  75. v_int64 getSchemaVersion();
  76. };
  77. }}
  78. #endif // oatpp_orm_SchemaMigration_hpp