|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package sql2pgroll_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + |
| 10 | + "github.com/xataio/pgroll/pkg/migrations" |
| 11 | + "github.com/xataio/pgroll/pkg/sql2pgroll" |
| 12 | +) |
| 13 | + |
| 14 | +func TestConvertToMigration(t *testing.T) { |
| 15 | + tests := map[string]struct { |
| 16 | + sql string |
| 17 | + expectedOps migrations.Operations |
| 18 | + expectedErr bool |
| 19 | + }{ |
| 20 | + "empty SQL statement": { |
| 21 | + sql: "", |
| 22 | + expectedOps: nil, |
| 23 | + expectedErr: false, |
| 24 | + }, |
| 25 | + "single SQL statement": { |
| 26 | + sql: "DROP TYPE t1;", |
| 27 | + expectedOps: migrations.Operations{ |
| 28 | + &migrations.OpRawSQL{ |
| 29 | + Up: "DROP TYPE t1", |
| 30 | + }, |
| 31 | + }, |
| 32 | + expectedErr: false, |
| 33 | + }, |
| 34 | + "single multiline statement with comments": { |
| 35 | + sql: `CREATE TABLE t1 ( |
| 36 | +id INT, -- my id column |
| 37 | +name TEXT -- my name column |
| 38 | +); |
| 39 | +`, |
| 40 | + expectedOps: migrations.Operations{ |
| 41 | + &migrations.OpCreateTable{ |
| 42 | + Name: "t1", |
| 43 | + Columns: []migrations.Column{ |
| 44 | + { |
| 45 | + Name: "id", |
| 46 | + Type: "int", |
| 47 | + Nullable: true, |
| 48 | + }, |
| 49 | + { |
| 50 | + Name: "name", |
| 51 | + Type: "text", |
| 52 | + Nullable: true, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + expectedErr: false, |
| 58 | + }, |
| 59 | + "single function definition multiline with comments": { |
| 60 | + sql: `CREATE OR REPLACE FUNCTION check_password(uname TEXT, pass TEXT) |
| 61 | +RETURNS BOOLEAN AS $$ -- check password for username |
| 62 | +DECLARE passed BOOLEAN; |
| 63 | +BEGIN |
| 64 | + SELECT (pwd = $2) INTO passed |
| 65 | + FROM pwds -- from passwords table |
| 66 | + WHERE username = $1; -- select password for username |
| 67 | + RETURN passed; |
| 68 | +END; $$ LANGUAGE plpgsql |
| 69 | +SECURITY DEFINER`, |
| 70 | + expectedOps: migrations.Operations{ |
| 71 | + &migrations.OpRawSQL{ |
| 72 | + Up: `CREATE OR REPLACE FUNCTION check_password(uname TEXT, pass TEXT) |
| 73 | +RETURNS BOOLEAN AS $$ -- check password for username |
| 74 | +DECLARE passed BOOLEAN; |
| 75 | +BEGIN |
| 76 | + SELECT (pwd = $2) INTO passed |
| 77 | + FROM pwds -- from passwords table |
| 78 | + WHERE username = $1; -- select password for username |
| 79 | + RETURN passed; |
| 80 | +END; $$ LANGUAGE plpgsql |
| 81 | +SECURITY DEFINER`, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + "multiple SQL raw migration statements": { |
| 86 | + sql: "DROP TYPE t1; DROP TYPE t2;", |
| 87 | + expectedOps: migrations.Operations{ |
| 88 | + &migrations.OpRawSQL{ |
| 89 | + Up: "DROP TYPE t1", |
| 90 | + }, |
| 91 | + &migrations.OpRawSQL{ |
| 92 | + Up: "DROP TYPE t2", |
| 93 | + }, |
| 94 | + }, |
| 95 | + expectedErr: false, |
| 96 | + }, |
| 97 | + "multiple SQL migrations to raw and regular pgroll operations": { |
| 98 | + sql: "CREATE TABLE t1 (id INT); DROP INDEX idx1; DROP TYPE t1; ALTER TABLE t1 ADD COLUMN name TEXT;", |
| 99 | + expectedOps: migrations.Operations{ |
| 100 | + &migrations.OpCreateTable{ |
| 101 | + Name: "t1", |
| 102 | + Columns: []migrations.Column{ |
| 103 | + { |
| 104 | + Name: "id", |
| 105 | + Type: "int", |
| 106 | + Nullable: true, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + &migrations.OpDropIndex{ |
| 111 | + Name: "idx1", |
| 112 | + }, |
| 113 | + &migrations.OpRawSQL{ |
| 114 | + Up: "DROP TYPE t1", |
| 115 | + }, |
| 116 | + &migrations.OpAddColumn{ |
| 117 | + Table: "t1", |
| 118 | + Column: migrations.Column{ |
| 119 | + Name: "name", |
| 120 | + Type: "text", |
| 121 | + Nullable: true, |
| 122 | + }, |
| 123 | + Up: sql2pgroll.PlaceHolderSQL, |
| 124 | + }, |
| 125 | + }, |
| 126 | + expectedErr: false, |
| 127 | + }, |
| 128 | + "multiple unknown DDL statements": { |
| 129 | + sql: "CREATE TYPE t1 AS ENUM ('a', 'b'); CREATE DOMAIN d1 AS TEXT; CREATE SCHEMA s1; CREATE EXTENSION e1;", |
| 130 | + expectedOps: migrations.Operations{ |
| 131 | + &migrations.OpRawSQL{ |
| 132 | + Up: "CREATE TYPE t1 AS ENUM ('a', 'b')", |
| 133 | + }, |
| 134 | + &migrations.OpRawSQL{ |
| 135 | + Up: "CREATE DOMAIN d1 AS TEXT", |
| 136 | + }, |
| 137 | + &migrations.OpRawSQL{ |
| 138 | + Up: "CREATE SCHEMA s1", |
| 139 | + }, |
| 140 | + &migrations.OpRawSQL{ |
| 141 | + Up: "CREATE EXTENSION e1", |
| 142 | + }, |
| 143 | + }, |
| 144 | + expectedErr: false, |
| 145 | + }, |
| 146 | + "multiple empty SQL statements": { |
| 147 | + sql: ";;", |
| 148 | + }, |
| 149 | + "multiple statements with empty SQL statement": { |
| 150 | + sql: "CREATE TABLE t1 (id INT);; DROP TYPE t1;;", |
| 151 | + expectedOps: migrations.Operations{ |
| 152 | + &migrations.OpCreateTable{ |
| 153 | + Name: "t1", |
| 154 | + Columns: []migrations.Column{ |
| 155 | + { |
| 156 | + Name: "id", |
| 157 | + Type: "int", |
| 158 | + Nullable: true, |
| 159 | + }, |
| 160 | + }, |
| 161 | + }, |
| 162 | + &migrations.OpRawSQL{ |
| 163 | + Up: "DROP TYPE t1", |
| 164 | + }, |
| 165 | + }, |
| 166 | + expectedErr: false, |
| 167 | + }, |
| 168 | + "multiple multiline statments with comments": { |
| 169 | + sql: `DROP TYPE t1; -- drop type t1 |
| 170 | +DROP INDEX ixd1; -- drop my index |
| 171 | +`, |
| 172 | + expectedOps: migrations.Operations{ |
| 173 | + &migrations.OpRawSQL{ |
| 174 | + Up: "DROP TYPE t1", |
| 175 | + }, |
| 176 | + &migrations.OpDropIndex{ |
| 177 | + Name: "ixd1", |
| 178 | + }, |
| 179 | + }, |
| 180 | + expectedErr: false, |
| 181 | + }, |
| 182 | + "multiple statements with function definition multiline with comments": { |
| 183 | + sql: `DROP TABLE t1; DROP INDEX idx2; CREATE OR REPLACE FUNCTION check_password(uname TEXT, pass TEXT) |
| 184 | +RETURNS BOOLEAN AS $$ -- check password for username |
| 185 | +DECLARE passed BOOLEAN; |
| 186 | +BEGIN |
| 187 | + SELECT (pwd = $2) INTO passed |
| 188 | + FROM pwds -- from passwords table |
| 189 | + WHERE username = $1; -- select password for username |
| 190 | + RETURN passed; |
| 191 | +END; $$ LANGUAGE plpgsql |
| 192 | +SECURITY DEFINER; |
| 193 | +CREATE INDEX idx1 ON t1 (id); |
| 194 | +CREATE TYPE t1;`, |
| 195 | + expectedOps: migrations.Operations{ |
| 196 | + &migrations.OpDropTable{ |
| 197 | + Name: "t1", |
| 198 | + }, |
| 199 | + &migrations.OpDropIndex{ |
| 200 | + Name: "idx2", |
| 201 | + }, |
| 202 | + &migrations.OpRawSQL{ |
| 203 | + Up: `CREATE OR REPLACE FUNCTION check_password(uname TEXT, pass TEXT) |
| 204 | +RETURNS BOOLEAN AS $$ -- check password for username |
| 205 | +DECLARE passed BOOLEAN; |
| 206 | +BEGIN |
| 207 | + SELECT (pwd = $2) INTO passed |
| 208 | + FROM pwds -- from passwords table |
| 209 | + WHERE username = $1; -- select password for username |
| 210 | + RETURN passed; |
| 211 | +END; $$ LANGUAGE plpgsql |
| 212 | +SECURITY DEFINER`, |
| 213 | + }, |
| 214 | + &migrations.OpCreateIndex{ |
| 215 | + Name: "idx1", |
| 216 | + Table: "t1", |
| 217 | + Columns: []string{"id"}, |
| 218 | + Method: "btree", |
| 219 | + }, |
| 220 | + &migrations.OpRawSQL{ |
| 221 | + Up: "CREATE TYPE t1", |
| 222 | + }, |
| 223 | + }, |
| 224 | + expectedErr: false, |
| 225 | + }, |
| 226 | + "syntax error in second statement": { |
| 227 | + sql: "DROP INDEX idx1; DROP INDX idx2", |
| 228 | + expectedOps: nil, |
| 229 | + expectedErr: true, |
| 230 | + }, |
| 231 | + } |
| 232 | + |
| 233 | + for name, tc := range tests { |
| 234 | + t.Run(name, func(t *testing.T) { |
| 235 | + ops, err := sql2pgroll.Convert(tc.sql) |
| 236 | + if tc.expectedErr { |
| 237 | + assert.NotNil(t, err) |
| 238 | + } else { |
| 239 | + assert.Nil(t, err) |
| 240 | + } |
| 241 | + assert.Len(t, ops, len(tc.expectedOps)) |
| 242 | + assert.Equal(t, tc.expectedOps, ops) |
| 243 | + }) |
| 244 | + } |
| 245 | +} |
0 commit comments