CouchDB Document Model Changes?












8














Rails uses the concept of migrations to deal with model changes using the ActiveRecord API.



CouchDB uses JSON (nested maps and arrays) to represent its model objects.



In working with CouchDB so far, I don't see good ways of recognizing when the document's structure has changed (other than being disciplined as a developer), or for migrating documents from an old to a new model.



Are there existing features or do you have best practices for handling model changes in CouchDB?










share|improve this question



























    8














    Rails uses the concept of migrations to deal with model changes using the ActiveRecord API.



    CouchDB uses JSON (nested maps and arrays) to represent its model objects.



    In working with CouchDB so far, I don't see good ways of recognizing when the document's structure has changed (other than being disciplined as a developer), or for migrating documents from an old to a new model.



    Are there existing features or do you have best practices for handling model changes in CouchDB?










    share|improve this question

























      8












      8








      8


      5





      Rails uses the concept of migrations to deal with model changes using the ActiveRecord API.



      CouchDB uses JSON (nested maps and arrays) to represent its model objects.



      In working with CouchDB so far, I don't see good ways of recognizing when the document's structure has changed (other than being disciplined as a developer), or for migrating documents from an old to a new model.



      Are there existing features or do you have best practices for handling model changes in CouchDB?










      share|improve this question













      Rails uses the concept of migrations to deal with model changes using the ActiveRecord API.



      CouchDB uses JSON (nested maps and arrays) to represent its model objects.



      In working with CouchDB so far, I don't see good ways of recognizing when the document's structure has changed (other than being disciplined as a developer), or for migrating documents from an old to a new model.



      Are there existing features or do you have best practices for handling model changes in CouchDB?







      ruby-on-rails ruby couchdb






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 24 '08 at 21:28









      Kyle Burton

      19.9k94360




      19.9k94360
























          3 Answers
          3






          active

          oldest

          votes


















          9














          Time for RDBMS de-brainwashing. :)



          One of the biggest points of couchdb's schema-less design is directly aimed at preventing the need for migrations. The JSON representation of objects makes it easy to just duck type your objects.



          For example, given that you have a blog type web app with posts and whatever fancy things people store in a blog. Your post documents have fields like author, title, created at, etc. Now you come along and think to yourself, "I should track what phase the moon is in when I publish my posts..." you can just start adding moon_phase as an attribute to new posts.



          If you want to be complete you'd go back and add moon_phase to old posts, but that's not strictly necessary.



          In your views, you can access moon_phase as an attribute. And it'll be null or cause an exception or something. (Not a JS expert, I think null is the right answer)



          Thing is, it doesn't really matter. If you feel like changing something just change it. Though make sure your views understand that change. Which in my experience doesn't really require much.



          Also, if you're really paranoid, you might store a version/type attribute, as in:



          {
          _id: "foo-post",
          _rev: "23490AD",
          type: "post",
          typevers: 0,
          moon_phase: "full"
          }


          Hope that helps.






          share|improve this answer































            3














            If you're into having schemas and still want to use CouchDB you get an "impedance mismatch".



            Nevertheless, having "migrations" is not that hard. Add a schema_version element to each document. Then have your "document reading function" include updating. Something like this:



            def read(doc_id):
            doc = db.get(doc_id)
            if doc.schema_version == 1:
            # version 1 had names broken down too much
            doc.name = "%s %s" % (doc.first, doc.last)
            del doc.first
            del doc.last
            doc.schema_version = 2
            db.put(doc)
            if doc.schema_version == 2: weight
            # version 2 used kg instead of g
            doc.weight_g = doc.weight_kg * 1000
            del doc.volume_kg
            doc.schema_version = 3
            db.put(doc)
            return doc


            If you want to upgrade the whole DB at once just call read(doc_id) for every document.






            share|improve this answer































              3














              Check out ActiveCouch.



              CouchDB is schema-less on purpose, so there is not a 1-to-1 mapping of concepts from the ActiveRecord migrations to a CouchDB equivalent. However, ActiveCouch does include migrations for CouchDB's 'views'.






              share|improve this answer



















              • 1




                Looks like activecouch is now on GitHub - github.com/arunthampi/activecouch/tree/master
                – Evan
                Feb 20 '09 at 8:19











              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f130092%2fcouchdb-document-model-changes%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              9














              Time for RDBMS de-brainwashing. :)



              One of the biggest points of couchdb's schema-less design is directly aimed at preventing the need for migrations. The JSON representation of objects makes it easy to just duck type your objects.



              For example, given that you have a blog type web app with posts and whatever fancy things people store in a blog. Your post documents have fields like author, title, created at, etc. Now you come along and think to yourself, "I should track what phase the moon is in when I publish my posts..." you can just start adding moon_phase as an attribute to new posts.



              If you want to be complete you'd go back and add moon_phase to old posts, but that's not strictly necessary.



              In your views, you can access moon_phase as an attribute. And it'll be null or cause an exception or something. (Not a JS expert, I think null is the right answer)



              Thing is, it doesn't really matter. If you feel like changing something just change it. Though make sure your views understand that change. Which in my experience doesn't really require much.



              Also, if you're really paranoid, you might store a version/type attribute, as in:



              {
              _id: "foo-post",
              _rev: "23490AD",
              type: "post",
              typevers: 0,
              moon_phase: "full"
              }


              Hope that helps.






              share|improve this answer




























                9














                Time for RDBMS de-brainwashing. :)



                One of the biggest points of couchdb's schema-less design is directly aimed at preventing the need for migrations. The JSON representation of objects makes it easy to just duck type your objects.



                For example, given that you have a blog type web app with posts and whatever fancy things people store in a blog. Your post documents have fields like author, title, created at, etc. Now you come along and think to yourself, "I should track what phase the moon is in when I publish my posts..." you can just start adding moon_phase as an attribute to new posts.



                If you want to be complete you'd go back and add moon_phase to old posts, but that's not strictly necessary.



                In your views, you can access moon_phase as an attribute. And it'll be null or cause an exception or something. (Not a JS expert, I think null is the right answer)



                Thing is, it doesn't really matter. If you feel like changing something just change it. Though make sure your views understand that change. Which in my experience doesn't really require much.



                Also, if you're really paranoid, you might store a version/type attribute, as in:



                {
                _id: "foo-post",
                _rev: "23490AD",
                type: "post",
                typevers: 0,
                moon_phase: "full"
                }


                Hope that helps.






                share|improve this answer


























                  9












                  9








                  9






                  Time for RDBMS de-brainwashing. :)



                  One of the biggest points of couchdb's schema-less design is directly aimed at preventing the need for migrations. The JSON representation of objects makes it easy to just duck type your objects.



                  For example, given that you have a blog type web app with posts and whatever fancy things people store in a blog. Your post documents have fields like author, title, created at, etc. Now you come along and think to yourself, "I should track what phase the moon is in when I publish my posts..." you can just start adding moon_phase as an attribute to new posts.



                  If you want to be complete you'd go back and add moon_phase to old posts, but that's not strictly necessary.



                  In your views, you can access moon_phase as an attribute. And it'll be null or cause an exception or something. (Not a JS expert, I think null is the right answer)



                  Thing is, it doesn't really matter. If you feel like changing something just change it. Though make sure your views understand that change. Which in my experience doesn't really require much.



                  Also, if you're really paranoid, you might store a version/type attribute, as in:



                  {
                  _id: "foo-post",
                  _rev: "23490AD",
                  type: "post",
                  typevers: 0,
                  moon_phase: "full"
                  }


                  Hope that helps.






                  share|improve this answer














                  Time for RDBMS de-brainwashing. :)



                  One of the biggest points of couchdb's schema-less design is directly aimed at preventing the need for migrations. The JSON representation of objects makes it easy to just duck type your objects.



                  For example, given that you have a blog type web app with posts and whatever fancy things people store in a blog. Your post documents have fields like author, title, created at, etc. Now you come along and think to yourself, "I should track what phase the moon is in when I publish my posts..." you can just start adding moon_phase as an attribute to new posts.



                  If you want to be complete you'd go back and add moon_phase to old posts, but that's not strictly necessary.



                  In your views, you can access moon_phase as an attribute. And it'll be null or cause an exception or something. (Not a JS expert, I think null is the right answer)



                  Thing is, it doesn't really matter. If you feel like changing something just change it. Though make sure your views understand that change. Which in my experience doesn't really require much.



                  Also, if you're really paranoid, you might store a version/type attribute, as in:



                  {
                  _id: "foo-post",
                  _rev: "23490AD",
                  type: "post",
                  typevers: 0,
                  moon_phase: "full"
                  }


                  Hope that helps.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 26 '08 at 18:24

























                  answered Sep 26 '08 at 18:19









                  Paul J. Davis

                  1,479129




                  1,479129

























                      3














                      If you're into having schemas and still want to use CouchDB you get an "impedance mismatch".



                      Nevertheless, having "migrations" is not that hard. Add a schema_version element to each document. Then have your "document reading function" include updating. Something like this:



                      def read(doc_id):
                      doc = db.get(doc_id)
                      if doc.schema_version == 1:
                      # version 1 had names broken down too much
                      doc.name = "%s %s" % (doc.first, doc.last)
                      del doc.first
                      del doc.last
                      doc.schema_version = 2
                      db.put(doc)
                      if doc.schema_version == 2: weight
                      # version 2 used kg instead of g
                      doc.weight_g = doc.weight_kg * 1000
                      del doc.volume_kg
                      doc.schema_version = 3
                      db.put(doc)
                      return doc


                      If you want to upgrade the whole DB at once just call read(doc_id) for every document.






                      share|improve this answer




























                        3














                        If you're into having schemas and still want to use CouchDB you get an "impedance mismatch".



                        Nevertheless, having "migrations" is not that hard. Add a schema_version element to each document. Then have your "document reading function" include updating. Something like this:



                        def read(doc_id):
                        doc = db.get(doc_id)
                        if doc.schema_version == 1:
                        # version 1 had names broken down too much
                        doc.name = "%s %s" % (doc.first, doc.last)
                        del doc.first
                        del doc.last
                        doc.schema_version = 2
                        db.put(doc)
                        if doc.schema_version == 2: weight
                        # version 2 used kg instead of g
                        doc.weight_g = doc.weight_kg * 1000
                        del doc.volume_kg
                        doc.schema_version = 3
                        db.put(doc)
                        return doc


                        If you want to upgrade the whole DB at once just call read(doc_id) for every document.






                        share|improve this answer


























                          3












                          3








                          3






                          If you're into having schemas and still want to use CouchDB you get an "impedance mismatch".



                          Nevertheless, having "migrations" is not that hard. Add a schema_version element to each document. Then have your "document reading function" include updating. Something like this:



                          def read(doc_id):
                          doc = db.get(doc_id)
                          if doc.schema_version == 1:
                          # version 1 had names broken down too much
                          doc.name = "%s %s" % (doc.first, doc.last)
                          del doc.first
                          del doc.last
                          doc.schema_version = 2
                          db.put(doc)
                          if doc.schema_version == 2: weight
                          # version 2 used kg instead of g
                          doc.weight_g = doc.weight_kg * 1000
                          del doc.volume_kg
                          doc.schema_version = 3
                          db.put(doc)
                          return doc


                          If you want to upgrade the whole DB at once just call read(doc_id) for every document.






                          share|improve this answer














                          If you're into having schemas and still want to use CouchDB you get an "impedance mismatch".



                          Nevertheless, having "migrations" is not that hard. Add a schema_version element to each document. Then have your "document reading function" include updating. Something like this:



                          def read(doc_id):
                          doc = db.get(doc_id)
                          if doc.schema_version == 1:
                          # version 1 had names broken down too much
                          doc.name = "%s %s" % (doc.first, doc.last)
                          del doc.first
                          del doc.last
                          doc.schema_version = 2
                          db.put(doc)
                          if doc.schema_version == 2: weight
                          # version 2 used kg instead of g
                          doc.weight_g = doc.weight_kg * 1000
                          del doc.volume_kg
                          doc.schema_version = 3
                          db.put(doc)
                          return doc


                          If you want to upgrade the whole DB at once just call read(doc_id) for every document.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Feb 11 '09 at 15:21









                          James A. Rosen

                          38k53166250




                          38k53166250










                          answered Jan 4 '09 at 11:07









                          max

                          15.8k84667




                          15.8k84667























                              3














                              Check out ActiveCouch.



                              CouchDB is schema-less on purpose, so there is not a 1-to-1 mapping of concepts from the ActiveRecord migrations to a CouchDB equivalent. However, ActiveCouch does include migrations for CouchDB's 'views'.






                              share|improve this answer



















                              • 1




                                Looks like activecouch is now on GitHub - github.com/arunthampi/activecouch/tree/master
                                – Evan
                                Feb 20 '09 at 8:19
















                              3














                              Check out ActiveCouch.



                              CouchDB is schema-less on purpose, so there is not a 1-to-1 mapping of concepts from the ActiveRecord migrations to a CouchDB equivalent. However, ActiveCouch does include migrations for CouchDB's 'views'.






                              share|improve this answer



















                              • 1




                                Looks like activecouch is now on GitHub - github.com/arunthampi/activecouch/tree/master
                                – Evan
                                Feb 20 '09 at 8:19














                              3












                              3








                              3






                              Check out ActiveCouch.



                              CouchDB is schema-less on purpose, so there is not a 1-to-1 mapping of concepts from the ActiveRecord migrations to a CouchDB equivalent. However, ActiveCouch does include migrations for CouchDB's 'views'.






                              share|improve this answer














                              Check out ActiveCouch.



                              CouchDB is schema-less on purpose, so there is not a 1-to-1 mapping of concepts from the ActiveRecord migrations to a CouchDB equivalent. However, ActiveCouch does include migrations for CouchDB's 'views'.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 12 '18 at 12:31









                              Voles

                              3,98064075




                              3,98064075










                              answered Sep 24 '08 at 23:32









                              Ian Terrell

                              8,355103964




                              8,355103964








                              • 1




                                Looks like activecouch is now on GitHub - github.com/arunthampi/activecouch/tree/master
                                – Evan
                                Feb 20 '09 at 8:19














                              • 1




                                Looks like activecouch is now on GitHub - github.com/arunthampi/activecouch/tree/master
                                – Evan
                                Feb 20 '09 at 8:19








                              1




                              1




                              Looks like activecouch is now on GitHub - github.com/arunthampi/activecouch/tree/master
                              – Evan
                              Feb 20 '09 at 8:19




                              Looks like activecouch is now on GitHub - github.com/arunthampi/activecouch/tree/master
                              – Evan
                              Feb 20 '09 at 8:19


















                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f130092%2fcouchdb-document-model-changes%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              Tangent Lines Diagram Along Smooth Curve

                              Yusuf al-Mu'taman ibn Hud

                              Zucchini