[gom] gom: Add support for creating new tables in a version



commit 5ad762eaaba654d690ed53e6038aacce7358619d
Author: Bastien Nocera <hadess hadess net>
Date:   Tue Aug 25 00:06:02 2015 +0200

    gom: Add support for creating new tables in a version
    
    To support creating new tables in a particular version of the database,
    we special case the primary key's "new in version" tag to create the
    table in that version.
    
    For example, if a table with 2 columns, one primary key, and one
    attribute, needs to be created in version 4 of the database, you'll need
    to tag both the primary key property as well as the attribute one as
    being new in version 4 of the database.
    
    Think of this as an easter egg, as you'd really be expected to create
    all the necessary tables in version 1, or to manually migrate your
    database otherwise.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=753563

 gom/gom-command-builder.c |   15 +++++++++++++--
 1 files changed, 13 insertions(+), 2 deletions(-)
---
diff --git a/gom/gom-command-builder.c b/gom/gom-command-builder.c
index a80deda..a87f5de 100644
--- a/gom/gom-command-builder.c
+++ b/gom/gom-command-builder.c
@@ -125,6 +125,17 @@ is_new_in_version (GParamSpec *pspec,
    return (GPOINTER_TO_INT(g_param_spec_get_qdata(pspec, GOM_RESOURCE_NEW_IN_VERSION)) + 1) == version;
 }
 
+static gboolean
+table_is_new_in_version (GomResourceClass *klass,
+                         guint             version)
+{
+   GParamSpec *primary_pspec;
+
+   primary_pspec = g_object_class_find_property(G_OBJECT_CLASS(klass),
+                                                klass->primary_key);
+   return is_new_in_version(primary_pspec, version);
+}
+
 static void
 add_fields (GString          *str,
             GomResourceClass *klass)
@@ -448,8 +459,8 @@ gom_command_builder_build_create (GomCommandBuilder *builder,
 
    pspecs = g_object_class_list_properties(G_OBJECT_CLASS(klass), &n_pspecs);
 
-   /* Create the table if it doesn't already exist*/
-   if (version == 1) {
+   /* Create the table if it doesn't already exist */
+   if (table_is_new_in_version(klass, version)) {
       str = g_string_new("CREATE TABLE IF NOT EXISTS ");
       add_table_name(str, klass);
       g_string_append(str, "(");


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]