diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2018-10-12 14:31:10 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2018-10-12 15:43:26 +0300 |
commit | b9055ec9d4057822f2955066bdec59c71418feef (patch) | |
tree | a696da9a3a4a198ec5d723537c98a416c9f43768 | |
parent | e97c6b9b9c90a6f97019021d6bfcc73ed92580cc (diff) |
Recreate database schema extras on migration
-rw-r--r-- | INSTALL | 2 | ||||
-rw-r--r-- | INSTALL-DEV | 2 | ||||
-rw-r--r-- | libbrep/.gitignore | 4 | ||||
-rw-r--r-- | libbrep/build-extra.sql | 3 | ||||
-rw-r--r-- | libbrep/package-extra.sql | 7 | ||||
-rw-r--r-- | libbrep/package.hxx | 2 | ||||
-rw-r--r-- | libbrep/package.xml | 2 | ||||
-rw-r--r-- | migrate/migrate.cxx | 59 |
8 files changed, 57 insertions, 24 deletions
@@ -208,7 +208,7 @@ $ install/bin/brep-migrate build To verify: -$ psql -d brep_package -c 'SELECT name, summary FROM repository' +$ psql -d brep_package -c 'SELECT canonical_name, summary FROM repository' $ psql -d brep_build -c 'SELECT package_name FROM build' # Empty row set. $ psql -d brep_build -c 'SELECT DISTINCT name FROM build_package' diff --git a/INSTALL-DEV b/INSTALL-DEV index b051cb2..e6c46cd 100644 --- a/INSTALL-DEV +++ b/INSTALL-DEV @@ -127,7 +127,7 @@ $ migrate/brep-migrate build To verify: -$ psql -d brep_package -c 'SELECT name, summary FROM repository' +$ psql -d brep_package -c 'SELECT canonical_name, summary FROM repository' $ psql -d brep_build -c 'SELECT package_name FROM build' # Empty row set. $ psql -d brep_build -c 'SELECT DISTINCT name FROM build_package' diff --git a/libbrep/.gitignore b/libbrep/.gitignore index 1fbfc28..1cf5d3e 100644 --- a/libbrep/.gitignore +++ b/libbrep/.gitignore @@ -2,9 +2,13 @@ common-odb.?xx package-odb.?xx package.sql +package-???-pre.sql +package-???-post.sql package-extra.hxx build-odb.?xx build-package-odb.?xx build.sql +build-???-pre.sql +build-???-post.sql build-extra.hxx diff --git a/libbrep/build-extra.sql b/libbrep/build-extra.sql index cc43239..35ba361 100644 --- a/libbrep/build-extra.sql +++ b/libbrep/build-extra.sql @@ -2,6 +2,9 @@ -- parser complexity, there is a number of restrictions, see package-extra.sql -- file for details. -- +-- Increment the database 'build' schema version when update this file, see +-- package-extra.sql file for details. +-- DROP FOREIGN TABLE IF EXISTS build_package_constraints; diff --git a/libbrep/package-extra.sql b/libbrep/package-extra.sql index 93c61d8..86f055b 100644 --- a/libbrep/package-extra.sql +++ b/libbrep/package-extra.sql @@ -8,6 +8,13 @@ -- * strings other then function bodies must be quoted with ' or " -- * statements must end with ";\n" -- +-- Increment the database 'package' schema version when update this file. Note +-- that the brep-migrate utility executes the drop SQL statements prior to the +-- schema migration and the create statements afterwards. That, in particular, +-- means that the extras may depend on the tables but not the other way +-- around. Make sure that the drop statements properly handle entities created +-- for all schema versions starting from LIBBREP_PACKAGE_SCHEMA_VERSION_BASE. +-- -- There is no need to drop to_tsvector() explicitly, as we can rely on "DROP -- TYPE IF EXISTS weighted_text CASCADE" statement below, which will drop all diff --git a/libbrep/package.hxx b/libbrep/package.hxx index fbe6f60..af4581c 100644 --- a/libbrep/package.hxx +++ b/libbrep/package.hxx @@ -21,7 +21,7 @@ // #define LIBBREP_PACKAGE_SCHEMA_VERSION_BASE 7 -#pragma db model version(LIBBREP_PACKAGE_SCHEMA_VERSION_BASE, 7, closed) +#pragma db model version(LIBBREP_PACKAGE_SCHEMA_VERSION_BASE, 8, open) namespace brep { diff --git a/libbrep/package.xml b/libbrep/package.xml index 7b74349..07c1be7 100644 --- a/libbrep/package.xml +++ b/libbrep/package.xml @@ -1,4 +1,6 @@ <changelog xmlns="http://www.codesynthesis.com/xmlns/odb/changelog" database="pgsql" schema-name="package" version="1"> + <changeset version="8"/> + <model version="7"> <table name="repository" kind="object"> <column name="tenant" type="TEXT" null="false"/> diff --git a/migrate/migrate.cxx b/migrate/migrate.cxx index af0890f..d84e282 100644 --- a/migrate/migrate.cxx +++ b/migrate/migrate.cxx @@ -40,10 +40,10 @@ public: schema (const char* extra, string name); void - create (database&) const; + create (database&, bool extra_only = false) const; void - drop (database&) const; + drop (database&, bool extra_only = false) const; private: string name_; @@ -177,7 +177,7 @@ schema (const char* s, string name) } void schema:: -drop (database& db) const +drop (database& db, bool extra_only) const { for (const auto& s: drop_statements_) // If the statement execution fails, the corresponding source file line @@ -187,15 +187,17 @@ drop (database& db) const // db.execute (s); - schema_catalog::drop_schema (db, name_); + if (!extra_only) + schema_catalog::drop_schema (db, name_); } void schema:: -create (database& db) const +create (database& db, bool extra_only) const { - drop (db); + drop (db, extra_only); - schema_catalog::create_schema (db, name_); + if (!extra_only) + schema_catalog::create_schema (db, name_); for (const auto& s: create_statements_) db.execute (s); @@ -289,6 +291,9 @@ try // schema_version schema_version (db.schema_version (db_schema)); + odb::schema_version schema_current_version ( + schema_catalog::current_version (db, db_schema)); + // It is impossible to operate with the database which is out of the // [base_version, current_version] range due to the lack of the knowlege // required not just for migration, but for the database wiping as well. @@ -301,7 +306,7 @@ try throw failed (); } - if (schema_version > schema_catalog::current_version (db, db_schema)) + if (schema_version > schema_current_version) { cerr << "error: database schema is too new" << endl; throw failed (); @@ -317,8 +322,9 @@ try // Let the user decide if they want to migrate or just drop the entire // database (followed with the database creation for the --recreate option). // - if ((create || drop) && schema_version != 0 && - schema_version != schema_catalog::current_version (db, db_schema)) + if ((create || drop) && + schema_version != 0 && + schema_version != schema_current_version) { cerr << "error: database schema requires migration" << endl << " info: either migrate the database first or drop the entire " @@ -326,34 +332,45 @@ try throw failed (); } - transaction t (db.begin ()); - - if (create || drop) - { - static const char package_extras[] = { + static const char package_extras[] = { #include <libbrep/package-extra.hxx> - , '\0'}; + , '\0'}; - static const char build_extras[] = { + static const char build_extras[] = { #include <libbrep/build-extra.hxx> - , '\0'}; + , '\0'}; + + schema s (db_schema == "package" ? package_extras : build_extras, + db_schema); - schema s (db_schema == "package" ? package_extras : build_extras, - db_schema); + transaction t (db.begin ()); + if (create || drop) + { if (create) s.create (db); else if (drop) s.drop (db); } - else + else if (schema_version != schema_current_version) { + // Drop the extras, migrate the database tables and data, and create the + // extras afterwards. + // + // Note that here we assume that the latest extras drop SQL statements can + // handle entities created by the create statements of the earlier schemas + // (see libbrep/package-extra.sql for details). + // + s.drop (db, true /* extra_only */); + // Register the data migration functions. // // static const data_migration_entry<2, LIBBREP_XXX_SCHEMA_VERSION_BASE> // migrate_v2_entry (&migrate_v2); // schema_catalog::migrate (db, 0, db_schema); + + s.create (db, true /* extra_only */); } t.commit (); |