Skip to content

Commit

Permalink
Drop views in Redshift to allow for changes to the view's columns (an…
Browse files Browse the repository at this point in the history
…d their types). (#270)
  • Loading branch information
BenBirt authored Jun 18, 2019
1 parent d928c22 commit 9678cb6
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 33 deletions.
4 changes: 2 additions & 2 deletions api/api.package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"main": "index.js",
"types": "index.d.ts",
"dependencies": {
"@dataform/core": "1.0.0-alpha.2",
"@dataform/protos": "1.0.0-alpha.2",
"@dataform/core": "1.0.0-alpha.3",
"@dataform/protos": "1.0.0-alpha.3",
"@google-cloud/bigquery": "^1.3.0",
"@types/glob": "^7.1.1",
"bluebird": "^3.5.3",
Expand Down
6 changes: 3 additions & 3 deletions cli/cli.package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"dtfm": "index.js"
},
"dependencies": {
"@dataform/api": "1.0.0-alpha.2",
"@dataform/core": "1.0.0-alpha.2",
"@dataform/protos": "1.0.0-alpha.2",
"@dataform/api": "1.0.0-alpha.3",
"@dataform/core": "1.0.0-alpha.3",
"@dataform/protos": "1.0.0-alpha.3",
"@types/readline-sync": "^1.4.3",
"@types/yargs": "^11.1.1",
"chokidar": "^2.0.4",
Expand Down
2 changes: 1 addition & 1 deletion common.package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.0-alpha.2",
"version": "1.0.0-alpha.3",
"homepage": "https://github.com/dataform-co/dataform",
"license": "MIT",
"keywords": [
Expand Down
50 changes: 25 additions & 25 deletions core/adapters/redshift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export class RedshiftAdapter extends Adapter implements IAdapter {
): Tasks {
const tasks = Tasks.create();
// Drop the existing view or table if we are changing it's type.
if (tableMetadata && tableMetadata.type != this.baseTableType(table.type)) {
if (tableMetadata && tableMetadata.type !== this.baseTableType(table.type)) {
tasks.add(
Task.statement(this.dropIfExists(table.target, this.oppositeTableType(table.type)))
);
}
if (table.type == "incremental") {
if (runConfig.fullRefresh || !tableMetadata || tableMetadata.type == "view") {
if (table.type === "incremental") {
if (runConfig.fullRefresh || !tableMetadata || tableMetadata.type === "view") {
tasks.addAll(this.createOrReplace(table));
} else {
// The table exists, insert new rows.
Expand All @@ -52,16 +52,15 @@ export class RedshiftAdapter extends Adapter implements IAdapter {
assertion: dataform.IAssertion,
projectConfig: dataform.IProjectConfig
): Tasks {
const tasks = Tasks.create();
const target =
assertion.target ||
dataform.Target.create({
schema: projectConfig.assertionSchema,
name: assertion.name
});
tasks.add(Task.statement(this.createOrReplaceView(target, assertion.query)));
tasks.add(Task.assertion(`select sum(1) as row_count from ${this.resolveTarget(target)}`));
return tasks;
return Tasks.create()
.add(Task.statement(this.createOrReplaceView(target, assertion.query)))
.add(Task.assertion(`select sum(1) as row_count from ${this.resolveTarget(target)}`));
}

public createOrReplaceView(target: dataform.ITarget, query: string) {
Expand All @@ -70,39 +69,40 @@ export class RedshiftAdapter extends Adapter implements IAdapter {
}

public createOrReplace(table: dataform.ITable) {
if (table.type == "view") {
return Tasks.create().add(
Task.statement(`
if (table.type === "view") {
return (
Tasks.create()
// Drop the view in case we are changing the number of column(s) (or their types).
.add(Task.statement(this.dropIfExists(table.target, this.baseTableType(table.type))))
.add(
Task.statement(`
create or replace view ${this.resolveTarget(table.target)}
as ${table.query}`)
)
);
} else {
const tempTableTarget = dataform.Target.create({
schema: table.target.schema,
name: table.target.name + "_temp"
});
}
const tempTableTarget = dataform.Target.create({
schema: table.target.schema,
name: table.target.name + "_temp"
});

const tasks = Tasks.create();
tasks.add(Task.statement(this.dropIfExists(tempTableTarget, this.baseTableType(table.type))));
tasks.add(Task.statement(this.createTable(table, tempTableTarget)));
tasks.add(Task.statement(this.dropIfExists(table.target, "table")));
tasks.add(
return Tasks.create()
.add(Task.statement(this.dropIfExists(tempTableTarget, this.baseTableType(table.type))))
.add(Task.statement(this.createTable(table, tempTableTarget)))
.add(Task.statement(this.dropIfExists(table.target, "table")))
.add(
Task.statement(
`alter table ${this.resolveTarget(tempTableTarget)} rename to "${table.target.name}"`
)
);
return tasks;
}
}

public createTable(table: dataform.ITable, target: dataform.ITarget) {
if (table.redshift) {
let query = `create table ${this.resolveTarget(target)}`;

if (table.redshift.distStyle && table.redshift.distKey) {
query = `${query} diststyle ${table.redshift.distStyle} distkey (${
table.redshift.distKey
})`;
query = `${query} diststyle ${table.redshift.distStyle} distkey (${table.redshift.distKey})`;
}
if (table.redshift.sortStyle && table.redshift.sortKeys) {
query = `${query} ${table.redshift.sortStyle} sortkey (${table.redshift.sortKeys.join(
Expand Down
2 changes: 1 addition & 1 deletion core/core.package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"main": "index.js",
"types": "index.d.ts",
"dependencies": {
"@dataform/protos": "1.0.0-alpha.2",
"@dataform/protos": "1.0.0-alpha.3",
"moo": "^0.5.0",
"protobufjs": "^6.8.8"
}
Expand Down
2 changes: 1 addition & 1 deletion crossdb/crossdb.package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@dataform/crossdb",
"description": "Cross database utilities for dataform.",
"dependencies": {
"@dataform/core": "1.0.0-alpha.2"
"@dataform/core": "1.0.0-alpha.3"
},
"main": "index.js",
"types": "index.d.ts",
Expand Down

0 comments on commit 9678cb6

Please sign in to comment.