├── .gitignore ├── LICENSE ├── README.adoc ├── docker-compose.yml ├── pom.xml └── src ├── main └── groovy │ └── org │ └── cassalog │ └── core │ ├── Bootstrap.groovy │ ├── BootstrapException.groovy │ ├── Cassalog.groovy │ ├── CassalogBuilder.groovy │ ├── CassalogImpl.groovy │ ├── CassandraVersionException.groovy │ ├── ChangeLog.groovy │ ├── ChangeSet.groovy │ ├── ChangeSetAlteredException.groovy │ ├── ChangeSetException.groovy │ ├── ChangeSetListener.groovy │ ├── ChangeSetValidationException.groovy │ ├── CqlChangeSet.groovy │ ├── CreateKeyspace.groovy │ ├── KeyspaceUndefinedException.groovy │ ├── ReplicationSettings.groovy │ ├── SchemaAgreementException.groovy │ ├── SetKeyspace.groovy │ └── VerificationFunctions.groovy └── test ├── groovy └── org │ └── cassalog │ └── core │ ├── BootstrapTest.groovy │ ├── CassalogBaseTest.groovy │ ├── CassalogTest.groovy │ ├── IncludeTest.groovy │ ├── LoadChangeSetsTest.groovy │ ├── MultipleCQLStatementsTest.groovy │ └── SetKeyspaceTest.groovy └── resources ├── append_changes ├── script1.groovy └── script2.groovy ├── basic_validation ├── script1.groovy └── script2.groovy ├── bootstrap ├── bootstrap_fail.groovy ├── bootstrap_include.groovy ├── bootstrap_test.groovy ├── included_bootstrap.groovy └── script1.groovy ├── change_id ├── script1.groovy └── script2.groovy ├── create_keyspace ├── script1.groovy ├── script2.groovy ├── script3.groovy ├── script4.groovy ├── script5.groovy └── script6.groovy ├── fail_modified_changeset ├── script1.groovy └── script2.groovy ├── include ├── multiple_includes.groovy ├── nested.groovy ├── script1.groovy ├── script2.groovy ├── script3.groovy └── script4.groovy ├── logback.groovy ├── multiple_buckets ├── script1.groovy └── script2.groovy ├── multiple_cql_statements └── script1.groovy ├── resume_updates ├── columns.groovy ├── create_tables.groovy └── insert.groovy ├── set_keyspace ├── script1.groovy ├── script2.groovy └── script3.groovy ├── single_change └── schema.groovy └── tags_test └── script1.groovy /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | pom.xml.tag 3 | pom.xml.releaseBackup 4 | pom.xml.versionsBackup 5 | pom.xml.next 6 | release.properties 7 | dependency-reduced-pom.xml 8 | buildNumber.properties 9 | 10 | # IDE files 11 | .project 12 | .classpath 13 | .settings 14 | .idea 15 | *.ipr 16 | *.iml 17 | *.iws 18 | nb-configuration.xml 19 | 20 | # KDE 21 | .directory 22 | 23 | # OSX 24 | .DS_Store 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.adoc: -------------------------------------------------------------------------------- 1 | = Cassalog 2 | 3 | Cassalog is a schema change management library and tool for 4 | http://cassandra.apache.org[Apache Cassandra] that can be used with 5 | applications running on the JVM. 6 | 7 | == Why? 8 | Just as application code evolves and changes so do database schemas. If you are 9 | building an application and intend to support upgrading from one version to 10 | another, then managing schema changes is essential. If you are lucky, you might 11 | be able to get by with running some simple upgrade scripts to bring the schema 12 | up to date with the new version. This likely will not work however if you 13 | support multiple upgrade paths. For example, suppose we have versions 1 and 2, 14 | and are introducing version 3 of an application. We want to allow upgrading to 15 | version 3 from either 1 or 2 in addition to upgrading from 1 to 2. 16 | 17 | You could add schema upgrade logic to application code, but that is often a 18 | less that ideal solution as it convolutes the code base. Fortunately, there are 19 | tool for managing schema changes like http://www.liquibase.org/[Liquibase], 20 | http://flywaydb.org/[Flyway], and 21 | http://guides.rubyonrails.org/active_record_basics.html[Active Record] for Ruby 22 | on Rails applications. These tools however, are designed specifically for 23 | relational databases. I previously spent time trying to patch Liquibase to 24 | support Cassandra but found that it was not a good fit. Cassalog is designed 25 | solely for use with Cassandra, not for any other database systems. 26 | 27 | Cassalog is written in Groovy. There are several reasons for this. First, 28 | Groovy offers great interoperability with Java, making it usable and accessible 29 | to application running on the JVM. Groovy's dynamic and meta programming 30 | features make it easy to write domain specific languages. Groovy has multi-line 31 | strings and string interpolation out of the box, both of which can be really 32 | useful for writing schema change scripts. Lastly, with Cassalog schema changes 33 | are not written in XML or JSON. Instead they are written as Groovy scripts 34 | giving you the full power and flexibility of Groovy. 35 | 36 | == Usage 37 | The Cassalog class is the primary class with which you will interact. 38 | 39 | [source,groovy] 40 | ---- 41 | // Groovy 42 | def script = // load schema change script 43 | def session = // obtain DataStax driver Session object 44 | def cassalog = new Cassalog(session: session) 45 | cassalog.execute(script) 46 | ---- 47 | 48 | [source,java] 49 | ---- 50 | // Java 51 | URI script = // load schema change script 52 | Session session = // obtain DataStax driver Session object 53 | Cassalog cassalog = new Cassalog(); 54 | cassalog.setSession(session); 55 | cassalog.execute(script); 56 | ---- 57 | 58 | And here is what a cassalog script might look like, 59 | 60 | [source,groovy] 61 | ---- 62 | createKeyspace { 63 | version '0.1' 64 | name 'my_keyspace' 65 | author 'admin' 66 | description 'Set up a keyspace for unit tests' 67 | } 68 | 69 | schemaChange { 70 | version '0.1.1' 71 | author 'admin' 72 | description 'Create table for storing time series data' 73 | cql """ 74 | CREATE TABLE metrics ( 75 | id uuid, 76 | time timeuuid, 77 | value double, 78 | PRIMARY KEY (id, time) 79 | ) 80 | """ 81 | } 82 | ---- 83 | 84 | TIP: Schema changes are applied in the order that they are declared in the 85 | script(s) regardless of the assigned versions. 86 | 87 | == Features 88 | * Tagging 89 | * Execute arbitrary Groovy / Java code in schema change scripts 90 | * Pass variables to scripts 91 | * Changes can stored across multiple scripts 92 | * Schema change detection 93 | 94 | === Tagging 95 | You can specify tags when running Cassalog, e.g. 96 | 97 | [source,groovy] 98 | ---- 99 | // Groovy 100 | def script = // load schema change script 101 | def session = // obtain DataStax driver Session object 102 | def cassalog = new Cassalog(session: session) 103 | cassalog.execute(script, ['dev', 'test_data']) 104 | ---- 105 | 106 | [source,java] 107 | ---- 108 | // Java 109 | URI script = // load schema change script 110 | Session session = // obtain DataStax driver Session object 111 | Cassalog cassalog = new Cassalog(); 112 | cassalog.setSession(session); 113 | cassalog.execute(script, Collections.asList("dev", "test_data")); 114 | ---- 115 | 116 | Cassalog will apply schema changes that have not already been run and that 117 | 118 | * Dot not specify any tags or 119 | * Specify tags and include the `dev` and `test_data` tags 120 | 121 | === Execute arbitrary code 122 | Cassandra is frequently used for time series data. Suppose we have a metrics 123 | table, and we want to generate some sample data for tests. 124 | 125 | [source,groovy] 126 | ---- 127 | schemaChange { 128 | version '1.0' 129 | cql """ 130 | CREATE TABLE metrics ( 131 | id text PRIMARY KEY, 132 | time timestamp, 133 | value double 134 | ) 135 | """ 136 | } 137 | 138 | testData = [] 139 | random = new Random 140 | 10.times { i -> 141 | testData << "INSERT INTO metrics (id, time, value) VALUES ('$i', ${new Date().time + 100}, ${random.nextDouble()})" 142 | } 143 | 144 | schemaChange { 145 | version '1.0.1' 146 | tags 'test_data' 147 | cql testData 148 | } 149 | ---- 150 | 151 | This script first calls the `schemaChange` function to create the metrics table. 152 | The next few lines generate a list of INSERT statements with some test data. 153 | Finally, we have another call to `schemaChange`. It specifies the test_data 154 | tag and passes the `testData` list to the `cql` parameter. 155 | 156 | === Pass variables to scripts 157 | You can pass arbitrary variables to scripts, not just strings. 158 | 159 | [source,groovy] 160 | ---- 161 | // Groovy 162 | def vars = [ 163 | metricIds: ['M1', 'M2', 'M3'], 164 | startDate: new Date() 165 | maxValue: 100, 166 | minValue: 50 167 | ] 168 | cassalog.execute(script, vars) 169 | ---- 170 | 171 | [source,java] 172 | ---- 173 | // Java 174 | Map vars = ImmutableMap.of( 175 | "metricIds", asList("M1", "M2", "M3"), 176 | "startDate", new Date(), 177 | "maxValue", 100, 178 | "minValue", 50 179 | ); 180 | cassalog.execute(script, vars); 181 | ---- 182 | 183 | === Changes can stored across multiple scripts 184 | You can use the `include` function to store changes in multiple script to 185 | keep your schema changes more modular and better organized. 186 | 187 | [source,groovy] 188 | ---- 189 | include '/dbchanges/base_tables.groovy' 190 | 191 | include '/dbchanges/seed_data.groovy' 192 | ---- 193 | 194 | The `include` function currently takes a single string argument that should 195 | specify the absolute path of a script on the classpath or from the configured `baseScriptsPath`. 196 | 197 | `baseScriptsPath` is an absolute path to where the other include scripts are located e.g. `/Users/john/cassalog/scripts`. 198 | 199 | === Schema change detection 200 | Cassalog does not store the CQL code associated with each schema change. It 201 | computes a hash of the CQL and stores that instead. If the hash in the change 202 | log differs from the hash of the CQL in the source script, Cassalog will throw 203 | a ChangeSetAlteredException. 204 | 205 | You will need to manually resolve the issue that caused the 206 | ChangeSetAlteredException. Cassandra does not support transactions like a 207 | relational database, so there no rollback functionality to fall back on. 208 | 209 | == Change Log Table 210 | All schema changes are recorded in the change log table, _cassalog_. The table 211 | will be created the first time Cassalog is run. Change log data looks like, 212 | 213 | [noformat] 214 | ---- 215 | bucket | revision | applied_at | author | description | hash | version | tags 216 | --------+----------+--------------------------+--------+-----------------------------------------------------+ 217 | 0 | 0 | 2016-01-28 11:09:54-0500 | admin | First table | 0xe361957eeb | 1.0 | {'legacy'} 218 | 0 | 1 | 2016-01-28 11:09:54-0500 | admin | Second table | 0xf336e725d4 | 1.1 | {'legacy'} 219 | 0 | 2 | 2016-01-28 11:09:55-0500 | admin | Third table | 0xcecef5f840 | 1.2 | {'legacy', 'dev'} 220 | 0 | 3 | 2016-01-28 11:09:55-0500 | admin | Fourth table | 0x4b5d24b77c | 1.3 | {'legacy'} 221 | ---- 222 | 223 | Here is a brief overview of the schema. 224 | 225 | [noformat] 226 | ---- 227 | CREATE TABLE cassalog ( 228 | bucket int, 229 | revision int, 230 | applied_at timestamp, 231 | author text, 232 | description text, 233 | hash blob, 234 | version text, 235 | tags set, 236 | PRIMARY KEY (bucket, revision) 237 | ) 238 | ---- 239 | 240 | *author* + 241 | The username, or email address, etc. of the person making the change. This is 242 | an optional field and can be null. 243 | 244 | *description* + 245 | A summary of the changes. This is an optional field and can be null. 246 | 247 | *hash* + 248 | Cassalog does not store the CQL statements that it executes. Instead it stores a 249 | hash that uniquely identifies the CQL statement(s). Cassalog generates this 250 | hash value. 251 | 252 | *version* + 253 | The version can be an arbitrary string. It should be a unique identifier for the 254 | change; however, Cassalog does not enforce uniqueness. This is a required field. 255 | 256 | *tags* + 257 | An optional set of user-supplied tags. 258 | 259 | *revision* + 260 | Cassalog assigns a revision number to each change that it applies. It uses the 261 | revision number to keep track of the order in which changes are applied. If the 262 | order of schema changes in a source script is changed, then a 263 | ChangeSetAlteredException will be thrown. 264 | 265 | *bucket* + 266 | Cassalog stores multiple rows per physical partition. This is a revision offset. 267 | The bucket size defaults to 100. 268 | 269 | == Building from Source 270 | Cassandra is built with Maven and requires a JVM version 1.7 or later. Test 271 | execution requires a running Cassandra cluster (which can be a single node) with 272 | a node listening on 127.0.0.1. Cassandra 2.0 or later should be used. 273 | 274 | [source,bash] 275 | ---- 276 | git clone https://github.com/jsanda/cassalog.git 277 | cd cassalog 278 | mvn install 279 | ---- 280 | 281 | TIP: If you want to build without having a running Cassandra instance, you can 282 | run `mvn install -DskipTests` 283 | 284 | == Setting up Cassandra for development or testing 285 | The recommended way to set up Cassandra is by using 286 | https://github.com/pcmanus/ccm[ccm (Cassandra Cluster Manager)]. 287 | 288 | As Cassalog evolves and looks to support different versions of Cassandra and 289 | CQL, ccm is the likely tool of choice to use for testing against different 290 | versions. 291 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | cassandra: 2 | image: 'cassandra:3.0.0' 3 | ports: 4 | - '9042:9042' 5 | - '7199:7199' 6 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 4.0.0 22 | 23 | org.cassalog 24 | cassalog 25 | 0.7.0 26 | Cassalog 27 | A Cassandra schema change management tool for applications running on the JVM 28 | https://github.com/jsanda/cassalog 29 | 2016 30 | 31 | 32 | 33 | Apache License, Version 2.0 34 | http://www.apache.org/licenses/LICENSE-2.0.txt 35 | repo 36 | 37 | 38 | 39 | 40 | 41 | John Sanda 42 | http://johnsanda.blogspot.com/ 43 | -5 44 | 45 | 46 | 47 | 48 | 3.0.0 49 | 16.0.1 50 | 2.4.5 51 | 6.9.10 52 | 1.1.3 53 | 1.5 54 | 2.5.3 55 | 2.4 56 | 57 | 58 | https://repository.jboss.org/nexus 59 | ${jboss.nexus.base.url}/service/local/staging/deploy/maven2/ 60 | ${jboss.nexus.base.url}/content/repositories/snapshots/ 61 | 62 | 63 | 64 | scm:git:git@github.com:jsanda/cassalog.git 65 | scm:git:git@github.com:jsanda/cassalog.git 66 | https://github.com/jsanda/cassalog 67 | head 68 | 69 | 70 | 71 | 72 | jboss-releases-repository 73 | JBoss Releases Repository 74 | ${jboss.releases.repo.url} 75 | 76 | 77 | jboss-snapshots-repository 78 | JBoss Snapshots Repository 79 | ${jboss.snapshots.repo.url} 80 | 81 | 82 | 83 | 84 | 85 | org.codehaus.groovy 86 | groovy 87 | ${version.org.codehaus.groovy} 88 | 89 | 90 | 91 | com.datastax.cassandra 92 | cassandra-driver-core 93 | ${version.com.datastax.cassandra} 94 | 95 | 96 | 97 | org.testng 98 | testng 99 | ${version.org.testng} 100 | test 101 | 102 | 103 | 104 | ch.qos.logback 105 | logback-classic 106 | ${version.ch.qos.logback} 107 | test 108 | 109 | 110 | 111 | 112 | 113 | 114 | org.codehaus.gmavenplus 115 | gmavenplus-plugin 116 | ${version.maven-gmavenplus-plugin} 117 | 118 | 119 | 120 | addSources 121 | addTestSources 122 | compile 123 | testCompile 124 | 125 | 126 | 127 | 128 | 129 | org.apache.maven.plugins 130 | maven-compiler-plugin 131 | 132 | 133 | org.apache.maven.plugins 134 | maven-source-plugin 135 | ${version.maven-source-plugin} 136 | 137 | 138 | attach-sources 139 | 140 | jar-no-fork 141 | test-jar-no-fork 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | release 152 | 153 | 154 | 155 | org.apache.maven.plugins 156 | maven-release-plugin 157 | ${version.maven-release-plugin} 158 | 159 | 160 | 161 | 162 | 163 | 164 | test.debug 165 | 166 | 167 | test.debug 168 | 169 | 170 | 171 | 172 | 173 | -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8789,server=y,suspend=y 174 | 175 | 176 | 177 | 178 | -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/Bootstrap.groovy: -------------------------------------------------------------------------------- 1 | package org.cassalog.core 2 | 3 | /** 4 | * @author jsanda 5 | */ 6 | class Bootstrap { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/BootstrapException.groovy: -------------------------------------------------------------------------------- 1 | package org.cassalog.core 2 | 3 | /** 4 | * @author jsanda 5 | */ 6 | class BootstrapException extends RuntimeException { 7 | 8 | BootstrapException() { 9 | super() 10 | } 11 | 12 | BootstrapException(String message) { 13 | super(message) 14 | } 15 | 16 | BootstrapException(String message, Throwable cause) { 17 | super(message, cause) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/Cassalog.groovy: -------------------------------------------------------------------------------- 1 | package org.cassalog.core 2 | 3 | /** 4 | * @author jsanda 5 | */ 6 | interface Cassalog { 7 | 8 | void execute(URI script) 9 | 10 | void execute(URI script, Map vars) 11 | 12 | void execute(URI script, Collection tags) 13 | 14 | void execute(URI script, Collection tags, Map vars) 15 | 16 | void execute(URI script, Collection tags, Map vars, ChangeSetListener listener) 17 | 18 | List load(URI script, Collection tags, Map vars) 19 | } -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/CassalogBuilder.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | import com.datastax.driver.core.Session 20 | 21 | /** 22 | * @author jsanda 23 | */ 24 | class CassalogBuilder { 25 | 26 | private String keyspace 27 | 28 | private Session session 29 | 30 | private String baseScriptsPath 31 | 32 | CassalogBuilder withKeyspace(String keyspace) { 33 | this.keyspace = keyspace 34 | return this 35 | } 36 | 37 | CassalogBuilder withSession(Session session) { 38 | this.session = session 39 | return this 40 | } 41 | 42 | CassalogBuilder withBaseScriptsPath(String baseScriptsPath) { 43 | this.baseScriptsPath = baseScriptsPath 44 | return this 45 | } 46 | 47 | Cassalog build() { 48 | return new CassalogImpl(keyspace: keyspace, session: session, baseScriptsPath: baseScriptsPath) 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/CassalogImpl.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | import com.datastax.driver.core.BoundStatement 20 | import com.datastax.driver.core.ConsistencyLevel 21 | import com.datastax.driver.core.PreparedStatement 22 | import com.datastax.driver.core.Session 23 | import com.datastax.driver.core.SimpleStatement 24 | import com.datastax.driver.core.exceptions.InvalidQueryException 25 | import groovy.util.logging.Slf4j 26 | 27 | import java.nio.ByteBuffer 28 | /** 29 | * @author jsanda 30 | */ 31 | @Slf4j 32 | class CassalogImpl implements Cassalog { 33 | 34 | static final String CHANGELOG_TABLE = 'cassalog' 35 | 36 | static final int DEFAULT_BUCKET_SIZE = 50 37 | 38 | static final ChangeSetListener NO_OP_LISTENER = { } 39 | 40 | String keyspace 41 | 42 | Session session 43 | 44 | String baseScriptsPath 45 | 46 | int bucketSize = DEFAULT_BUCKET_SIZE 47 | 48 | PreparedStatement insertSchemaChange 49 | 50 | PreparedStatement updateAppliedAt 51 | 52 | ConsistencyLevel consistencyLevel 53 | 54 | @Override 55 | void execute(URI script) { 56 | execute(script, Collections.emptySet()) 57 | } 58 | 59 | @Override 60 | void execute(URI script, Map vars) { 61 | execute(script, Collections.emptySet(), vars) 62 | } 63 | 64 | @Override 65 | void execute(URI script, Collection tags) { 66 | execute(script, tags, [keyspace: keyspace]) 67 | } 68 | 69 | void execute(URI script, Collection tags, Map vars) { 70 | execute(script, tags, vars, NO_OP_LISTENER) 71 | } 72 | 73 | @Override 74 | void execute(URI script, Collection tags, Map vars, ChangeSetListener listener) { 75 | verifyCassandraVersion() 76 | verifySchemaAgreement() 77 | determineConsistencyLevel() 78 | 79 | log.info("Executing ${[script: script, tags: tags, vars: vars]}") 80 | 81 | def changeSets = [] 82 | def changeLog 83 | 84 | GroovyShell shell = new GroovyShell(createBinding(changeSets, vars)) 85 | shell.evaluate(script) 86 | 87 | if (changeSets[0] instanceof CreateKeyspace) { 88 | keyspace = changeSets[0].name 89 | if (changeSets[0].recreate) { 90 | log.debug("Dropping keyspace $keyspace") 91 | executeCQL("DROP KEYSPACE IF EXISTS $keyspace") 92 | } 93 | if (!keyspaceExists()) { 94 | applyChangeSet(changeSets[0], 0, false, listener) 95 | createChangeLogTableIfNecessary() 96 | initPreparedStatements() 97 | executeCQL(insertSchemaChange.bind(0, 0, changeSets[0].version, changeSets[0].hash, changeSets[0].author, 98 | changeSets[0].description, changeSets[0].tags)) 99 | executeCQL(updateAppliedAt.bind(new Date(), 0, 0)) 100 | } 101 | } 102 | 103 | def bootstrap = { } 104 | if (changeSets[0] instanceof Closure) { 105 | bootstrap = changeSets.remove(0) 106 | } 107 | ensureNoBootstrap(changeSets) 108 | bootstrap() 109 | 110 | if (keyspace == null) { 111 | throw new KeyspaceUndefinedException() 112 | } 113 | 114 | createChangeLogTableIfNecessary() 115 | 116 | changeLog = new ChangeLog(session: session, keyspace: keyspace, bucketSize: bucketSize, 117 | consistencyLevel: consistencyLevel) 118 | changeLog.load() 119 | 120 | initPreparedStatements() 121 | 122 | def tagsFilter = { changeSetTags -> tags.empty || changeSetTags.any { tags.contains(it) } } 123 | 124 | changeSets.findAll {change -> change.tags.empty || tagsFilter(change.tags)}.eachWithIndex { def change, int i -> 125 | change.validate() 126 | 127 | if (change instanceof CreateKeyspace && change.active == true) { 128 | executeCQL("USE $keyspace") 129 | } 130 | 131 | if (change.alwaysRun) { 132 | if (i < changeLog.size) { 133 | validateChange(change, changeLog[i]) 134 | applyChangeSet(change, i, false, listener) 135 | } else { 136 | applyChangeSet(change, i, true, listener) 137 | } 138 | 139 | } else if (i < changeLog.size) { 140 | validateChange(change, changeLog[i]) 141 | if (changeLog[i].appliedAt == null) { 142 | if (change.verifyFunction != null && change.verifyFunction()) { 143 | updateAppliedAt(i, changeLog[i].timestamp) 144 | listener.changeSetApplied(change) 145 | } else { 146 | applyChangeSet(change, i, true, listener) 147 | } 148 | } 149 | } else { 150 | try { 151 | applyChangeSet(change, i, true, listener) 152 | } catch (InvalidQueryException e) { 153 | throw new ChangeSetException(e) 154 | } 155 | } 156 | } 157 | } 158 | 159 | List load(URI script, Collection tags, Map vars) { 160 | def changeSets = [] 161 | GroovyShell shell = new GroovyShell(createBinding(changeSets, vars)) 162 | shell.evaluate(script) 163 | 164 | if (changeSets[0] instanceof Closure) { 165 | changeSets.remove(0) 166 | } 167 | ensureNoBootstrap(changeSets) 168 | 169 | def tagsFilter = { changeSetTags -> tags.empty || changeSetTags.any { tags.contains(it) } } 170 | tagsFilter = { changeSetTags -> tags.empty || changeSetTags.any { tags.contains(it) } } 171 | return changeSets.findAll({ changeSet -> tagsFilter(changeSet.tags) }) 172 | } 173 | 174 | private void ensureNoBootstrap(List changes) { 175 | if (changes.find { it instanceof Closure } != null) { 176 | throw new BootstrapException("The bootstrap function can only be used before any schemaChange function calls") 177 | } 178 | } 179 | 180 | private void validateChange(change, appliedChange) { 181 | if (appliedChange.version != change.version) { 182 | throw new ChangeSetAlteredException("The version [$change.version] for $change does not match the version " + 183 | "[${appliedChange.version}] in the change log") 184 | } 185 | 186 | if (appliedChange.hash != change.hash) { 187 | throw new ChangeSetAlteredException("The hash [${toHex(change.hash)}] for $change does not match the hash " + 188 | "[${toHex(appliedChange.hash)}] in the change log") 189 | } 190 | } 191 | 192 | Binding createBinding(List schemaChanges, Map vars) { 193 | Map scriptVars = new HashMap(vars) 194 | Binding binding = new Binding(scriptVars) 195 | 196 | scriptVars.createKeyspace = { schemaChanges << createKeyspace(it, binding) } 197 | scriptVars.bootstrap = { schemaChanges << bootstrap(it, binding) } 198 | // scriptVars.init = { 199 | // it.resolveStrategy = Closure.DELEGATE_FIRST 200 | // schemaChanges << it 201 | // } 202 | scriptVars.schemaChange = { schemaChanges << createCqlChangeSet(it, binding) } 203 | scriptVars.setKeyspace = { schemaChanges << new SetKeyspace(name: keyspace) } 204 | scriptVars.include = { schemaChanges.addAll(include(it, vars)) } 205 | scriptVars.putAll(createScriptHelperFunctions()) 206 | 207 | 208 | return new Binding(scriptVars) 209 | } 210 | 211 | def createKeyspace(Closure closure, Binding binding) { 212 | closure.delegate = new CreateKeyspace() 213 | def code = closure.rehydrate(closure.delegate, binding, this) 214 | code.resolveStrategy = Closure.DELEGATE_FIRST 215 | 216 | code() 217 | return code.delegate 218 | } 219 | 220 | def bootstrap(Closure closure, Binding binding) { 221 | closure.delegate = new Bootstrap() 222 | closure.resolveStrategy = Closure.DELEGATE_FIRST 223 | return closure 224 | } 225 | 226 | def createCqlChangeSet(Closure closure, Binding binding) { 227 | closure.delegate = new CqlChangeSet() 228 | def code = closure 229 | code.resolveStrategy = Closure.DELEGATE_FIRST 230 | code() 231 | return code.delegate 232 | } 233 | 234 | def include(String script, Map vars) { 235 | def dbchanges = [] 236 | 237 | def scriptVars = new HashMap(vars) 238 | def binding = new Binding(scriptVars) 239 | 240 | scriptVars.createKeyspace = { dbchanges << createKeyspace(it, binding) } 241 | scriptVars.bootstrap = { dbchanges << it } 242 | scriptVars.schemaChange = { dbchanges << createCqlChangeSet(it, binding) } 243 | scriptVars.setKeyspace = { dbchanges << new SetKeyspace(name: keyspace) } 244 | scriptVars.include = { dbchanges.addAll(include(it, vars)) } 245 | scriptVars.putAll(createScriptHelperFunctions()) 246 | 247 | def shell = new GroovyShell(binding) 248 | def scriptURI 249 | if (baseScriptsPath != null && baseScriptsPath != "") { 250 | scriptURI = new File(baseScriptsPath + script).toURI() 251 | } else { 252 | scriptURI = getClass().getResource(script).toURI() 253 | } 254 | shell.evaluate(scriptURI) 255 | 256 | return dbchanges 257 | } 258 | 259 | Map createScriptHelperFunctions() { 260 | def verificationFunctions = new VerificationFunctions(session: session) 261 | return [ 262 | keyspaceExists: { return keyspaceExists(it) }, 263 | isSchemaVersioned: { return isSchemaVersioned(it) }, 264 | getTables: { 265 | def resultSet 266 | def tables = [] 267 | if (cassandraMajorVersion == 2) { 268 | resultSet = executeCQL("SELECT columnfamily_name FROM system.schema_columnfamilies WHERE " + 269 | "keyspace_name = '$it'") 270 | } else { 271 | resultSet = executeCQL("SELECT table_name FROM system_schema.tables WHERE keyspace_name = '$it'") 272 | } 273 | resultSet.all().each { tables << it.getString(0) } 274 | return tables 275 | }, 276 | getUDTs: { 277 | def resultSet 278 | def types = [] 279 | if (cassandraMajorVersion == 2) { 280 | resultSet = executeCQL("SELECT type_name FROM system.schema_usertypes WHERE keyspace_name = '$it'") 281 | } else { 282 | resultSet = executeCQL("SELECT type_name FROM system_schema.types WHERE keyspace_name = '$it'") 283 | } 284 | resultSet.all().each { types << it.getString(0) } 285 | return types 286 | }, 287 | tableExists: verificationFunctions.&tableExists, 288 | tableDoesNotExist: verificationFunctions.&tableDoesNotExist, 289 | columnExists: verificationFunctions.&columnExists, 290 | columnDoesNotExist: verificationFunctions.&columnDoesNotExist, 291 | typeExists: verificationFunctions.&typeExists, 292 | typeDoesNotExist: verificationFunctions.&typeDoesNotExist 293 | ] 294 | } 295 | 296 | boolean keyspaceExists() { 297 | return keyspaceExists(keyspace) 298 | } 299 | 300 | boolean keyspaceExists(String keyspace) { 301 | def resultSet 302 | if (cassandraMajorVersion == 2) { 303 | resultSet = executeCQL( 304 | "SELECT keyspace_name FROM system.schema_keyspaces WHERE keyspace_name = '$keyspace'" 305 | ) 306 | } else { 307 | resultSet = executeCQL( 308 | "SELECT keyspace_name FROM system_schema.keyspaces WHERE keyspace_name = '$keyspace'" 309 | ) 310 | } 311 | return !resultSet.exhausted 312 | } 313 | 314 | def initPreparedStatements() { 315 | if (insertSchemaChange != null) { 316 | return 317 | } 318 | insertSchemaChange = session.prepare(""" 319 | INSERT INTO ${keyspace}.$CHANGELOG_TABLE (bucket, revision, version, hash, author, description, tags) 320 | VALUES (?, ?, ?, ?, ?, ?, ?) 321 | """ 322 | ) 323 | 324 | updateAppliedAt = session.prepare( 325 | "UPDATE ${keyspace}.$CHANGELOG_TABLE SET applied_at = ? WHERE bucket = ? AND revision = ?") 326 | } 327 | 328 | def createChangeLogTableIfNecessary() { 329 | if (!isSchemaVersioned()) { 330 | log.info("Creating change log table ${keyspace}.$CHANGELOG_TABLE") 331 | executeCQL(""" 332 | CREATE TABLE ${keyspace}.$CHANGELOG_TABLE( 333 | bucket int, 334 | revision int, 335 | version text, 336 | applied_at timestamp, 337 | hash blob, 338 | author text, 339 | description text, 340 | tags set, 341 | PRIMARY KEY (bucket, revision) 342 | ) 343 | """) 344 | } 345 | } 346 | 347 | boolean isSchemaVersioned() { 348 | return isSchemaVersioned(keyspace) 349 | } 350 | 351 | boolean isSchemaVersioned(String keyspace) { 352 | def resultSet 353 | if (cassandraMajorVersion == 2) { 354 | resultSet = executeCQL( 355 | "SELECT * FROM system.schema_columnfamilies " + 356 | "WHERE keyspace_name = '$keyspace' AND columnfamily_name = '$CHANGELOG_TABLE'" 357 | ) 358 | } else { 359 | resultSet = executeCQL( 360 | "SELECT * FROM system_schema.tables " + 361 | "WHERE keyspace_name = '$keyspace' AND table_name = '$CHANGELOG_TABLE'" 362 | ) 363 | } 364 | 365 | return !resultSet.exhausted 366 | } 367 | 368 | def applyChangeSet(ChangeSet changeSet, int index, boolean updateChangeLog, ChangeSetListener listener) { 369 | log.info("""Applying ChangeSet 370 | -- version: $changeSet.version 371 | ${changeSet.cql.join('\n')} 372 | --""" 373 | ) 374 | if (updateChangeLog) { 375 | insertChangeSet(changeSet, index) 376 | } 377 | changeSet.cql.each { executeCQL(it) } 378 | if (updateChangeLog) { 379 | updateAppliedAt(index) 380 | } 381 | listener.changeSetApplied(changeSet) 382 | } 383 | 384 | def insertChangeSet(ChangeSet changeSet, int index) { 385 | int bucket = index / bucketSize 386 | executeCQL(insertSchemaChange.bind(bucket, index, changeSet.version, changeSet.hash, changeSet.author, 387 | changeSet.description, changeSet.tags)) 388 | } 389 | 390 | def updateAppliedAt(int index) { 391 | updateAppliedAt(index, new Date()) 392 | } 393 | 394 | def updateAppliedAt(int index, Date timestamp) { 395 | int bucket = index / bucketSize 396 | executeCQL(updateAppliedAt.bind(timestamp, bucket, index)) 397 | } 398 | 399 | def toHex(ByteBuffer buffer) { 400 | return buffer.array().encodeHex().toString() 401 | } 402 | 403 | def executeCQL(String cql) { 404 | def statement = new SimpleStatement(cql) 405 | statement.consistencyLevel = consistencyLevel 406 | return session.execute(statement) 407 | } 408 | 409 | def executeCQL(BoundStatement statement) { 410 | try { 411 | statement.consistencyLevel = consistencyLevel 412 | return session.execute(statement) 413 | } catch (Exception e) { 414 | log.error("CQL error", e) 415 | return null 416 | } 417 | } 418 | 419 | def determineConsistencyLevel() { 420 | def resultSet = session.execute("SELECT peer FROM system.peers") 421 | int clusterSize = resultSet.all().size() 422 | if (clusterSize == 0) { 423 | consistencyLevel = ConsistencyLevel.LOCAL_ONE 424 | } else { 425 | consistencyLevel = ConsistencyLevel.LOCAL_QUORUM 426 | } 427 | } 428 | 429 | /** 430 | * Verifies that all nodes are running the same major version and that the major version is at least 2. 431 | */ 432 | def verifyCassandraVersion() { 433 | def majorVersion = null 434 | session.cluster.metadata.allHosts.each { host -> 435 | if (majorVersion == null) { 436 | // TODO make this null safe 437 | majorVersion = host.cassandraVersion.major 438 | } else { 439 | if (host.cassandraVersion != null && host.cassandraVersion.major != majorVersion) { 440 | throw new CassandraVersionException("All Cassandra nodes should be running the same major version.") 441 | } 442 | } 443 | } 444 | if (majorVersion == 1) { 445 | throw new CassandraVersionException("Cassandra 1.x is not supported.") 446 | } 447 | } 448 | 449 | int getCassandraMajorVersion() { 450 | return session.cluster.metadata.allHosts.find { host -> host.cassandraVersion != null}.cassandraVersion.major 451 | } 452 | 453 | def verifySchemaAgreement() { 454 | if (!session.cluster.metadata.checkSchemaAgreement()) { 455 | throw new SchemaAgreementException() 456 | } 457 | } 458 | 459 | } 460 | -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/CassandraVersionException.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | /** 20 | * @author jsanda 21 | */ 22 | class CassandraVersionException extends RuntimeException { 23 | 24 | CassandraVersionException() { 25 | super() 26 | } 27 | 28 | CassandraVersionException(String message) { 29 | super(message) 30 | } 31 | 32 | CassandraVersionException(String message, Throwable cause) { 33 | super(message, cause) 34 | } 35 | 36 | CassandraVersionException(Throwable cause) { 37 | super(cause) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/ChangeLog.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | import com.datastax.driver.core.ConsistencyLevel 20 | import com.datastax.driver.core.PreparedStatement 21 | import com.datastax.driver.core.Row 22 | import com.datastax.driver.core.Session 23 | import com.datastax.driver.core.SimpleStatement 24 | 25 | import static org.cassalog.core.CassalogImpl.CHANGELOG_TABLE 26 | 27 | /** 28 | * This class provides a view of the change log stored in the database. Change log entries can be read after calling 29 | * the {@link ChangeLog#load() load} method. Creating and Updating the change log table is currently done by 30 | * SchemaManager. 31 | * 32 | * @author jsanda 33 | */ 34 | class ChangeLog { 35 | 36 | Session session 37 | 38 | /** 39 | * The keyspace in which the change log table lives. 40 | */ 41 | String keyspace 42 | 43 | /** 44 | * Specifies the number of ChangeSets per bucket, i.e., the number of rows per partition in the change log table. 45 | */ 46 | int bucketSize 47 | 48 | /** 49 | * The total number of revisions in the change log. 50 | */ 51 | private int numRevisions 52 | 53 | /** 54 | * Each bucket corresponds to a partition in the change log table, and each bucket contains a list of ChangeSets. 55 | */ 56 | def buckets = [] 57 | 58 | ConsistencyLevel consistencyLevel 59 | 60 | private PreparedStatement loadBucket 61 | 62 | private PreparedStatement insertChangeSet 63 | 64 | private PreparedStatement updateAppliedAt 65 | 66 | void load() { 67 | initPreparedStatements() 68 | 69 | def findBuckets = new SimpleStatement("SELECT DISTINCT bucket FROM ${keyspace}.$CHANGELOG_TABLE") 70 | findBuckets.consistencyLevel = consistencyLevel 71 | def bucketResultSet = session.execute(findBuckets) 72 | 73 | if (!bucketResultSet.exhausted) { 74 | int bucket = bucketResultSet.all().max { it.getInt(0) }.getInt(0) 75 | def findRevisions = new SimpleStatement( 76 | "SELECT revision FROM ${keyspace}.$CHANGELOG_TABLE " + 77 | "WHERE bucket = $bucket " + 78 | "ORDER BY revision DESC" 79 | ) 80 | findRevisions.consistencyLevel = consistencyLevel 81 | 82 | def revisionsResultSet = session.execute(findRevisions) 83 | numRevisions = revisionsResultSet.all().first().getInt(0) + 1 84 | } 85 | } 86 | 87 | private void initPreparedStatements() { 88 | loadBucket = session.prepare(""" 89 | SELECT version, applied_at, hash, author, description, tags, writetime(hash) 90 | FROM ${keyspace}.$CHANGELOG_TABLE 91 | WHERE bucket = ? 92 | """ 93 | ) 94 | 95 | insertChangeSet = session.prepare(""" 96 | INSERT INTO ${keyspace}.${CHANGELOG_TABLE} (bucket, revision, version, hash, author, description, tags, 97 | applied_at) 98 | VALUES (?, ?, ?, ?, ?, ?, ?, ?) 99 | """ 100 | ) 101 | 102 | updateAppliedAt = session.prepare( 103 | "UPDATE ${keyspace}.$CHANGELOG_TABLE SET applied_at = ? WHERE bucket = ? AND revision = ?") 104 | } 105 | 106 | int getSize() { 107 | return numRevisions 108 | } 109 | 110 | def getAt(int revision) { 111 | int index = (int) (revision / bucketSize) 112 | if (index >= numRevisions) { 113 | return null 114 | } 115 | if (buckets[index] == null) { 116 | loadBucket(index) 117 | } 118 | def bucket = buckets[index] 119 | int offset = revision % bucketSize 120 | return bucket[offset] 121 | } 122 | 123 | def loadBucket(int bucket) { 124 | def resultSet = session.execute(loadBucket.bind(bucket)) 125 | def changeSets = [] 126 | resultSet.each { changeSets << toChangeSet(it) } 127 | buckets[bucket] = changeSets 128 | } 129 | 130 | def leftShift(ChangeSet changeSet) { 131 | add(changeSet) 132 | } 133 | 134 | def add(ChangeSet changeSet) { 135 | int index = (int) (numRevisions / bucketSize) 136 | if (buckets[index] == null) { 137 | buckets[index] = [] 138 | } 139 | buckets[index] << changeSet 140 | session.execute(insertChangeSet.bind(index, numRevisions++, changeSet.version, changeSet.hash, 141 | changeSet.author, changeSet.description, changeSet.tags, changeSet.appliedAt)) 142 | } 143 | 144 | def toChangeSet(Row row) { 145 | return new ChangeSet( 146 | version: row.getString(0), 147 | appliedAt: row.getTimestamp(1), 148 | hash: row.getBytes(2), 149 | author: row.getString(3), 150 | description: row.getString(4), 151 | tags: row.getSet(5, String), 152 | timestamp: new Date((row.getLong(6) / 1000) as Long) 153 | ) 154 | } 155 | 156 | } 157 | -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/ChangeSet.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | import groovy.transform.EqualsAndHashCode 20 | import groovy.transform.ToString 21 | 22 | import java.nio.ByteBuffer 23 | import java.security.MessageDigest 24 | 25 | /** 26 | * Specifies schema changes to be applied. The changes are in form of CQL statements along with an id and some optional 27 | * meta data. 28 | * 29 | * @author jsanda 30 | */ 31 | @ToString(includeNames = true) 32 | @EqualsAndHashCode(includeFields = true) 33 | class ChangeSet { 34 | 35 | /** 36 | * A user-defined version for the ChangeSet. Version strings should be unique; however, Cassalog does not enforce 37 | * it. It should also be noted that Cassalog does not enforce ordering of changes based on the version. 38 | */ 39 | String version 40 | 41 | /** 42 | * A timestamp of when the schema changes are made. 43 | */ 44 | Date appliedAt 45 | 46 | /** 47 | * A hash of the CQL that gets stored in the database. It is used to detect whether or not the change set has been 48 | * altered since the schema changes were applied. 49 | */ 50 | ByteBuffer hash 51 | 52 | /** 53 | * A user-defined, optional set of tags. This field is persisted but currently not used. In the future there will be 54 | * support for executing change sets based on some tag matching criteria. 55 | */ 56 | Set tags = [] as Set 57 | 58 | /** 59 | * An optional field for specifying who made the change. 60 | */ 61 | String author 62 | 63 | /** 64 | * An optional field for providing a description or some comments about the change. 65 | */ 66 | String description 67 | 68 | /** 69 | * This is Cassandra's internal timestamp of when the row was written. 70 | */ 71 | Date timestamp 72 | 73 | /** 74 | * By default a change set is applied only if it has not already been executed and more precisely not recorded in the 75 | * change log table. There are times when we want to execute a change set even if it has already been applied. A good 76 | * example of this would be for the USE statement. 77 | */ 78 | boolean alwaysRun 79 | 80 | Closure verifyFunction 81 | 82 | ByteBuffer getHash() { 83 | if (hash == null) { 84 | def sha1 = MessageDigest.getInstance("SHA1") 85 | def digest = sha1.digest(cql.join().bytes) 86 | hash = ByteBuffer.wrap(digest) 87 | } 88 | return hash 89 | } 90 | 91 | void version(def version) { 92 | this.version = version as String 93 | } 94 | 95 | void author(def author) { 96 | this.author = author as String 97 | } 98 | 99 | void description(def description) { 100 | this.description = description as String; 101 | } 102 | 103 | void tags(String... tags) { 104 | this.tags = tags as Set 105 | } 106 | 107 | /** 108 | * Perform basic validation to ensure required fields are set. Subclasses should override this to perform 109 | * additional validation. 110 | * 111 | * @throws ChangeSetValidationException if {@link #getVersion()} is null 112 | */ 113 | void validate() { 114 | if (version == null) { 115 | throw new ChangeSetValidationException('The version property must be set') 116 | } 117 | } 118 | 119 | void verify(Closure verifyFunction) { 120 | this.verifyFunction = verifyFunction 121 | } 122 | 123 | } 124 | -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/ChangeSetAlteredException.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | /** 20 | * When a change set has already been applied and is stored in the change log in the database, and the change set is 21 | * subsequently altered, this exception will be thrown. 22 | * 23 | * @author jsanda 24 | */ 25 | class ChangeSetAlteredException extends ChangeSetException { 26 | 27 | def ChangeSetAlteredException(String message) { 28 | super(message) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/ChangeSetException.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | /** 20 | * @author jsanda 21 | */ 22 | class ChangeSetException extends RuntimeException { 23 | 24 | ChangeSetException() { 25 | super() 26 | } 27 | 28 | ChangeSetException(String message) { 29 | super(message) 30 | } 31 | 32 | ChangeSetException(String message, Throwable cause) { 33 | super(message, cause) 34 | } 35 | 36 | ChangeSetException(Throwable cause) { 37 | super(cause) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/ChangeSetListener.groovy: -------------------------------------------------------------------------------- 1 | package org.cassalog.core 2 | 3 | /** 4 | * @author jsanda 5 | */ 6 | interface ChangeSetListener { 7 | 8 | void changeSetApplied(ChangeSet changeSet) 9 | 10 | } -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/ChangeSetValidationException.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | /** 20 | * Thrown when a change set fails basic validation checks such as failing to set required fields. 21 | * @author jsanda 22 | */ 23 | class ChangeSetValidationException extends ChangeSetException { 24 | 25 | def ChangeSetValidationException(String message) { 26 | super(message) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/CqlChangeSet.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | import groovy.transform.EqualsAndHashCode 20 | import groovy.transform.ToString 21 | 22 | /** 23 | * @author jsanda 24 | */ 25 | @ToString(includeNames = true, includeSuper = true) 26 | @EqualsAndHashCode(includeFields = true, callSuper = true) 27 | class CqlChangeSet extends ChangeSet { 28 | 29 | /** 30 | * The CQL to execute. It is not stored in the database. 31 | */ 32 | def cql = [] 33 | 34 | void cql(def cql) { 35 | this.cql << cql as String; 36 | } 37 | 38 | void cql(List cql) { 39 | this.cql.addAll(cql*.toString()) 40 | } 41 | 42 | void validate() { 43 | super.validate() 44 | if (cql.empty) { 45 | throw new ChangeSetValidationException('The cql property must be set') 46 | } 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/CreateKeyspace.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | import groovy.transform.EqualsAndHashCode 20 | import groovy.transform.ToString 21 | 22 | /** 23 | * @author jsanda 24 | */ 25 | @ToString(includeNames = true, includeSuper = true) 26 | @EqualsAndHashCode(includeFields = true, callSuper = true) 27 | class CreateKeyspace extends ChangeSet { 28 | 29 | /** 30 | * The keyspace name 31 | */ 32 | String name 33 | 34 | /** 35 | * If set to true, Cassalog will set this as the active keyspace when applying changes. True by default. 36 | */ 37 | boolean active = true 38 | 39 | boolean durableWrites 40 | 41 | /** 42 | * If set to true, Cassalog will drop the keyspace if it exists before creating it. This behavior only applies to 43 | * the initial change set. This setting can be useful in an environment for running automated tests in which you 44 | * want to start with a new schema for each test run. False by default. 45 | */ 46 | boolean recreate 47 | 48 | ReplicationSettings replication = new ReplicationSettings() 49 | 50 | List getCql() { 51 | def cql = """ 52 | CREATE KEYSPACE $name WITH replication = { 'class': '${replication.strategy}', 'replication_factor': ${replication.replicationFactor} } 53 | """ 54 | return [cql] 55 | } 56 | 57 | void name(def name) { 58 | this.name = name as String 59 | } 60 | 61 | void active(boolean use) { 62 | this.active = use 63 | } 64 | 65 | void recreate(boolean recreate) { 66 | this.recreate = recreate 67 | } 68 | 69 | void durable_writes(boolean durableWrites) { 70 | this.durableWrites = durableWrites 71 | } 72 | 73 | void replication(Closure closure) { 74 | closure.delegate = replication 75 | closure.resolveStrategy = Closure.OWNER_FIRST 76 | closure() 77 | } 78 | 79 | void validate() { 80 | super.validate() 81 | if (name == null) { 82 | throw new ChangeSetValidationException('The name property must be set') 83 | } 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/KeyspaceUndefinedException.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | /** 20 | * Thrown when trying to run Cassalog without having set the keyspace. There are two ways in which the keyspace can be 21 | * set. First is by setting the keyspace property on the Cassalog instance. Second is by using the createKeyspace 22 | * function as the first function in a script. When the createKeyspace function is the first function used in a script, 23 | * Cassalog will used the specified keyspace for the script execution. That is, it will look for or create the change 24 | * log table in that script. 25 | * 26 | * @author jsanda 27 | */ 28 | class KeyspaceUndefinedException extends RuntimeException { 29 | 30 | KeyspaceUndefinedException() { 31 | super() 32 | } 33 | 34 | KeyspaceUndefinedException(String message) { 35 | super(message) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/ReplicationSettings.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | /** 20 | * @author jsanda 21 | */ 22 | class ReplicationSettings { 23 | 24 | String strategy = 'SimpleStrategy' 25 | 26 | int replicationFactor = 1 27 | 28 | void strategy(def strategy) { 29 | this.strategy = strategy as String 30 | } 31 | 32 | void replication_factor(int replicationFactor) { 33 | this.replicationFactor = replicationFactor 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/SchemaAgreementException.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | /** 20 | * @author jsanda 21 | */ 22 | class SchemaAgreementException extends RuntimeException { 23 | 24 | SchemaAgreementException() { 25 | super() 26 | } 27 | 28 | SchemaAgreementException(String message) { 29 | super(message) 30 | } 31 | 32 | SchemaAgreementException(String message, Throwable cause) { 33 | super(message, cause) 34 | } 35 | 36 | SchemaAgreementException(Throwable cause) { 37 | super(cause) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/SetKeyspace.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | /** 20 | * @author jsanda 21 | */ 22 | class SetKeyspace extends ChangeSet { 23 | 24 | /** 25 | * The keyspace name 26 | */ 27 | String name 28 | 29 | SetKeyspace() { 30 | version = 'set-keyspace' 31 | alwaysRun = true 32 | } 33 | 34 | void name(def name) { 35 | this.name = name as String 36 | } 37 | 38 | List getCql() { 39 | return ["USE $name"] 40 | } 41 | 42 | void validate() { 43 | super.validate() 44 | if (name == null) { 45 | throw new ChangeSetValidationException('The name property must be set') 46 | } 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/groovy/org/cassalog/core/VerificationFunctions.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | import com.datastax.driver.core.Session 20 | 21 | /** 22 | * @author jsanda 23 | */ 24 | class VerificationFunctions { 25 | 26 | Session session 27 | 28 | def getKeyspaceMetaData(String keyspace) { 29 | return session.cluster.metadata.keyspaces.find { it.name == keyspace } 30 | } 31 | 32 | boolean tableExists(String keyspace, String table) { 33 | return getKeyspaceMetaData(keyspace)?.tables?.find { it.name == table } != null 34 | } 35 | 36 | boolean tableDoesNotExist(String keyspace, String table) { 37 | return !tableExists(keyspace, table) 38 | } 39 | 40 | boolean columnExists(String keyspace, String table, String column) { 41 | def tableMetaData = getKeyspaceMetaData(keyspace)?.tables?.find { it.name == table } 42 | return tableMetaData?.columns?.find { it.name == column } != null 43 | } 44 | 45 | boolean columnDoesNotExist(String keyspace, String table, String column) { 46 | return !columnExists(keyspace, table, column) 47 | } 48 | 49 | boolean typeExists(String keyspace, String type) { 50 | def keyspaceMetaData = getKeyspaceMetaData(keyspace) 51 | return keyspaceMetaData?.getUserType(type) != null 52 | } 53 | 54 | boolean typeDoesNotExist(String keyspace, String type) { 55 | return !typeExists(keyspace, type) 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/test/groovy/org/cassalog/core/BootstrapTest.groovy: -------------------------------------------------------------------------------- 1 | package org.cassalog.core 2 | 3 | import org.testng.annotations.Test 4 | 5 | import static org.testng.Assert.assertEquals 6 | import static org.testng.Assert.expectThrows 7 | 8 | /** 9 | * @author jsanda 10 | */ 11 | class BootstrapTest extends CassalogBaseTest { 12 | 13 | @Test 14 | void applyChangesWithBootstrap() { 15 | keyspace = 'bootstrap_test' 16 | 17 | resetSchema(keyspace) 18 | 19 | def script = getClass().getResource('/bootstrap/bootstrap_test.groovy').toURI() 20 | 21 | CassalogImpl cassalog = new CassalogImpl(keyspace: keyspace, session: session) 22 | cassalog.execute(script, [keyspace: keyspace, session: session]) 23 | } 24 | 25 | @Test 26 | void applyIncludedChangesWithBootstrap() { 27 | keyspace = 'bootstrap_test' 28 | 29 | resetSchema(keyspace) 30 | 31 | def script = getClass().getResource('/bootstrap/bootstrap_include.groovy').toURI() 32 | 33 | CassalogImpl cassalog = new CassalogImpl(keyspace: keyspace, session: session) 34 | cassalog.execute(script, [keyspace: keyspace, session: session]) 35 | } 36 | 37 | @Test 38 | void loadChangeSetsWithBootstrap() { 39 | keyspace = 'bootstrap_test' 40 | 41 | resetSchema(keyspace) 42 | 43 | def script = getClass().getResource('/bootstrap/bootstrap_test.groovy').toURI() 44 | 45 | CassalogImpl cassalog = new CassalogImpl(keyspace: keyspace, session: session) 46 | 47 | assertEquals(cassalog.load(script, [], [keyspace: keyspace, session: session]).size(), 2) 48 | } 49 | 50 | @Test(expectedExceptions = BootstrapException) 51 | void bootstrapCanOnlyBeUsedBeforeSchemaChanges() { 52 | keyspace = 'bootstrap_test' 53 | 54 | resetSchema(keyspace) 55 | 56 | def script = getClass().getResource('/bootstrap/bootstrap_fail.groovy').toURI() 57 | 58 | CassalogImpl cassalog = new CassalogImpl(keyspace: keyspace, session: session) 59 | cassalog.execute(script, [keyspace: keyspace, session: session]) 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/test/groovy/org/cassalog/core/CassalogBaseTest.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | import com.datastax.driver.core.Cluster 19 | import com.datastax.driver.core.KeyspaceMetadata 20 | import com.datastax.driver.core.PreparedStatement 21 | import com.datastax.driver.core.QueryOptions 22 | import com.datastax.driver.core.Row 23 | import com.datastax.driver.core.Session 24 | import org.testng.annotations.AfterTest 25 | import org.testng.annotations.BeforeSuite 26 | 27 | import static org.testng.Assert.* 28 | /** 29 | * @author jsanda 30 | */ 31 | class CassalogBaseTest { 32 | static String keyspace 33 | 34 | static Session session 35 | 36 | static PreparedStatement findTableName 37 | 38 | static Cluster cluster 39 | 40 | static VerificationFunctions verificationFunctions 41 | 42 | @BeforeSuite 43 | static void initTest() { 44 | cluster = new Cluster.Builder() 45 | .addContactPoint('127.0.0.1') 46 | .withQueryOptions(new QueryOptions(refreshSchemaIntervalMillis: 0)) 47 | .build() 48 | session = cluster.connect() 49 | // findTableName = session.prepare( 50 | // "SELECT columnfamily_name FROM system.schema_columnfamilies " + 51 | // "WHERE keyspace_name = ? AND columnfamily_name = ?" 52 | // ) 53 | findTableName = session.prepare( 54 | "SELECT table_name FROM system_schema.tables " + 55 | "WHERE keyspace_name = ? AND table_name = ?" 56 | ) 57 | verificationFunctions = new VerificationFunctions(session: session) 58 | } 59 | 60 | @AfterTest 61 | void cleanupKeyspace() { 62 | resetSchema(keyspace) 63 | } 64 | 65 | static Optional findKeyspace(String keyspace) { 66 | cluster.metadata.keyspaces.stream().filter {filter -> filter.name == keyspace}.findFirst() 67 | } 68 | 69 | static void resetSchema(String keyspace) { 70 | session.execute("DROP KEYSPACE IF EXISTS $keyspace") 71 | session.execute( 72 | "CREATE KEYSPACE $keyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}") 73 | } 74 | 75 | static def findChangeSets(keyspace, bucket) { 76 | def resultSet = session.execute( 77 | "SELECT version, hash, applied_at, author, description, tags FROM ${keyspace}.$CassalogImpl.CHANGELOG_TABLE " + 78 | "WHERE bucket = $bucket" 79 | ) 80 | return resultSet.all() 81 | } 82 | 83 | static void assertChangeSetEquals(Row actual, ChangeSet expected) { 84 | assertEquals(actual.getString(0), expected.version) 85 | assertNotNull(actual.getBytes(1)) 86 | assertNotNull(actual.getTimestamp(2)) 87 | assertEquals(actual.getString(3), expected.author) 88 | assertEquals(actual.getString(4), expected.description) 89 | assertEquals(actual.getSet(5, String), expected.tags) 90 | } 91 | 92 | static void assertTableExists(String keyspace, String table) { 93 | def resultSet = session.execute(findTableName.bind(keyspace, table)) 94 | assertFalse(resultSet.exhausted, "The table ${keyspace}.$table does not exist") 95 | } 96 | 97 | static void assertTableDoesNotExist(String keyspace, String table) { 98 | def resultSet = session.execute(findTableName.bind(keyspace, table)) 99 | assertTrue(resultSet.exhausted, "The table ${keyspace}.$table exists") 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /src/test/groovy/org/cassalog/core/CassalogTest.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | import org.testng.annotations.Test 20 | 21 | import static org.testng.Assert.* 22 | 23 | /** 24 | * @author jsanda 25 | */ 26 | class CassalogTest extends CassalogBaseTest { 27 | 28 | @Test 29 | void applySingleChangeToExistingKeyspace() { 30 | keyspace = 'single_change' 31 | 32 | resetSchema(keyspace) 33 | 34 | URI script = getClass().getResource('/single_change/schema.groovy').toURI() 35 | 36 | CassalogImpl casslog = new CassalogImpl(keyspace: keyspace, session: session) 37 | casslog.execute(script) 38 | 39 | assertTableExists(keyspace, 'test1') 40 | 41 | def rows = findChangeSets(keyspace, 0) 42 | 43 | assertEquals(rows.size(), 1, "Expected to find one row in $CassalogImpl.CHANGELOG_TABLE") 44 | assertEquals(rows[0].getString(0), 'first-table') 45 | assertNotNull(rows[0].getBytes(1)) 46 | assertEquals(rows[0].getString(3), 'admin') 47 | assertEquals(rows[0].getString(4), 'test') 48 | assertTrue(rows[0].getSet(5, String.class).empty) 49 | 50 | Date appliedAt = rows[0].getTimestamp(2) 51 | assertNotNull(appliedAt) 52 | 53 | // Now running again should be a no op 54 | casslog.execute(script) 55 | 56 | rows = findChangeSets(keyspace, 0) 57 | assertEquals(rows.size(), 1, "Expected to find one row in $CassalogImpl.CHANGELOG_TABLE") 58 | assertEquals(rows[0].getTimestamp(2), appliedAt) 59 | } 60 | 61 | @Test 62 | void executeScriptThatCreatesKeyspace() { 63 | def keyspace = 'cassalog_create_ks' 64 | def change1Id = 'create-cassalog_dev' 65 | def change2Id = 'first-table' 66 | 67 | session.execute("DROP KEYSPACE IF EXISTS $keyspace") 68 | 69 | def script = getClass().getResource('/create_keyspace/script1.groovy').toURI() 70 | 71 | CassalogImpl cassalog = new CassalogImpl(session: session) 72 | cassalog.execute(script, [keyspace: keyspace, id1: change1Id]) 73 | 74 | 75 | def updatedScript = getClass().getResource('/create_keyspace/script2.groovy').toURI() 76 | cassalog.execute(updatedScript, [keyspace: keyspace, id1: change1Id, id2: change2Id]) 77 | 78 | assertTableExists(keyspace, 'test1') 79 | 80 | def rows = findChangeSets(keyspace, 0) 81 | assertEquals(rows.size(), 2) 82 | assertChangeSetEquals(rows[0], new ChangeSet(version: 'create-cassalog_dev', author: 'admin', 83 | description: 'create keyspace test')) 84 | assertChangeSetEquals(rows[1], new ChangeSet(version: 'first-table', author: 'admin', description: 'test')) 85 | } 86 | 87 | @Test 88 | void createAndUseKeyspace() { 89 | def keyspace = 'cassalog_create_use_ks' 90 | def change1Id = 'keyspsace-test' 91 | def change2Id = 'table-test' 92 | 93 | session.execute("DROP KEYSPACE IF EXISTS $keyspace") 94 | 95 | def script = getClass().getResource('/create_keyspace/script3.groovy').toURI() 96 | 97 | def cassalog = new CassalogImpl(session: session) 98 | cassalog.execute(script, [keyspace: keyspace, change1Id: change1Id, change2Id: change2Id]) 99 | 100 | assertTableExists(keyspace, 'test1') 101 | 102 | def rows = findChangeSets(keyspace, 0) 103 | assertEquals(rows.size(), 2) 104 | assertChangeSetEquals(rows[0], new ChangeSet(version: change1Id, author: 'admin', description: 'create keyspace test')) 105 | assertChangeSetEquals(rows[1], new ChangeSet(version: change2Id, author: 'admin', description: 'test')) 106 | } 107 | 108 | @Test 109 | void createKeyspaceWithParameters() { 110 | def keyspace = 'cassalog_create_use_ks' 111 | 112 | session.execute("DROP KEYSPACE IF EXISTS $keyspace") 113 | 114 | def script = getClass().getResource('/create_keyspace/script6.groovy').toURI() 115 | 116 | def cassalog = new CassalogImpl(session: session) 117 | def replicationFactor = 3 118 | cassalog.execute(script, [keyspace: keyspace, replicationFactor: replicationFactor]) 119 | 120 | def theKeyspace = findKeyspace(keyspace) 121 | assertTrue(theKeyspace.isPresent()) 122 | assertEquals(theKeyspace.get().replication["replication_factor"].toInteger(), replicationFactor) 123 | 124 | def rows = findChangeSets(keyspace, 0) 125 | assertEquals(rows.size(), 1) 126 | assertChangeSetEquals(rows[0], new ChangeSet(version: '1', author: 'admin', 127 | description: 'create keyspace test')) 128 | } 129 | 130 | @Test(expectedExceptions = [ChangeSetException]) 131 | void createAndDoNotUseKeyspace() { 132 | def keyspace = 'cassalog_do_not_use_ks' 133 | def change1Id = 'keyspsace-test' 134 | def change2Id = 'table-test' 135 | 136 | session.execute("DROP KEYSPACE IF EXISTS $keyspace") 137 | 138 | def script = getClass().getResource('/create_keyspace/script4.groovy').toURI() 139 | 140 | def cassalog = new CassalogImpl(session: session) 141 | cassalog.execute(script, [keyspace: keyspace, change1Id: change1Id, change2Id: change2Id]) 142 | } 143 | 144 | @Test 145 | void recreateKeyspace() { 146 | def keyspace = 'cassalog_recreate_ks' 147 | def id1 = 'recreate-test' 148 | 149 | def script = getClass().getResource('/create_keyspace/script5.groovy').toURI() 150 | 151 | // def cluster2 = new Cluster.Builder().addContactPoint('127.0.0.1').build() 152 | // def session2 = cluster2.connect() 153 | 154 | def cassalog = new CassalogImpl(session: session) 155 | cassalog.execute(script, [keyspace: keyspace, id1: id1]) 156 | 157 | def rows = findChangeSets(keyspace, 0) 158 | assertEquals(rows.size(), 1) 159 | 160 | Date dateApplied = rows[0].getTimestamp(2) 161 | 162 | // We will rerun the script and since the recreate attribute is set to true, Cassalog should drop the keyspace 163 | // and recreate it. This of course will result in the change log table being recreated. We should still only 164 | // have a single row in the change log. The timestamp should be later though. 165 | cassalog.execute(script, [keyspace: keyspace, id1: id1]) 166 | 167 | rows = findChangeSets(keyspace, 0) 168 | assertEquals(rows.size(), 1) 169 | 170 | assertTrue(dateApplied < rows[0].getTimestamp(2)) 171 | } 172 | 173 | @Test 174 | void appendChangesToExistingScript() { 175 | keyspace = 'append_changes' 176 | 177 | resetSchema(keyspace) 178 | 179 | def script1 = getClass().getResource("/append_changes/script1.groovy").toURI() 180 | def script2 = getClass().getResource("/append_changes/script2.groovy").toURI() 181 | 182 | CassalogImpl cassalog = new CassalogImpl(keyspace: keyspace, session: session) 183 | 184 | cassalog.execute(script1, [keyspace: keyspace]) 185 | cassalog.execute(script2, [keyspace: keyspace]) 186 | 187 | assertTableExists(keyspace, 'test2') 188 | 189 | def resultSet = session.execute( 190 | "SELECT version, hash, applied_at, author, description, tags FROM ${keyspace}.$CassalogImpl.CHANGELOG_TABLE " + 191 | "WHERE bucket = 0" 192 | ) 193 | def rows = resultSet.all() 194 | 195 | assertEquals(rows.size(), 2, "Expected to find two rows in $CassalogImpl.CHANGELOG_TABLE") 196 | assertEquals(rows[0].getString(0), 'first-table') 197 | assertEquals(rows[1].getString(0), 'second-table') 198 | assertNotNull(rows[1].getBytes(1)) 199 | assertEquals(rows[1].getString(3), 'admin') 200 | assertEquals(rows[1].getString(4), 'second table test') 201 | assertEquals(rows[1].getSet(5, String.class), ['red', 'blue'] as Set) 202 | } 203 | 204 | @Test 205 | void applyChangesAcrossMultipleBuckets() { 206 | keyspace = 'multiple_buckets' 207 | 208 | resetSchema(keyspace) 209 | 210 | def script1 = getClass().getResource('/multiple_buckets/script1.groovy').toURI() 211 | def script2 = getClass().getResource('/multiple_buckets/script2.groovy').toURI() 212 | 213 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session, bucketSize: 2) 214 | cassalog.execute(script1, [keyspace: keyspace]) 215 | cassalog.execute(script2) 216 | 217 | assertTableExists(keyspace, 'test3') 218 | 219 | // Now let's rerun the schema change script to make sure it is a no-op. Doing this will verifyFunction that we are searching 220 | // across buckets for changes that have already been applied. 221 | cassalog.execute(script2, [keyspace: keyspace]) 222 | } 223 | 224 | @Test(expectedExceptions = ChangeSetAlteredException) 225 | void modifyingAppliedChangeSetShouldFail() { 226 | keyspace = 'fail_modified_changeset' 227 | 228 | resetSchema(keyspace) 229 | 230 | def script = getClass().getResource('/fail_modified_changeset/script1.groovy').toURI() 231 | def modifiedScript = getClass().getResource('/fail_modified_changeset/script2.groovy').toURI() 232 | 233 | CassalogImpl cassalog = new CassalogImpl(keyspace: keyspace, session: session) 234 | cassalog.execute(script, [keyspace: keyspace]) 235 | cassalog.execute(modifiedScript, [keyspace: keyspace]) 236 | 237 | assertTableDoesNotExist(keyspace, 'test2') 238 | 239 | def rows = findChangeSets(keyspace, 0) 240 | 241 | assertEquals(rows.size(), 1) 242 | assertEquals(rows[0].getString(0), 'first-table') 243 | } 244 | 245 | @Test(dependsOnMethods = 'modifyingAppliedChangeSetShouldFail') 246 | void abortAfterFailure() { 247 | keyspace = 'fail_modified_changeset' 248 | 249 | assertTableDoesNotExist(keyspace, 'test3') 250 | } 251 | 252 | @Test(expectedExceptions = ChangeSetAlteredException) 253 | void changingIdOfAppliedChangeSetShouldFail() { 254 | keyspace = 'change_id' 255 | 256 | resetSchema(keyspace) 257 | 258 | def script = getClass().getResource('/change_id/script1.groovy').toURI() 259 | def modifiedScript = getClass().getResource('/change_id/script2.groovy').toURI() 260 | 261 | def casslog = new CassalogImpl(keyspace: keyspace, session: session) 262 | casslog.execute(script, [keyspace: keyspace]) 263 | casslog.execute(modifiedScript, [keyspace: keyspace]) 264 | } 265 | 266 | @Test 267 | void setUpBasicValidation() { 268 | resetSchema('basic_validation') 269 | } 270 | 271 | @Test(expectedExceptions = ChangeSetValidationException, dependsOnMethods = 'setUpBasicValidation') 272 | void versionIsRequired() { 273 | keyspace = 'basic_validation' 274 | 275 | def script = getClass().getResource('/basic_validation/script1.groovy').toURI() 276 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session) 277 | cassalog.execute(script, [keyspace: keyspace]) 278 | } 279 | 280 | @Test(expectedExceptions = ChangeSetValidationException, dependsOnMethods = 'setUpBasicValidation') 281 | void cqlIsRequired() { 282 | keyspace = 'basic_validation' 283 | 284 | def script = getClass().getResource('/basic_validation/script2.groovy').toURI() 285 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session) 286 | cassalog.execute(script, [keyspace: keyspace]) 287 | } 288 | 289 | @Test 290 | void applyChangesWithTag() { 291 | keyspace = 'tags_test' 292 | resetSchema(keyspace) 293 | 294 | def script = getClass().getResource('/tags_test/script1.groovy').toURI() 295 | def appliedChanges = [] 296 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session) 297 | cassalog.execute(script, ['dev'], [keyspace: keyspace], { appliedChanges << it }) 298 | 299 | assertEquals(appliedChanges.size(), 2) 300 | assertEquals(appliedChanges[0].version, 'table-1') 301 | assertEquals(appliedChanges[1].version, 'dev-data') 302 | 303 | def changeLogRows = findChangeSets(keyspace, 0) 304 | 305 | def verifyChangeLog = { rows -> 306 | assertEquals(rows.size(), 2) 307 | assertChangeSetEquals(rows[0], new ChangeSet(version: 'table-1', author: 'admin', tags: ['dev', 'stage'])) 308 | assertChangeSetEquals(rows[1], new ChangeSet(version: 'dev-data', author: 'admin', tags: ['dev'])) 309 | } 310 | verifyChangeLog(changeLogRows) 311 | 312 | cassalog.execute(script, ['dev'], [keyspace: keyspace]) 313 | 314 | // Make sure that the change log has not been altered 315 | changeLogRows = findChangeSets(keyspace, 0) 316 | verifyChangeLog(changeLogRows) 317 | } 318 | 319 | @Test 320 | void resumeCreateTableWhenChangeWasAlreadyApplied() { 321 | keyspace = 'resume_create_table_applied' 322 | resetSchema(keyspace) 323 | 324 | String cql = "CREATE TABLE ${keyspace}.test1 (x int, y text, PRIMARY KEY (x))" 325 | 326 | def script = getClass().getResource('/resume_updates/create_tables.groovy').toURI() 327 | 328 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session) 329 | cassalog.createChangeLogTableIfNecessary() 330 | cassalog.initPreparedStatements() 331 | 332 | def changeSet = new CqlChangeSet(cql: [cql], version: '1.0') 333 | cassalog.insertChangeSet(changeSet, 0) 334 | 335 | cassalog.execute(script, [keyspace: keyspace]) 336 | 337 | assertTableExists(keyspace, 'test1') 338 | assertTableExists(keyspace, 'test2') 339 | } 340 | 341 | @Test 342 | void resumeCreateTableWhenChangeHasNotBeenApplied() { 343 | keyspace = 'resume_create_table_not_applied' 344 | resetSchema(keyspace) 345 | 346 | String cql = "CREATE TABLE ${keyspace}.test1 (x int, y text, PRIMARY KEY (x))" 347 | 348 | def script = getClass().getResource('/resume_updates/create_tables.groovy').toURI() 349 | 350 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session) 351 | cassalog.createChangeLogTableIfNecessary() 352 | cassalog.initPreparedStatements() 353 | 354 | def changeSet = new CqlChangeSet(cql: [cql], version: '1.0') 355 | cassalog.insertChangeSet(changeSet, 0) 356 | 357 | cassalog.execute(script, [keyspace: keyspace]) 358 | 359 | assertTableExists(keyspace, 'test1') 360 | assertTableExists(keyspace, 'test2') 361 | } 362 | 363 | @Test 364 | void resumeAddColumnWhenChangeWasAlreadyApplied() { 365 | keyspace = 'resume_add_column_applied' 366 | resetSchema(keyspace) 367 | 368 | session.execute("CREATE TABLE ${keyspace}.test (x int, y text, PRIMARY KEY (x))") 369 | 370 | def script = getClass().getResource('/resume_updates/columns.groovy').toURI() 371 | 372 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session) 373 | cassalog.createChangeLogTableIfNecessary() 374 | cassalog.initPreparedStatements() 375 | 376 | def addColumn = new CqlChangeSet(cql: ["ALTER TABLE ${keyspace}.test ADD z text"], version: '1.0') 377 | 378 | cassalog.insertChangeSet(addColumn, 0) 379 | 380 | cassalog.execute(script, [keyspace: keyspace]) 381 | 382 | // No assertions necessary, just want to make sure cassalog runs without error 383 | } 384 | 385 | @Test 386 | void resumeAddColumnWhenChangeHasNotBeenApplied() { 387 | keyspace = 'resume_add_column_not_applied' 388 | resetSchema(keyspace) 389 | 390 | session.execute("CREATE TABLE ${keyspace}.test (x int, y text, PRIMARY KEY (x))") 391 | 392 | def script = getClass().getResource('/resume_updates/columns.groovy').toURI() 393 | 394 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session) 395 | cassalog.createChangeLogTableIfNecessary() 396 | cassalog.initPreparedStatements() 397 | 398 | def addColumn = new CqlChangeSet(cql: ["ALTER TABLE ${keyspace}.test ADD z text"], version: '1.0') 399 | 400 | cassalog.insertChangeSet(addColumn, 0) 401 | cassalog.execute(script, [keyspace: keyspace]) 402 | 403 | assertTrue(verificationFunctions.columnExists(keyspace, 'test', 'z')) 404 | } 405 | 406 | @Test 407 | void resumeDropColumnWhenChangeHasBeenApplied() { 408 | keyspace = 'resume_drop_column_applied' 409 | resetSchema(keyspace) 410 | 411 | session.execute("CREATE TABLE ${keyspace}.test (x int, y text, PRIMARY KEY (x))") 412 | 413 | def script = getClass().getResource('/resume_updates/columns.groovy').toURI() 414 | 415 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session) 416 | cassalog.createChangeLogTableIfNecessary() 417 | cassalog.initPreparedStatements() 418 | 419 | def addColumn = new CqlChangeSet(cql: ["ALTER TABLE ${keyspace}.test ADD z text"], version: '1.0') 420 | def dropColumn = new CqlChangeSet(cql: ["ALTER TABLE ${keyspace}.test DROP y"], version: '2.0') 421 | 422 | cassalog.applyChangeSet(addColumn, 0, true, CassalogImpl.NO_OP_LISTENER) 423 | cassalog.insertChangeSet(dropColumn, 1) 424 | cassalog.execute(script, [keyspace: keyspace]) 425 | 426 | assertTrue(verificationFunctions.columnDoesNotExist(keyspace, 'test', 'y')) 427 | } 428 | 429 | @Test 430 | void resumeInsertRowWhenChangeHasNotBeenApplied() { 431 | keyspace = 'resume_insert_row_not_applied' 432 | resetSchema(keyspace) 433 | 434 | def script = getClass().getResource('/resume_updates/insert.groovy').toURI() 435 | 436 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session) 437 | cassalog.createChangeLogTableIfNecessary() 438 | cassalog.initPreparedStatements() 439 | 440 | def createTable = new CqlChangeSet(cql: ["CREATE TABLE ${keyspace}.test (x int PRIMARY KEY, y int)"], 441 | version: '1.0') 442 | cassalog.applyChangeSet(createTable, 0, true, CassalogImpl.NO_OP_LISTENER) 443 | def insert = new CqlChangeSet(cql: ["INSERT INTO ${keyspace}.test (x, y) VALUES (1, 2)"], version: '2.0') 444 | cassalog.insertChangeSet(insert, 1) 445 | cassalog.execute(script, [keyspace: keyspace]) 446 | 447 | def resultSet = session.execute("select y from ${keyspace}.test where x = 1") 448 | def rows = resultSet.all() 449 | assertEquals(rows.size(), 1) 450 | assertEquals(2 as Integer, rows[0].getInt(0)) 451 | } 452 | } 453 | -------------------------------------------------------------------------------- /src/test/groovy/org/cassalog/core/IncludeTest.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | import org.testng.annotations.Test 20 | 21 | import static org.testng.Assert.assertEquals 22 | 23 | /** 24 | * @author jsanda 25 | */ 26 | class IncludeTest extends CassalogBaseTest { 27 | 28 | @Test 29 | void includeScriptAfterChangesClasspath() { 30 | keyspace = 'include_after' 31 | resetSchema(keyspace) 32 | 33 | def script = getClass().getResource('/include/script1.groovy').toURI() 34 | 35 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session) 36 | cassalog.execute(script, [keyspace: keyspace]) 37 | 38 | assertTableExists(keyspace, 'test1') 39 | assertTableExists(keyspace, 'test2') 40 | 41 | def rows = findChangeSets(keyspace, 0) 42 | assertEquals(rows.size(), 2) 43 | assertChangeSetEquals(rows[0], new ChangeSet(version: 'first-table')) 44 | assertChangeSetEquals(rows[1], new ChangeSet(version: 'second-table')) 45 | } 46 | 47 | @Test 48 | void includeScriptBeforeChangesClasspath() { 49 | keyspace = 'include_before' 50 | resetSchema(keyspace) 51 | 52 | def script = getClass().getResource('/include/script3.groovy').toURI() 53 | 54 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session) 55 | cassalog.execute(script, [keyspace: keyspace]) 56 | 57 | def rows = findChangeSets(keyspace, 0) 58 | assertEquals(rows.size(), 2) 59 | assertChangeSetEquals(rows[0], new ChangeSet(version: 'second-table')) 60 | assertChangeSetEquals(rows[1], new ChangeSet(version: 'third-table')) 61 | } 62 | 63 | @Test 64 | void multipleIncludesClasspath() { 65 | keyspace = 'multiple_includes' 66 | resetSchema(keyspace) 67 | 68 | def script = getClass().getResource('/include/multiple_includes.groovy').toURI() 69 | 70 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session) 71 | cassalog.execute(script, [keyspace: keyspace]) 72 | 73 | def rows = findChangeSets(keyspace, 0) 74 | assertEquals(rows.size(), 2) 75 | assertChangeSetEquals(rows[0], new ChangeSet(version: 'second-table')) 76 | assertChangeSetEquals(rows[1], new ChangeSet(version: 'fourth-table')) 77 | } 78 | 79 | @Test 80 | void nestedIncludesClasspath() { 81 | keyspace = 'nested_includes' 82 | resetSchema(keyspace) 83 | 84 | def script = getClass().getResource('/include/nested.groovy').toURI() 85 | 86 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session) 87 | cassalog.execute(script, [keyspace: keyspace]) 88 | 89 | def rows = findChangeSets(keyspace, 0) 90 | assertEquals(rows.size(), 2) 91 | assertChangeSetEquals(rows[0], new ChangeSet(version: 'second-table')) 92 | assertChangeSetEquals(rows[1], new ChangeSet(version: 'third-table')) 93 | } 94 | @Test 95 | void includeScriptAfterChangesBasePath() { 96 | keyspace = 'include_after' 97 | resetSchema(keyspace) 98 | 99 | def script = getClass().getResource('/include/script1.groovy').toURI() 100 | 101 | def baseScriptsPath = new File(getClass().getResource('/').toURI()).absolutePath 102 | 103 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session, baseScriptsPath: baseScriptsPath) 104 | cassalog.execute(script, [keyspace: keyspace]) 105 | 106 | assertTableExists(keyspace, 'test1') 107 | assertTableExists(keyspace, 'test2') 108 | 109 | def rows = findChangeSets(keyspace, 0) 110 | assertEquals(rows.size(), 2) 111 | assertChangeSetEquals(rows[0], new ChangeSet(version: 'first-table')) 112 | assertChangeSetEquals(rows[1], new ChangeSet(version: 'second-table')) 113 | } 114 | 115 | @Test 116 | void includeScriptBeforeChangesBasePath() { 117 | keyspace = 'include_before' 118 | resetSchema(keyspace) 119 | 120 | def script = getClass().getResource('/include/script3.groovy').toURI() 121 | def baseScriptsPath = new File(getClass().getResource('/').toURI()).absolutePath 122 | 123 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session, baseScriptsPath: baseScriptsPath) 124 | cassalog.execute(script, [keyspace: keyspace]) 125 | 126 | def rows = findChangeSets(keyspace, 0) 127 | assertEquals(rows.size(), 2) 128 | assertChangeSetEquals(rows[0], new ChangeSet(version: 'second-table')) 129 | assertChangeSetEquals(rows[1], new ChangeSet(version: 'third-table')) 130 | } 131 | 132 | @Test 133 | void multipleIncludesBasePath() { 134 | keyspace = 'multiple_includes' 135 | resetSchema(keyspace) 136 | 137 | def script = getClass().getResource('/include/multiple_includes.groovy').toURI() 138 | def baseScriptsPath = new File(getClass().getResource('/').toURI()).absolutePath 139 | 140 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session, baseScriptsPath: baseScriptsPath) 141 | cassalog.execute(script, [keyspace: keyspace]) 142 | 143 | def rows = findChangeSets(keyspace, 0) 144 | assertEquals(rows.size(), 2) 145 | assertChangeSetEquals(rows[0], new ChangeSet(version: 'second-table')) 146 | assertChangeSetEquals(rows[1], new ChangeSet(version: 'fourth-table')) 147 | } 148 | 149 | @Test 150 | void nestedIncludesBasePath() { 151 | keyspace = 'nested_includes' 152 | resetSchema(keyspace) 153 | 154 | def script = getClass().getResource('/include/nested.groovy').toURI() 155 | def baseScriptsPath = new File(getClass().getResource('/').toURI()).absolutePath 156 | 157 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session, baseScriptsPath: baseScriptsPath) 158 | cassalog.execute(script, [keyspace: keyspace]) 159 | 160 | def rows = findChangeSets(keyspace, 0) 161 | assertEquals(rows.size(), 2) 162 | assertChangeSetEquals(rows[0], new ChangeSet(version: 'second-table')) 163 | assertChangeSetEquals(rows[1], new ChangeSet(version: 'third-table')) 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /src/test/groovy/org/cassalog/core/LoadChangeSetsTest.groovy: -------------------------------------------------------------------------------- 1 | package org.cassalog.core 2 | 3 | import org.testng.annotations.Test 4 | 5 | import static org.testng.Assert.assertEquals 6 | 7 | /** 8 | * @author jsanda 9 | */ 10 | class LoadChangeSetsTest extends CassalogBaseTest { 11 | 12 | @Test 13 | void loadChangeSets() { 14 | keyspace = 'tags_test' 15 | def script = getClass().getResource('/tags_test/script1.groovy').toURI() 16 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session) 17 | def changeSets = cassalog.load(script, ['dev'], [keyspace: keyspace]) 18 | 19 | assertEquals(changeSets.size(), 2) 20 | assertEquals(changeSets[0].version, 'table-1') 21 | assertEquals(changeSets[1].version, 'dev-data') 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/test/groovy/org/cassalog/core/MultipleCQLStatementsTest.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | import org.testng.annotations.Test 20 | 21 | import static org.testng.Assert.assertEquals 22 | 23 | /** 24 | * @author jsanda 25 | */ 26 | class MultipleCQLStatementsTest extends CassalogBaseTest { 27 | 28 | @Test 29 | void executeMultipleCQLStatements() { 30 | keyspace = 'multiple_cql_statements' 31 | resetSchema(keyspace) 32 | 33 | def script = getClass().getResource('/multiple_cql_statements/script1.groovy').toURI() 34 | 35 | def cassalog = new CassalogImpl(session: session, keyspace: keyspace) 36 | cassalog.execute(script, [keyspace: keyspace]) 37 | 38 | assertTableExists(keyspace, 'test') 39 | 40 | def rows = session.execute("SELECT x, y FROM test") 41 | assertEquals(rows.size(), 3) 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/test/groovy/org/cassalog/core/SetKeyspaceTest.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package org.cassalog.core 18 | 19 | import org.testng.annotations.Test 20 | 21 | /** 22 | * @author jsanda 23 | */ 24 | class SetKeyspaceTest extends CassalogBaseTest { 25 | 26 | @Test 27 | void setKeyspace() { 28 | keyspace = 'set_keyspace' 29 | resetSchema(keyspace) 30 | 31 | def script = getClass().getResource('/set_keyspace/script1.groovy').toURI() 32 | 33 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session) 34 | cassalog.execute(script) 35 | 36 | assertTableExists(keyspace, 'test') 37 | } 38 | 39 | @Test 40 | void alwaysRun() { 41 | keyspace = 'alwaysrun' 42 | resetSchema(keyspace) 43 | 44 | def script = getClass().getResource('/set_keyspace/script3.groovy').toURI() 45 | 46 | def cassalog = new CassalogImpl(keyspace: keyspace, session: session) 47 | cassalog.createChangeLogTableIfNecessary() 48 | cassalog.initPreparedStatements() 49 | def setKeyspace = new SetKeyspace() 50 | setKeyspace.name = keyspace 51 | // Directly execute the change set to simulate a situation a script has start execution and then later 52 | // executed again. The latter execution should re-apply the setKeyspace command since it defaults to 53 | // always run. 54 | cassalog.applyChangeSet(setKeyspace, 0, true, CassalogImpl.NO_OP_LISTENER) 55 | // Switching to the the system keyspace will make the script fail if the setKeyspace function does not get 56 | // executed. 57 | session.execute("USE system") 58 | cassalog.execute(script) 59 | 60 | assertTableExists(keyspace, 'always_run') 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/test/resources/append_changes/script1.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | version 'first-table' 3 | author 'admin' 4 | description 'test' 5 | cql """ 6 | CREATE TABLE ${keyspace}.test1 ( 7 | x int, 8 | y text, 9 | z text, 10 | PRIMARY KEY (x, y) 11 | ) 12 | """ 13 | } -------------------------------------------------------------------------------- /src/test/resources/append_changes/script2.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | version 'first-table' 3 | author 'admin' 4 | description 'test' 5 | cql """ 6 | CREATE TABLE ${keyspace}.test1 ( 7 | x int, 8 | y text, 9 | z text, 10 | PRIMARY KEY (x, y) 11 | ) 12 | """ 13 | } 14 | 15 | schemaChange { 16 | version 'second-table' 17 | author 'admin' 18 | description 'second table test' 19 | tags 'red', 'blue' 20 | cql """ 21 | CREATE TABLE ${keyspace}.test2 ( 22 | x2 int, 23 | y2 text, 24 | z2 text, 25 | PRIMARY KEY (x2, y2) 26 | ) 27 | """ 28 | } -------------------------------------------------------------------------------- /src/test/resources/basic_validation/script1.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | author 'admin' 3 | cql """ 4 | CREATE TABLE ${keyspace}.test ( 5 | x int, 6 | y int, 7 | PRIMARY KEY (x) 8 | ) 9 | """ 10 | } -------------------------------------------------------------------------------- /src/test/resources/basic_validation/script2.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | version 'no-cql' 3 | author 'admin' 4 | } -------------------------------------------------------------------------------- /src/test/resources/bootstrap/bootstrap_fail.groovy: -------------------------------------------------------------------------------- 1 | bootstrap { 2 | session.execute("CREATE TABLE ${keyspace}.test2 (x text PRIMARY KEY)") 3 | } 4 | 5 | schemaChange { 6 | version 'first-table' 7 | cql """ 8 | CREATE TABLE ${keyspace}.test1 ( 9 | x int, 10 | y text, 11 | z text, 12 | PRIMARY KEY (x, y) 13 | ) 14 | """ 15 | } 16 | 17 | schemaChange { 18 | version 'second-table' 19 | cql "DROP TABLE ${keyspace}.test2" 20 | } 21 | 22 | bootstrap { 23 | throw new RuntimeException("This should not execute") 24 | } -------------------------------------------------------------------------------- /src/test/resources/bootstrap/bootstrap_include.groovy: -------------------------------------------------------------------------------- 1 | include '/bootstrap/included_bootstrap.groovy' 2 | include '/bootstrap/script1.groovy' -------------------------------------------------------------------------------- /src/test/resources/bootstrap/bootstrap_test.groovy: -------------------------------------------------------------------------------- 1 | bootstrap { 2 | session.execute("CREATE TABLE ${keyspace}.test2 (x text PRIMARY KEY)") 3 | } 4 | 5 | schemaChange { 6 | version 'first-table' 7 | cql """ 8 | CREATE TABLE ${keyspace}.test1 ( 9 | x int, 10 | y text, 11 | z text, 12 | PRIMARY KEY (x, y) 13 | ) 14 | """ 15 | } 16 | 17 | // This change will fail if the bootstrap callback does not execute 18 | schemaChange { 19 | version 'second-table' 20 | cql "DROP TABLE ${keyspace}.test2" 21 | } -------------------------------------------------------------------------------- /src/test/resources/bootstrap/included_bootstrap.groovy: -------------------------------------------------------------------------------- 1 | bootstrap { 2 | session.execute("CREATE TABLE ${keyspace}.test2 (x text PRIMARY KEY)") 3 | } -------------------------------------------------------------------------------- /src/test/resources/bootstrap/script1.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | version 'first-table' 3 | cql """ 4 | CREATE TABLE ${keyspace}.test1 ( 5 | x int, 6 | y text, 7 | z text, 8 | PRIMARY KEY (x, y) 9 | ) 10 | """ 11 | } 12 | 13 | // This change will fail if the bootstrap callback does not execute 14 | schemaChange { 15 | version 'second-table' 16 | cql "DROP TABLE ${keyspace}.test2" 17 | } -------------------------------------------------------------------------------- /src/test/resources/change_id/script1.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | version 'first-table' 3 | author 'admin' 4 | description 'test 1' 5 | cql """ 6 | CREATE TABLE ${keyspace}.test1 ( 7 | x int, 8 | y int, 9 | PRIMARY KEY (x) 10 | ) 11 | """ 12 | } -------------------------------------------------------------------------------- /src/test/resources/change_id/script2.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | version 'table-1' 3 | author 'admin' 4 | description 'test 1' 5 | cql """ 6 | CREATE TABLE ${keyspace}.test1 ( 7 | x int, 8 | y int, 9 | PRIMARY KEY (x) 10 | ) 11 | """ 12 | } -------------------------------------------------------------------------------- /src/test/resources/create_keyspace/script1.groovy: -------------------------------------------------------------------------------- 1 | createKeyspace { 2 | version id1 3 | name keyspace 4 | author 'admin' 5 | description 'create keyspace test' 6 | replication { 7 | replication_factor 2 8 | } 9 | } -------------------------------------------------------------------------------- /src/test/resources/create_keyspace/script2.groovy: -------------------------------------------------------------------------------- 1 | createKeyspace { 2 | version id1 3 | name keyspace 4 | author 'admin' 5 | description 'create keyspace test' 6 | replication { 7 | replication_factor 2 8 | } 9 | } 10 | 11 | schemaChange { 12 | version id2 13 | author 'admin' 14 | description 'test' 15 | cql """ 16 | CREATE TABLE ${keyspace}.test1 ( 17 | x int, 18 | y text, 19 | z text, 20 | PRIMARY KEY (x, y) 21 | ) 22 | """ 23 | } -------------------------------------------------------------------------------- /src/test/resources/create_keyspace/script3.groovy: -------------------------------------------------------------------------------- 1 | createKeyspace { 2 | version change1Id 3 | name keyspace 4 | author 'admin' 5 | description 'create keyspace test' 6 | replication { 7 | replication_factor 2 8 | } 9 | } 10 | 11 | schemaChange { 12 | version change2Id 13 | author 'admin' 14 | description 'test' 15 | // Note that the CREATE TABLE statement does not prefix the table name with the keyspace. This is because the 16 | // createKeyspace function by default sets the working keyspace to the one created with the USE CQL command. 17 | cql """ 18 | CREATE TABLE test1 ( 19 | x int, 20 | y text, 21 | z text, 22 | PRIMARY KEY (x, y) 23 | ) 24 | """ 25 | } -------------------------------------------------------------------------------- /src/test/resources/create_keyspace/script4.groovy: -------------------------------------------------------------------------------- 1 | createKeyspace { 2 | version change1Id 3 | name keyspace 4 | // Note that the active attribute is set to false. This means that the following CREATE TABLE statement must 5 | // prefix the table name with the keyspace in order for the table to be created in the correct keyspace. 6 | active false 7 | } 8 | 9 | schemaChange { 10 | version change2Id 11 | cql """ 12 | CREATE TABLE test1 ( 13 | x int, 14 | y text, 15 | z text, 16 | PRIMARY KEY (x, y) 17 | ) 18 | """ 19 | } -------------------------------------------------------------------------------- /src/test/resources/create_keyspace/script5.groovy: -------------------------------------------------------------------------------- 1 | createKeyspace { 2 | version id1 3 | name keyspace 4 | recreate true 5 | active false 6 | } -------------------------------------------------------------------------------- /src/test/resources/create_keyspace/script6.groovy: -------------------------------------------------------------------------------- 1 | createKeyspace { 2 | version 1 3 | name keyspace 4 | author 'admin' 5 | description 'create keyspace test' 6 | replication { 7 | replication_factor replicationFactor 8 | } 9 | } -------------------------------------------------------------------------------- /src/test/resources/fail_modified_changeset/script1.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | version 'first-table' 3 | author 'admin' 4 | description 'test 1' 5 | cql """ 6 | CREATE TABLE ${keyspace}.test1 ( 7 | x1 int, 8 | y1 text, 9 | z1 text, 10 | PRIMARY KEY (x1, y1) 11 | ) 12 | """ 13 | } -------------------------------------------------------------------------------- /src/test/resources/fail_modified_changeset/script2.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | version 'first-table' 3 | author 'admin' 4 | description 'test 1' 5 | cql """ 6 | CREATE TABLE ${keyspace}.test1 ( 7 | x1 int, 8 | y1 text, 9 | z1 text, 10 | PRIMARY KEY (x1, z1) 11 | ) 12 | """ 13 | } -------------------------------------------------------------------------------- /src/test/resources/include/multiple_includes.groovy: -------------------------------------------------------------------------------- 1 | include '/include/script2.groovy' 2 | include '/include/script4.groovy' -------------------------------------------------------------------------------- /src/test/resources/include/nested.groovy: -------------------------------------------------------------------------------- 1 | include '/include/script3.groovy' -------------------------------------------------------------------------------- /src/test/resources/include/script1.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | version 'first-table' 3 | cql """ 4 | CREATE TABLE ${keyspace}.test1 ( 5 | x int, 6 | y text, 7 | z text, 8 | PRIMARY KEY (x, y) 9 | ) 10 | """ 11 | } 12 | 13 | include '/include/script2.groovy' -------------------------------------------------------------------------------- /src/test/resources/include/script2.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | version 'second-table' 3 | cql """ 4 | CREATE TABLE ${keyspace}.test2 ( 5 | x int, 6 | y text, 7 | z text, 8 | PRIMARY KEY (y, z) 9 | ) 10 | """ 11 | } -------------------------------------------------------------------------------- /src/test/resources/include/script3.groovy: -------------------------------------------------------------------------------- 1 | include '/include/script2.groovy' 2 | 3 | schemaChange { 4 | version 'third-table' 5 | cql """ 6 | CREATE TABLE ${keyspace}.test3 ( 7 | x int, 8 | y text, 9 | z text, 10 | PRIMARY KEY (y, z) 11 | ) 12 | """ 13 | } -------------------------------------------------------------------------------- /src/test/resources/include/script4.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | version 'fourth-table' 3 | cql """ 4 | CREATE TABLE ${keyspace}.test4 ( 5 | x int, 6 | y text, 7 | z text, 8 | PRIMARY KEY (y, z) 9 | ) 10 | """ 11 | } -------------------------------------------------------------------------------- /src/test/resources/logback.groovy: -------------------------------------------------------------------------------- 1 | appender("file", FileAppender) { 2 | file = "target/test.log" 3 | append = false 4 | encoder(PatternLayoutEncoder) { 5 | pattern = "%-5level %date{ISO8601} [%thread] %class:%M:%L - %message%n" 6 | } 7 | } 8 | 9 | appender("console", ConsoleAppender) { 10 | encoder(PatternLayoutEncoder) { 11 | pattern = "%-5level %date{ISO8601} [%thread] %class:%M:%L - %message%n" 12 | } 13 | } 14 | 15 | root(WARN, ["console", "file"]) 16 | logger("org.cassalog", DEBUG, ["console", "file"], additivity = false) 17 | logger("com.datastax", INFO, ["console", "file"], additivity = false) -------------------------------------------------------------------------------- /src/test/resources/multiple_buckets/script1.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | version 'first-table' 3 | author 'admin' 4 | description 'test' 5 | cql """ 6 | CREATE TABLE ${keyspace}.test1( 7 | x1 int, 8 | y1 text, 9 | z1 text, 10 | PRIMARY KEY(x1, y1) 11 | ) 12 | """ 13 | } 14 | 15 | schemaChange { 16 | version 'second-table' 17 | author 'admin' 18 | description 'second table test' 19 | tags 'red', 'blue' 20 | cql """ 21 | CREATE TABLE ${keyspace}.test2 ( 22 | x2 int, 23 | y2 text, 24 | z2 text, 25 | PRIMARY KEY(x2, y2) 26 | ) 27 | """ 28 | } -------------------------------------------------------------------------------- /src/test/resources/multiple_buckets/script2.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | version 'first-table' 3 | author 'admin' 4 | description 'test' 5 | cql """ 6 | CREATE TABLE ${keyspace}.test1( 7 | x1 int, 8 | y1 text, 9 | z1 text, 10 | PRIMARY KEY(x1, y1) 11 | ) 12 | """ 13 | } 14 | 15 | schemaChange { 16 | version 'second-table' 17 | author 'admin' 18 | description 'second table test' 19 | tags 'red', 'blue' 20 | cql """ 21 | CREATE TABLE ${keyspace}.test2 ( 22 | x2 int, 23 | y2 text, 24 | z2 text, 25 | PRIMARY KEY(x2, y2) 26 | ) 27 | """ 28 | } 29 | 30 | schemaChange { 31 | version 'third-table' 32 | author 'admin' 33 | description '3rd table' 34 | tags '1', '2', '3' 35 | cql """ 36 | CREATE TABLE ${keyspace}.test3 ( 37 | x3 text, 38 | y3 int, 39 | z3 text, 40 | PRIMARY KEY (x3, z3) 41 | ) 42 | """ 43 | } 44 | 45 | -------------------------------------------------------------------------------- /src/test/resources/multiple_cql_statements/script1.groovy: -------------------------------------------------------------------------------- 1 | setKeyspace keyspace 2 | 3 | schemaChange { 4 | version '1.0' 5 | cql """ 6 | CREATE TABLE test ( 7 | x int, 8 | y text, 9 | PRIMARY KEY (x) 10 | ) 11 | """ 12 | } 13 | 14 | schemaChange { 15 | version '1.1' 16 | cql ([ 17 | "INSERT INTO test (x, y) VALUES (0, '1')", 18 | "INSERT INTO test (x, y) VALUES (1, '2')", 19 | "INSERT INTO test (x, y) VALUES (2, '3')" 20 | ]) 21 | } -------------------------------------------------------------------------------- /src/test/resources/resume_updates/columns.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | version '1.0' 3 | cql "ALTER TABLE ${keyspace}.test ADD z text" 4 | verify { columnExists(keyspace, "test", "z") } 5 | } 6 | 7 | schemaChange { 8 | version '2.0' 9 | cql "ALTER TABLE ${keyspace}.test DROP y" 10 | verify { columnDoesNotExist(keyspace, "test", "z") } 11 | } -------------------------------------------------------------------------------- /src/test/resources/resume_updates/create_tables.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | version '1.0' 3 | cql "CREATE TABLE ${keyspace}.test1 (x int, y text, PRIMARY KEY (x))" 4 | verify { tableExists(keyspace, "test1") } 5 | } 6 | 7 | schemaChange { 8 | version '2.0' 9 | cql """ 10 | CREATE TABLE ${keyspace}.test2 ( 11 | x int, 12 | y text, 13 | PRIMARY KEY (x) 14 | ) 15 | """ 16 | } -------------------------------------------------------------------------------- /src/test/resources/resume_updates/insert.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | version '1.0' 3 | cql "CREATE TABLE ${keyspace}.test (x int PRIMARY KEY, y int)" 4 | verify { tableExists(keyspace, "test") } 5 | } 6 | 7 | schemaChange { 8 | version '2.0' 9 | cql "INSERT INTO ${keyspace}.test (x, y) VALUES (1, 2)" 10 | } -------------------------------------------------------------------------------- /src/test/resources/set_keyspace/script1.groovy: -------------------------------------------------------------------------------- 1 | setKeyspace keyspace 2 | 3 | schemaChange { 4 | version 'first-table' 5 | cql """ 6 | CREATE TABLE test ( 7 | x int, 8 | y text, 9 | z text, 10 | PRIMARY KEY (x, y) 11 | ) 12 | """ 13 | } -------------------------------------------------------------------------------- /src/test/resources/set_keyspace/script2.groovy: -------------------------------------------------------------------------------- 1 | setKeyspace 'does_not_exist' 2 | 3 | schemaChange { 4 | version 'first-table' 5 | cql """ 6 | CREATE TABLE test ( 7 | x int, 8 | y text, 9 | z text, 10 | PRIMARY KEY (x, y) 11 | ) 12 | """ 13 | } -------------------------------------------------------------------------------- /src/test/resources/set_keyspace/script3.groovy: -------------------------------------------------------------------------------- 1 | setKeyspace keyspace 2 | 3 | schemaChange { 4 | version 'first-table' 5 | cql """ 6 | CREATE TABLE always_run ( 7 | x int, 8 | y text, 9 | z text, 10 | PRIMARY KEY (x, y) 11 | ) 12 | """ 13 | } 14 | -------------------------------------------------------------------------------- /src/test/resources/single_change/schema.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | version 'first-table' 3 | author 'admin' 4 | description 'test' 5 | cql """ 6 | CREATE TABLE ${keyspace}.test1 ( 7 | x int, 8 | y text, 9 | z text, 10 | PRIMARY KEY (x, y) 11 | ) 12 | """ 13 | } 14 | -------------------------------------------------------------------------------- /src/test/resources/tags_test/script1.groovy: -------------------------------------------------------------------------------- 1 | schemaChange { 2 | version 'table-1' 3 | author 'admin' 4 | tags 'dev', 'stage' 5 | cql """ 6 | CREATE TABLE ${keyspace}.test1 ( 7 | x int, 8 | y int, 9 | PRIMARY KEY (x) 10 | ) 11 | """ 12 | } 13 | 14 | schemaChange { 15 | version 'dev-data' 16 | author 'admin' 17 | tags 'dev' 18 | cql "INSERT INTO ${keyspace}.test1 (x, y) VALUES (1, 2)" 19 | } 20 | 21 | schemaChange { 22 | version 'stage-data' 23 | author 'admin' 24 | tags 'stage' 25 | cql "INSERT INTO ${keyspace}.test1 (x, y) VALUES (2, 3)" 26 | } --------------------------------------------------------------------------------