<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sharon Rozinsky on Medium]]></title>
        <description><![CDATA[Stories by Sharon Rozinsky on Medium]]></description>
        <link>https://medium.com/@sharon_roz?source=rss-7136f6a74f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xcLIOXgd_0SYozLz4WxCiA.jpeg</url>
            <title>Stories by Sharon Rozinsky on Medium</title>
            <link>https://medium.com/@sharon_roz?source=rss-7136f6a74f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 08:08:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sharon_roz/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Git Interactive Rebase — The Gem You Have To Know About]]></title>
            <link>https://medium.com/@sharon_roz/git-interactive-rebase-the-gem-you-have-to-know-about-16550d3be786?source=rss-7136f6a74f------2</link>
            <guid isPermaLink="false">https://medium.com/p/16550d3be786</guid>
            <category><![CDATA[git]]></category>
            <dc:creator><![CDATA[Sharon Rozinsky]]></dc:creator>
            <pubDate>Sat, 30 Dec 2017 14:54:19 GMT</pubDate>
            <atom:updated>2017-12-30T14:54:19.234Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the first things you learn when you start to use git is that you should commit as early and as often as you can. Committing insures that your changes are saved (at least locally until you push them) and your work is tracked.<br>When you make changes and re-commit, you would usually use the <em>amend</em> option which adds your changes to the latest commit instead of creating a new one.<br>But what happens when you do have several commits and you want to go back to a specific commit before the last one and change it? This is where the git interactive rebase options comes very handy.</p><p>Git is probably the most popular and widely used version control system out there. It has many features and allows you to do many things. I have been using with git for several years now and recently got back to using it at work after a year and a half of working with Subversion.</p><p>When working with git I usually tend to commit often but most times I would just git commit --amend and add the changes to the one and only commit that would eventually get pushed. Lately I started working on a big story that is logically broken up to 3 parts and would need to be pushed only when all 3 parts are done. I thought it would be good to have 3 commits representing my work. So I wrote the code and whenever I needed to change the last commit I just used the amend option. I finished my work and went on to review my commits and, as you can suspect, I needed to make changes in the first two commits. Than it hit me: how do I change any of the previous commits?</p><p>The first thought that came to mind was to just add a fourth commit with the required fixes, but it seemed like there has to be a better solution built into git. And what do you know, there is: interactive rebase.</p><p>As it turns out there are quite a few things you can do using git interactive rebase so I thought I’ll just explain by using an example. I have a repository from some sample tutorial project I was using and I created three commits. The content of the commits doesn’t really matter.</p><p>To run git interactive rebase you just run: git rebase -i @~3 where @ is just a shorthand for HEAD and 3 is the number of last commits you want to include in the interactive rebase.</p><p>At this point git will switch to the vim editor showing something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YZvNv0gB6ZvMEln7Es80zw.png" /><figcaption>basic git interactive console screen</figcaption></figure><p>To work with the interactive rebase you just have to edit the line of the relevant commit, changing the word <em>pick</em> to the desired option and save the file.</p><p>To follow the example you can take a look at the following output of git log when I started, showing 3 commits I have made</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*th2L1Sq629nTlyVwqM4WPg.png" /></figure><h3>Change A Previous Commit (e, edit)</h3><p>So given that you have several commits and you want to make changes to a commit which is not the last one, in the interactive rebase screen just change the word <em>pick</em> to <em>e.</em> This option would cause git to move the HEAD to the relevant commit and allow you to make changes as if it was the last commit you just made. git log would show:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uUy-klNeVS9WGlroEZJdQg.png" /></figure><p>Now just make the changes, use git commit --amend to amend the changes to the commit and use git rebase --continue to make git replay the next commit (in our example this would be the third commit) and finish the rebase process.<br>That’s it, you just edited the second commit.</p><h3>Change A Previous Commit Message (r, reword)</h3><p>To change just the commit message of a previous commit use the <em>r</em> option. This would switch to the git commit vim screen and give you the option to change the commit message. In my example I changed the message of the first commit so git log will now show:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rccFEtvoxl7QC1EOV0MMtA.png" /></figure><h3>Change Order Of Commits</h3><p>Using git interactive rebase you can also change the order of commits. If you find yourself in a situation in which you need a specific commit to happen before another commit, just switch the order of lines in the git interactive rebase screen and save it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dqT8PsZibNLtRRKr7OfY_g.png" /></figure><p>And accordingly git log will show:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lO2Jq6i6s8uGKAxGJIjWrQ.png" /></figure><h3>Squash Commits (s, squash)</h3><p>Squashing commits is the action of combining several commits into one. Git interactive rebase makes it very easy to squash two or more sequential commits. To Squash a commit into another one, in the git interactive screen just change the word pick in the line of the later commit to <em>s</em>. This would open the git commit screen and give you the option to change the commit message of the new combined commit. By default it combines the messages of all squashed commits. In my example I squashed the first and second commit. git log will show:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XKOnGIa6DQWJecXige--ng.png" /></figure><h3>Removing A Commit (d, drop)</h3><p>To remove a commit you have 2 options:</p><ol><li>In the line of the commit to be removed change the word pick to <em>d</em> and save.</li><li>Delete the line of the commit to be removed and save.</li></ol><p>Both options are equivalent.</p><h3>Splitting Commits</h3><p>Splitting commits is actually not another action in the git interactive rebase screen, it’s just something you can do using the edit option. To split a commit just edit the commit using the <em>e</em> option. As I pointed out, this would move the HEAD to the chosen commit. Now use git reset HEAD^ . This would remove the commit but keep the changes in the files as un-staged. Now you can git add and git commit as many times as you want, creating several commits. When done use git rebase --continue to make git play the next commits and finishing the rebase.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=16550d3be786" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Firebase By Example]]></title>
            <link>https://medium.com/@sharon_roz/firebase-by-example-6aac197b8c11?source=rss-7136f6a74f------2</link>
            <guid isPermaLink="false">https://medium.com/p/6aac197b8c11</guid>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[firebase]]></category>
            <dc:creator><![CDATA[Sharon Rozinsky]]></dc:creator>
            <pubDate>Fri, 22 Dec 2017 15:45:35 GMT</pubDate>
            <atom:updated>2017-12-22T15:45:35.361Z</atom:updated>
            <content:encoded><![CDATA[<p>For the past couple of months I’ve been meaning to create a project that would be based on a serverless architecture. The idea of creating a project without having to take care of a physical infrastructure at all seemed interesting but also got me thinking if it’s really possible and if so for what type of project would it be best suited for.<br>In this post I’ll try to explain what serverless architecture is and how you can get started with writing an application based on this type of architecture using Google Firebase.</p><p>I have heard and read about serverless architecture in many blogs, YouTube videos and conferences. I was looking for the right opportunity and idea to create something based on a serverless architecture and then I came upon a video by Brad Traversy. He has an awesome YouTube channel with high quality videos in which he demonstrates different technologies, mostly web based, by creating small projects. In one of his videos he created a small web page based on html and javascript only and used Firebase as a database to save some data. I recommend this video and his channel in general (<a href="https://www.youtube.com/watch?v=PP4Tr0l08NE&amp;t=18s">https://www.youtube.com/watch?v=PP4Tr0l08NE&amp;t=18s</a>). I based my project on some of the stuff he did and continued to explore Firebase from there.</p><p>So let’s get started with what is serverless architecture and what is Firebase and than we will move on to something more practical and I’ll explain how to get started with the different features in Firebase.</p><h3>What Is A Serverless Architecture?</h3><p>Wikipedia describes serverless architecture or computing as:</p><blockquote>A cloud computing execution model in which the cloud provider dynamically manages the allocation of machine resources.</blockquote><p>I think the best way to understand serverless architecture is by looking at the history of software deployment architecture (Don’t worry it won’t be a history lesson):</p><ul><li>We started with something like mainframe computers (OK too far)</li><li>Then we moved to buying physical servers and deploying our software on top of them. Companies owned the servers and they managed them at their facilities.</li><li>The next step was to upgrade the abilities of these servers. Virtual computing was introduced and companies were able to utilize existing hardware much better by creating many VM’s on a single physical server.</li><li>At a later time, big service providers started offering physical hardware at remote locations, meaning physically managed by them but available for the paying customer (the companies that build the applications) to control remotely.</li><li>Then came the cloud. Big companies like Amazon, Google and Microsoft were starting to offer what is known as Platform As A Service and Infrastructure As A Service. Customers can buy VM’s as a computing resource, storage resources and more and control it remotely and securely. The pricing model is varies but the concept is you pay only for what you use. This infrastructure can also scale automatically to handle cases of high load. A more advanced option is to buy the VM’s as a package that contains the platform that you need, for example a VM containing the database you need already configured and ready to use.</li><li>The final step at which we are today is the serverless era. Now the service providing companies offer something more than just an infrastructure packaged as VM’s, they offer the infrastructure element you need, without the server. Obviously there are servers but you, as a developer, are unaware of them, so if you need a database you get access to one and you don’t even know or care where the physical infrastructure resides and how it’s configured. What about file storage? same here, you just get access to the file storage itself and that’s it. The access would usually be through API’s.<br>This model also introduces more application specific options like authentication, message notifications, analytics for your application and more. Usually you just pay for what you use and you don’t have to worry about any infrastructure or IT.</li></ul><p>This was a very high level list of infrastructure architecture options and I’m sure there are and were some more options but there is no doubt that these are the most commonly used options throughout the years. You can see that the evolution of infrastructure leads to a place in which the developer, or company building a software product has to worry less and less about the physical infrastructure. This enables companies to focus on their business logic and not on IT and operations and come up with working products much quicker. We have to remember that this doesn’t suite every company due to different concerns like security so we can’t really consider this to be a magical solution.</p><h3>But I heard Serverless is actually Function As A Service</h3><p>Ok, so there is one more thing about the definition of serverless we have to clear out. A many times, when people refer to serverless today they talk about Function As A Service. This is actually the ability to write code that is later deployed on some application runtime in the cloud. As a programmer all you have to do is just write the code and deploy it and the service provider that handles this runtime will handle everything else (hardware, scale etc…).<br>Note that there are some caveats to this type of service. You have to write your code in some specific manner and it’s usually based on the fact that you write functions that are than transformed to HTTP endpoints that the rest of your application can use. There are more options as you will see when we get to the Firebase example but it’s important to understand that using FaaS doesn’t mean, for example, that you can just write a big java monolith, deploy it in the cloud and let the service provider take care of everything else (at least for now).</p><p>Popular example for FaaS providers are: AWS Lambda, Google Cloud Functions and Firebase Cloud Functions, Azure Functions and more.</p><p>Why I think saying serverless is FAAS, is kind of wrong? well if you think about it, and as I mentioned in the previous paragraph, serverless architecture can provide more than just the platform for your code to run on. You can also get Database services and file storage services which leads to an even bigger set of services you can get as we will see in the Firebase example. All of which are given in the form of an access to a service without the developer having to care about infrastructure, scale etc. So the way I see it serverless is much bigger than FaaS.</p><h3>So Can I Build Every Type of Project Using A Serverless Architecture?</h3><p>Building the example project, which I’ll explain more about later, I really started to think what type of projects/applications I can build by using this type of architecture. I’m working in a big company developing enterprise software and it was clear to me that creating this type of software using only serverless services doesn’t seem very easy.<br>It’s not that I don’t serverless providers don’t offer critical components like a Database, file storage or a runtime for my code, but it seems like it’s kind of strict in the serverless world, meaning I can’t just write a full blown java project for example, with many dependencies and libraries and throw it on my serverless provider and everything will work in some magical way.<br>Right now it seems to me that serverless, specifically speaking about FaaS, is more suited to be a part of your application rather than it’s core. So if you write an application and choose microservices as an architecture, it’s easy to integrate FaaS to perform some tasks that are well defined and bounded by domain. These tasks can be executed on the serverless provider environment which will take care of scaling and availability for me.</p><p>Another great option serverless architecture can be useful for is as a solution for small companies who are just getting started, or self built applications used as POC’s for something bigger. In this cases it’s easier and quicker to start on a serverless basis since all the services are easily accessible and the amount of time needed for creating the infrastructure is considerably lower.</p><p>I think serverless today can’t be the base for enterprise software or large projects but it can certainly help solve many cases. In the future I definitely see more offerings that can suite large scale projects and enterprise software.</p><h3>What is Firebase?</h3><p>Firebase is Google’s take on serverless architecture. It’s actually a company that Google bought about 3 years ago. Firebase integrated many of it’s features with Google cloud features and now it serves as a platform that offers developers tools to build and create applications.<br>Google cloud platform still exists and it didn’t switch to be called Firebase or anything like this. There are some services like Cloud Functions that are offered on both platform and some services that are offered on only one of them.</p><p>Firebase offers some of the following features:</p><ul><li>Hosting</li><li>Authentication</li><li>Database (realtime DB + a new NoSQL database)</li><li>File Storage</li><li>Cloud Messaging</li><li>Analytics</li><li>Cloud Functions</li></ul><p>These are only some of the features offered by Firebase and actually as I write these lines the Firebase dev summit 2017 took place and new features were introduced. The common thing to all of these is that the infrastructure on which they are offered is completely transparent to the user. To use a feature you just define it for your project using the Firebase web console or the Firebase CLI tool. Then you implement or integrate with it using the Firebase SDK and just like that you can build an application with all the necessary components without having to worry about the hardware.</p><p>One of the most important things that appeal to developers that choose to use such a service is that Firebase also takes care of scaling for you. So for example lets say that your application uses Firebases realtime DB, in peak times where your application puts a load on the DB, Firebase would scale the DB out to accommodate the load. When everything cools downs Firebase will scale down. This scaling also means that you only pay more if there is a real need to scale your infrastructure.</p><p>Besides infrastructure elements Firebase also offers services like push notifications and messaging, performance monitoring and analytics to keep track of how your application is doing.</p><p>It’s important to note that Google is not the only company to offer such a service. Amazon has AWS, Microsoft offers similar features through Azure and there are others like IBM. I was more familiar with Firebase so I decided to experience with it but I’m sure the other services work in a similar way.</p><h3>Getting Started With Firebase</h3><p>The first thing you have to do is go to <a href="https://firebase.google.com/">https://firebase.google.com</a> and sign with your google account. Click get started or go the console and create a new firebase project.</p><p>Now you can start using the different firebase services. Firebase offers a CLI tool that can help you get started with creating the project and integrating the firebase services into your project. Some services are actually easier to handle in the firebase console as we will see.</p><p>As I mentioned I have built a small webpage that uses Firebases different features. You can take a look at the code in here: <a href="https://github.com/sharon-rozinsky/firebase-by-example">https://github.com/sharon-rozinsky/firebase-by-example</a></p><p>Notice that my hosted application is still up but due to security rules that I intentionally applied some of the features will not work as I run on the free firebase tier that is limited.</p><p>To explain everything, I have broken the application to the features it uses and I try to explain every feature and how you can use it. In my explanation I’m referring to the code so it’s easier if you look at it as well.</p><p>Let’s start building the application using Firebase.</p><h4><strong>Hosting</strong></h4><p>1. Install firebase tools, this contains the firebase cli tool. You will need node and npm so if you don’t have them go to: <a href="https://nodejs.org/en/">https://nodejs.org/en/</a>. <br>Now run: <em>npm install -g firebase-tools</em> this will take a couple of minutes.</p><p>2. First time you use the firebase cli you will be prompt to login using the: <strong><em>firebase login</em></strong> command. run it and a google login webpage will opened. Choose the user with which you want to login and click next.</p><p>3. Back in the command line go to the local project directory on your pc and using the firebase cli enter: <strong><em>firebase init</em></strong>.<br> In the menu presented choose the Hosting option. This might be confusing but don’t forget to press space to select the feature you want and than press enter to continue.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rosyge6iRpZuWZkN3Ib1sg.png" /></figure><p>4. Select the firebase project to associate.<br> For the hosting setup you can choose the directory you want to serve as the root directory. If you leave the default it would create a directory called public so make sure you move your files into it (except the firebase.json file). For our purpose we can choose the single-page app setup.<br>This will create the firebase.json settings file which will give you different options like defning a custom 404 page, rewrites, redirects and more. <a href="https://firebase.google.com/docs/hosting/url-redirects-rewrites">https://firebase.google.com/docs/hosting/url-redirects-rewrites</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*zstdIWnQUSTFTNSDXO9ZXA.png" /><figcaption>firebase.json file</figcaption></figure><p>5. make sure you have an index.html file in the root directory otherwise firebase will create one for you.<br> 6. Now let’s test to make sure that everything is configured properly before deploying to firebase. To do so, use the firebase serve command. This will open a local server on <a href="http://localhost:5000/">http://localhost:5000</a>. When you go to this address, if everything is working fine you will see your web application.<br> 7. Now that everything is properly configured we can call <strong><em>firebase deploy</em></strong> to deploy the project to firebase. When the project is deployed, the firebase cli will give us the public URL to use in order to access our application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/776/1*zdrLYl1-WGr7SUrazIxdAw.png" /><figcaption>Public URL to access our project</figcaption></figure><p>8. over at the firebase console (<a href="https://console.firebase.google.com/">https://console.firebase.google.com/</a>) we can see under the hosting section that we have a project hosted and the history of the firebase deploy commands. This is useful to keep track of when deploys take place.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qZsfPDVtQcCfWil0klyVeQ.png" /></figure><p>* One weird problem I had when redploying is that my stylesheet file was not updated. This was due to caching in chrome so make sure you refresh the page using ctrl+shift+R.</p><h4>Storage</h4><p>1. Another great feature by firabse is file storage. You can use firebase to upload and download files without the hastle of managing a file server.<br> 2. To interact with the firebase SDK you have to configure firebase in your project. This configuration can be copied from the main page in the firebase console. Just click add Firebase yo your web app and copy the code snippet to your project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9_E7IbolL6lJo83pf6OAdw.png" /></figure><p>In our case, the sample project is a web application. We have the main.js file in which we add the firebase configuration which is specific for our project.<br>3. Firebase uses security rules to limit the access to the file storage made by the application. The most basic and default rule limits the access to authenticated users. This means that if a user is not authenticated the application he would not be able to use the file storage. For development purposes only, we would disable this rule but of course this is not a good idea to perform in production. You would want to use security rules so that access to your file storage is controlled and managed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R02dROYf1_Cu8H4Bb6F8Wg.png" /></figure><p>4. To work with the file storage we need to get a reference to it.</p><pre>var storageRef = firebase.storage().ref(&#39;media_content&#39;);</pre><p>Firebase storage is based on a folder/file structure similar to an operating system and these are placed in buckets. When the application holds a reference to a file location it can upload to that location, download from it and do various things with it’s metadata. Traversing the file structure is possible using the parent, child and root properties.<br> 5. In our sample project, we create a reference to a directory called ‘media_content’. The user can click the choose file button, select a file and it will be uploaded to the firebases file storage. Nothing interesting but the cool thing is that it’s really easy to perform.<br> 6. When the file is uploaded we can use the firebase console to view it under the Storage section.<br> 7. Specific documentation can be found here: <a href="https://firebase.google.com/docs/storage/web/start">https://firebase.google.com/docs/storage/web/start</a>.</p><h4>Firestore</h4><p>1. This is the new database offering from Firebase. This is actually a NoSQL style database that enables saving data in a collections/documents form. Compared to the real time db this will give you a more sophisticated DB experience with complicated queries, indexes and more. Google also states that this DB will be faster than the real time DB.<br>2. This service is still in beta. From the Firebase console, you can create an access to firestore for your application from the Database section. Currently it will give you the option to start firestore in a secured or non secured mode. The difference being that if you choose the non secure mode the security rules will allow access to your projects firestore for anyone who holds a reference to this DB.<br>3. In the sample code note that we need to reference the firebase-firestore.js file as well as the firebase.js file (this is added in the index.html file at the bottom part).</p><pre>&lt;script src=&quot;https://www.gstatic.com/firebasejs/4.5.1/firebase.js&quot;&gt;&lt;/script&gt;</pre><pre>&lt;script src=&quot;https://www.gstatic.com/firebasejs/4.5.1/firebase-firestore.js&quot;&gt;&lt;/script&gt;</pre><p>4. When adding data to firestore you can either add or set a document.<br>To set a document you have to specify an identifier. If the document identified by the identifier doesn’t exist it will be added, otherwise it will be overwritten. You can also merge new data to a document using the {merge: true} flag.<br>Often times, identifying a document is unnecessery so you can use add to add it to some collection. In this case an id is generated for you. Other than the id part, set and add work the same.<br>5. Firestore supports transactions and batched writes.</p><h4>Authentication</h4><p>1. Firebase integrates with many authentication providers and eases authentication management. To use a certain authentication provider we need to first enable it from the firebase console.<br>2. Enable the specific provider. Specifically for google authentication we can either use redirect or popup.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4nDfn9V4vp-S1e8Y8R9OfA.png" /><figcaption>Authentication Providers In Firebase Console</figcaption></figure><p>3. In the main.js file add firebase.auth().onAuthStateChanged to the application to figure out what is the state of the currently logged in user. If the state changes we can act on it. This will also be called when the page is loaded.<br> 4. In the firebase console we can see all users who signed in and manage them.<br> 5. There are even options to define email templates for: email address verification, password reset, email address change and SMS verification and the firebase SDK gives an easy options to use them.<br> 6. From the firebase console we can add users manually if that helps somehow, maybe for demo purposes.</p><h4>Firebase Cloud Functions</h4><p>1. Cloud functions give you the ability to write your code without the need to take care of the infrastructure on which the code would run. You just write your code, or functions to be exact and firebase takes care of managing the code. If your application load increases firebase will take care of that by scaling the infrastructure.<br>Currently cloud functions can only be written in Javascript and they are executed in a Node.js runtime.<br>2. The type of code you would usually write and run under cloud functions is composed of functions that can react to different Firebase generated triggers and HTTP triggers. Firebase triggers would be different events that occur on Firebase components that are part of your project like real time databse triggers, cloud storage triggers etc. So for example you can write a function that listens to the onCreate event on the real time database and every time new data is created in the DB, your function would be triggered.<br>The second type of triggers are HTTP triggers which practically act as HTTP endpoints turning the functions you write to a REST API.<br>3. To add cloud functions to a project we need to use the firebase CLI again (notice how we didn’t really have to use the CLI for the previous Firebase features except the initial hosting. A lot of tasks can be handled from the Firebase console).<br>From the root directory of your project issue the following command: <strong><em>firebase init functions</em></strong>.<br>Follow the instructions, they are really straightforward. The most interesting thing that can be done at this step is add npm dependencies but this can also be managed manually later. <br>4. The previous step adds a functions directory to your project. In this directory there are 2 files:<br>index.js — this is the file where you would write your functions.<br>package.json — the standard npm package file that describes your code and dependencies.<br>If a node_modules directory doesn&#39;t appear in the functions directory run npm install in the functions directory.<br>5. Note that at the basic free tier, external dependencies are not supported (other than the Google APIs).<br>6. To deploy the functions. This could be done by issuing: <br><strong><em>firebase deploy — only</em></strong> functions. This would only deploy your cloud functions (you can even deploy only a certain function by modifying the command and adding <strong><em>functions:functionName</em></strong>).<br>To get things started, we can just remove the comments from the default hello world function in the index.js file and make sure everything is working fine.<br>7. Firebase console shows logs for the cloud functions.<br>8. When Firebase completes the deploy process you will see something like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/1*tj7XAIQSmPxql1hHiIZDdg.png" /></figure><p>At the bottom we can see the URL for the endpoint that is actually our function. Over at the firebase console the function would also appear in the dashboard</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wYMsOxBI8rD8e6w9Y2pK2A.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6aac197b8c11" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Database Schema Migration — Just Do It (Automatically)]]></title>
            <link>https://medium.com/@sharon_roz/database-schema-migration-just-do-it-automatically-5303c2cfa352?source=rss-7136f6a74f------2</link>
            <guid isPermaLink="false">https://medium.com/p/5303c2cfa352</guid>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[liquibase]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Sharon Rozinsky]]></dc:creator>
            <pubDate>Mon, 07 Aug 2017 08:43:39 GMT</pubDate>
            <atom:updated>2017-08-07T08:47:48.509Z</atom:updated>
            <content:encoded><![CDATA[<p>Upgrading your database schema is a necessary part of any project using a relational database. In this post I’ll cover the process of automatically upgrading database schemes (often referred to as database migration) and how I have done it in a project I’m working on.</p><p>Relational databases have been around for a long time. They play an important role in many projects and even though NOSQL databases are a huge trend, relational databases will probably stick around for a long time as they are just better suited for many use cases.</p><p>An unarguable fact is that the database schema is an evolving part of a project as much as the code is. <br>When we write a new feature that requires persistence, we write a piece of code that performs a certain logic required by that feature but we will also, most likely, add a table or a column to the database in order to support that feature.<br>This change to the database schema has to be saved somewhere. So we can, for example, create a dedicated directory that sits alongside the program code and contain the script files that perform these modifications. The schema changes would probably be saved in a .txt or a .sql script files and since we are responsible developers who might have even read posts like this one: <a href="https://blog.codinghorror.com/get-your-database-under-version-control/">https://blog.codinghorror.com/get-your-database-under-version-control/</a> we will make sure the change scripts are saved as part of our version control, the same way our code is.</p><p>Now, when the other team members want to apply these changes locally or maybe into production they just update their code base and apply these changes. But wait a minute, how exactly do they do it?<br>The naive way is just to run it manually against the database. Our schema changes are, in the end, just a bunch of SQL queries that can be ran using any SQL client against our database. That might be fine if we are upgrading to a major version not very often, but for a development team, upgrading the code on a daily or even an hourly basis, this task can become very tedious. <br>The slightly better approach would be to write a small script that would automatically run the changes for you. Going that road you can quickly find out that it’s not an easy task as you need to run only the new queries so you will have to inspect your database and figure out which changes were applied and which were not yet applied.</p><p>In the age of CI/CD and advanced build-deploy pipelines, a more robust and automatic solution is required to solve this problem. There are different frameworks and libraries that are specifically designed to solve this problem. These frameworks usually offer things like:</p><ul><li>A version control experience where a set of changes is logically tied together and can explain the essence of the change.</li><li>Better integration with build managers and automation servers that automatically incorporate the schema changes into the pipeline and apply them on the database instance.</li><li>Advanced features like rollback in case of a failure.</li></ul><p>The most important thing that frameworks such as these do is add a state your database schema. Like your code, at any given time you know what version your database schema is at and what changes were already applied on it.<br>As a developer working on your local database instance you don’t have to guess which scripts you already ran, the database can be updated automatically when you update your code.<br>From the QA and OPS perspective, the database can be tested and upgraded gradually, and in case of failure of one of the changes in the upgrade scripts, it’s easier to pin point the exact change that caused the problem and the person who made the change. If a failure happens, mitigation in the form of a rollback can also be automatically applied to keep the database coherent (if supported by the framework).</p><h3>My 2 Cents On How To Integrate A Database Migration Framework In Your Project</h3><p>As part of my work on a large scale Java project, my team was looking for a way to improve the process of database upgrades. We used to work with script files that were saved in our code base and when we pulled the latest code changes we always had to figure out if the database should be upgraded as well. When the QA teams upgraded their environments with our latest builds they had to do the same. Every upgrade to a database instance, was done manually so you had to remember what changes you already ran. Running the same change twice would sometimes result in simple errors and in other times would modify the database in a way that the code was not expecting to handle thus causing different problems when the system was running. Not running a certain change would usually cause the code to throw exceptions as the queries to the database would fail.</p><p>I started looking for frameworks that would help solve our problems. I had put together a list of points and issues I would like the chosen framework to address:</p><ul><li>Our change scripts were structured in a certain way, each version (including a minor version) had it’s own dedicated script file in which all changes were added. Not a complicated structure but it had to be kept, so the framework would have to support this structure.</li><li>The framework would have to support both local development environments and official builds deployed on servers without having to write the upgrade scripts twice or in a different way.</li><li>The upgrades must be applied automatically when upgrading the local environment or a testing server.</li><li>The framework wouldn’t drastically change the way we write our upgrade scripts. If it requires scripts written in a certain way I would rather it had a minimal impact compared to how we do it today.</li></ul><p>The two best options I have found were Liquibase and Flyway DB. Both frameworks met most, if not all of my demands and the integration into my existing project was simple enough.<br>Due to the nature of our project I needed the option of running upgrades on a per version basis, meaning we can issue a database upgrade using the change scripts found in a certain file (which matches a version in our case). Using Liquibase I found it easy to achieve by using the context option so eventually I chose Liquibase as the migration framework.<br>Both frameworks have their advantages and disadvantages (although I feel they are quite insignificant) and for a certain project each one might be a better fit than the other so in any case I recommend checking both options.</p><h4>How Does Liquibase Work</h4><p>This is a tl;dr part that briefly describes how Liquibase works if you want to get started with it. In any case I recommend going through the documentation in the official site: <a href="http://www.liquibase.org/">http://www.liquibase.org/</a>. It’s well written and easy to get started.<br>My project had the upgrade scripts written in .sql files so the change wasn’t too big.</p><ul><li>From the developers point of view, if you are accustomed to writing a .sql script files you will need to change two things:<br>- Your .sql file will have to start with this line -- liquibase formatted sql <br>- each changeset (a line or several lines of sql code representing a change) you create needs to be prefixed with a line similar to this: -- changeset developer_name:some_id and optionally suffixed with <br>-- comment: description of the changeset</li><li>To summarize, if a script file used to look like this:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51082497076bcba21aa946bd844595f8/href">https://medium.com/media/51082497076bcba21aa946bd844595f8/href</a></iframe><p>It would now look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc9a2c633b453417c1c9dad543ef3883/href">https://medium.com/media/dc9a2c633b453417c1c9dad543ef3883/href</a></iframe><p>You can see that the change is not that big, just adding a few SQL comments that let Liquibase do it’s job.</p><ul><li>You need to add a file that describes the different changeset files you want Liquibase to scan when it operates. The file is called db.changelog-master.xml by default. When issuing Liquibase commands by any of the methods that would be described below you will have to reference this file. In my case I would have a line in this file for every changeset file which represents a version of my project.<br>See <a href="http://www.liquibase.org/documentation/databasechangelog.html">http://www.liquibase.org/documentation/databasechangelog.html</a>.</li><li>It’s important to mention that Liquibase offers other ways of describing a changeset. This actually means that you can write your upgrade scripts in a “non-sql” descriptive language. See <a href="http://www.liquibase.org/documentation/json_format.html">http://www.liquibase.org/documentation/json_format.html</a> for an example.<br>My personal preference was to stick with .sql files since it gives me full control of my upgrade queries.</li></ul><p>This is mostly it for the part of writing the change scripts. Now we need Liquibase to run the changes.</p><h4>Using Liquibase</h4><p>Liquibase adds 2 tables to your DB in order to do it’s job. The important one is DATABASECHANGELOG which acts as an audit table and lists the different changesets that were executed, their execution date and the comments associated with the changeset.<br>The second table is DATABASECHANGELOGLOCK that Liquibase uses to handle updates from multiple sources. The information in this table is not very useful for everyday use.<br>Liquibase has many commands but for my everyday use I found these 3 to be the most important:</p><ol><li><strong>update</strong>: this is the command that tells Liquibase to run any changeset that was not yet ran based on the list of script files described in the db.changelog-master.xml file. The DATABASECHANGELOG will be updated accordingly.</li><li><strong>changelogSync</strong>: This command acts as the update command only it doesn’t actually run any changeset. It just update the DATABASECHANGELOG table as if the changesets were applied. This command is useful if for some reason you want to handle some changesets on your own and just sync Liquibase.</li><li><strong>clearCheckSums</strong>: Liquibase assumes that a changeset that was written and executed would not be changed in the future. To keep your DB upgrades coherent it calculates a checksum on the upgrade script file and if you go back and modify a changeset that was already applied it would fail with an error stating the changesets that were changed (this value is also stored in the MD5SUM column in the DATABASECHANGELOG table). If for some reason you do need to go back and change things, run this command to zero out the check sums and the next time you issue the update command Liquibase will calculate all check sums again.</li></ol><h4>Integration With Build Tools</h4><p>1.<strong> </strong>Maven Integration: Liquibase can be integrated with maven using a maven plugin. This is a great way for developers working with a local DB instance to automatically upgrade the DB anytime someone pushes a changeset. You can either have Liquibase run in one of Maven’s life cycles or run it directly using the maven plugin. A sample maven configuration:</p><pre>&lt;build&gt;<br>  &lt;plugins&gt;<br>    &lt;plugin&gt;<br>      &lt;groupId&gt;org.liquibase&lt;/groupId&gt;<br>      &lt;artifactId&gt;liquibase-maven-plugin&lt;/artifactId&gt;<br>      &lt;version&gt;3.5.1&lt;/version&gt;<br>      &lt;configuration&gt;<br>        &lt;changeLogFile&gt;db.changelog-master.xml&lt;/changeLogFile&gt;<br>        &lt;driver&gt;org.mariadb.jdbc.Driver&lt;/driver&gt;<br>        &lt;url&gt;MYSQL_SERVER_URL&lt;/url&gt;<br>        &lt;username&gt;USER_NAME&lt;/username&gt;<br>        &lt;password&gt;PASSWORD&lt;/password&gt;<br>        &lt;contexts&gt;${contexts}&lt;/contexts&gt;       <br>      &lt;/configuration&gt;<br>      &lt;executions&gt;<br>        &lt;execution&gt;<br>          &lt;phase&gt;install&lt;/phase&gt;<br>          &lt;goals&gt;<br>            &lt;goal&gt;update&lt;/goal&gt;         <br>          &lt;/goals&gt;<br>        &lt;/execution&gt;<br>      &lt;/executions&gt;<br>      &lt;dependencies&gt;<br>        &lt;dependency&gt;<br>          &lt;groupId&gt;org.mariadb.jdbc&lt;/groupId&gt;<br>          &lt;artifactId&gt;mariadb-java-client&lt;/artifactId&gt;<br>          &lt;version&gt;1.4.6&lt;/version&gt;<br>        &lt;/dependency&gt;<br>      &lt;/dependencies&gt;<br>    &lt;/plugin&gt;<br>  &lt;/plugins&gt;<br>&lt;/build&gt;</pre><p>Now you can just run mvn clean install or call Liquibase directly directly using mvn liquibase:&lt;COMMAND&gt;</p><p>2. Integrate Liquibase in your CI/CD tools (Jenkins etc…) using the CLI tool. See the official documentation: <a href="http://www.liquibase.org/documentation/command_line.html">http://www.liquibase.org/documentation/command_line.html</a></p><h4>Worth Mentioning</h4><p>Liquibase is a feature rich framework and you can achieve a lot with it. There are some features I experimented with but didn’t end up using due to my project’s constraints, but I still think they can be really useful:</p><ul><li>rollback: Liquibase let’s you perform a rollback on changesets. This basically means you can define the script’s you want to run for every changeset in case you want to perform a rollback. If you use the different changelog formats (not the .sql file format) that Liquibase offers, some actions like create table can auto generate a rollback action.<br>See <a href="http://www.liquibase.org/documentation/rollback.html">http://www.liquibase.org/documentation/rollback.html</a></li><li>Liquibase offers a Java API you can use in order to integrate it as part of your code and not only use it as an external framework: <a href="http://www.liquibase.org/javadoc/liquibase/Liquibase.html">http://www.liquibase.org/javadoc/liquibase/Liquibase.html</a></li><li>runOnChange: this is something I found useful if you need to change your changesets frequently. As stated before, this action would lead to a checksum error, but if you add the --runOnChange:true flag Liquibase would ignore the error, run the changeset again and compute the new checksum for this changeset.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5303c2cfa352" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>