static String emptySnapshotAsJson() { return new PostgresLiquibaseSnapshot().getSnapshotAsJson(); }
private void addIndex(TableDescription td, String tableId, ColumnDescription columnDescription, String indexNameSuffix, Map<String, String> indexIds, List<String> columnIds) { // Postgres names are maxed at 63 bytes, as long as we use ascii we should have no more than // 63 characters String indexName = toHashedName( td.getTableName() + "_idx_" + indexNameSuffix, 63); String indexId = addIndex(tableId, indexName, false, columnIds); indexIds.put(indexName, indexId); this.columnDescriptionPerIndexId.put(indexName, columnDescription); }
public void addTableDescription(TableDescription td) { addTable(td); }
public PostgresLiquibaseSnapshot getSnapshot() { PostgresLiquibaseSnapshot snapshot = new PostgresLiquibaseSnapshot(); snapshot.addTableDescriptions(getTableDescriptions()); return snapshot; }
private String addTable(TableDescription td) { JsonObject table = new JsonObject(); table.addProperty("name", td.getTableName()); String tableId = addSnapshotId(table, LIQUIBASE_STRUCTURE_CORE_TABLE, td.getTableName()); String dataColumnId = addColumn(tableId, "data", false, "jsonb", false); columnIds.put("data", dataColumnId); .forEach(column -> addColumnIndex(td, tableId, column, columnIds, indexIds, null)); .forEach(column -> addColumnIndex(td, tableId, column, columnIds, indexIds, documentSelfLinkColumnId)); String primaryKeyIndexId = addIndex(tableId, primaryKeyIndexName, true, primaryKeyColumnIds); indexIds.put(primaryKeyIndexName, primaryKeyIndexId); String primaryKey = addPrimaryKey(tableId, primaryKeyIndexName, primaryKeyIndexId, primaryKeyColumnIds);
catalog.addProperty("default", true); catalog.addProperty("name", catalogName); String catalogId = addSnapshotId(catalog, LIQUIBASE_STRUCTURE_CORE_CATALOG, catalogName); JsonArray catalogArray = new JsonArray(); catalogArray.add(catalogArrayWrapper); addObject(LIQUIBASE_STRUCTURE_CORE_CATALOG, catalogArray); schema.addProperty("default", true); schema.addProperty("name", schemaName); this.schemaId = addSnapshotId(schema, LIQUIBASE_STRUCTURE_CORE_SCHEMA, schemaName); JsonArray schemaArray = new JsonArray(); schemaArray.add(schemaArrayWrapper); addObject(LIQUIBASE_STRUCTURE_CORE_SCHEMA, schemaArray); addObject(LIQUIBASE_STRUCTURE_CORE_COLUMN, columnArray); addObject(LIQUIBASE_STRUCTURE_CORE_TABLE, tableArray); addObject(LIQUIBASE_STRUCTURE_CORE_PRIMARY_KEY, primaryKeyArray); addObject(LIQUIBASE_STRUCTURE_CORE_INDEX, indexArray);
String columnId = addColumn(tableId, indexColumnName, nullable, typeName, !isNativeColumn); if (isNativeColumn) { columnIds.put(columnName, columnId); addIndex(td, tableId, columnDescription, indexNameSuffix, indexIds, Collections.singletonList(columnId)); (indexType.equalsIgnoreCase("hash") || indexType.equalsIgnoreCase("btree"))) { addIndex(td, tableId, columnDescription, "sort_" + indexNameSuffix, indexIds, Arrays.asList(columnId, documentSelfLinkColumnId));
private String addIndex(String tableId, String name, boolean unique, List<String> columnIds) { JsonArray columns = new JsonArray(); columnIds.forEach(columns::add); JsonObject index = new JsonObject(); index.addProperty("name", name); index.addProperty("table", tableId); index.addProperty("unique", unique); index.add("columns", columns); String indexId = addSnapshotId(index, LIQUIBASE_STRUCTURE_CORE_INDEX, tableId, name); JsonObject indexArrayWrapper = new JsonObject(); indexArrayWrapper.add("index", index); indexArray.add(indexArrayWrapper); return indexId; }
public static String getLatestSnapshotAsJson(String snapshotResourcePath) { try { // TODO: Potential issue with wrong class loader for getting resources String json = PostgresHostUtils .getResourceAsString(Thread.currentThread().getContextClassLoader(), snapshotResourcePath); if (json.isEmpty()) { // Throw an Exception for empty content to treat it as empty database throw new IOException("Empty content"); } return json; } catch (IOException e) { // Use empty snapshot if latest snapshot is missing, this is used for // creating initial changelog logger.warning(String.format( "Missing %s resource, using empty snapshot for Liquibase change log creation: %s", snapshotResourcePath, e)); return PostgresLiquibaseSnapshot.emptySnapshotAsJson(); } }
private String addPrimaryKey(String tableId, String name, String primaryKeyIndexId, List<String> columnIds) { JsonArray columns = new JsonArray(); columnIds.forEach(columns::add); JsonObject primaryKey = new JsonObject(); // TODO: Avoid setting name, causing issues //primaryKey.addProperty("name", name); primaryKey.addProperty("table", tableId); primaryKey.addProperty("backingIndex", primaryKeyIndexId); primaryKey.add("columns", columns); String primaryKeyId = addSnapshotId(primaryKey, LIQUIBASE_STRUCTURE_CORE_PRIMARY_KEY, tableId); JsonObject primaryKeyArrayWrapper = new JsonObject(); primaryKeyArrayWrapper.add("primaryKey", primaryKey); primaryKeyArray.add(primaryKeyArrayWrapper); return primaryKeyId; }
private String addColumn(String tableId, String name, boolean nullable, String typeName, boolean computed) { JsonObject type = new JsonObject(); type.addProperty("typeName", typeName); String sizeUnit; switch (typeName) { case "text": sizeUnit = "CHAR"; type.addProperty("characterOctetLength", "2147483647!{java.lang.Integer}"); break; default: sizeUnit = "BYTE"; } type.addProperty("columnSizeUnit", sizeUnit + "!{liquibase.structure.core.DataType$ColumnSizeUnit}"); type.addProperty("radix", "10!{java.lang.Integer}"); JsonObject column = new JsonObject(); column.addProperty("name", name); column.addProperty("relation", tableId); column.addProperty("nullable", nullable); column.addProperty("computed", computed); //column.addProperty("order", order + "!{java.lang.Integer}"); column.add("type", type); String columnId = addSnapshotId(column, LIQUIBASE_STRUCTURE_CORE_COLUMN, tableId, name); JsonObject columnArrayWrapper = new JsonObject(); columnArrayWrapper.add("column", column); columnArray.add(columnArrayWrapper); return columnId; }